Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/infobar/InfoBarControlLayoutTest.java

Issue 2764173003: Convert chrome InstrumentationTestCases to JUnit4 [2] (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.infobar; 5 package org.chromium.chrome.browser.infobar;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 import android.support.test.InstrumentationRegistry;
8 import android.support.test.filters.SmallTest; 9 import android.support.test.filters.SmallTest;
9 import android.test.InstrumentationTestCase;
10 import android.test.UiThreadTest; 10 import android.test.UiThreadTest;
11 import android.view.View; 11 import android.view.View;
12 import android.view.View.MeasureSpec; 12 import android.view.View.MeasureSpec;
13 import android.view.ViewGroup.LayoutParams; 13 import android.view.ViewGroup.LayoutParams;
14 14
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19
15 import org.chromium.chrome.R; 20 import org.chromium.chrome.R;
16 import org.chromium.chrome.browser.infobar.InfoBarControlLayout.ControlLayoutPar ams; 21 import org.chromium.chrome.browser.infobar.InfoBarControlLayout.ControlLayoutPar ams;
22 import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
17 23
18 /** 24 /**
19 * Tests for InfoBarControlLayout. This suite doesn't check for specific detail s, like margins 25 * Tests for InfoBarControlLayout. This suite doesn't check for specific detail s, like margins
20 * paddings, and instead focuses on whether controls are placed correctly. 26 * paddings, and instead focuses on whether controls are placed correctly.
21 */ 27 */
22 public class InfoBarControlLayoutTest extends InstrumentationTestCase { 28 @RunWith(ChromeJUnit4ClassRunner.class)
29 public class InfoBarControlLayoutTest {
23 private static final int SWITCH_ID_1 = 1; 30 private static final int SWITCH_ID_1 = 1;
24 private static final int SWITCH_ID_2 = 2; 31 private static final int SWITCH_ID_2 = 2;
25 private static final int SWITCH_ID_3 = 3; 32 private static final int SWITCH_ID_3 = 3;
26 private static final int SWITCH_ID_4 = 4; 33 private static final int SWITCH_ID_4 = 4;
27 private static final int SWITCH_ID_5 = 5; 34 private static final int SWITCH_ID_5 = 5;
28 private static final int INFOBAR_WIDTH = 3200; 35 private static final int INFOBAR_WIDTH = 3200;
29 36
30 private Context mContext; 37 private Context mContext;
31 38
32 @Override 39 @Before
33 public void setUp() throws Exception { 40 public void setUp() throws Exception {
34 super.setUp(); 41 mContext = InstrumentationRegistry.getInstrumentation().getTargetContext ();
35 mContext = getInstrumentation().getTargetContext();
36 mContext.setTheme(R.style.MainTheme); 42 mContext.setTheme(R.style.MainTheme);
37 } 43 }
38 44
39 /** 45 /**
40 * A small control on the last line takes up the full width. 46 * A small control on the last line takes up the full width.
41 */ 47 */
48 @Test
42 @SmallTest 49 @SmallTest
43 @UiThreadTest 50 @UiThreadTest
44 public void testOneSmallControlTakesFullWidth() { 51 public void testOneSmallControlTakesFullWidth() {
45 InfoBarControlLayout layout = new InfoBarControlLayout(mContext); 52 InfoBarControlLayout layout = new InfoBarControlLayout(mContext);
46 layout.setLayoutParams( 53 layout.setLayoutParams(
47 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CO NTENT)); 54 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CO NTENT));
48 View smallSwitch = layout.addSwitch(0, 0, "A", SWITCH_ID_1, false); 55 View smallSwitch = layout.addSwitch(0, 0, "A", SWITCH_ID_1, false);
49 56
50 // Trigger the measurement algorithm. 57 // Trigger the measurement algorithm.
51 int parentWidthSpec = 58 int parentWidthSpec =
52 MeasureSpec.makeMeasureSpec(INFOBAR_WIDTH, MeasureSpec.AT_MOST); 59 MeasureSpec.makeMeasureSpec(INFOBAR_WIDTH, MeasureSpec.AT_MOST);
53 int parentHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPEC IFIED); 60 int parentHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPEC IFIED);
54 layout.measure(parentWidthSpec, parentHeightSpec); 61 layout.measure(parentWidthSpec, parentHeightSpec);
55 62
56 // Small control takes the full width of the layout because it's put on its own line. 63 // Small control takes the full width of the layout because it's put on its own line.
57 ControlLayoutParams params = InfoBarControlLayout.getControlLayoutParams (smallSwitch); 64 ControlLayoutParams params = InfoBarControlLayout.getControlLayoutParams (smallSwitch);
58 assertEquals(0, params.top); 65 Assert.assertEquals(0, params.top);
59 assertEquals(0, params.start); 66 Assert.assertEquals(0, params.start);
60 assertEquals(2, params.columnsRequired); 67 Assert.assertEquals(2, params.columnsRequired);
61 assertEquals(INFOBAR_WIDTH, smallSwitch.getMeasuredWidth()); 68 Assert.assertEquals(INFOBAR_WIDTH, smallSwitch.getMeasuredWidth());
62 } 69 }
63 70
64 /** 71 /**
65 * Tests the layout algorithm on a set of five controls, the second of which is a huge control 72 * Tests the layout algorithm on a set of five controls, the second of which is a huge control
66 * and takes up the whole line. The other smaller controls try to pack them selves as tightly 73 * and takes up the whole line. The other smaller controls try to pack them selves as tightly
67 * as possible, strecthing out if necessary for aesthetics, resulting in a l ayout like this: 74 * as possible, strecthing out if necessary for aesthetics, resulting in a l ayout like this:
68 * 75 *
69 * ------------------------- 76 * -------------------------
70 * | A (small) | 77 * | A (small) |
71 * ------------------------- 78 * -------------------------
72 * | B (big) | 79 * | B (big) |
73 * ------------------------- 80 * -------------------------
74 * | C (small) | D (small) | 81 * | C (small) | D (small) |
75 * ------------------------- 82 * -------------------------
76 * | E (small) | 83 * | E (small) |
77 * ------------------------- 84 * -------------------------
78 */ 85 */
86 @Test
79 @SmallTest 87 @SmallTest
80 @UiThreadTest 88 @UiThreadTest
81 public void testComplexSwitchLayout() { 89 public void testComplexSwitchLayout() {
82 // Add five controls to the layout. 90 // Add five controls to the layout.
83 InfoBarControlLayout layout = new InfoBarControlLayout(mContext); 91 InfoBarControlLayout layout = new InfoBarControlLayout(mContext);
84 layout.setLayoutParams( 92 layout.setLayoutParams(
85 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CO NTENT)); 93 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CO NTENT));
86 94
87 View switch1 = layout.addSwitch(0, 0, "A", SWITCH_ID_1, false); 95 View switch1 = layout.addSwitch(0, 0, "A", SWITCH_ID_1, false);
88 View switch2 = layout.addSwitch(0, 0, "B", SWITCH_ID_2, false); 96 View switch2 = layout.addSwitch(0, 0, "B", SWITCH_ID_2, false);
(...skipping 10 matching lines...) Expand all
99 int parentHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPEC IFIED); 107 int parentHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPEC IFIED);
100 layout.measure(parentWidthSpec, parentHeightSpec); 108 layout.measure(parentWidthSpec, parentHeightSpec);
101 109
102 ControlLayoutParams params1 = InfoBarControlLayout.getControlLayoutParam s(switch1); 110 ControlLayoutParams params1 = InfoBarControlLayout.getControlLayoutParam s(switch1);
103 ControlLayoutParams params2 = InfoBarControlLayout.getControlLayoutParam s(switch2); 111 ControlLayoutParams params2 = InfoBarControlLayout.getControlLayoutParam s(switch2);
104 ControlLayoutParams params3 = InfoBarControlLayout.getControlLayoutParam s(switch3); 112 ControlLayoutParams params3 = InfoBarControlLayout.getControlLayoutParam s(switch3);
105 ControlLayoutParams params4 = InfoBarControlLayout.getControlLayoutParam s(switch4); 113 ControlLayoutParams params4 = InfoBarControlLayout.getControlLayoutParam s(switch4);
106 ControlLayoutParams params5 = InfoBarControlLayout.getControlLayoutParam s(switch5); 114 ControlLayoutParams params5 = InfoBarControlLayout.getControlLayoutParam s(switch5);
107 115
108 // Small control takes the full width of the layout because the next one doesn't fit. 116 // Small control takes the full width of the layout because the next one doesn't fit.
109 assertEquals(0, params1.top); 117 Assert.assertEquals(0, params1.top);
110 assertEquals(0, params1.start); 118 Assert.assertEquals(0, params1.start);
111 assertEquals(2, params1.columnsRequired); 119 Assert.assertEquals(2, params1.columnsRequired);
112 assertEquals(INFOBAR_WIDTH, switch1.getMeasuredWidth()); 120 Assert.assertEquals(INFOBAR_WIDTH, switch1.getMeasuredWidth());
113 121
114 // Big control gets shunted onto the next row and takes up the whole spa ce. 122 // Big control gets shunted onto the next row and takes up the whole spa ce.
115 assertTrue(params2.top > switch1.getMeasuredHeight()); 123 Assert.assertTrue(params2.top > switch1.getMeasuredHeight());
116 assertEquals(0, params2.start); 124 Assert.assertEquals(0, params2.start);
117 assertEquals(2, params2.columnsRequired); 125 Assert.assertEquals(2, params2.columnsRequired);
118 assertEquals(INFOBAR_WIDTH, switch2.getMeasuredWidth()); 126 Assert.assertEquals(INFOBAR_WIDTH, switch2.getMeasuredWidth());
119 127
120 // Small control gets placed onto the next line and takes only half the width. 128 // Small control gets placed onto the next line and takes only half the width.
121 int bottomOfSwitch2 = params2.top + switch2.getMeasuredHeight(); 129 int bottomOfSwitch2 = params2.top + switch2.getMeasuredHeight();
122 assertTrue(params3.top > bottomOfSwitch2); 130 Assert.assertTrue(params3.top > bottomOfSwitch2);
123 assertEquals(0, params3.start); 131 Assert.assertEquals(0, params3.start);
124 assertEquals(1, params3.columnsRequired); 132 Assert.assertEquals(1, params3.columnsRequired);
125 assertTrue(switch3.getMeasuredWidth() < INFOBAR_WIDTH); 133 Assert.assertTrue(switch3.getMeasuredWidth() < INFOBAR_WIDTH);
126 134
127 // Small control gets placed next to the previous small control. 135 // Small control gets placed next to the previous small control.
128 assertEquals(params3.top, params4.top); 136 Assert.assertEquals(params3.top, params4.top);
129 assertTrue(params4.start > switch3.getMeasuredWidth()); 137 Assert.assertTrue(params4.start > switch3.getMeasuredWidth());
130 assertEquals(1, params4.columnsRequired); 138 Assert.assertEquals(1, params4.columnsRequired);
131 assertTrue(switch4.getMeasuredWidth() < INFOBAR_WIDTH); 139 Assert.assertTrue(switch4.getMeasuredWidth() < INFOBAR_WIDTH);
132 140
133 // Last small control has no room left and gets put on its own line, tak ing the full width. 141 // Last small control has no room left and gets put on its own line, tak ing the full width.
134 int bottomOfSwitch4 = params4.top + switch4.getMeasuredHeight(); 142 int bottomOfSwitch4 = params4.top + switch4.getMeasuredHeight();
135 assertTrue(params5.top > bottomOfSwitch4); 143 Assert.assertTrue(params5.top > bottomOfSwitch4);
136 assertEquals(0, params5.start); 144 Assert.assertEquals(0, params5.start);
137 assertEquals(2, params5.columnsRequired); 145 Assert.assertEquals(2, params5.columnsRequired);
138 assertEquals(INFOBAR_WIDTH, switch5.getMeasuredWidth()); 146 Assert.assertEquals(INFOBAR_WIDTH, switch5.getMeasuredWidth());
139 } 147 }
140 148
141 /** 149 /**
142 * Tests that the message is always the full width of the layout. 150 * Tests that the message is always the full width of the layout.
143 */ 151 */
152 @Test
144 @SmallTest 153 @SmallTest
145 @UiThreadTest 154 @UiThreadTest
146 public void testFullWidthMessageControl() { 155 public void testFullWidthMessageControl() {
147 // Add two controls to the layout. The main message automatically reque sts the full width. 156 // Add two controls to the layout. The main message automatically reque sts the full width.
148 InfoBarControlLayout layout = new InfoBarControlLayout(mContext); 157 InfoBarControlLayout layout = new InfoBarControlLayout(mContext);
149 layout.setLayoutParams( 158 layout.setLayoutParams(
150 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CO NTENT)); 159 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CO NTENT));
151 160
152 View view1 = layout.addMainMessage("A"); 161 View view1 = layout.addMainMessage("A");
153 View view2 = layout.addSwitch(0, 0, "B", SWITCH_ID_2, false); 162 View view2 = layout.addSwitch(0, 0, "B", SWITCH_ID_2, false);
154 163
155 // Trigger the measurement algorithm. 164 // Trigger the measurement algorithm.
156 int parentWidthSpec = 165 int parentWidthSpec =
157 MeasureSpec.makeMeasureSpec(INFOBAR_WIDTH, MeasureSpec.AT_MOST); 166 MeasureSpec.makeMeasureSpec(INFOBAR_WIDTH, MeasureSpec.AT_MOST);
158 int parentHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPEC IFIED); 167 int parentHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPEC IFIED);
159 layout.measure(parentWidthSpec, parentHeightSpec); 168 layout.measure(parentWidthSpec, parentHeightSpec);
160 169
161 ControlLayoutParams params1 = InfoBarControlLayout.getControlLayoutParam s(view1); 170 ControlLayoutParams params1 = InfoBarControlLayout.getControlLayoutParam s(view1);
162 ControlLayoutParams params2 = InfoBarControlLayout.getControlLayoutParam s(view2); 171 ControlLayoutParams params2 = InfoBarControlLayout.getControlLayoutParam s(view2);
163 172
164 // Main message takes up the full space. 173 // Main message takes up the full space.
165 assertEquals(0, params1.top); 174 Assert.assertEquals(0, params1.top);
166 assertEquals(0, params1.start); 175 Assert.assertEquals(0, params1.start);
167 assertEquals(2, params1.columnsRequired); 176 Assert.assertEquals(2, params1.columnsRequired);
168 assertEquals(INFOBAR_WIDTH, view1.getMeasuredWidth()); 177 Assert.assertEquals(INFOBAR_WIDTH, view1.getMeasuredWidth());
169 178
170 // Small control gets shunted onto the next row. 179 // Small control gets shunted onto the next row.
171 assertTrue(params2.top > view1.getMeasuredHeight()); 180 Assert.assertTrue(params2.top > view1.getMeasuredHeight());
172 assertEquals(0, params2.start); 181 Assert.assertEquals(0, params2.start);
173 assertEquals(2, params2.columnsRequired); 182 Assert.assertEquals(2, params2.columnsRequired);
174 assertEquals(INFOBAR_WIDTH, view2.getMeasuredWidth()); 183 Assert.assertEquals(INFOBAR_WIDTH, view2.getMeasuredWidth());
175 } 184 }
176 } 185 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698