Chromium Code Reviews| 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; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 /** | 25 /** |
| 26 * Tests for the ItemChooserDialog class. | 26 * Tests for the ItemChooserDialog class. |
| 27 */ | 27 */ |
| 28 public class ItemChooserDialogTest extends ChromeActivityTestCaseBase<ChromeActi vity> | 28 public class ItemChooserDialogTest extends ChromeActivityTestCaseBase<ChromeActi vity> |
| 29 implements ItemChooserDialog.ItemSelectedCallback { | 29 implements ItemChooserDialog.ItemSelectedCallback { |
| 30 | 30 |
| 31 ItemChooserDialog mChooserDialog; | 31 ItemChooserDialog mChooserDialog; |
| 32 | 32 |
| 33 String mLastSelectedId = "None"; | 33 String mLastSelectedId = "None"; |
| 34 | 34 |
| 35 private static final ItemChooserDialog.ItemChooserRowIcon TEST_ICON1 = | |
| 36 new ItemChooserDialog.ItemChooserRowIcon( | |
| 37 R.drawable.ic_signal_cellular_0_bar_grey600_24dp, | |
| 38 R.drawable.ic_signal_cellular_0_bar_white_24dp, "test icon1" ); | |
| 39 private static final ItemChooserDialog.ItemChooserRowIcon TEST_ICON2 = | |
| 40 new ItemChooserDialog.ItemChooserRowIcon( | |
| 41 R.drawable.ic_signal_cellular_1_bar_grey600_24dp, | |
| 42 R.drawable.ic_signal_cellular_1_bar_white_24dp, "test icon2" ); | |
| 43 | |
| 35 public ItemChooserDialogTest() { | 44 public ItemChooserDialogTest() { |
| 36 super(ChromeActivity.class); | 45 super(ChromeActivity.class); |
| 37 } | 46 } |
| 38 | 47 |
| 39 // ChromeActivityTestCaseBase: | 48 // ChromeActivityTestCaseBase: |
| 40 | 49 |
| 41 @Override | 50 @Override |
| 42 protected void setUp() throws Exception { | 51 protected void setUp() throws Exception { |
| 43 super.setUp(); | 52 super.setUp(); |
| 44 mChooserDialog = createDialog(); | 53 mChooserDialog = createDialog(); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 108 | 117 |
| 109 CriteriaHelper.pollUiThread( | 118 CriteriaHelper.pollUiThread( |
| 110 Criteria.equals(expectedItemId, new Callable<String>() { | 119 Criteria.equals(expectedItemId, new Callable<String>() { |
| 111 @Override | 120 @Override |
| 112 public String call() { | 121 public String call() { |
| 113 return mLastSelectedId; | 122 return mLastSelectedId; |
| 114 } | 123 } |
| 115 })); | 124 })); |
| 116 } | 125 } |
| 117 | 126 |
| 127 private int getImageContainerVisibilityForItem(ListView items, int pos) { | |
| 128 final int first = items.getFirstVisiblePosition(); | |
| 129 final int last = first + items.getChildCount() - 1; | |
|
juncai
2016/09/12 20:05:49
Maybe use: getLastVisiblePosition() here for |last
ortuno
2016/09/13 03:00:45
Done.
| |
| 130 | |
| 131 View item; | |
| 132 if (pos < first || pos > last) { | |
| 133 item = items.getAdapter().getView(pos, null, items); | |
| 134 } else { | |
| 135 final int visiblePos = pos - first; | |
| 136 item = items.getChildAt(visiblePos); | |
| 137 } | |
| 138 return item.findViewById(R.id.imageContainer).getVisibility(); | |
| 139 } | |
| 140 | |
| 118 @SmallTest | 141 @SmallTest |
| 119 public void testSimpleItemSelection() throws InterruptedException { | 142 public void testSimpleItemSelection() throws InterruptedException { |
| 120 Dialog dialog = mChooserDialog.getDialogForTesting(); | 143 Dialog dialog = mChooserDialog.getDialogForTesting(); |
| 121 assertTrue(dialog.isShowing()); | 144 assertTrue(dialog.isShowing()); |
| 122 | 145 |
| 123 TextViewWithClickableSpans statusView = (TextViewWithClickableSpans) | 146 TextViewWithClickableSpans statusView = (TextViewWithClickableSpans) |
| 124 dialog.findViewById(R.id.status); | 147 dialog.findViewById(R.id.status); |
| 125 final ListView items = (ListView) dialog.findViewById(R.id.items); | 148 final ListView items = (ListView) dialog.findViewById(R.id.items); |
| 126 final Button button = (Button) dialog.findViewById(R.id.positive); | 149 final Button button = (Button) dialog.findViewById(R.id.positive); |
| 127 | 150 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 195 // Disable one item and try to select it. | 218 // Disable one item and try to select it. |
| 196 mChooserDialog.setEnabled("key", false); | 219 mChooserDialog.setEnabled("key", false); |
| 197 selectItem(dialog, 1, "None", false); | 220 selectItem(dialog, 1, "None", false); |
| 198 // The other is still selectable. | 221 // The other is still selectable. |
| 199 selectItem(dialog, 2, "key2", true); | 222 selectItem(dialog, 2, "key2", true); |
| 200 | 223 |
| 201 mChooserDialog.dismiss(); | 224 mChooserDialog.dismiss(); |
| 202 } | 225 } |
| 203 | 226 |
| 204 @SmallTest | 227 @SmallTest |
| 228 public void testUpdateItemWithIconToNoIcon() throws InterruptedException { | |
| 229 Dialog dialog = mChooserDialog.getDialogForTesting(); | |
| 230 assertTrue(dialog.isShowing()); | |
| 231 | |
| 232 ItemChooserDialog.ItemAdapter itemAdapter = mChooserDialog.getItemAdapte rForTesting(); | |
| 233 final ListView items = (ListView) dialog.findViewById(R.id.items); | |
| 234 | |
| 235 // Initially the itemAdapter is empty. | |
| 236 assertTrue(itemAdapter.isEmpty()); | |
| 237 | |
| 238 // Add item 1. | |
| 239 ItemChooserDialog.ItemChooserRow item1 = | |
| 240 new ItemChooserDialog.ItemChooserRow("key1", "desc1", TEST_ICON1 ); | |
| 241 mChooserDialog.addOrUpdateItem(item1); | |
| 242 assertEquals(View.VISIBLE, getImageContainerVisibilityForItem(items, 0)) ; | |
| 243 assertEquals(1, itemAdapter.getCount()); | |
| 244 assertEquals(itemAdapter.getItem(0), item1); | |
| 245 | |
| 246 // Add item 1 with no icon. | |
| 247 ItemChooserDialog.ItemChooserRow item1WithNoIcon = | |
| 248 new ItemChooserDialog.ItemChooserRow("key1", "desc1"); | |
| 249 mChooserDialog.addOrUpdateItem(item1WithNoIcon); | |
| 250 assertEquals(1, itemAdapter.getCount()); | |
| 251 // We should still see the original item with the icon. | |
| 252 ItemChooserDialog.ItemChooserRow expectedItem = | |
| 253 new ItemChooserDialog.ItemChooserRow("key1", "desc1", TEST_ICON1 ); | |
| 254 assertEquals(itemAdapter.getItem(0), expectedItem); | |
| 255 assertEquals(View.VISIBLE, getImageContainerVisibilityForItem(items, 0)) ; | |
| 256 | |
| 257 mChooserDialog.setIdleState(); | |
| 258 mChooserDialog.dismiss(); | |
| 259 } | |
| 260 | |
| 261 @SmallTest | |
| 262 public void testUpdateItemWithNoIconToIcon() throws InterruptedException { | |
| 263 Dialog dialog = mChooserDialog.getDialogForTesting(); | |
| 264 assertTrue(dialog.isShowing()); | |
| 265 | |
| 266 ItemChooserDialog.ItemAdapter itemAdapter = mChooserDialog.getItemAdapte rForTesting(); | |
| 267 final ListView items = (ListView) dialog.findViewById(R.id.items); | |
| 268 | |
| 269 // Initially the itemAdapter is empty. | |
| 270 assertTrue(itemAdapter.isEmpty()); | |
| 271 | |
| 272 // Add item 1 with no icon. | |
| 273 ItemChooserDialog.ItemChooserRow item1WithNoIcon = | |
| 274 new ItemChooserDialog.ItemChooserRow("key1", "desc1"); | |
| 275 mChooserDialog.addOrUpdateItem(item1WithNoIcon); | |
| 276 assertEquals(View.GONE, getImageContainerVisibilityForItem(items, 0)); | |
| 277 assertEquals(1, itemAdapter.getCount()); | |
| 278 assertEquals(itemAdapter.getItem(0), item1WithNoIcon); | |
| 279 | |
| 280 // Add item 1 with icon. | |
| 281 ItemChooserDialog.ItemChooserRow item1 = | |
| 282 new ItemChooserDialog.ItemChooserRow("key1", "desc1", TEST_ICON1 ); | |
| 283 mChooserDialog.addOrUpdateItem(item1); | |
| 284 assertEquals(1, itemAdapter.getCount()); | |
| 285 ItemChooserDialog.ItemChooserRow expectedItem = | |
| 286 new ItemChooserDialog.ItemChooserRow("key1", "desc1", TEST_ICON1 ); | |
| 287 assertEquals(itemAdapter.getItem(0), expectedItem); | |
| 288 assertEquals(View.VISIBLE, getImageContainerVisibilityForItem(items, 0)) ; | |
| 289 | |
| 290 mChooserDialog.setIdleState(); | |
| 291 mChooserDialog.dismiss(); | |
| 292 } | |
| 293 | |
| 294 @SmallTest | |
| 295 public void testUpdateItemWithNoIconToNoIcon() throws InterruptedException { | |
| 296 Dialog dialog = mChooserDialog.getDialogForTesting(); | |
| 297 assertTrue(dialog.isShowing()); | |
| 298 | |
| 299 ItemChooserDialog.ItemAdapter itemAdapter = mChooserDialog.getItemAdapte rForTesting(); | |
| 300 final ListView items = (ListView) dialog.findViewById(R.id.items); | |
| 301 | |
| 302 // Initially the itemAdapter is empty. | |
| 303 assertTrue(itemAdapter.isEmpty()); | |
| 304 | |
| 305 // Add item 1 with no icon. | |
| 306 ItemChooserDialog.ItemChooserRow item1WithNoIcon = | |
| 307 new ItemChooserDialog.ItemChooserRow("key1", "desc1"); | |
| 308 mChooserDialog.addOrUpdateItem(item1WithNoIcon); | |
| 309 assertEquals(View.GONE, getImageContainerVisibilityForItem(items, 0)); | |
| 310 assertEquals(1, itemAdapter.getCount()); | |
| 311 assertEquals(itemAdapter.getItem(0), item1WithNoIcon); | |
| 312 | |
| 313 // Add item 1 with no icon again. | |
| 314 mChooserDialog.addOrUpdateItem(item1WithNoIcon); | |
| 315 assertEquals(1, itemAdapter.getCount()); | |
| 316 ItemChooserDialog.ItemChooserRow expectedItem = | |
| 317 new ItemChooserDialog.ItemChooserRow("key1", "desc1"); | |
| 318 assertEquals(itemAdapter.getItem(0), expectedItem); | |
| 319 assertEquals(View.GONE, getImageContainerVisibilityForItem(items, 0)); | |
| 320 | |
| 321 mChooserDialog.setIdleState(); | |
| 322 mChooserDialog.dismiss(); | |
| 323 } | |
| 324 | |
| 325 @SmallTest | |
| 205 public void testAddOrUpdateItemAndRemoveItemFromList() throws InterruptedExc eption { | 326 public void testAddOrUpdateItemAndRemoveItemFromList() throws InterruptedExc eption { |
| 206 Dialog dialog = mChooserDialog.getDialogForTesting(); | 327 Dialog dialog = mChooserDialog.getDialogForTesting(); |
| 207 assertTrue(dialog.isShowing()); | 328 assertTrue(dialog.isShowing()); |
| 208 | 329 |
| 209 TextViewWithClickableSpans statusView = (TextViewWithClickableSpans) | 330 TextViewWithClickableSpans statusView = (TextViewWithClickableSpans) |
| 210 dialog.findViewById(R.id.status); | 331 dialog.findViewById(R.id.status); |
| 211 final ListView items = (ListView) dialog.findViewById(R.id.items); | 332 final ListView items = (ListView) dialog.findViewById(R.id.items); |
| 212 final Button button = (Button) dialog.findViewById(R.id.positive); | 333 final Button button = (Button) dialog.findViewById(R.id.positive); |
| 213 | 334 |
| 214 ItemChooserDialog.ItemAdapter itemAdapter = mChooserDialog.getItemAdapte rForTesting(); | 335 ItemChooserDialog.ItemAdapter itemAdapter = mChooserDialog.getItemAdapte rForTesting(); |
| 215 ItemChooserDialog.ItemChooserRow nonExistentItem = | 336 ItemChooserDialog.ItemChooserRow nonExistentItem = |
| 216 new ItemChooserDialog.ItemChooserRow("key", "key"); | 337 new ItemChooserDialog.ItemChooserRow("key", "key"); |
| 217 | 338 |
| 218 // Initially the itemAdapter is empty. | 339 // Initially the itemAdapter is empty. |
| 219 assertTrue(itemAdapter.isEmpty()); | 340 assertTrue(itemAdapter.isEmpty()); |
| 220 | 341 |
| 221 // Try removing an item from an empty itemAdapter. | 342 // Try removing an item from an empty itemAdapter. |
| 222 mChooserDialog.removeItemFromList(nonExistentItem); | 343 mChooserDialog.removeItemFromList(nonExistentItem); |
| 223 assertTrue(itemAdapter.isEmpty()); | 344 assertTrue(itemAdapter.isEmpty()); |
| 224 | 345 |
| 225 // Add item 1. | 346 // Add item 1. |
| 226 ItemChooserDialog.ItemChooserRow item1 = | 347 ItemChooserDialog.ItemChooserRow item1 = |
| 227 new ItemChooserDialog.ItemChooserRow("key1", "desc1"); | 348 new ItemChooserDialog.ItemChooserRow("key1", "desc1", TEST_ICON1 ); |
| 228 mChooserDialog.addOrUpdateItem(item1); | 349 mChooserDialog.addOrUpdateItem(item1); |
| 229 assertEquals(1, itemAdapter.getCount()); | 350 assertEquals(1, itemAdapter.getCount()); |
| 230 assertEquals(itemAdapter.getItem(0), item1); | 351 assertEquals(itemAdapter.getItem(0), item1); |
| 352 assertEquals(View.VISIBLE, getImageContainerVisibilityForItem(items, 0)) ; | |
| 231 | 353 |
| 232 // Add item 1 with different description. | 354 // Add item 1 with different description and icon. |
| 233 ItemChooserDialog.ItemChooserRow item1_again = | 355 ItemChooserDialog.ItemChooserRow item1Again = |
| 234 new ItemChooserDialog.ItemChooserRow("key1", "desc1_again"); | 356 new ItemChooserDialog.ItemChooserRow("key1", "desc1_again", TEST _ICON2); |
| 235 mChooserDialog.addOrUpdateItem(item1_again); | 357 mChooserDialog.addOrUpdateItem(item1Again); |
| 236 assertEquals(1, itemAdapter.getCount()); | 358 assertEquals(1, itemAdapter.getCount()); |
| 237 assertEquals(itemAdapter.getItem(0), item1_again); | 359 assertEquals(itemAdapter.getItem(0), item1Again); |
| 360 assertEquals(View.VISIBLE, getImageContainerVisibilityForItem(items, 0)) ; | |
| 238 | 361 |
| 239 // Add item 2. | 362 // Add item 2. |
| 240 ItemChooserDialog.ItemChooserRow item2 = | 363 ItemChooserDialog.ItemChooserRow item2 = |
| 241 new ItemChooserDialog.ItemChooserRow("key2", "desc2"); | 364 new ItemChooserDialog.ItemChooserRow("key2", "desc2"); |
| 242 mChooserDialog.addOrUpdateItem(item2); | 365 mChooserDialog.addOrUpdateItem(item2); |
| 243 assertEquals(2, itemAdapter.getCount()); | 366 assertEquals(2, itemAdapter.getCount()); |
| 244 assertEquals(itemAdapter.getItem(0), item1_again); | 367 assertEquals(itemAdapter.getItem(0), item1Again); |
| 245 assertEquals(itemAdapter.getItem(1), item2); | 368 assertEquals(itemAdapter.getItem(1), item2); |
| 369 assertEquals(View.GONE, getImageContainerVisibilityForItem(items, 1)); | |
| 246 | 370 |
| 247 mChooserDialog.setIdleState(); | 371 mChooserDialog.setIdleState(); |
| 248 | 372 |
| 249 // Try removing an item that doesn't exist. | 373 // Try removing an item that doesn't exist. |
| 250 mChooserDialog.removeItemFromList(nonExistentItem); | 374 mChooserDialog.removeItemFromList(nonExistentItem); |
| 251 assertEquals(2, itemAdapter.getCount()); | 375 assertEquals(2, itemAdapter.getCount()); |
| 252 | 376 |
| 253 // Remove item 2. | 377 // Remove item 2. |
| 254 mChooserDialog.removeItemFromList(item2); | 378 mChooserDialog.removeItemFromList(item2); |
| 255 assertEquals(1, itemAdapter.getCount()); | 379 assertEquals(1, itemAdapter.getCount()); |
| 256 assertEquals(itemAdapter.getItem(0), item1_again); | 380 assertEquals(itemAdapter.getItem(0), item1Again); |
| 257 | 381 |
| 258 // The list should be visible with one item, it should not show | 382 // The list should be visible with one item, it should not show |
| 259 // the empty view and the button should not be enabled. | 383 // the empty view and the button should not be enabled. |
| 260 // The chooser should show a status message at the bottom. | 384 // The chooser should show a status message at the bottom. |
| 261 assertEquals(View.VISIBLE, items.getVisibility()); | 385 assertEquals(View.VISIBLE, items.getVisibility()); |
| 262 assertEquals(View.GONE, items.getEmptyView().getVisibility()); | 386 assertEquals(View.GONE, items.getEmptyView().getVisibility()); |
| 263 assertEquals("statusIdleSomeFound", statusView.getText().toString()); | 387 assertEquals("statusIdleSomeFound", statusView.getText().toString()); |
| 264 assertFalse(button.isEnabled()); | 388 assertFalse(button.isEnabled()); |
| 265 | 389 |
| 266 // Remove item 1. | 390 // Remove item 1. |
| 267 mChooserDialog.removeItemFromList(item1_again); | 391 mChooserDialog.removeItemFromList(item1Again); |
| 268 assertTrue(itemAdapter.isEmpty()); | 392 assertTrue(itemAdapter.isEmpty()); |
| 269 | 393 |
| 270 // Listview should now be showing empty, with an empty view visible | 394 // Listview should now be showing empty, with an empty view visible |
| 271 // and the button should not be enabled. | 395 // and the button should not be enabled. |
| 272 // The chooser should show a status message at the bottom. | 396 // The chooser should show a status message at the bottom. |
| 273 assertEquals(View.GONE, items.getVisibility()); | 397 assertEquals(View.GONE, items.getVisibility()); |
| 274 assertEquals(View.VISIBLE, items.getEmptyView().getVisibility()); | 398 assertEquals(View.VISIBLE, items.getEmptyView().getVisibility()); |
| 275 assertEquals("statusIdleNoneFound", statusView.getText().toString()); | 399 assertEquals("statusIdleNoneFound", statusView.getText().toString()); |
| 276 assertFalse(button.isEnabled()); | 400 assertFalse(button.isEnabled()); |
| 277 | 401 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 348 | 472 |
| 349 // 1460 * .3 is 438, which rounds above the maximum height. | 473 // 1460 * .3 is 438, which rounds above the maximum height. |
| 350 assertEquals(408, ItemChooserDialog.getListHeight(1460, 1.0f)); | 474 assertEquals(408, ItemChooserDialog.getListHeight(1460, 1.0f)); |
| 351 | 475 |
| 352 // 1100px is 500dp at a density of 2.2. 500 * .3 is 150dp, which is 48dp * | 476 // 1100px is 500dp at a density of 2.2. 500 * .3 is 150dp, which is 48dp * |
| 353 // 3.125. 48dp * 3.5 is 168dp. 168dp * 2.2px/dp is 369.6, which rounds t o | 477 // 3.125. 48dp * 3.5 is 168dp. 168dp * 2.2px/dp is 369.6, which rounds t o |
| 354 // 370. | 478 // 370. |
| 355 assertEquals(370, ItemChooserDialog.getListHeight(1100, 2.2f)); | 479 assertEquals(370, ItemChooserDialog.getListHeight(1100, 2.2f)); |
| 356 } | 480 } |
| 357 } | 481 } |
| OLD | NEW |