Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Include all component tracker locations #20568

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();

}
}
Loading