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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Eclipse Public License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.eclipse.org/org/documents/epl-v10.php
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.ide.common.layout;
import static com.android.ide.common.layout.LayoutConstants.ANDROID_URI;
import static com.android.ide.common.layout.LayoutConstants.ATTR_ID;
import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_HEIGHT;
import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_WIDTH;
import static com.android.ide.common.layout.LayoutConstants.ATTR_ORIENTATION;
import static com.android.ide.common.layout.LayoutConstants.VALUE_HORIZONTAL;
import static com.android.ide.common.layout.LayoutConstants.VALUE_VERTICAL;
import com.android.ide.common.api.DropFeedback;
import com.android.ide.common.api.IDragElement;
import com.android.ide.common.api.IMenuCallback;
import com.android.ide.common.api.INode;
import com.android.ide.common.api.IViewRule;
import com.android.ide.common.api.MenuAction;
import com.android.ide.common.api.Point;
import com.android.ide.common.api.Rect;
import com.android.ide.common.api.MenuAction.Choices;
import java.util.List;
import java.util.Map;
/** Test the {@link LinearLayoutRule} */
public class LinearLayoutRuleTest extends LayoutTestBase {
// Utility for other tests
protected void dragIntoEmpty(Rect dragBounds) {
boolean haveBounds = dragBounds.isValid();
IViewRule rule = new LinearLayoutRule();
INode targetNode = TestNode.create("android.widget.LinearLayout").id(
"@+id/LinearLayout01").bounds(new Rect(0, 0, 240, 480));
Point dropPoint = new Point(10, 5);
IDragElement[] elements = TestDragElement.create(TestDragElement.create(
"android.widget.Button", dragBounds).id("@+id/Button01"));
// Enter target
DropFeedback feedback = rule.onDropEnter(targetNode, elements);
assertNotNull(feedback);
assertFalse(feedback.invalidTarget);
assertNotNull(feedback.painter);
feedback = rule.onDropMove(targetNode, elements, feedback, dropPoint);
assertNotNull(feedback);
assertFalse(feedback.invalidTarget);
// Paint feedback and make sure it's what we expect
TestGraphics graphics = new TestGraphics();
assertNotNull(feedback.painter);
feedback.painter.paint(graphics, targetNode, feedback);
assertEquals(
// Expect to see a recipient rectangle around the bounds of the
// LinearLayout,
// as well as a single vertical line as a drop preview located
// along the left
// edge (for this horizontal linear layout) showing insert
// position at index 0,
// and finally a rectangle for the bounds of the inserted button
// centered over
// the middle
"[useStyle(DROP_RECIPIENT), "
+
// Bounds rectangle
"drawRect(Rect[0,0,240,480]), "
+ "useStyle(DROP_ZONE), drawLine(1,0,1,480), "
+ "useStyle(DROP_ZONE_ACTIVE), " + "useStyle(DROP_PREVIEW), " +
// Insert position line
"drawLine(1,0,1,480)" + (haveBounds ?
// Outline of dragged node centered over position line
", useStyle(DROP_PREVIEW), " + "drawRect(Rect[1,0,100,80])"
// Nothing when we don't have bounds
: "") + "]", graphics.getDrawn().toString());
// Attempt a drop
assertEquals(0, targetNode.getChildren().length);
rule.onDropped(targetNode, elements, feedback, dropPoint);
assertEquals(1, targetNode.getChildren().length);
assertEquals("@+id/Button01", targetNode.getChildren()[0].getStringAttr(
ANDROID_URI, ATTR_ID));
}
// Utility for other tests
protected INode dragInto(boolean vertical, Rect dragBounds, Point dragPoint,
int insertIndex, int currentIndex,
String... graphicsFragments) {
INode linearLayout = TestNode.create("android.widget.LinearLayout").id(
"@+id/LinearLayout01").bounds(new Rect(0, 0, 240, 480)).set(ANDROID_URI,
ATTR_ORIENTATION,
vertical ? VALUE_VERTICAL : VALUE_HORIZONTAL)
.add(
TestNode.create("android.widget.Button").id("@+id/Button01").bounds(
new Rect(0, 0, 100, 80)),
TestNode.create("android.widget.Button").id("@+id/Button02").bounds(
new Rect(0, 100, 100, 80)),
TestNode.create("android.widget.Button").id("@+id/Button03").bounds(
new Rect(0, 200, 100, 80)),
TestNode.create("android.widget.Button").id("@+id/Button04").bounds(
new Rect(0, 300, 100, 80)));
return super.dragInto(new LinearLayoutRule(), linearLayout, dragBounds, dragPoint, null,
insertIndex, currentIndex, graphicsFragments);
}
// Check that the context menu registers the expected menu items
public void testContextMenu() {
LinearLayoutRule rule = new LinearLayoutRule();
initialize(rule, "android.widget.LinearLayout");
INode node = TestNode.create("android.widget.Button").id("@+id/Button012");
List<MenuAction> contextMenu = rule.getContextMenu(node);
assertEquals(4, contextMenu.size());
assertEquals("Layout Width", contextMenu.get(0).getTitle());
assertEquals("Layout Height", contextMenu.get(1).getTitle());
assertEquals("Properties", contextMenu.get(2).getTitle());
assertEquals("Orientation", contextMenu.get(3).getTitle());
MenuAction propertiesMenu = contextMenu.get(2);
assertTrue(propertiesMenu.getClass().getName(), propertiesMenu instanceof MenuAction.Group);
// TODO: Test Properties-list
}
public void testContextMenuCustom() {
LinearLayoutRule rule = new LinearLayoutRule();
initialize(rule, "android.widget.LinearLayout");
INode node = TestNode.create("android.widget.Button").id("@+id/Button012")
.set(ANDROID_URI, ATTR_LAYOUT_WIDTH, "42dip")
.set(ANDROID_URI, ATTR_LAYOUT_HEIGHT, "50sp");
List<MenuAction> contextMenu = rule.getContextMenu(node);
assertEquals(4, contextMenu.size());
assertEquals("Layout Width", contextMenu.get(0).getTitle());
MenuAction menuAction = contextMenu.get(0);
assertTrue(menuAction instanceof MenuAction.Choices);
MenuAction.Choices choices = (Choices) menuAction;
Map<String, String> items = choices.getChoices();
assertTrue(items.containsKey("42dip"));
assertTrue(items.containsValue("42dip"));
assertEquals("Other...", items.get("zcustom"));
assertEquals("Match Parent", items.get("match_parent"));
assertEquals("42dip", choices.getCurrent());
}
// Check that the context menu manipulates the orientation attribute
public void testOrientation() {
LinearLayoutRule rule = new LinearLayoutRule();
initialize(rule, "android.widget.LinearLayout");
INode node = TestNode.create("android.widget.Button").id("@+id/Button012");
assertNull(node.getStringAttr(ANDROID_URI, ATTR_ORIENTATION));
List<MenuAction> contextMenu = rule.getContextMenu(node);
assertEquals(4, contextMenu.size());
MenuAction orientationAction = contextMenu.get(3);
assertTrue(orientationAction.getClass().getName(),
orientationAction instanceof MenuAction.Choices);
MenuAction.Choices choices = (Choices) orientationAction;
IMenuCallback callback = choices.getCallback();
callback.action(orientationAction, VALUE_VERTICAL, true);
String orientation = node.getStringAttr(ANDROID_URI,
ATTR_ORIENTATION);
assertEquals(VALUE_VERTICAL, orientation);
callback.action(orientationAction, VALUE_HORIZONTAL, true);
orientation = node.getStringAttr(ANDROID_URI, ATTR_ORIENTATION);
assertEquals(VALUE_HORIZONTAL, orientation);
}
public void testDragInEmptyWithBounds() {
dragIntoEmpty(new Rect(0, 0, 100, 80));
}
public void testDragInEmptyWithoutBounds() {
dragIntoEmpty(new Rect(0, 0, 0, 0));
}
public void testDragInVerticalTop() {
dragInto(true,
// Bounds of the dragged item
new Rect(0, 0, 105, 80),
// Drag point
new Point(30, -10),
// Expected insert location
0,
// Not dragging one of the existing children
-1,
// Bounds rectangle
"useStyle(DROP_RECIPIENT), drawRect(Rect[0,0,240,480])",
// Drop zones
"useStyle(DROP_ZONE), drawLine(0,0,240,0), drawLine(0,90,240,90), "
+ "drawLine(0,190,240,190), drawLine(0,290,240,290), "
+ "drawLine(0,381,240,381)",
// Active nearest line
"useStyle(DROP_ZONE_ACTIVE), useStyle(DROP_PREVIEW), drawLine(0,0,240,0)",
// Preview of the dropped rectangle
"useStyle(DROP_PREVIEW), drawRect(Rect[0,-40,105,80])");
// Without drag bounds it should be identical except no preview
// rectangle
dragInto(true,
new Rect(0, 0, 0, 0), // Invalid
new Point(30, -10), 0, -1,
"useStyle(DROP_ZONE_ACTIVE), useStyle(DROP_PREVIEW), drawLine(0,0,240,0)");
}
public void testDragInVerticalBottom() {
dragInto(true,
// Bounds of the dragged item
new Rect(0, 0, 105, 80),
// Drag point
new Point(30, 500),
// Expected insert location
4,
// Not dragging one of the existing children
-1,
// Bounds rectangle
"useStyle(DROP_RECIPIENT), drawRect(Rect[0,0,240,480])",
// Drop zones
"useStyle(DROP_ZONE), drawLine(0,0,240,0), drawLine(0,90,240,90), "
+ "drawLine(0,190,240,190), drawLine(0,290,240,290), drawLine(0,381,240,381), ",
// Active nearest line
"useStyle(DROP_ZONE_ACTIVE), useStyle(DROP_PREVIEW), drawLine(0,381,240,381)",
// Preview of the dropped rectangle
"useStyle(DROP_PREVIEW), drawRect(Rect[0,381,105,80])");
// Check without bounds too
dragInto(true, new Rect(0, 0, 105, 80), new Point(30, 500), 4, -1,
"useStyle(DROP_PREVIEW), drawRect(Rect[0,381,105,80])");
}
public void testDragInVerticalMiddle() {
dragInto(true,
// Bounds of the dragged item
new Rect(0, 0, 105, 80),
// Drag point
new Point(0, 170),
// Expected insert location
2,
// Not dragging one of the existing children
-1,
// Bounds rectangle
"useStyle(DROP_RECIPIENT), drawRect(Rect[0,0,240,480])",
// Drop zones
"useStyle(DROP_ZONE), drawLine(0,0,240,0), drawLine(0,90,240,90), "
+ "drawLine(0,190,240,190), drawLine(0,290,240,290)",
// Active nearest line
"useStyle(DROP_ZONE_ACTIVE), useStyle(DROP_PREVIEW), drawLine(0,190,240,190)",
// Preview of the dropped rectangle
"useStyle(DROP_PREVIEW), drawRect(Rect[0,150,105,80])");
// Check without bounds too
dragInto(true, new Rect(0, 0, 105, 80), new Point(0, 170), 2, -1,
"useStyle(DROP_PREVIEW), drawRect(Rect[0,150,105,80])");
}
public void testDragInVerticalMiddleSelfPos() {
// Drag the 2nd button, down to the position between 3rd and 4th
dragInto(true,
// Bounds of the dragged item
new Rect(0, 100, 100, 80),
// Drag point
new Point(0, 250),
// Expected insert location
2,
// Dragging 1st item
1,
// Bounds rectangle
"useStyle(DROP_RECIPIENT), drawRect(Rect[0,0,240,480])",
// Drop zones - these are different because we exclude drop
// zones around the
// dragged item itself (it doesn't make sense to insert directly
// before or after
// myself
"useStyle(DROP_ZONE), drawLine(0,0,240,0), drawLine(0,290,240,290), "
+ "drawLine(0,381,240,381)",
// Preview line along insert axis
"useStyle(DROP_ZONE_ACTIVE), useStyle(DROP_PREVIEW), drawLine(0,290,240,290)",
// Preview of dropped rectangle
"useStyle(DROP_PREVIEW), drawRect(Rect[0,250,100,80])");
// Test dropping on self (no position change):
dragInto(true,
// Bounds of the dragged item
new Rect(0, 100, 100, 80),
// Drag point
new Point(0, 210),
// Expected insert location
1,
// Dragging from same pos
1,
// Bounds rectangle
"useStyle(DROP_RECIPIENT), drawRect(Rect[0,0,240,480])",
// Drop zones - these are different because we exclude drop
// zones around the
// dragged item itself (it doesn't make sense to insert directly
// before or after
// myself
"useStyle(DROP_ZONE), drawLine(0,0,240,0), drawLine(0,290,240,290), "
+ "drawLine(0,381,240,381)",
// No active nearest line when you're over the self pos!
// Preview of the dropped rectangle
"useStyle(DROP_ZONE_ACTIVE), useStyle(DROP_PREVIEW), drawRect(Rect[0,100,100,80])");
}
public void testDragToLastPosition() {
// Drag a button to the last position -- and confirm that the preview rectangle
// is now shown midway between the second to last and last positions, but fully
// below the drop zone line:
dragInto(true,
// Bounds of the dragged item
new Rect(0, 100, 100, 80),
// Drag point
new Point(0, 400),
// Expected insert location
3,
// Dragging 1st item
1,
// Bounds rectangle
"useStyle(DROP_RECIPIENT), drawRect(Rect[0,0,240,480])",
// Drop Zones
"useStyle(DROP_ZONE), drawLine(0,0,240,0), drawLine(0,290,240,290), " +
"drawLine(0,381,240,381), ",
// Active Drop Zone
"useStyle(DROP_ZONE_ACTIVE), useStyle(DROP_PREVIEW), drawLine(0,381,240,381)",
// Drop Preview
"seStyle(DROP_PREVIEW), drawRect(Rect[0,381,100,80])");
}
// Left to test:
// Check inserting at last pos with multiple children
// Check inserting with no bounds rectangle for dragged element
}
|