aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/common/layout/grid/GridModelTest.java
blob: c4a58c6e413e1f193533b16ba081bfbcdc50fa5d (plain)
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
/*
 * Copyright (C) 2011 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.grid;

import static com.android.SdkConstants.ANDROID_URI;
import static com.android.SdkConstants.ATTR_COLUMN_COUNT;
import static com.android.SdkConstants.ATTR_LAYOUT_COLUMN;
import static com.android.SdkConstants.ATTR_LAYOUT_COLUMN_SPAN;
import static com.android.SdkConstants.ATTR_LAYOUT_ROW;
import static com.android.SdkConstants.FQCN_BUTTON;

import com.android.ide.common.api.INode;
import com.android.ide.common.api.Rect;
import com.android.ide.common.layout.LayoutTestBase;
import com.android.ide.common.layout.TestNode;
import com.android.ide.common.layout.grid.GridModel.ViewData;

import java.util.Arrays;
import java.util.Collections;


@SuppressWarnings("javadoc")
public class GridModelTest extends LayoutTestBase {
    public void testRemoveFlag() {
        assertEquals("left", GridModel.removeFlag("top", "top|left"));
        assertEquals("left", GridModel.removeFlag("top", "top | left"));
        assertEquals("top", GridModel.removeFlag("left", "top|left"));
        assertEquals("top", GridModel.removeFlag("left", "top | left"));
        assertEquals("left | center", GridModel.removeFlag("top", "top | left | center"));
        assertEquals(null, GridModel.removeFlag("top", "top"));
    }

    public void testReadModel1() {
        TestNode targetNode = TestNode.create("android.widget.GridLayout").id("@+id/GridLayout1")
                .bounds(new Rect(0, 0, 240, 480)).set(ANDROID_URI, ATTR_COLUMN_COUNT, "3");

        GridModel model = GridModel.get(null, targetNode, null);
        assertEquals(3, model.declaredColumnCount);
        assertEquals(1, model.actualColumnCount);
        assertEquals(1, model.actualRowCount);

        targetNode.add(TestNode.create(FQCN_BUTTON).id("@+id/Button1"));
        targetNode.add(TestNode.create(FQCN_BUTTON).id("@+id/Button2"));
        targetNode.add(TestNode.create(FQCN_BUTTON).id("@+id/Button3"));
        targetNode.add(TestNode.create(FQCN_BUTTON).id("@+id/Button4"));

        model = GridModel.get(null, targetNode, null);
        assertEquals(3, model.declaredColumnCount);
        assertEquals(3, model.actualColumnCount);
        assertEquals(2, model.actualRowCount);
    }

    public void testSplitColumn() {
        TestNode targetNode = TestNode.create("android.widget.GridLayout").id("@+id/GridLayout1")
                .bounds(new Rect(0, 0, 240, 480)).set(ANDROID_URI, ATTR_COLUMN_COUNT, "3");
        TestNode b1 = TestNode.create(FQCN_BUTTON).id("@+id/Button1");
        TestNode b2 = TestNode.create(FQCN_BUTTON).id("@+id/Button2");
        TestNode b3 = TestNode.create(FQCN_BUTTON).id("@+id/Button3");
        TestNode b4 = TestNode.create(FQCN_BUTTON).id("@+id/Button4");
        targetNode.add(b1);
        targetNode.add(b2);
        targetNode.add(b3);
        targetNode.add(b4);
        b4.setAttribute(ANDROID_URI, ATTR_LAYOUT_COLUMN_SPAN, "2");

        GridModel model = GridModel.get(new LayoutTestBase.TestRulesEngine(targetNode.getFqcn()),
                targetNode, null);
        assertEquals(3, model.declaredColumnCount);
        assertEquals(3, model.actualColumnCount);
        assertEquals(2, model.actualRowCount);

        model.applyPositionAttributes();
        assertEquals("0", b1.getStringAttr(ANDROID_URI, ATTR_LAYOUT_COLUMN));
        assertEquals("0", b1.getStringAttr(ANDROID_URI, ATTR_LAYOUT_ROW));

        assertEquals("1", b2.getStringAttr(ANDROID_URI, ATTR_LAYOUT_COLUMN));
        assertEquals("0", b2.getStringAttr(ANDROID_URI, ATTR_LAYOUT_ROW));

        assertEquals("2", b3.getStringAttr(ANDROID_URI, ATTR_LAYOUT_COLUMN));
        assertEquals("0", b3.getStringAttr(ANDROID_URI, ATTR_LAYOUT_ROW));

        assertEquals("0", b4.getStringAttr(ANDROID_URI, ATTR_LAYOUT_COLUMN));
        assertEquals("1", b4.getStringAttr(ANDROID_URI, ATTR_LAYOUT_ROW));
        assertEquals("2", b4.getStringAttr(ANDROID_URI, ATTR_LAYOUT_COLUMN_SPAN));

        model.splitColumn(1, false /*insertMarginColumn*/, 100 /*columnWidthDp*/, 300 /* x */);
        model.applyPositionAttributes();

        assertEquals(4, model.declaredColumnCount);
        assertEquals(4, model.actualColumnCount);
        assertEquals(2, model.actualRowCount);

        assertEquals("0", b1.getStringAttr(ANDROID_URI, ATTR_LAYOUT_COLUMN));
        assertEquals("0", b1.getStringAttr(ANDROID_URI, ATTR_LAYOUT_ROW));

        assertEquals("1", b2.getStringAttr(ANDROID_URI, ATTR_LAYOUT_COLUMN));
        assertEquals("2", b2.getStringAttr(ANDROID_URI, ATTR_LAYOUT_COLUMN_SPAN));
        assertEquals("0", b2.getStringAttr(ANDROID_URI, ATTR_LAYOUT_ROW));

        assertEquals("3", b3.getStringAttr(ANDROID_URI, ATTR_LAYOUT_COLUMN));
        assertEquals("0", b3.getStringAttr(ANDROID_URI, ATTR_LAYOUT_ROW));

        assertEquals("0", b4.getStringAttr(ANDROID_URI, ATTR_LAYOUT_COLUMN));
        assertEquals("1", b4.getStringAttr(ANDROID_URI, ATTR_LAYOUT_ROW));
        assertEquals("3", b4.getStringAttr(ANDROID_URI, ATTR_LAYOUT_COLUMN_SPAN));
    }

    public void testDeletion1() {
        String xml =
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
            "<GridLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
            "    android:layout_width=\"match_parent\"\n" +
            "    android:layout_height=\"match_parent\"\n" +
            "    android:columnCount=\"4\" >\n" +
            "\n" +
            "    <Button\n" +
            "        android:id=\"@+id/button1\"\n" +
            "        android:layout_column=\"1\"\n" +
            "        android:layout_gravity=\"left|top\"\n" +
            "        android:layout_row=\"1\"\n" +
            "        android:text=\"Button\" />\n" +
            "\n" +
            "    <TextView\n" +
            "        android:id=\"@+id/TextView1\"\n" +
            "        android:layout_column=\"3\"\n" +
            "        android:layout_gravity=\"left|top\"\n" +
            "        android:layout_row=\"1\"\n" +
            "        android:text=\"Text\" />\n" +
            "\n" +
            "    <Space\n" +
            "        android:id=\"@+id/wspace1\"\n" +
            "        android:layout_width=\"21dp\"\n" +
            "        android:layout_height=\"1dp\"\n" +
            "        android:layout_column=\"0\"\n" +
            "        android:layout_row=\"0\" />\n" +
            "\n" +
            "    <Space\n" +
            "        android:id=\"@+id/hspace1\"\n" +
            "        android:layout_width=\"1dp\"\n" +
            "        android:layout_height=\"55dp\"\n" +
            "        android:layout_column=\"0\"\n" +
            "        android:layout_row=\"0\" />\n" +
            "\n" +
            "    <Space\n" +
            "        android:id=\"@+id/wspace2\"\n" +
            "        android:layout_width=\"10dp\"\n" +
            "        android:layout_height=\"1dp\"\n" +
            "        android:layout_column=\"2\"\n" +
            "        android:layout_row=\"0\" />\n" +
            "\n" +
            "</GridLayout>";

        TestNode targetNode = TestNode.createFromXml(xml);
        assertNotNull(targetNode);

        TestNode button1 = TestNode.findById(targetNode, "@+id/button1");
        TestNode textView1 = TestNode.findById(targetNode, "@+id/TextView1");
        TestNode wspace1 = TestNode.findById(targetNode, "@+id/wspace1");
        TestNode wspace2 = TestNode.findById(targetNode, "@+id/wspace2");
        TestNode hspace1 = TestNode.findById(targetNode, "@+id/hspace1");
        assertNotNull(wspace1);
        assertNotNull(hspace1);
        assertNotNull(wspace2);
        assertNotNull(button1);
        assertNotNull(textView1);

        // Assign some bounds such that the model makes sense when merging spacer sizes
        // TODO: MAke test utility method to automatically assign half divisions!!
        button1.bounds(new Rect(90, 10, 100, 40));
        textView1.bounds(new Rect(200, 10, 100, 40));
        wspace1.bounds(new Rect(0, 0, 90, 1));
        wspace1.bounds(new Rect(190, 0, 10, 1));
        hspace1.bounds(new Rect(0, 0, 1, 10));

        GridModel model = GridModel.get(new LayoutTestBase.TestRulesEngine(targetNode.getFqcn()),
                targetNode, null);
        assertEquals(4, model.declaredColumnCount);
        assertEquals(4, model.actualColumnCount);
        assertEquals(2, model.actualRowCount);

        ViewData textViewData = model.getView(textView1);
        assertEquals(3, textViewData.column);

        // Delete button1
        button1.getParent().removeChild(button1);
        model.onDeleted(Arrays.<INode>asList(button1));
        model.applyPositionAttributes();

        assertEquals(2, model.declaredColumnCount);
        assertEquals(2, model.actualColumnCount);
        assertEquals(2, model.actualRowCount);
        assertNotNull(model.getView(textView1));
        assertNull(model.getView(button1));

        assertEquals(
                "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<GridLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
                "    android:layout_width=\"match_parent\"\n" +
                "    android:layout_height=\"match_parent\"\n" +
                "    android:columnCount=\"2\">\n" +
                "\n" +
                "    <TextView\n" +
                "        android:id=\"@+id/TextView1\"\n" +
                "        android:layout_column=\"1\"\n" +
                "        android:layout_gravity=\"left|top\"\n" +
                "        android:layout_row=\"1\"\n" +
                "        android:text=\"Text\">\n" +
                "    </TextView>\n" +
                "\n" +
                "    <Space\n" +
                "        android:id=\"@+id/wspace1\"\n" +
                "        android:layout_width=\"66dp\"\n" +
                "        android:layout_height=\"1dp\"\n" +
                "        android:layout_column=\"0\"\n" +
                "        android:layout_row=\"0\">\n" +
                "    </Space>\n" +
                "\n" +
                "    <Space\n" +
                "        android:id=\"@+id/hspace1\"\n" +
                "        android:layout_width=\"1dp\"\n" +
                "        android:layout_height=\"55dp\"\n" +
                "        android:layout_column=\"0\"\n" +
                "        android:layout_row=\"0\">\n" +
                "    </Space>\n" +
                "\n" +
                "</GridLayout>", TestNode.toXml(targetNode));

        // Delete textView1

        textView1.getParent().removeChild(textView1);
        model.onDeleted(Arrays.<INode>asList(textView1));
        model.applyPositionAttributes();

        assertEquals(2, model.declaredColumnCount);
        assertEquals(0, model.actualColumnCount);
        assertEquals(0, model.actualRowCount);
        assertNull(model.getView(textView1));
        assertNull(model.getView(button1));

        assertEquals(
                "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<GridLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
                "    android:layout_width=\"match_parent\"\n" +
                "    android:layout_height=\"match_parent\"\n" +
                "    android:columnCount=\"0\">\n" +
                "\n" +
                "</GridLayout>", TestNode.toXml(targetNode));

    }

    public void testDelete2() throws Exception {
        String xml =
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
            "<GridLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
            "    android:layout_width=\"match_parent\"\n" +
            "    android:layout_height=\"match_parent\"\n" +
            "    android:columnCount=\"4\"\n" +
            "    android:orientation=\"vertical\" >\n" +
            "\n" +
            "    <Button\n" +
            "        android:id=\"@+id/button1\"\n" +
            "        android:layout_column=\"0\"\n" +
            "        android:layout_columnSpan=\"2\"\n" +
            "        android:layout_gravity=\"left|top\"\n" +
            "        android:layout_row=\"0\"\n" +
            "        android:text=\"Button1\" />\n" +
            "\n" +
            "    <Button\n" +
            "        android:id=\"@+id/button3\"\n" +
            "        android:layout_column=\"1\"\n" +
            "        android:layout_columnSpan=\"2\"\n" +
            "        android:layout_gravity=\"left|top\"\n" +
            "        android:layout_row=\"1\"\n" +
            "        android:text=\"Button2\" />\n" +
            "\n" +
            "    <Button\n" +
            "        android:id=\"@+id/button2\"\n" +
            "        android:layout_column=\"2\"\n" +
            "        android:layout_gravity=\"left|top\"\n" +
            "        android:layout_row=\"0\"\n" +
            "        android:text=\"Button3\" />\n" +
            "\n" +
            "    <Space\n" +
            "        android:id=\"@+id/spacer_177\"\n" +
            "        android:layout_width=\"46dp\"\n" +
            "        android:layout_height=\"1dp\"\n" +
            "        android:layout_column=\"0\"\n" +
            "        android:layout_row=\"0\" />\n" +
            "\n" +
            "</GridLayout>";

        TestNode targetNode = TestNode.createFromXml(xml);
        assertNotNull(targetNode);

        TestNode button1 = TestNode.findById(targetNode, "@+id/button1");
        TestNode button2 = TestNode.findById(targetNode, "@+id/button2");
        TestNode button3 = TestNode.findById(targetNode, "@+id/button3");
        TestNode hspacer = TestNode.findById(targetNode, "@+id/spacer_177");
        assertNotNull(button1);
        assertNotNull(button2);
        assertNotNull(button3);
        assertNotNull(hspacer);

        // Assign some bounds such that the model makes sense when merging spacer sizes
        // TODO: MAke test utility method to automatically assign half divisions!!
        button1.bounds(new Rect(0, 0, 100, 40));
        button2.bounds(new Rect(100, 0, 100, 40));
        button3.bounds(new Rect(50, 40, 100, 40));
        hspacer.bounds(new Rect(0, 0, 50, 1));

        GridModel model = GridModel.get(new LayoutTestBase.TestRulesEngine(targetNode.getFqcn()),
                targetNode, null);
        assertEquals(4, model.declaredColumnCount);
        assertEquals(3, model.actualColumnCount);
        assertEquals(2, model.actualRowCount);

        ViewData buttonData = model.getView(button1);
        assertEquals(0, buttonData.column);

        // Delete button1
        button1.getParent().removeChild(button1);
        model.onDeleted(Arrays.<INode>asList(button1));
        model.applyPositionAttributes();

        assertEquals(3, model.declaredColumnCount);
        assertEquals(3, model.actualColumnCount);
        assertEquals(2, model.actualRowCount);
        assertNull(model.getView(button1));

        assertEquals(
                "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<GridLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
                "    android:layout_width=\"match_parent\"\n" +
                "    android:layout_height=\"match_parent\"\n" +
                "    android:columnCount=\"3\"\n" +
                "    android:orientation=\"vertical\">\n" +
                "\n" +
                "    <Button\n" +
                "        android:id=\"@+id/button3\"\n" +
                "        android:layout_column=\"1\"\n" +
                "        android:layout_columnSpan=\"2\"\n" +
                "        android:layout_gravity=\"left|top\"\n" +
                "        android:layout_row=\"1\"\n" +
                "        android:text=\"Button2\">\n" +
                "    </Button>\n" +
                "\n" +
                "    <Button\n" +
                "        android:id=\"@+id/button2\"\n" +
                "        android:layout_column=\"2\"\n" +
                "        android:layout_gravity=\"left|top\"\n" +
                "        android:layout_row=\"0\"\n" +
                "        android:text=\"Button3\">\n" +
                "    </Button>\n" +
                "\n" +
                "    <Space\n" +
                "        android:id=\"@+id/spacer_177\"\n" +
                "        android:layout_width=\"46dp\"\n" +
                "        android:layout_height=\"1dp\"\n" +
                "        android:layout_column=\"0\"\n" +
                "        android:layout_row=\"0\">\n" +
                "    </Space>\n" +
                "\n" +
                "</GridLayout>", TestNode.toXml(targetNode));
    }

    public void testDelete3_INCOMPLETE() throws Exception {
        String xml =
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
            "<GridLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
            "  android:layout_width=\"match_parent\" android:layout_height=\"match_parent\"\n" +
            "  android:columnCount=\"6\">\n" +
            "  <Button android:id=\"@+id/button1\" android:layout_column=\"1\"\n" +
            "    android:layout_columnSpan=\"2\" android:layout_gravity=\"left|top\"\n" +
            "    android:layout_row=\"1\" android:layout_rowSpan=\"2\" android:text=\"Button\" />\n" +
            "  <TextView android:id=\"@+id/TextView1\" android:layout_column=\"4\"\n" +
            "    android:layout_gravity=\"left|top\" android:layout_row=\"1\"\n" +
            "    android:text=\"Text\" />\n" +
            "  <Button android:id=\"@+id/button3\" android:layout_column=\"5\"\n" +
            "    android:layout_gravity=\"left|top\" android:layout_row=\"2\"\n" +
            "    android:layout_rowSpan=\"2\" android:text=\"Button\" />\n" +
            "  <Button android:id=\"@+id/button2\" android:layout_column=\"2\"\n" +
            "    android:layout_columnSpan=\"3\" android:layout_gravity=\"left|top\"\n" +
            "    android:layout_row=\"4\" android:text=\"Button\" />\n" +
            "  <Space android:id=\"@+id/wspace1\" android:layout_width=\"21dp\"\n" +
            "    android:layout_height=\"1dp\" android:layout_column=\"0\"\n" +
            "    android:layout_row=\"0\" />\n" +
            "  <Space android:id=\"@+id/spacer_630\" android:layout_width=\"1dp\"\n" +
            "    android:layout_height=\"55dp\" android:layout_column=\"0\"\n" +
            "    android:layout_row=\"0\" />\n" +
            "  <Space android:id=\"@+id/wspace2\" android:layout_width=\"10dp\"\n" +
            "    android:layout_height=\"1dp\" android:layout_column=\"3\"\n" +
            "    android:layout_row=\"0\" />\n" +
            "  <Space android:id=\"@+id/spacer_619\" android:layout_width=\"59dp\"\n" +
            "    android:layout_height=\"1dp\" android:layout_column=\"1\"\n" +
            "    android:layout_row=\"0\" />\n" +
            "  <Space android:id=\"@+id/spacer_102\" android:layout_width=\"1dp\"\n" +
            "    android:layout_height=\"30dp\" android:layout_column=\"0\"\n" +
            "    android:layout_row=\"1\" />\n" +
            "  <Space android:id=\"@+id/spacer_109\" android:layout_width=\"1dp\"\n" +
            "    android:layout_height=\"28dp\" android:layout_column=\"0\"\n" +
            "    android:layout_row=\"2\" />\n" +
            "  <Space android:id=\"@+id/spacer_146\" android:layout_width=\"1dp\"\n" +
            "    android:layout_height=\"70dp\" android:layout_column=\"0\"\n" +
            "    android:layout_row=\"3\" />\n" +
            "</GridLayout>";
        TestNode targetNode = TestNode.createFromXml(xml);
        assertNotNull(targetNode);
        targetNode.assignBounds(
            "android.widget.GridLayout [0,109,480,800] <GridLayout>\n" +
            "    android.widget.Button [32,83,148,155] <Button> @+id/button1\n" +
            "    android.widget.TextView [163,83,205,109] <TextView> @+id/TextView1\n" +
            "    android.widget.Button [237,128,353,200] <Button> @+id/button3\n" +
            "    android.widget.Button [121,275,237,347] <Button> @+id/button2\n" +
            "    android.widget.Space [0,0,32,2] <Space> @+id/wspace1\n" +
            "    android.widget.Space [0,0,2,83] <Space> @+id/spacer_630\n" +
            "    android.widget.Space [148,0,163,2] <Space> @+id/wspace2\n" +
            "    android.widget.Space [32,0,121,2] <Space> @+id/spacer_619\n" +
            "    android.widget.Space [0,83,2,128] <Space> @+id/spacer_102\n" +
            "    android.widget.Space [0,128,2,170] <Space> @+id/spacer_109\n" +
            "    android.widget.Space [0,170,2,275] <Space> @+id/spacer_146\n");
        TestNode layout = TestNode.findById(targetNode, "@+id/GridLayout1");
        //TestNode button1 = TestNode.findById(targetNode, "@+id/button1");
        TestNode button2 = TestNode.findById(targetNode, "@+id/button2");
        TestNode button3 = TestNode.findById(targetNode, "@+id/button3");

        GridModel model = GridModel.get(new LayoutTestBase.TestRulesEngine(targetNode.getFqcn()),
                targetNode, null);
        assertEquals(6, model.declaredColumnCount);
        assertEquals(6, model.actualColumnCount);
        assertEquals(5, model.actualRowCount);

        // TODO: Delete button2 or button3: bad stuff happens visually
        fail("Finish test");
    }

    public void testDelete4_INCOMPLETE() {
        String xml = "" +
            "<GridLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
            "  xmlns:tools=\"http://schemas.android.com/tools\" " +
            "  android:layout_width=\"match_parent\"\n" +
            "  android:layout_height=\"match_parent\" android:columnCount=\"3\"\n" +
            "  android:gravity=\"center\" android:text=\"@string/hello_world\"\n" +
            "  tools:context=\".MainActivity\">\n" +
            "  <Button android:id=\"@+id/button2\" android:layout_column=\"1\"\n" +
            "    android:layout_columnSpan=\"2\" android:layout_gravity=\"left|top\"\n" +
            "    android:layout_row=\"1\" android:text=\"Button\" />\n" +
            "  <Button android:id=\"@+id/button1\" android:layout_column=\"1\"\n" +
            "    android:layout_columnSpan=\"2\" android:layout_gravity=\"left|top\"\n" +
            "    android:layout_row=\"3\" android:text=\"Button\" />\n" +
            "  <Space android:id=\"@+id/spacer_167\" android:layout_width=\"74dp\"\n" +
            "    android:layout_height=\"1dp\" android:layout_column=\"0\"\n" +
            "    android:layout_row=\"0\" />\n" +
            "  <Space android:id=\"@+id/spacer_133\" android:layout_width=\"1dp\"\n" +
            "    android:layout_height=\"21dp\" android:layout_column=\"0\"\n" +
            "    android:layout_row=\"0\" />\n" +
            "  <Space android:id=\"@+id/spacer_142\" android:layout_width=\"1dp\"\n" +
            "    android:layout_height=\"26dp\" android:layout_column=\"0\"\n" +
            "    android:layout_row=\"2\" />\n" +
            "  <Space android:id=\"@+id/spacer_673\" android:layout_width=\"43dp\"\n" +
            "    android:layout_height=\"1dp\" android:layout_column=\"1\"\n" +
            "    android:layout_row=\"0\" />\n" +
            "  <Space android:id=\"@+id/spacer_110\" android:layout_width=\"202dp\"\n" +
            "    android:layout_height=\"15dp\" android:layout_column=\"2\" />\n" +
            "</GridLayout>";
        TestNode targetNode = TestNode.createFromXml(xml);
        assertNotNull(targetNode);
        targetNode.assignBounds(
                "android.widget.GridLayout [0,109,480,800] <GridLayout>\n" +
                "    android.widget.Button [111,32,227,104] <Button> @+id/button2\n" +
                "    android.widget.Button [111,143,227,215] <Button> @+id/button1\n" +
                "    android.widget.Space [0,0,111,2] <Space> @+id/spacer_167\n" +
                "    android.widget.Space [0,0,2,32] <Space> @+id/spacer_133\n" +
                "    android.widget.Space [0,104,2,143] <Space> @+id/spacer_142\n" +
                "    android.widget.Space [111,0,176,2] <Space> @+id/spacer_673\n" +
                "    android.widget.Space [176,668,479,691] <Space> @+id/spacer_110");


        // Remove button2; button1 shifts to the right!

        //TestNode layout = TestNode.findById(targetNode, "@+id/GridLayout1");
        //TestNode button1 = TestNode.findById(targetNode, "@+id/button1");
        TestNode button2 = TestNode.findById(targetNode, "@+id/button2");
        TestNode button3 = TestNode.findById(targetNode, "@+id/button3");
        assertEquals(new Rect(111, 32, 227 - 111, 104 - 32), button2.getBounds());

        GridModel model = GridModel.get(new LayoutTestBase.TestRulesEngine(targetNode.getFqcn()),
                targetNode, null);
        assertEquals(3, model.declaredColumnCount);
        assertEquals(3, model.actualColumnCount);
        assertEquals(4, model.actualRowCount);
        fail("Finish test");
    }

    public void testDelete5_INCOMPLETE() {
        String xml =
                "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<GridLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
                "  android:id=\"@+id/GridLayout1\" android:layout_width=\"match_parent\"\n" +
                "  android:layout_height=\"match_parent\" android:columnCount=\"4\"\n" +
                "  android:orientation=\"vertical\">\n" +
                "  <Button android:id=\"@+id/button1\" android:layout_column=\"0\"\n" +
                "    android:layout_gravity=\"center_horizontal|bottom\"\n" +
                "    android:layout_row=\"0\" android:text=\"Button\" />\n" +
                "  <Space android:layout_width=\"66dp\" android:layout_height=\"1dp\"\n" +
                "    android:layout_column=\"0\" android:layout_row=\"0\" />\n" +
                "  <Button android:id=\"@+id/button3\" android:layout_column=\"2\"\n" +
                "    android:layout_gravity=\"left|bottom\" android:layout_row=\"0\"\n" +
                "    android:text=\"Button\" />\n" +
                "  <Button android:id=\"@+id/button2\" android:layout_column=\"3\"\n" +
                "    android:layout_columnSpan=\"2\" android:layout_gravity=\"left|bottom\"\n" +
                "    android:layout_row=\"0\" android:text=\"Button\" />\n" +
                "  <Space android:id=\"@+id/spacer_109\" android:layout_width=\"51dp\"\n" +
                "    android:layout_height=\"1dp\" android:layout_column=\"1\"\n" +
                "    android:layout_row=\"0\" />\n" +
                "  <Space android:layout_width=\"129dp\" android:layout_height=\"1dp\"\n" +
                "    android:layout_column=\"2\" android:layout_row=\"0\" />\n" +
                "  <Space android:layout_width=\"62dp\" android:layout_height=\"1dp\"\n" +
                "    android:layout_column=\"3\" android:layout_row=\"0\" />\n" +
                "  <Space android:id=\"@+id/spacer_397\" android:layout_width=\"1dp\"\n" +
                "    android:layout_height=\"103dp\" android:layout_column=\"0\"\n" +
                "    android:layout_row=\"0\" />\n" +
                "  <Space android:layout_width=\"1dp\" android:layout_height=\"356dp\"\n" +
                "    android:layout_column=\"0\" android:layout_row=\"1\" />\n" +
                "</GridLayout>";
        TestNode targetNode = TestNode.createFromXml(xml);
        assertNotNull(targetNode);

        targetNode.assignBounds(
                "android.widget.GridLayout [0,109,480,800] <GridLayout> @+id/GridLayout1\n" +
                "    android.widget.Button [0,83,116,155] <Button> @+id/button1\n" +
                "    android.widget.Space [0,0,99,2] <Space>\n" +
                "    android.widget.Button [193,83,309,155] <Button> @+id/button3\n" +
                "    android.widget.Button [387,83,503,155] <Button> @+id/button2\n" +
                "    android.widget.Space [116,0,193,2] <Space> @+id/spacer_109\n" +
                "    android.widget.Space [193,0,387,2] <Space>\n" +
                "    android.widget.Space [387,0,480,2] <Space>\n" +
                "    android.widget.Space [0,0,2,155] <Space> @+id/spacer_397\n" +
                "    android.widget.Space [0,155,2,689] <Space>");

        // Delete button3. This causes an array out of bounds exception currently.

        //TestNode layout = TestNode.findById(targetNode, "@+id/GridLayout1");
        //TestNode button1 = TestNode.findById(targetNode, "@+id/button1");
        TestNode button2 = TestNode.findById(targetNode, "@+id/button2");
        TestNode button3 = TestNode.findById(targetNode, "@+id/button3");
        assertEquals(new Rect(387, 83, 503 - 387, 155- 83), button2.getBounds());

        GridModel model = GridModel.get(new LayoutTestBase.TestRulesEngine(targetNode.getFqcn()),
                targetNode, null);
        assertEquals(4, model.declaredColumnCount);
        assertEquals(4, model.actualColumnCount);
        assertEquals(2, model.actualRowCount);

        model.onDeleted(Collections.<INode>singletonList(button3));
        // Exception fixed; todo: Test that the model updates are correct.

        assertEquals(3, model.declaredColumnCount);
        assertEquals(3, model.actualColumnCount);
        assertEquals(2, model.actualRowCount);

        fail("Finish test");
    }

    public void testInsert1() throws Exception {
        String xml =
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
            "<GridLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
            "    android:id=\"@+id/GridLayout1\"\n" +
            "    android:layout_width=\"match_parent\"\n" +
            "    android:layout_height=\"match_parent\"\n" +
            "    android:columnCount=\"4\"\n" +
            "    android:orientation=\"vertical\" >\n" +
            "\n" +
            "    <Button\n" +
            "        android:id=\"@+id/button1\"\n" +
            "        android:layout_column=\"0\"\n" +
            "        android:layout_columnSpan=\"4\"\n" +
            "        android:layout_gravity=\"center_horizontal|bottom\"\n" +
            "        android:layout_row=\"0\"\n" +
            "        android:text=\"Button\" />\n" +
            "\n" +
            "    <Button\n" +
            "        android:id=\"@+id/button2\"\n" +
            "        android:layout_column=\"2\"\n" +
            "        android:layout_gravity=\"left|top\"\n" +
            "        android:layout_row=\"1\"\n" +
            "        android:text=\"Button\" />\n" +
            "\n" +
            "    <Button\n" +
            "        android:id=\"@+id/button3\"\n" +
            "        android:layout_column=\"3\"\n" +
            "        android:layout_gravity=\"left|top\"\n" +
            "        android:layout_row=\"1\"\n" +
            "        android:text=\"Button\" />\n" +
            "\n" +
            "    <Space\n" +
            "        android:id=\"@+id/spacer_393\"\n" +
            "        android:layout_width=\"81dp\"\n" +
            "        android:layout_height=\"1dp\"\n" +
            "        android:layout_column=\"1\"\n" +
            "        android:layout_row=\"0\" />\n" +
            "\n" +
            "    <Space\n" +
            "        android:id=\"@+id/spacer_397\"\n" +
            "        android:layout_width=\"1dp\"\n" +
            "        android:layout_height=\"103dp\"\n" +
            "        android:layout_column=\"0\"\n" +
            "        android:layout_row=\"0\" />\n" +
            "\n" +
            "</GridLayout>";

        TestNode targetNode = TestNode.createFromXml(xml);
        assertNotNull(targetNode);

        TestNode layout = TestNode.findById(targetNode, "@+id/GridLayout1");
        TestNode button1 = TestNode.findById(targetNode, "@+id/button1");
        TestNode button2 = TestNode.findById(targetNode, "@+id/button2");
        TestNode button3 = TestNode.findById(targetNode, "@+id/button3");
        TestNode hspacer = TestNode.findById(targetNode, "@+id/spacer_393");
        TestNode vspacer = TestNode.findById(targetNode, "@+id/spacer_397");
        assertNotNull(layout);
        assertNotNull(button1);
        assertNotNull(button2);
        assertNotNull(button3);
        assertNotNull(hspacer);

        // Obtained by setting ViewHierarchy.DUMP_INFO=true:
        layout.bounds(new Rect(0, 109, 480, 800-109));
        button1.bounds(new Rect(182, 83, 298-182, 155-83));
        button2.bounds(new Rect(124, 155, 240-124, 227-155));
        button3.bounds(new Rect(240, 155, 356-240, 227-155));
        hspacer.bounds(new Rect(2, 0, 124-2, 2));
        vspacer.bounds(new Rect(0, 0, 2, 155));

        GridModel model = GridModel.get(new LayoutTestBase.TestRulesEngine(targetNode.getFqcn()),
                targetNode, null);
        assertEquals(4, model.declaredColumnCount);
        assertEquals(4, model.actualColumnCount);
        assertEquals(2, model.actualRowCount);


        model.splitColumn(1, false, 21, 32);
        int index = model.getInsertIndex(2, 1);
        GridModel.ViewData next = model.getView(index);
        INode newChild = targetNode.insertChildAt(FQCN_BUTTON, index);
        next.applyPositionAttributes();
        model.setGridAttribute(newChild, ATTR_LAYOUT_COLUMN, 1);
        model.setGridAttribute(newChild, ATTR_LAYOUT_COLUMN_SPAN, 3);
    }

    public void testInsert2() throws Exception {
        // Drop into a view where there is a centered view: when dropping to the right of
        // it (on a row further down), ensure that the row span is increased for the
        // non-left-justified centered view which does not horizontally overlap the view
        String xml =
                "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<GridLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
                "    android:id=\"@+id/GridLayout1\"\n" +
                "    android:layout_width=\"match_parent\"\n" +
                "    android:layout_height=\"match_parent\"\n" +
                "    android:columnCount=\"3\"\n" +
                "    android:orientation=\"vertical\" >\n" +
                "\n" +
                "    <Button\n" +
                "        android:id=\"@+id/button1\"\n" +
                "        android:layout_column=\"0\"\n" +
                "        android:layout_columnSpan=\"3\"\n" +
                "        android:layout_gravity=\"center_horizontal|bottom\"\n" +
                "        android:layout_row=\"0\"\n" +
                "        android:text=\"Button\" />\n" +
                "\n" +
                "    <Button\n" +
                "        android:id=\"@+id/button2\"\n" +
                "        android:layout_column=\"1\"\n" +
                "        android:layout_gravity=\"left|top\"\n" +
                "        android:layout_row=\"1\"\n" +
                "        android:text=\"Button\" />\n" +
                "\n" +
                "    <Button\n" +
                "        android:id=\"@+id/button3\"\n" +
                "        android:layout_column=\"2\"\n" +
                "        android:layout_gravity=\"left|top\"\n" +
                "        android:layout_row=\"1\"\n" +
                "        android:text=\"Button\" />\n" +
                "\n" +
                "    <Space\n" +
                "        android:id=\"@+id/spacer_393\"\n" +
                "        android:layout_width=\"81dp\"\n" +
                "        android:layout_height=\"1dp\"\n" +
                "        android:layout_column=\"0\"\n" +
                "        android:layout_row=\"0\" />\n" +
                "\n" +
                "    \n" +
                "    <Space\n" +
                "        android:id=\"@+id/spacer_397\"\n" +
                "        android:layout_width=\"1dp\"\n" +
                "        android:layout_height=\"103dp\"\n" +
                "        android:layout_column=\"0\"\n" +
                "        android:layout_row=\"0\" />\n" +
                "\n" +
                "    \n" +
                "</GridLayout>";

        TestNode targetNode = TestNode.createFromXml(xml);
        assertNotNull(targetNode);

        TestNode layout = TestNode.findById(targetNode, "@+id/GridLayout1");
        TestNode button1 = TestNode.findById(targetNode, "@+id/button1");
        TestNode button2 = TestNode.findById(targetNode, "@+id/button2");
        TestNode button3 = TestNode.findById(targetNode, "@+id/button3");
        TestNode hspacer = TestNode.findById(targetNode, "@+id/spacer_393");
        TestNode vspacer = TestNode.findById(targetNode, "@+id/spacer_397");
        assertNotNull(layout);
        assertNotNull(button1);
        assertNotNull(button2);
        assertNotNull(button3);
        assertNotNull(hspacer);

        // Obtained by setting ViewHierarchy.DUMP_INFO=true:
        layout.bounds(new Rect(0, 109, 480, 800-109));
        button1.bounds(new Rect(182, 83, 298-182, 155-83));
        button2.bounds(new Rect(122, 155, 238-122, 227-155));
        button3.bounds(new Rect(238, 155, 354-238, 227-155));
        hspacer.bounds(new Rect(0, 0, 122, 2));
        vspacer.bounds(new Rect(0, 0, 2, 155));

        GridModel model = GridModel.get(new LayoutTestBase.TestRulesEngine(targetNode.getFqcn()),
                targetNode, null);
        assertEquals(3, model.declaredColumnCount);
        assertEquals(3, model.actualColumnCount);
        assertEquals(2, model.actualRowCount);

        ViewData view = model.getView(button1);
        assertNotNull(view);
        assertEquals(0, view.column);
        assertEquals(3, view.columnSpan);
        assertEquals("3", view.node.getStringAttr(ANDROID_URI, ATTR_LAYOUT_COLUMN_SPAN));

        model.splitColumn(3, false, 53, 318);
        assertEquals(0, view.column);
        assertEquals(4, view.columnSpan);
        assertEquals("4", view.node.getStringAttr(ANDROID_URI, ATTR_LAYOUT_COLUMN_SPAN));
    }

    public void testInsert3_BROKEN() throws Exception {
        // Check that when we insert a new gap column near an existing column, the
        // view in that new column does not get moved
        String xml =
                "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<GridLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
                "    android:id=\"@+id/GridLayout1\"\n" +
                "    android:layout_width=\"match_parent\"\n" +
                "    android:layout_height=\"match_parent\"\n" +
                "    android:columnCount=\"3\"\n" +
                "    android:orientation=\"vertical\" >\n" +
                "\n" +
                "    <Button\n" +
                "        android:id=\"@+id/button1\"\n" +
                "        android:layout_column=\"0\"\n" +
                "        android:layout_columnSpan=\"3\"\n" +
                "        android:layout_gravity=\"center_horizontal|bottom\"\n" +
                "        android:layout_row=\"0\"\n" +
                "        android:text=\"Button\" />\n" +
                "\n" +
                "    <Button\n" +
                "        android:id=\"@+id/button2\"\n" +
                "        android:layout_column=\"1\"\n" +
                "        android:layout_gravity=\"left|top\"\n" +
                "        android:layout_row=\"1\"\n" +
                "        android:text=\"Button\" />\n" +
                "\n" +
                "    <Button\n" +
                "        android:id=\"@+id/button3\"\n" +
                "        android:layout_column=\"2\"\n" +
                "        android:layout_gravity=\"left|top\"\n" +
                "        android:layout_row=\"1\"\n" +
                "        android:text=\"Button\" />\n" +
                "\n" +
                "    <Space\n" +
                "        android:id=\"@+id/spacer_393\"\n" +
                "        android:layout_width=\"81dp\"\n" +
                "        android:layout_height=\"1dp\"\n" +
                "        android:layout_column=\"0\"\n" +
                "        android:layout_row=\"0\" />\n" +
                "\n" +
                "    \n" +
                "    <Space\n" +
                "        android:id=\"@+id/spacer_397\"\n" +
                "        android:layout_width=\"1dp\"\n" +
                "        android:layout_height=\"103dp\"\n" +
                "        android:layout_column=\"0\"\n" +
                "        android:layout_row=\"0\" />\n" +
                "\n" +
                "    \n" +
                "</GridLayout>";

        TestNode targetNode = TestNode.createFromXml(xml);
        assertNotNull(targetNode);

        TestNode layout = TestNode.findById(targetNode, "@+id/GridLayout1");
        TestNode button1 = TestNode.findById(targetNode, "@+id/button1");
        TestNode button2 = TestNode.findById(targetNode, "@+id/button2");
        TestNode button3 = TestNode.findById(targetNode, "@+id/button3");
        TestNode hspacer = TestNode.findById(targetNode, "@+id/spacer_393");
        TestNode vspacer = TestNode.findById(targetNode, "@+id/spacer_397");
        assertNotNull(layout);
        assertNotNull(button1);
        assertNotNull(button2);
        assertNotNull(button3);
        assertNotNull(hspacer);

        // Obtained by setting ViewHierarchy.DUMP_INFO=true:
        layout.bounds(new Rect(0, 109, 480, 800-109));
        button1.bounds(new Rect(182, 83, 298-182, 155-83));
        button2.bounds(new Rect(122, 155, 238-122, 227-155));
        button3.bounds(new Rect(238, 155, 354-238, 227-155));
        hspacer.bounds(new Rect(0, 0, 122, 2));
        vspacer.bounds(new Rect(0, 0, 2, 155));

        GridModel model = GridModel.get(new LayoutTestBase.TestRulesEngine(targetNode.getFqcn()),
                targetNode, null);
        assertEquals(3, model.declaredColumnCount);
        assertEquals(3, model.actualColumnCount);
        assertEquals(2, model.actualRowCount);

        ViewData view = model.getView(button3);
        assertNotNull(view);
        assertEquals(2, view.column);
        assertEquals(1, view.columnSpan);
        assertNull("1", view.node.getStringAttr(ANDROID_URI, ATTR_LAYOUT_COLUMN_SPAN));

        model.splitColumn(2, true, 10, 253);
        // TODO: Finish this test: Assert that the cells are in the right place
        //assertEquals(4, view.column);
        //assertEquals(1, view.columnSpan);
        //assertEquals("4", view.node.getStringAttr(ANDROID_URI, ATTR_LAYOUT_COLUMN_SPAN));
        fail("Finish test");
    }
}