| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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.ntp; | 5 package org.chromium.chrome.browser.ntp; |
| 6 | 6 |
| 7 import android.support.test.filters.SmallTest; | 7 import android.support.test.filters.SmallTest; |
| 8 | 8 |
| 9 import org.chromium.base.ThreadUtils; | 9 import org.chromium.base.ThreadUtils; |
| 10 import org.chromium.base.test.util.CallbackHelper; |
| 10 import org.chromium.chrome.browser.UrlConstants; | 11 import org.chromium.chrome.browser.UrlConstants; |
| 11 import org.chromium.chrome.browser.tab.Tab; | 12 import org.chromium.chrome.browser.tab.Tab; |
| 12 import org.chromium.chrome.browser.tabmodel.TabModel.TabSelectionType; | 13 import org.chromium.chrome.browser.tabmodel.TabModel.TabSelectionType; |
| 13 import org.chromium.chrome.browser.widget.FadingBackgroundView; | 14 import org.chromium.chrome.browser.widget.FadingBackgroundView; |
| 14 import org.chromium.chrome.browser.widget.bottomsheet.BottomSheet; | 15 import org.chromium.chrome.browser.widget.bottomsheet.BottomSheet; |
| 16 import org.chromium.chrome.browser.widget.bottomsheet.EmptyBottomSheetObserver; |
| 15 import org.chromium.chrome.test.BottomSheetTestCaseBase; | 17 import org.chromium.chrome.test.BottomSheetTestCaseBase; |
| 16 import org.chromium.chrome.test.util.ChromeTabUtils; | 18 import org.chromium.chrome.test.util.ChromeTabUtils; |
| 17 import org.chromium.chrome.test.util.NewTabPageTestUtils; | 19 import org.chromium.chrome.test.util.NewTabPageTestUtils; |
| 18 | 20 |
| 21 import java.util.concurrent.TimeoutException; |
| 22 |
| 19 /** | 23 /** |
| 20 * Tests for the {@link ChromeHomeNewTabPage}. | 24 * Tests for the {@link ChromeHomeNewTabPage}. |
| 21 */ | 25 */ |
| 22 public class ChromeHomeNewTabPageTest extends BottomSheetTestCaseBase { | 26 public class ChromeHomeNewTabPageTest extends BottomSheetTestCaseBase { |
| 23 private FadingBackgroundView mFadingBackgroundView; | 27 private FadingBackgroundView mFadingBackgroundView; |
| 28 private StateChangeBottomSheetObserver mObserver; |
| 29 private int mStateChangeCurrentCalls; |
| 30 |
| 31 /** On observer used to record state change events on the bottom sheet. */ |
| 32 private static class StateChangeBottomSheetObserver extends EmptyBottomSheet
Observer { |
| 33 /** A {@link CallbackHelper} that waits for the bottom sheet state to ch
ange. */ |
| 34 private final CallbackHelper mStateChangedCallbackHelper = new CallbackH
elper(); |
| 35 |
| 36 @Override |
| 37 public void onSheetStateChanged(int state) { |
| 38 mStateChangedCallbackHelper.notifyCalled(); |
| 39 } |
| 40 } |
| 24 | 41 |
| 25 @Override | 42 @Override |
| 26 public void setUp() throws Exception { | 43 public void setUp() throws Exception { |
| 27 super.setUp(); | 44 super.setUp(); |
| 28 | 45 |
| 46 mObserver = new StateChangeBottomSheetObserver(); |
| 47 mBottomSheet.addObserver(mObserver); |
| 48 |
| 29 mFadingBackgroundView = getActivity().getFadingBackgroundView(); | 49 mFadingBackgroundView = getActivity().getFadingBackgroundView(); |
| 50 |
| 51 // Once setup is done, get the initial call count for onStateChanged(). |
| 52 mStateChangeCurrentCalls = mObserver.mStateChangedCallbackHelper.getCall
Count(); |
| 30 } | 53 } |
| 31 | 54 |
| 32 @SmallTest | 55 @SmallTest |
| 33 public void testCloseNTP_OneTab() throws IllegalArgumentException, Interrupt
edException { | 56 public void testCloseNTP_OneTab() |
| 57 throws IllegalArgumentException, InterruptedException, TimeoutExcept
ion { |
| 34 // Load the NTP. | 58 // Load the NTP. |
| 35 Tab tab = getActivity().getActivityTab(); | 59 Tab tab = getActivity().getActivityTab(); |
| 36 loadUrl(UrlConstants.NTP_URL); | 60 loadUrl(UrlConstants.NTP_URL); |
| 37 NewTabPageTestUtils.waitForNtpLoaded(tab); | 61 NewTabPageTestUtils.waitForNtpLoaded(tab); |
| 38 | 62 |
| 39 validateState(true); | 63 validateState(true, true); |
| 40 | 64 |
| 41 // Close the new tab. | 65 // Close the new tab. |
| 42 closeNewTab(); | 66 closeNewTab(); |
| 43 assertEquals(0, getActivity().getTabModelSelector().getTotalTabCount()); | 67 assertEquals(0, getActivity().getTabModelSelector().getTotalTabCount()); |
| 44 assertFalse("Overview mode should not be showing", | 68 assertFalse("Overview mode should not be showing", |
| 45 getActivity().getLayoutManager().overviewVisible()); | 69 getActivity().getLayoutManager().overviewVisible()); |
| 46 } | 70 } |
| 47 | 71 |
| 48 @SmallTest | 72 @SmallTest |
| 49 public void testCloseNTP_TwoTabs() throws IllegalArgumentException, Interrup
tedException { | 73 public void testCloseNTP_TwoTabs() |
| 74 throws IllegalArgumentException, InterruptedException, TimeoutExcept
ion { |
| 50 // Create a new tab. | 75 // Create a new tab. |
| 51 createNewTab(); | 76 createNewTab(); |
| 52 | 77 |
| 53 // Close the new tab. | 78 // Close the new tab. |
| 54 closeNewTab(); | 79 closeNewTab(); |
| 55 assertEquals(1, getActivity().getTabModelSelector().getTotalTabCount()); | 80 assertEquals(1, getActivity().getTabModelSelector().getTotalTabCount()); |
| 56 assertFalse("Overview mode should not be showing", | 81 assertFalse("Overview mode should not be showing", |
| 57 getActivity().getLayoutManager().overviewVisible()); | 82 getActivity().getLayoutManager().overviewVisible()); |
| 58 } | 83 } |
| 59 | 84 |
| 60 @SmallTest | 85 @SmallTest |
| 61 public void testCloseNTP_TwoTabs_OverviewMode() | 86 public void testCloseNTP_TwoTabs_OverviewMode() |
| 62 throws IllegalArgumentException, InterruptedException { | 87 throws IllegalArgumentException, InterruptedException, TimeoutExcept
ion { |
| 63 // Switch to overview mode. | 88 // Switch to overview mode. |
| 64 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 89 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 65 @Override | 90 @Override |
| 66 public void run() { | 91 public void run() { |
| 67 getActivity().getLayoutManager().showOverview(false); | 92 getActivity().getLayoutManager().showOverview(false); |
| 68 } | 93 } |
| 69 }); | 94 }); |
| 70 | 95 |
| 71 // Create a new tab. | 96 // Create a new tab. |
| 72 createNewTab(); | 97 createNewTab(); |
| 73 | 98 |
| 74 // Close the new tab. | 99 // Close the new tab. |
| 75 closeNewTab(); | 100 closeNewTab(); |
| 76 assertEquals(1, getActivity().getTabModelSelector().getTotalTabCount()); | 101 assertEquals(1, getActivity().getTabModelSelector().getTotalTabCount()); |
| 77 assertTrue("Overview mode should be showing", | 102 assertTrue("Overview mode should be showing", |
| 78 getActivity().getLayoutManager().overviewVisible()); | 103 getActivity().getLayoutManager().overviewVisible()); |
| 79 } | 104 } |
| 80 | 105 |
| 81 @SmallTest | 106 @SmallTest |
| 82 public void testToggleSelectedTab() throws IllegalArgumentException, Interru
ptedException { | 107 public void testToggleSelectedTab() |
| 108 throws IllegalArgumentException, InterruptedException, TimeoutExcept
ion { |
| 83 // Create a new tab. | 109 // Create a new tab. |
| 84 createNewTab(); | 110 createNewTab(); |
| 85 | 111 |
| 86 // Select the original tab. | 112 // Select the original tab. |
| 87 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 113 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 88 @Override | 114 @Override |
| 89 public void run() { | 115 public void run() { |
| 90 // It's not possible for the user to select a new tab while the
bottom sheet is | 116 // It's not possible for the user to select a new tab while the
bottom sheet is |
| 91 // open. | 117 // open. |
| 92 getActivity().getBottomSheet().setSheetState(BottomSheet.SHEET_S
TATE_PEEK, false); | 118 getActivity().getBottomSheet().setSheetState(BottomSheet.SHEET_S
TATE_PEEK, false); |
| 93 getActivity().getCurrentTabModel().setIndex(0, TabSelectionType.
FROM_USER); | 119 getActivity().getCurrentTabModel().setIndex(0, TabSelectionType.
FROM_USER); |
| 94 } | 120 } |
| 95 }); | 121 }); |
| 96 | 122 |
| 97 validateState(false); | 123 validateState(false, false); |
| 98 | 124 |
| 99 // Select the NTP. | 125 // Select the NTP. |
| 100 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 126 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 101 @Override | 127 @Override |
| 102 public void run() { | 128 public void run() { |
| 103 getActivity().getCurrentTabModel().setIndex(1, TabSelectionType.
FROM_USER); | 129 getActivity().getCurrentTabModel().setIndex(1, TabSelectionType.
FROM_USER); |
| 104 } | 130 } |
| 105 }); | 131 }); |
| 106 | 132 |
| 107 validateState(true); | 133 validateState(true, true); |
| 108 } | 134 } |
| 109 | 135 |
| 110 private void createNewTab() throws InterruptedException { | 136 private void createNewTab() throws InterruptedException, TimeoutException { |
| 111 ChromeTabUtils.fullyLoadUrlInNewTab( | 137 ChromeTabUtils.fullyLoadUrlInNewTab( |
| 112 getInstrumentation(), getActivity(), UrlConstants.NTP_URL, false
); | 138 getInstrumentation(), getActivity(), UrlConstants.NTP_URL, false
); |
| 113 validateState(true); | 139 validateState(true, true); |
| 114 } | 140 } |
| 115 | 141 |
| 116 private void closeNewTab() { | 142 private void closeNewTab() throws InterruptedException, TimeoutException { |
| 117 Tab tab = getActivity().getActivityTab(); | 143 Tab tab = getActivity().getActivityTab(); |
| 118 final ChromeHomeNewTabPage mNewTabPage = (ChromeHomeNewTabPage) tab.getN
ativePage(); | 144 final ChromeHomeNewTabPage mNewTabPage = (ChromeHomeNewTabPage) tab.getN
ativePage(); |
| 119 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 145 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 120 @Override | 146 @Override |
| 121 public void run() { | 147 public void run() { |
| 122 mNewTabPage.getCloseButtonForTests().callOnClick(); | 148 mNewTabPage.getCloseButtonForTests().callOnClick(); |
| 123 } | 149 } |
| 124 }); | 150 }); |
| 125 | 151 |
| 126 validateState(false); | 152 validateState(false, true); |
| 127 } | 153 } |
| 128 | 154 |
| 129 private void validateState(boolean newTabPageSelected) { | 155 private void validateState(boolean newTabPageSelected, boolean animatesToSta
te) |
| 156 throws InterruptedException, TimeoutException { |
| 157 // Wait for two calls if animating; one is to SHEET_STATE_SCROLLING and
the other is to the |
| 158 // final state. |
| 159 mObserver.mStateChangedCallbackHelper.waitForCallback( |
| 160 mStateChangeCurrentCalls, animatesToState ? 2 : 1); |
| 161 |
| 130 if (newTabPageSelected) { | 162 if (newTabPageSelected) { |
| 131 assertEquals("Sheet should be at half height", BottomSheet.SHEET_STA
TE_HALF, | 163 assertEquals("Sheet should be at half height", BottomSheet.SHEET_STA
TE_HALF, |
| 132 mBottomSheet.getSheetState()); | 164 mBottomSheet.getSheetState()); |
| 133 assertFalse(mFadingBackgroundView.isEnabled()); | 165 assertFalse(mFadingBackgroundView.isEnabled()); |
| 134 assertEquals(0f, mFadingBackgroundView.getAlpha()); | 166 assertEquals(0f, mFadingBackgroundView.getAlpha()); |
| 135 } else { | 167 } else { |
| 136 assertEquals("Sheet should be peeking", BottomSheet.SHEET_STATE_PEEK
, | 168 assertEquals("Sheet should be peeking", BottomSheet.SHEET_STATE_PEEK
, |
| 137 mBottomSheet.getSheetState()); | 169 mBottomSheet.getSheetState()); |
| 138 assertTrue(mFadingBackgroundView.isEnabled()); | 170 assertTrue(mFadingBackgroundView.isEnabled()); |
| 139 } | 171 } |
| 172 |
| 173 // Once the state is validated, update the call count. |
| 174 mStateChangeCurrentCalls = mObserver.mStateChangedCallbackHelper.getCall
Count(); |
| 140 } | 175 } |
| 141 } | 176 } |
| OLD | NEW |