Chromium Code Reviews| Index: chrome/android/junit/src/org/chromium/chrome/browser/snackbar/SnackbarCollectionUnitTest.java |
| diff --git a/chrome/android/junit/src/org/chromium/chrome/browser/snackbar/SnackbarCollectionUnitTest.java b/chrome/android/junit/src/org/chromium/chrome/browser/snackbar/SnackbarCollectionUnitTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2a9ae1133cc4e58b6ba5d1ac4afb740d561a6cac |
| --- /dev/null |
| +++ b/chrome/android/junit/src/org/chromium/chrome/browser/snackbar/SnackbarCollectionUnitTest.java |
| @@ -0,0 +1,186 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.snackbar; |
| + |
| + |
| +import static org.junit.Assert.assertEquals; |
| +import static org.junit.Assert.assertFalse; |
| +import static org.junit.Assert.assertTrue; |
| + |
| +import org.chromium.base.test.util.Feature; |
| +import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarController; |
| +import org.junit.Test; |
| +import org.junit.runner.RunWith; |
| +import org.junit.runners.BlockJUnit4ClassRunner; |
| + |
| +/** |
| + * Tests for {@link SnackbarCollection}. |
| + */ |
| +@RunWith(BlockJUnit4ClassRunner.class) |
| +public class SnackbarCollectionUnitTest implements SnackbarController { |
| + private static final String ACTION_TITLE = "stack"; |
| + private static final String NOTIFICATION_TITLE = "queue"; |
| + |
| + private boolean mActionCalled; |
| + private boolean mDismissCalled; |
| + |
| + @Test |
| + @Feature({"Browser", "Snackbar"}) |
| + public void testAddOverride() throws Exception { |
|
newt (away)
2016/01/28 17:32:00
maybe: "testActionCoversNotification"
Ian Wen
2016/01/29 02:38:43
Done.
|
| + SnackbarCollection collection = new SnackbarCollection(); |
| + assertTrue(collection.isEmpty()); |
| + |
| + Snackbar notiBar = makeNotificationSnackbar(); |
| + collection.add(notiBar); |
| + assertFalse(collection.isEmpty()); |
| + assertEquals(notiBar, collection.getCurrent()); |
| + |
| + Snackbar actionBar = makeActionSnackbar(); |
| + collection.add(actionBar); |
| + assertTrue(onDismissWasCalled()); |
| + assertFalse(collection.isEmpty()); |
| + assertEquals("Notibar was not overriden by actionBar!", actionBar, collection.getCurrent()); |
|
newt (away)
2016/01/28 17:32:00
use terms that other developers who see this test
Ian Wen
2016/01/29 02:38:43
Done.
|
| + |
| + collection.removeCurrent(true); |
| + assertTrue(onActionWasCalled()); |
| + assertTrue(collection.isEmpty()); |
| + } |
| + |
| + @Test |
| + @Feature({"Browser", "Snackbar"}) |
| + public void testAddNotOverride() throws Exception { |
| + SnackbarCollection collection = new SnackbarCollection(); |
| + assertTrue(collection.isEmpty()); |
| + |
| + Snackbar actionBar = makeActionSnackbar(); |
| + collection.add(actionBar); |
| + assertFalse(collection.isEmpty()); |
| + assertEquals(actionBar, collection.getCurrent()); |
| + |
| + Snackbar notiBar = makeNotificationSnackbar(); |
| + collection.add(notiBar); |
| + assertFalse(onDismissWasCalled()); |
|
newt (away)
2016/01/28 17:32:00
It's confusing which snackbar onDismiss() might be
|
| + assertFalse(collection.isEmpty()); |
| + assertEquals("ActionBar was overriden by notiBar!", actionBar, collection.getCurrent()); |
| + |
| + collection.removeCurrent(true); |
| + assertTrue(onActionWasCalled()); |
| + assertFalse(collection.isEmpty()); |
| + assertEquals(notiBar, collection.getCurrent()); |
| + |
| + collection.removeCurrent(true); |
| + assertTrue(onActionWasCalled()); |
| + assertTrue(collection.isEmpty()); |
| + } |
| + |
| + @Test |
| + @Feature({"Browser", "Snackbar"}) |
| + public void testClear() throws Exception { |
| + SnackbarCollection collection = new SnackbarCollection(); |
| + for (int i = 0; i < 3; i++) { |
| + collection.add(makeActionSnackbar()); |
| + collection.add(makeNotificationSnackbar()); |
| + } |
| + assertFalse(collection.isEmpty()); |
| + |
| + collection.clear(); |
| + assertTrue(collection.isEmpty()); |
| + } |
| + |
| + @Test |
| + @Feature({"Browser", "Snackbar"}) |
| + public void testRemoveMatchingSnackbars() throws Exception { |
| + SnackbarCollection collection = new SnackbarCollection(); |
| + for (int i = 0; i < 3; i++) { |
| + collection.add(makeActionSnackbar()); |
| + collection.add(makeNotificationSnackbar()); |
| + } |
| + SnackbarController anotherController = new SnackbarController() { |
| + @Override |
| + public void onDismissNoAction(Object actionData) {} |
| + |
| + @Override |
| + public void onAction(Object actionData) {} |
| + }; |
| + for (int i = 0; i < 3; i++) { |
| + collection.add(makeActionSnackbar(anotherController)); |
| + collection.add(makeNotificationSnackbar(anotherController)); |
| + } |
| + |
| + collection.removeMatchingSnackbars(this); |
| + while (!collection.isEmpty()) { |
| + Snackbar removed = collection.removeCurrent(true); |
| + assertEquals(anotherController, removed.getController()); |
| + } |
| + } |
| + |
| + @Test |
| + @Feature({"Browser", "Snackbar"}) |
| + public void testRemoveMatchingSnackbarsWithData() throws Exception { |
| + SnackbarCollection collection = new SnackbarCollection(); |
| + for (int i = 0; i < 3; i++) { |
| + collection.add(makeActionSnackbar().setAction(ACTION_TITLE, i)); |
| + collection.add(makeNotificationSnackbar().setAction(NOTIFICATION_TITLE, i)); |
| + } |
| + SnackbarController anotherController = new SnackbarController() { |
| + @Override |
| + public void onDismissNoAction(Object actionData) {} |
| + |
| + @Override |
| + public void onAction(Object actionData) {} |
| + }; |
| + for (int i = 0; i < 3; i++) { |
| + collection.add(makeActionSnackbar(anotherController).setAction(ACTION_TITLE, i)); |
| + collection.add( |
| + makeNotificationSnackbar(anotherController).setAction(NOTIFICATION_TITLE, i)); |
| + } |
| + |
| + final Integer dataToRemove = 0; |
| + collection.removeMatchingSnackbars(this, dataToRemove); |
| + while (!collection.isEmpty()) { |
| + Snackbar removed = collection.removeCurrent(true); |
| + assertFalse(this == removed.getController() |
| + && dataToRemove.equals(removed.getActionData())); |
| + } |
| + } |
| + |
| + @Override |
| + public void onAction(Object actionData) { |
| + mActionCalled = true; |
| + } |
| + |
| + @Override |
| + public void onDismissNoAction(Object actionData) { |
| + mDismissCalled = true; |
| + } |
| + |
| + private boolean onActionWasCalled() { |
|
newt (away)
2016/01/28 17:32:00
You could use Mockito to mock out a SnackbarContro
Ian Wen
2016/01/29 02:38:43
Done.
|
| + boolean oldValue = mActionCalled; |
| + mActionCalled = false; |
|
newt (away)
2016/01/28 17:32:00
Resetting these values inside a method whose name
Ian Wen
2016/01/29 02:38:43
Done.
|
| + return oldValue; |
| + } |
| + |
| + private boolean onDismissWasCalled() { |
| + boolean oldValue = mDismissCalled; |
| + mDismissCalled = false; |
| + return oldValue; |
| + } |
| + |
| + private Snackbar makeActionSnackbar(SnackbarController controller) { |
| + return Snackbar.make(ACTION_TITLE, controller, Snackbar.TYPE_ACTION); |
| + } |
| + |
| + private Snackbar makeNotificationSnackbar(SnackbarController controller) { |
| + return Snackbar.make(NOTIFICATION_TITLE, controller, Snackbar.TYPE_NOTIFICATION); |
| + } |
| + |
| + private Snackbar makeActionSnackbar() { |
| + return makeActionSnackbar(this); |
| + } |
| + |
| + private Snackbar makeNotificationSnackbar() { |
| + return makeNotificationSnackbar(this); |
| + } |
| +} |