-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathComputedPositionTest.java
More file actions
155 lines (120 loc) · 6.35 KB
/
ComputedPositionTest.java
File metadata and controls
155 lines (120 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.demcha.compose.engine.components.layout;
import com.demcha.compose.engine.components.core.Entity;
import com.demcha.compose.engine.components.geometry.ContentSize;
import com.demcha.compose.engine.components.geometry.InnerBoxSize;
import com.demcha.compose.engine.components.layout.Anchor;
import com.demcha.compose.engine.components.layout.coordinator.ComputedPosition;
import com.demcha.compose.engine.components.layout.coordinator.PaddingCoordinate;
import com.demcha.compose.engine.components.layout.coordinator.Position;
import com.demcha.compose.engine.core.Canvas;
import com.demcha.compose.engine.components.style.Margin;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.MockedStatic;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
class ComputedPositionTest {
@Test
@DisplayName("zero() returns (0,0)")
void zero_returns00() {
ComputedPosition cp = ComputedPosition.zero();
assertThat(cp.x()).isZero();
assertThat(cp.y()).isZero();
}
@Test
@DisplayName("from(child, innerBox) uses existing Anchor.getComputedPosition(...)")
void from_child_inner_usesExistingAnchor() {
// Arrange
Entity child = mock(Entity.class);
Anchor anchor = mock(Anchor.class);
InnerBoxSize inner = new InnerBoxSize(100, 200);
ComputedPosition expected = new ComputedPosition(12.5, 34.5);
when(child.getComponent(Anchor.class)).thenReturn(Optional.of(anchor));
when(anchor.getComputedPosition(child, inner)).thenReturn(expected);
// Act
ComputedPosition actual = ComputedPosition.from(child, inner, PaddingCoordinate.zero());
// Assert
assertThat(actual).isEqualTo(expected);
verify(child, never()).addComponent(any()); // already had anchor
verify(anchor).getComputedPosition(child, inner);
}
@Test
@DisplayName("from(child, innerBox) creates and adds default Anchor when missing, then delegates")
void from_child_inner_addsDefaultAnchorWhenMissing() {
// Arrange
Entity child = mock(Entity.class);
InnerBoxSize inner = new InnerBoxSize(80, 60);
Anchor defaultAnchor = mock(Anchor.class);
Position position = mock(Position.class);
ComputedPosition expected = new ComputedPosition(5, 7);
when(child.getComponent(ContentSize.class)).thenReturn(Optional.of(new ContentSize(80,80)));
// FIX: Simulate that the Anchor component is MISSING.
when(child.getComponent(Anchor.class)).thenReturn(Optional.empty());
when (child.getComponent(Position.class)).thenReturn(Optional.of(new Position(5,7)));
// Mock the static method that is *actually* used for the default anchor.
try (MockedStatic<Anchor> ms = mockStatic(Anchor.class)) {
ms.when(Anchor::defaultAnchor).thenReturn(defaultAnchor);
when(defaultAnchor.getComputedPosition(child, inner)).thenReturn(expected);
// Act
ComputedPosition actual = ComputedPosition.from(child, inner, PaddingCoordinate.zero());
// Assert
assertThat(actual).isEqualTo(expected);
verify(child).addComponent(defaultAnchor); // This will now be called
verify(defaultAnchor).getComputedPosition(child, inner); // This will now be called
ms.verify(Anchor::defaultAnchor); // This will now be called
}
}
@Test
@DisplayName("from(child, parent) calls InnerBoxSize.from(parent) and delegates to anchor")
void from_child_parent_usesInnerBoxFromParent() {
// Arrange
Entity child = mock(Entity.class);
Entity parent = mock(Entity.class);
Anchor anchor = mock(Anchor.class);
InnerBoxSize parentInner = new InnerBoxSize(120, 240);
ComputedPosition expected = new ComputedPosition(10, 20);
// Anchor present on child
when(child.getComponent(Anchor.class)).thenReturn(Optional.of(anchor));
when(anchor.getComputedPosition(eq(child), eq(parentInner))).thenReturn(expected);
// If PaddingCoordinate.from(...) looks up ComputedPosition on the entity, stub it:
when(child.getComponent(ComputedPosition.class)).thenReturn(Optional.of(ComputedPosition.zero()));
try (MockedStatic<InnerBoxSize> innerMs = mockStatic(InnerBoxSize.class);
MockedStatic<PaddingCoordinate> padMs = mockStatic(PaddingCoordinate.class)) {
innerMs.when(() -> InnerBoxSize.from(parent)).thenReturn(Optional.of(parentInner));
// Return a harmless padding so the code path is satisfied
padMs.when(() -> PaddingCoordinate.from(any())).thenReturn(new PaddingCoordinate(0, 0));
// Act
ComputedPosition actual = ComputedPosition.from(child, parent);
// Assert
assertThat(actual).isEqualTo(expected);
innerMs.verify(() -> InnerBoxSize.from(parent));
verify(anchor).getComputedPosition(child, parentInner);
}
}
@Test
@DisplayName("from(child, pageSize) wraps Canvas in InnerBoxSize(width,height) and delegates")
void from_child_pageSize_wrapsIntoInnerBox() {
// Arrange
Entity child = mock(Entity.class);
Anchor anchor = mock(Anchor.class);
Canvas pageSize = mock(Canvas.class);
when(pageSize.width()).thenReturn(595.0f);
when(pageSize.height()).thenReturn(842.0f);
when(pageSize.margin()).thenReturn(Margin.zero());
when(child.getComponent(Anchor.class)).thenReturn(Optional.of(anchor));
ComputedPosition expected = new ComputedPosition(3, 4);
// We want to capture the InnerBoxSize passed to anchor.getComputedPosition
ArgumentCaptor<InnerBoxSize> innerCaptor = ArgumentCaptor.forClass(InnerBoxSize.class);
when(anchor.getComputedPosition(eq(child), innerCaptor.capture())).thenReturn(expected);
// Act
ComputedPosition actual = ComputedPosition.from(child, pageSize);
// Assert
assertThat(actual).isEqualTo(expected);
InnerBoxSize innerUsed = innerCaptor.getValue();
assertThat(innerUsed.width()).isEqualTo(595.0);
assertThat(innerUsed.height()).isEqualTo(842.0);
verify(anchor).getComputedPosition(eq(child), any(InnerBoxSize.class));
}
}