| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.widget; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.support.test.InstrumentationRegistry; | |
| 9 import android.support.test.filters.SmallTest; | |
| 10 import android.support.test.rule.UiThreadTestRule; | |
| 11 import android.test.UiThreadTest; | |
| 12 import android.view.View; | |
| 13 import android.view.ViewGroup.MarginLayoutParams; | |
| 14 import android.widget.RadioButton; | |
| 15 | |
| 16 import org.junit.Assert; | |
| 17 import org.junit.Before; | |
| 18 import org.junit.Rule; | |
| 19 import org.junit.Test; | |
| 20 import org.junit.runner.RunWith; | |
| 21 | |
| 22 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; | |
| 23 | |
| 24 import java.util.ArrayList; | |
| 25 import java.util.Arrays; | |
| 26 import java.util.List; | |
| 27 | |
| 28 /** | |
| 29 * Tests for {@link RadioButtonLayout}. | |
| 30 */ | |
| 31 @RunWith(ChromeJUnit4ClassRunner.class) | |
| 32 public class RadioButtonLayoutTest { | |
| 33 @Rule | |
| 34 public UiThreadTestRule mRule = new UiThreadTestRule(); | |
| 35 | |
| 36 private Context mContext; | |
| 37 | |
| 38 @Before | |
| 39 public void setUp() { | |
| 40 mContext = InstrumentationRegistry.getInstrumentation().getTargetContext
(); | |
| 41 } | |
| 42 | |
| 43 @Test | |
| 44 @SmallTest | |
| 45 @UiThreadTest | |
| 46 public void testMargins() { | |
| 47 RadioButtonLayout layout = new RadioButtonLayout(mContext); | |
| 48 | |
| 49 // Add one set of options. | |
| 50 List<CharSequence> messages = new ArrayList<CharSequence>(); | |
| 51 messages.add("a"); | |
| 52 messages.add("b"); | |
| 53 messages.add("c"); | |
| 54 layout.addOptions(messages, null); | |
| 55 | |
| 56 // Test the margins. | |
| 57 for (int i = 0; i < layout.getChildCount(); i++) { | |
| 58 View child = layout.getChildAt(i); | |
| 59 MarginLayoutParams params = (MarginLayoutParams) child.getLayoutPara
ms(); | |
| 60 | |
| 61 if (i < layout.getChildCount() - 1) { | |
| 62 Assert.assertNotEquals(0, params.bottomMargin); | |
| 63 } else { | |
| 64 Assert.assertEquals(0, params.bottomMargin); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 // Add more options. | |
| 69 List<CharSequence> moreMessages = new ArrayList<CharSequence>(); | |
| 70 moreMessages.add("d"); | |
| 71 moreMessages.add("e"); | |
| 72 moreMessages.add("f"); | |
| 73 layout.addOptions(moreMessages, null); | |
| 74 | |
| 75 // Test the margins. | |
| 76 for (int i = 0; i < layout.getChildCount(); i++) { | |
| 77 View child = layout.getChildAt(i); | |
| 78 MarginLayoutParams params = (MarginLayoutParams) child.getLayoutPara
ms(); | |
| 79 | |
| 80 if (i < layout.getChildCount() - 1) { | |
| 81 Assert.assertNotEquals(0, params.bottomMargin); | |
| 82 } else { | |
| 83 Assert.assertEquals(0, params.bottomMargin); | |
| 84 } | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 @Test | |
| 89 @SmallTest | |
| 90 @UiThreadTest | |
| 91 public void testAddOptions() { | |
| 92 RadioButtonLayout layout = new RadioButtonLayout(mContext); | |
| 93 | |
| 94 // Add one set of options. | |
| 95 List<CharSequence> messages = new ArrayList<CharSequence>(); | |
| 96 messages.add("a"); | |
| 97 messages.add("b"); | |
| 98 messages.add("c"); | |
| 99 List<String> tags = Arrays.asList("tag 1", "tag 2", "tag 3"); | |
| 100 layout.addOptions(messages, tags); | |
| 101 Assert.assertEquals(3, layout.getChildCount()); | |
| 102 for (int i = 0; i < layout.getChildCount(); i++) { | |
| 103 Assert.assertEquals(messages.get(i), ((RadioButton) layout.getChildA
t(i)).getText()); | |
| 104 Assert.assertEquals(tags.get(i), ((RadioButton) layout.getChildAt(i)
).getTag()); | |
| 105 } | |
| 106 | |
| 107 // Add even more options, but without tags. | |
| 108 List<CharSequence> moreMessages = new ArrayList<CharSequence>(); | |
| 109 moreMessages.add("d"); | |
| 110 moreMessages.add("e"); | |
| 111 moreMessages.add("f"); | |
| 112 layout.addOptions(moreMessages, null); | |
| 113 Assert.assertEquals(6, layout.getChildCount()); | |
| 114 for (int i = 0; i < 3; i++) { | |
| 115 Assert.assertEquals(messages.get(i), ((RadioButton) layout.getChildA
t(i)).getText()); | |
| 116 Assert.assertEquals(tags.get(i), ((RadioButton) layout.getChildAt(i)
).getTag()); | |
| 117 } | |
| 118 for (int i = 3; i < 6; i++) { | |
| 119 Assert.assertEquals( | |
| 120 moreMessages.get(i - 3), ((RadioButton) layout.getChildAt(i)
).getText()); | |
| 121 Assert.assertNull(((RadioButton) layout.getChildAt(i)).getTag()); | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 @Test | |
| 126 @SmallTest | |
| 127 @UiThreadTest | |
| 128 public void testSelection() { | |
| 129 RadioButtonLayout layout = new RadioButtonLayout(mContext); | |
| 130 | |
| 131 // Add one set of options. | |
| 132 List<CharSequence> messages = new ArrayList<CharSequence>(); | |
| 133 messages.add("a"); | |
| 134 messages.add("b"); | |
| 135 messages.add("c"); | |
| 136 layout.addOptions(messages, null); | |
| 137 Assert.assertEquals(3, layout.getChildCount()); | |
| 138 | |
| 139 // Nothing should be selected by default. | |
| 140 for (int i = 0; i < layout.getChildCount(); i++) { | |
| 141 RadioButton child = (RadioButton) layout.getChildAt(i); | |
| 142 Assert.assertFalse(child.isChecked()); | |
| 143 } | |
| 144 | |
| 145 // Select the second one. | |
| 146 layout.selectChildAtIndex(1); | |
| 147 for (int i = 0; i < layout.getChildCount(); i++) { | |
| 148 RadioButton child = (RadioButton) layout.getChildAt(i); | |
| 149 Assert.assertEquals(i == 1, child.isChecked()); | |
| 150 } | |
| 151 | |
| 152 // Add even more options. | |
| 153 List<CharSequence> moreMessages = new ArrayList<CharSequence>(); | |
| 154 moreMessages.add("d"); | |
| 155 moreMessages.add("e"); | |
| 156 moreMessages.add("f"); | |
| 157 layout.addOptions(moreMessages, null); | |
| 158 Assert.assertEquals(6, layout.getChildCount()); | |
| 159 | |
| 160 // Second child should still be checked. | |
| 161 for (int i = 0; i < layout.getChildCount(); i++) { | |
| 162 RadioButton child = (RadioButton) layout.getChildAt(i); | |
| 163 Assert.assertEquals(i == 1, child.isChecked()); | |
| 164 } | |
| 165 } | |
| 166 } | |
| OLD | NEW |