Skip to content

Commit

Permalink
fix: Include all component tracker locations (#20568)
Browse files Browse the repository at this point in the history
Some filtering was already earlier moved to Copilot and handling everything in the same place is easier
  • Loading branch information
Artur- authored and vaadin-bot committed Nov 28, 2024
1 parent 828a6bc commit 0da52b5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ public static void trackCreate(Component component) {
location = findRelevantLocation(null, relevantLocations, null);
}
createLocation.put(component, location);
createLocations.put(component, relevantLocations);
createLocations.put(component, Stream.of(stack)
.map(ComponentTracker::toLocation).toArray(Location[]::new));
}

/**
Expand Down Expand Up @@ -279,7 +280,8 @@ public static void trackAttach(Component component) {
location = createLocation.get(component);
}
attachLocation.put(component, location);
attachLocations.put(component, relevantLocations);
attachLocations.put(component, Stream.of(stack)
.map(ComponentTracker::toLocation).toArray(Location[]::new));
}

/**
Expand Down
56 changes: 35 additions & 21 deletions flow-server/src/test/java/com/vaadin/flow/ComponentTrackerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@
*/
package com.vaadin.flow;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.HasComponents;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.internal.ComponentTracker;
import com.vaadin.flow.component.internal.ComponentTracker.Location;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.stream.Stream;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.Map;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.HasComponents;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.internal.ComponentTracker;
import com.vaadin.flow.component.internal.ComponentTracker.Location;

/**
* Note that this is intentionally in the "wrong" package as internal packages
Expand Down Expand Up @@ -70,9 +71,10 @@ public void createLocationTracked() {
Component1 c1 = new Component1();
Component c2;
c2 = new Component1();
int c1Line = 71;

assertCreateLocation(c1, 70, getClass().getName());
assertCreateLocation(c2, 72, getClass().getName());
assertCreateLocation(c1, c1Line, getClass().getName());
assertCreateLocation(c2, c1Line + 2, getClass().getName());
}

@Test
Expand All @@ -83,18 +85,20 @@ public void attachLocationTracked() {

Layout layout = new Layout(c1);

assertCreateLocation(c1, 80, getClass().getName());
int c1Line = 82;

assertCreateLocation(c1, c1Line, getClass().getName());

layout.add(c2);

assertAttachLocation(c2, 88, getClass().getName());
assertAttachLocation(c2, c1Line + 10, getClass().getName());

// Last attach is tracked
layout.add(c3);
layout.remove(c3);
layout.add(c3);

assertAttachLocation(c3, 95, getClass().getName());
assertAttachLocation(c3, c1Line + 17, getClass().getName());
}

@Test
Expand All @@ -103,15 +107,16 @@ public void offsetApplied() {
Component c2 = new Component1();
Component c3 = new Component1();

assertCreateLocation(c1, 102, getClass().getName());
int c1Line = 106;
assertCreateLocation(c1, c1Line, getClass().getName());

ComponentTracker.refreshLocation(ComponentTracker.findCreate(c1), 3);

assertCreateLocation(c2, 103 + 3, getClass().getName());
assertCreateLocation(c2, c1Line + 1 + 3, getClass().getName());

ComponentTracker.refreshLocation(ComponentTracker.findCreate(c2), 1);

assertCreateLocation(c3, 104 + 3 + 1, getClass().getName());
assertCreateLocation(c3, c1Line + 2 + 3 + 1, getClass().getName());
}

@Test
Expand Down Expand Up @@ -170,9 +175,10 @@ private void assertCreateLocation(Component c, int lineNumber,
Assert.assertEquals(lineNumber, location.lineNumber());
Assert.assertEquals(name, location.className());

Location[] locations = ComponentTracker.findCreateLocations(c);
Assert.assertEquals(lineNumber, locations[1].lineNumber());
Assert.assertEquals(name, locations[1].className());
Location locationFromArray = getLocationFromArray(
ComponentTracker.findCreateLocations(c));
Assert.assertEquals(lineNumber, locationFromArray.lineNumber());
Assert.assertEquals(name, locationFromArray.className());
}

private void assertAttachLocation(Component c, int lineNumber,
Expand All @@ -181,9 +187,17 @@ private void assertAttachLocation(Component c, int lineNumber,
Assert.assertEquals(lineNumber, location.lineNumber());
Assert.assertEquals(name, location.className());

Location[] locations = ComponentTracker.findAttachLocations(c);
Assert.assertEquals(lineNumber, locations[0].lineNumber());
Assert.assertEquals(name, locations[0].className());
Location locationFromArray = getLocationFromArray(
ComponentTracker.findAttachLocations(c));

Assert.assertEquals(lineNumber, locationFromArray.lineNumber());
Assert.assertEquals(name, locationFromArray.className());
}

private Location getLocationFromArray(Location[] locations) {
return Stream.of(locations).filter(
l -> l.className().equals(ComponentTrackerTest.class.getName()))
.findFirst().orElseThrow();

}
}

0 comments on commit 0da52b5

Please sign in to comment.