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; | 5 package org.chromium.chrome.browser; |
6 | 6 |
7 import android.app.Dialog; | 7 import android.app.Dialog; |
8 import android.test.suitebuilder.annotation.SmallTest; | 8 import android.test.suitebuilder.annotation.SmallTest; |
9 import android.text.SpannableString; | 9 import android.text.SpannableString; |
10 import android.view.View; | 10 import android.view.View; |
| 11 import android.widget.ArrayAdapter; |
11 import android.widget.Button; | 12 import android.widget.Button; |
12 import android.widget.ListView; | 13 import android.widget.ListView; |
13 | 14 |
14 import org.chromium.base.ThreadUtils; | 15 import org.chromium.base.ThreadUtils; |
15 import org.chromium.chrome.R; | 16 import org.chromium.chrome.R; |
16 import org.chromium.chrome.test.ChromeActivityTestCaseBase; | 17 import org.chromium.chrome.test.ChromeActivityTestCaseBase; |
17 import org.chromium.content.browser.test.util.Criteria; | 18 import org.chromium.content.browser.test.util.Criteria; |
18 import org.chromium.content.browser.test.util.CriteriaHelper; | 19 import org.chromium.content.browser.test.util.CriteriaHelper; |
19 import org.chromium.content.browser.test.util.TouchCommon; | 20 import org.chromium.content.browser.test.util.TouchCommon; |
20 import org.chromium.ui.widget.TextViewWithClickableSpans; | 21 import org.chromium.ui.widget.TextViewWithClickableSpans; |
21 | 22 |
22 import java.util.ArrayList; | |
23 import java.util.List; | |
24 import java.util.concurrent.Callable; | 23 import java.util.concurrent.Callable; |
25 | 24 |
26 /** | 25 /** |
27 * Tests for the ItemChooserDialog class. | 26 * Tests for the ItemChooserDialog class. |
28 */ | 27 */ |
29 public class ItemChooserDialogTest extends ChromeActivityTestCaseBase<ChromeActi
vity> | 28 public class ItemChooserDialogTest extends ChromeActivityTestCaseBase<ChromeActi
vity> |
30 implements ItemChooserDialog.ItemSelectedCallback { | 29 implements ItemChooserDialog.ItemSelectedCallback { |
31 | 30 |
32 ItemChooserDialog mChooserDialog; | 31 ItemChooserDialog mChooserDialog; |
33 | 32 |
(...skipping 20 matching lines...) Expand all Loading... |
54 | 53 |
55 @Override | 54 @Override |
56 public void onItemSelected(String id) { | 55 public void onItemSelected(String id) { |
57 mLastSelectedId = id; | 56 mLastSelectedId = id; |
58 } | 57 } |
59 | 58 |
60 private ItemChooserDialog createDialog() { | 59 private ItemChooserDialog createDialog() { |
61 SpannableString title = new SpannableString("title"); | 60 SpannableString title = new SpannableString("title"); |
62 SpannableString searching = new SpannableString("searching"); | 61 SpannableString searching = new SpannableString("searching"); |
63 SpannableString noneFound = new SpannableString("noneFound"); | 62 SpannableString noneFound = new SpannableString("noneFound"); |
64 SpannableString statusActive = new SpannableString("statusActive"); | |
65 SpannableString statusIdleNoneFound = new SpannableString("statusIdleNon
eFound"); | 63 SpannableString statusIdleNoneFound = new SpannableString("statusIdleNon
eFound"); |
66 SpannableString statusIdleSomeFound = new SpannableString("statusIdleSom
eFound"); | 64 SpannableString statusIdleSomeFound = new SpannableString("statusIdleSom
eFound"); |
67 String positiveButton = new String("positiveButton"); | 65 String positiveButton = new String("positiveButton"); |
68 final ItemChooserDialog.ItemChooserLabels labels = | 66 final ItemChooserDialog.ItemChooserLabels labels = |
69 new ItemChooserDialog.ItemChooserLabels(title, searching, noneFo
und, statusActive, | 67 new ItemChooserDialog.ItemChooserLabels(title, searching, noneFo
und, |
70 statusIdleNoneFound, statusIdleSomeFound, positiveButton
); | 68 statusIdleNoneFound, statusIdleSomeFound, positiveButton
); |
71 ItemChooserDialog dialog = ThreadUtils.runOnUiThreadBlockingNoException( | 69 ItemChooserDialog dialog = ThreadUtils.runOnUiThreadBlockingNoException( |
72 new Callable<ItemChooserDialog>() { | 70 new Callable<ItemChooserDialog>() { |
73 @Override | 71 @Override |
74 public ItemChooserDialog call() { | 72 public ItemChooserDialog call() { |
75 ItemChooserDialog dialog = new ItemChooserDialog( | 73 ItemChooserDialog dialog = new ItemChooserDialog( |
76 getActivity(), ItemChooserDialogTest.this, l
abels); | 74 getActivity(), ItemChooserDialogTest.this, l
abels); |
77 return dialog; | 75 return dialog; |
78 } | 76 } |
79 }); | 77 }); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 dialog.findViewById(R.id.status); | 123 dialog.findViewById(R.id.status); |
126 final ListView items = (ListView) dialog.findViewById(R.id.items); | 124 final ListView items = (ListView) dialog.findViewById(R.id.items); |
127 final Button button = (Button) dialog.findViewById(R.id.positive); | 125 final Button button = (Button) dialog.findViewById(R.id.positive); |
128 | 126 |
129 // Before we add items to the dialog, the 'searching' message should be | 127 // Before we add items to the dialog, the 'searching' message should be |
130 // showing, the Commit button should be disabled and the list view hidde
n. | 128 // showing, the Commit button should be disabled and the list view hidde
n. |
131 assertEquals("searching", statusView.getText().toString()); | 129 assertEquals("searching", statusView.getText().toString()); |
132 assertFalse(button.isEnabled()); | 130 assertFalse(button.isEnabled()); |
133 assertEquals(View.GONE, items.getVisibility()); | 131 assertEquals(View.GONE, items.getVisibility()); |
134 | 132 |
135 List<ItemChooserDialog.ItemChooserRow> devices = | 133 mChooserDialog.addItemToList(new ItemChooserDialog.ItemChooserRow("key",
"key")); |
136 new ArrayList<ItemChooserDialog.ItemChooserRow>(); | 134 mChooserDialog.addItemToList(new ItemChooserDialog.ItemChooserRow("key2"
, "key2")); |
137 devices.add(new ItemChooserDialog.ItemChooserRow("key", "key")); | |
138 devices.add(new ItemChooserDialog.ItemChooserRow("key2", "key2")); | |
139 mChooserDialog.addItemsToList(devices); | |
140 | 135 |
141 // Two items showing, the empty view should be no more and the button | 136 // After discovery stops the list should be visible with two items, |
142 // should now be enabled. | |
143 assertEquals(View.VISIBLE, items.getVisibility()); | |
144 assertEquals(View.GONE, items.getEmptyView().getVisibility()); | |
145 assertEquals("statusActive", statusView.getText().toString()); | |
146 assertFalse(button.isEnabled()); | |
147 | |
148 mChooserDialog.setIdleState(); | |
149 // After discovery stops the list should still be visible, | |
150 // it should not show the empty view and the button should not be enable
d. | 137 // it should not show the empty view and the button should not be enable
d. |
151 // The chooser should show the status idle text. | 138 // The chooser should show the status idle text. |
152 assertEquals(View.VISIBLE, items.getVisibility()); | 139 assertEquals(View.VISIBLE, items.getVisibility()); |
153 assertEquals(View.GONE, items.getEmptyView().getVisibility()); | 140 assertEquals(View.GONE, items.getEmptyView().getVisibility()); |
154 assertEquals("statusIdleSomeFound", statusView.getText().toString()); | 141 assertEquals("statusIdleSomeFound", statusView.getText().toString()); |
155 assertFalse(button.isEnabled()); | 142 assertFalse(button.isEnabled()); |
156 | 143 |
157 // Select the first item and verify it got selected. | 144 // Select the first item and verify it got selected. |
158 selectItem(dialog, 1, "key", true); | 145 selectItem(dialog, 1, "key", true); |
159 | 146 |
(...skipping 26 matching lines...) Expand all Loading... |
186 assertFalse(button.isEnabled()); | 173 assertFalse(button.isEnabled()); |
187 | 174 |
188 mChooserDialog.dismiss(); | 175 mChooserDialog.dismiss(); |
189 } | 176 } |
190 | 177 |
191 @SmallTest | 178 @SmallTest |
192 public void testDisabledSelection() throws InterruptedException { | 179 public void testDisabledSelection() throws InterruptedException { |
193 Dialog dialog = mChooserDialog.getDialogForTesting(); | 180 Dialog dialog = mChooserDialog.getDialogForTesting(); |
194 assertTrue(dialog.isShowing()); | 181 assertTrue(dialog.isShowing()); |
195 | 182 |
196 List<ItemChooserDialog.ItemChooserRow> devices = | 183 mChooserDialog.addItemToList(new ItemChooserDialog.ItemChooserRow("key",
"key")); |
197 new ArrayList<ItemChooserDialog.ItemChooserRow>(); | 184 mChooserDialog.addItemToList(new ItemChooserDialog.ItemChooserRow("key2"
, "key2")); |
198 devices.add(new ItemChooserDialog.ItemChooserRow("key", "key")); | |
199 devices.add(new ItemChooserDialog.ItemChooserRow("key2", "key2")); | |
200 mChooserDialog.addItemsToList(devices); | |
201 | 185 |
202 // Disable one item and try to select it. | 186 // Disable one item and try to select it. |
203 mChooserDialog.setEnabled("key", false); | 187 mChooserDialog.setEnabled("key", false); |
204 selectItem(dialog, 1, "None", false); | 188 selectItem(dialog, 1, "None", false); |
205 // The other is still selectable. | 189 // The other is still selectable. |
206 selectItem(dialog, 2, "key2", true); | 190 selectItem(dialog, 2, "key2", true); |
207 | 191 |
208 mChooserDialog.dismiss(); | 192 mChooserDialog.dismiss(); |
209 } | 193 } |
| 194 |
| 195 @SmallTest |
| 196 public void testAddItemToListAndRemoveItemFromList() throws InterruptedExcep
tion { |
| 197 Dialog dialog = mChooserDialog.getDialogForTesting(); |
| 198 assertTrue(dialog.isShowing()); |
| 199 |
| 200 TextViewWithClickableSpans statusView = (TextViewWithClickableSpans) |
| 201 dialog.findViewById(R.id.status); |
| 202 final ListView items = (ListView) dialog.findViewById(R.id.items); |
| 203 final Button button = (Button) dialog.findViewById(R.id.positive); |
| 204 |
| 205 ArrayAdapter itemAdapter = mChooserDialog.getItemAdapterForTesting(); |
| 206 ItemChooserDialog.ItemChooserRow nonExistentItem = |
| 207 new ItemChooserDialog.ItemChooserRow("key", "key"); |
| 208 |
| 209 // Initially the itemAdapter is empty. |
| 210 assertTrue(itemAdapter.isEmpty()); |
| 211 |
| 212 // Try removing an item from an empty itemAdapter. |
| 213 mChooserDialog.removeItemFromList(nonExistentItem); |
| 214 assertTrue(itemAdapter.isEmpty()); |
| 215 |
| 216 // Add item 1. |
| 217 ItemChooserDialog.ItemChooserRow item1 = |
| 218 new ItemChooserDialog.ItemChooserRow("key1", "key1"); |
| 219 mChooserDialog.addItemToList(item1); |
| 220 assertEquals(1, itemAdapter.getCount()); |
| 221 assertEquals(itemAdapter.getItem(0), item1); |
| 222 |
| 223 // Add item 2. |
| 224 ItemChooserDialog.ItemChooserRow item2 = |
| 225 new ItemChooserDialog.ItemChooserRow("key2", "key2"); |
| 226 mChooserDialog.addItemToList(item2); |
| 227 assertEquals(2, itemAdapter.getCount()); |
| 228 assertEquals(itemAdapter.getItem(0), item1); |
| 229 assertEquals(itemAdapter.getItem(1), item2); |
| 230 |
| 231 // Try removing an item that doesn't exist. |
| 232 mChooserDialog.removeItemFromList(nonExistentItem); |
| 233 assertEquals(2, itemAdapter.getCount()); |
| 234 |
| 235 // Remove item 2. |
| 236 mChooserDialog.removeItemFromList(item2); |
| 237 assertEquals(1, itemAdapter.getCount()); |
| 238 // Make sure the remaining item is item 1. |
| 239 assertEquals(itemAdapter.getItem(0), item1); |
| 240 |
| 241 // The list should be visible with one item, it should not show |
| 242 // the empty view and the button should not be enabled. |
| 243 // The chooser should show a status message at the bottom. |
| 244 assertEquals(View.VISIBLE, items.getVisibility()); |
| 245 assertEquals(View.GONE, items.getEmptyView().getVisibility()); |
| 246 assertEquals("statusIdleSomeFound", statusView.getText().toString()); |
| 247 assertFalse(button.isEnabled()); |
| 248 |
| 249 // Remove item 1. |
| 250 mChooserDialog.removeItemFromList(item1); |
| 251 assertTrue(itemAdapter.isEmpty()); |
| 252 |
| 253 // Listview should now be showing empty, with an empty view visible |
| 254 // and the button should not be enabled. |
| 255 // The chooser should show a status message at the bottom. |
| 256 assertEquals(View.GONE, items.getVisibility()); |
| 257 assertEquals(View.VISIBLE, items.getEmptyView().getVisibility()); |
| 258 assertEquals("statusIdleNoneFound", statusView.getText().toString()); |
| 259 assertFalse(button.isEnabled()); |
| 260 |
| 261 mChooserDialog.dismiss(); |
| 262 } |
210 } | 263 } |
OLD | NEW |