| OLD | NEW |
| 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.test.InstrumentationTestCase; | 8 import android.test.InstrumentationTestCase; |
| 9 import android.test.UiThreadTest; | 9 import android.test.UiThreadTest; |
| 10 import android.test.suitebuilder.annotation.SmallTest; | 10 import android.test.suitebuilder.annotation.SmallTest; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 @Override | 32 @Override |
| 33 public void setUp() throws Exception { | 33 public void setUp() throws Exception { |
| 34 super.setUp(); | 34 super.setUp(); |
| 35 mContext = getInstrumentation().getTargetContext(); | 35 mContext = getInstrumentation().getTargetContext(); |
| 36 mContext.setTheme(R.style.MainTheme); | 36 mContext.setTheme(R.style.MainTheme); |
| 37 mMaxInfoBarWidth = mContext.getResources().getDimensionPixelSize(R.dimen
.infobar_max_width); | 37 mMaxInfoBarWidth = mContext.getResources().getDimensionPixelSize(R.dimen
.infobar_max_width); |
| 38 } | 38 } |
| 39 | 39 |
| 40 /** | 40 /** |
| 41 * A small control on the last line takes up the full width. |
| 42 */ |
| 43 @SmallTest |
| 44 @UiThreadTest |
| 45 public void testOneSmallControlTakesFullWidth() { |
| 46 InfoBarControlLayout layout = new InfoBarControlLayout(mContext); |
| 47 layout.setLayoutParams( |
| 48 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CO
NTENT)); |
| 49 View smallSwitch = layout.addSwitch(0, "A", SWITCH_ID_1, false); |
| 50 |
| 51 // Trigger the measurement algorithm. |
| 52 int parentWidthSpec = |
| 53 MeasureSpec.makeMeasureSpec(mMaxInfoBarWidth, MeasureSpec.AT_MOS
T); |
| 54 int parentHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPEC
IFIED); |
| 55 layout.measure(parentWidthSpec, parentHeightSpec); |
| 56 |
| 57 // Small control takes the full width of the layout because it's put on
its own line. |
| 58 ControlLayoutParams params = InfoBarControlLayout.getControlLayoutParams
(smallSwitch); |
| 59 assertEquals(0, params.top); |
| 60 assertEquals(0, params.start); |
| 61 assertEquals(2, params.columnsRequired); |
| 62 assertEquals(mMaxInfoBarWidth, smallSwitch.getMeasuredWidth()); |
| 63 } |
| 64 |
| 65 /** |
| 41 * Tests the layout algorithm on a set of five controls, the second of which
is a huge control | 66 * Tests the layout algorithm on a set of five controls, the second of which
is a huge control |
| 42 * and takes up the whole line. The other smaller controls try to pack them
selves as tightly | 67 * and takes up the whole line. The other smaller controls try to pack them
selves as tightly |
| 43 * as possible, resulting in a layout like this: | 68 * as possible, strecthing out if necessary for aesthetics, resulting in a l
ayout like this: |
| 44 * | 69 * |
| 45 * ----------------------- | 70 * ------------------------- |
| 46 * | A | empty | | 71 * | A (small) | |
| 47 * ----------------------- | 72 * ------------------------- |
| 48 * | B | | 73 * | B (big) | |
| 49 * ----------------------- | 74 * ------------------------- |
| 50 * | C | D | | 75 * | C (small) | D (small) | |
| 51 * ----------------------- | 76 * ------------------------- |
| 52 * | E | empty | | 77 * | E (small) | |
| 53 * ----------------------- | 78 * ------------------------- |
| 54 */ | 79 */ |
| 55 @SmallTest | 80 @SmallTest |
| 56 @UiThreadTest | 81 @UiThreadTest |
| 57 public void testComplexSwitchLayout() { | 82 public void testComplexSwitchLayout() { |
| 58 // Add five controls to the layout, with the second being as wide as the
layout. | 83 // Add five controls to the layout. |
| 59 InfoBarControlLayout layout = new InfoBarControlLayout(mContext); | 84 InfoBarControlLayout layout = new InfoBarControlLayout(mContext); |
| 60 layout.setLayoutParams( | 85 layout.setLayoutParams( |
| 61 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CO
NTENT)); | 86 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CO
NTENT)); |
| 62 | 87 |
| 63 View switch1 = layout.addSwitch(0, "A", SWITCH_ID_1, false); | 88 View switch1 = layout.addSwitch(0, "A", SWITCH_ID_1, false); |
| 64 View switch2 = layout.addSwitch(0, "B", SWITCH_ID_2, false); | 89 View switch2 = layout.addSwitch(0, "B", SWITCH_ID_2, false); |
| 65 View switch3 = layout.addSwitch(0, "C", SWITCH_ID_3, false); | 90 View switch3 = layout.addSwitch(0, "C", SWITCH_ID_3, false); |
| 66 View switch4 = layout.addSwitch(0, "D", SWITCH_ID_4, false); | 91 View switch4 = layout.addSwitch(0, "D", SWITCH_ID_4, false); |
| 67 View switch5 = layout.addSwitch(0, "E", SWITCH_ID_4, false); | 92 View switch5 = layout.addSwitch(0, "E", SWITCH_ID_4, false); |
| 93 |
| 94 // Make the second control require the full layout width. |
| 68 switch2.setMinimumWidth(mMaxInfoBarWidth); | 95 switch2.setMinimumWidth(mMaxInfoBarWidth); |
| 69 | 96 |
| 70 // Trigger the measurement algorithm. | 97 // Trigger the measurement algorithm. |
| 71 int parentWidthSpec = | 98 int parentWidthSpec = |
| 72 MeasureSpec.makeMeasureSpec(mMaxInfoBarWidth, MeasureSpec.AT_MOS
T); | 99 MeasureSpec.makeMeasureSpec(mMaxInfoBarWidth, MeasureSpec.AT_MOS
T); |
| 73 int parentHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPEC
IFIED); | 100 int parentHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPEC
IFIED); |
| 74 layout.measure(parentWidthSpec, parentHeightSpec); | 101 layout.measure(parentWidthSpec, parentHeightSpec); |
| 75 | 102 |
| 76 ControlLayoutParams params1 = InfoBarControlLayout.getControlLayoutParam
s(switch1); | 103 ControlLayoutParams params1 = InfoBarControlLayout.getControlLayoutParam
s(switch1); |
| 77 ControlLayoutParams params2 = InfoBarControlLayout.getControlLayoutParam
s(switch2); | 104 ControlLayoutParams params2 = InfoBarControlLayout.getControlLayoutParam
s(switch2); |
| 78 ControlLayoutParams params3 = InfoBarControlLayout.getControlLayoutParam
s(switch3); | 105 ControlLayoutParams params3 = InfoBarControlLayout.getControlLayoutParam
s(switch3); |
| 79 ControlLayoutParams params4 = InfoBarControlLayout.getControlLayoutParam
s(switch4); | 106 ControlLayoutParams params4 = InfoBarControlLayout.getControlLayoutParam
s(switch4); |
| 80 ControlLayoutParams params5 = InfoBarControlLayout.getControlLayoutParam
s(switch5); | 107 ControlLayoutParams params5 = InfoBarControlLayout.getControlLayoutParam
s(switch5); |
| 81 | 108 |
| 82 // Small control takes only half the width of the layout. | 109 // Small control takes the full width of the layout because the next one
doesn't fit. |
| 83 assertEquals(0, params1.top); | 110 assertEquals(0, params1.top); |
| 84 assertEquals(0, params1.start); | 111 assertEquals(0, params1.start); |
| 85 assertTrue(switch1.getMeasuredWidth() < mMaxInfoBarWidth); | 112 assertEquals(2, params1.columnsRequired); |
| 113 assertEquals(mMaxInfoBarWidth, switch1.getMeasuredWidth()); |
| 86 | 114 |
| 87 // Big control gets shunted onto the next row and takes up the whole spa
ce. | 115 // Big control gets shunted onto the next row and takes up the whole spa
ce. |
| 88 assertTrue(params2.top > switch1.getMeasuredHeight()); | 116 assertTrue(params2.top > switch1.getMeasuredHeight()); |
| 89 assertEquals(0, params2.start); | 117 assertEquals(0, params2.start); |
| 118 assertEquals(2, params2.columnsRequired); |
| 90 assertEquals(mMaxInfoBarWidth, switch2.getMeasuredWidth()); | 119 assertEquals(mMaxInfoBarWidth, switch2.getMeasuredWidth()); |
| 91 | 120 |
| 92 // Small control gets placed onto the next line and takes only half the
width. | 121 // Small control gets placed onto the next line and takes only half the
width. |
| 93 int bottomOfSwitch2 = params2.top + switch2.getMeasuredHeight(); | 122 int bottomOfSwitch2 = params2.top + switch2.getMeasuredHeight(); |
| 94 assertTrue(params3.top > bottomOfSwitch2); | 123 assertTrue(params3.top > bottomOfSwitch2); |
| 95 assertEquals(0, params3.start); | 124 assertEquals(0, params3.start); |
| 125 assertEquals(1, params3.columnsRequired); |
| 96 assertTrue(switch3.getMeasuredWidth() < mMaxInfoBarWidth); | 126 assertTrue(switch3.getMeasuredWidth() < mMaxInfoBarWidth); |
| 97 | 127 |
| 98 // Small control gets placed next to the previous small control. | 128 // Small control gets placed next to the previous small control. |
| 99 assertEquals(params3.top, params4.top); | 129 assertEquals(params3.top, params4.top); |
| 100 assertTrue(params4.start > switch3.getMeasuredWidth()); | 130 assertTrue(params4.start > switch3.getMeasuredWidth()); |
| 131 assertEquals(1, params4.columnsRequired); |
| 101 assertTrue(switch4.getMeasuredWidth() < mMaxInfoBarWidth); | 132 assertTrue(switch4.getMeasuredWidth() < mMaxInfoBarWidth); |
| 102 | 133 |
| 103 // Last small control has no room left and gets put on its own line. | 134 // Last small control has no room left and gets put on its own line, tak
ing the full width. |
| 104 int bottomOfSwitch4 = params4.top + switch4.getMeasuredHeight(); | 135 int bottomOfSwitch4 = params4.top + switch4.getMeasuredHeight(); |
| 105 assertTrue(params5.top > bottomOfSwitch4); | 136 assertTrue(params5.top > bottomOfSwitch4); |
| 106 assertEquals(0, params5.start); | 137 assertEquals(0, params5.start); |
| 107 assertTrue(switch5.getMeasuredWidth() < mMaxInfoBarWidth); | 138 assertEquals(2, params5.columnsRequired); |
| 139 assertEquals(mMaxInfoBarWidth, switch5.getMeasuredWidth()); |
| 108 } | 140 } |
| 109 | 141 |
| 110 /** | 142 /** |
| 111 * Tests that the message is always the full width of the layout. | 143 * Tests that the message is always the full width of the layout. |
| 112 */ | 144 */ |
| 113 @SmallTest | 145 @SmallTest |
| 114 @UiThreadTest | 146 @UiThreadTest |
| 115 public void testFullWidthMessageControl() { | 147 public void testFullWidthMessageControl() { |
| 116 // Add two controls to the layout. The main message automatically reque
sts the full width. | 148 // Add two controls to the layout. The main message automatically reque
sts the full width. |
| 117 InfoBarControlLayout layout = new InfoBarControlLayout(mContext); | 149 InfoBarControlLayout layout = new InfoBarControlLayout(mContext); |
| 118 layout.setLayoutParams( | 150 layout.setLayoutParams( |
| 119 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CO
NTENT)); | 151 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CO
NTENT)); |
| 120 | 152 |
| 121 View view1 = layout.addMainMessage("A"); | 153 View view1 = layout.addMainMessage("A"); |
| 122 View view2 = layout.addSwitch(0, "B", SWITCH_ID_2, false); | 154 View view2 = layout.addSwitch(0, "B", SWITCH_ID_2, false); |
| 123 | 155 |
| 124 // Trigger the measurement algorithm. | 156 // Trigger the measurement algorithm. |
| 125 int parentWidthSpec = | 157 int parentWidthSpec = |
| 126 MeasureSpec.makeMeasureSpec(mMaxInfoBarWidth, MeasureSpec.AT_MOS
T); | 158 MeasureSpec.makeMeasureSpec(mMaxInfoBarWidth, MeasureSpec.AT_MOS
T); |
| 127 int parentHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPEC
IFIED); | 159 int parentHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPEC
IFIED); |
| 128 layout.measure(parentWidthSpec, parentHeightSpec); | 160 layout.measure(parentWidthSpec, parentHeightSpec); |
| 129 | 161 |
| 130 ControlLayoutParams params1 = InfoBarControlLayout.getControlLayoutParam
s(view1); | 162 ControlLayoutParams params1 = InfoBarControlLayout.getControlLayoutParam
s(view1); |
| 131 ControlLayoutParams params2 = InfoBarControlLayout.getControlLayoutParam
s(view2); | 163 ControlLayoutParams params2 = InfoBarControlLayout.getControlLayoutParam
s(view2); |
| 132 | 164 |
| 133 // Main message takes up the full space. | 165 // Main message takes up the full space. |
| 134 assertEquals(0, params1.top); | 166 assertEquals(0, params1.top); |
| 135 assertEquals(0, params1.start); | 167 assertEquals(0, params1.start); |
| 168 assertEquals(2, params1.columnsRequired); |
| 136 assertEquals(mMaxInfoBarWidth, view1.getMeasuredWidth()); | 169 assertEquals(mMaxInfoBarWidth, view1.getMeasuredWidth()); |
| 137 | 170 |
| 138 // Small control gets shunted onto the next row. | 171 // Small control gets shunted onto the next row. |
| 139 assertTrue(params2.top > view1.getMeasuredHeight()); | 172 assertTrue(params2.top > view1.getMeasuredHeight()); |
| 140 assertEquals(0, params2.start); | 173 assertEquals(0, params2.start); |
| 141 assertTrue(mMaxInfoBarWidth > view2.getMeasuredWidth()); | 174 assertEquals(2, params2.columnsRequired); |
| 175 assertEquals(mMaxInfoBarWidth, view2.getMeasuredWidth()); |
| 142 } | 176 } |
| 143 } | 177 } |
| OLD | NEW |