| 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.widget.bottomsheet; | 5 package org.chromium.chrome.browser.widget.bottomsheet; |
| 6 | 6 |
| 7 import android.support.test.filters.MediumTest; | 7 import android.support.test.filters.MediumTest; |
| 8 | 8 |
| 9 import org.chromium.base.test.util.CallbackHelper; | 9 import org.chromium.base.test.util.CallbackHelper; |
| 10 import org.chromium.chrome.R; |
| 11 import org.chromium.chrome.browser.bookmarks.BookmarkSheetContent; |
| 12 import org.chromium.chrome.browser.download.DownloadSheetContent; |
| 13 import org.chromium.chrome.browser.history.HistorySheetContent; |
| 14 import org.chromium.chrome.browser.suggestions.SuggestionsBottomSheetContent; |
| 10 import org.chromium.chrome.browser.util.MathUtils; | 15 import org.chromium.chrome.browser.util.MathUtils; |
| 11 import org.chromium.chrome.browser.widget.bottomsheet.BottomSheet.BottomSheetCon
tent; | 16 import org.chromium.chrome.browser.widget.bottomsheet.BottomSheet.BottomSheetCon
tent; |
| 12 import org.chromium.chrome.test.BottomSheetTestCaseBase; | 17 import org.chromium.chrome.test.BottomSheetTestCaseBase; |
| 13 | 18 |
| 14 import java.util.concurrent.TimeoutException; | 19 import java.util.concurrent.TimeoutException; |
| 15 | 20 |
| 16 /** This class tests the functionality of the {@link BottomSheetObserver}. */ | 21 /** This class tests the functionality of the {@link BottomSheetObserver}. */ |
| 17 public class BottomSheetObserverTest extends BottomSheetTestCaseBase { | 22 public class BottomSheetObserverTest extends BottomSheetTestCaseBase { |
| 18 /** A handle to the sheet's observer. */ | 23 /** A handle to the sheet's observer. */ |
| 19 private TestBottomSheetObserver mObserver; | 24 private TestBottomSheetObserver mObserver; |
| 20 | 25 |
| 21 /** An observer used to record events that occur with respect to the bottom
sheet. */ | 26 /** An observer used to record events that occur with respect to the bottom
sheet. */ |
| 22 private static class TestBottomSheetObserver implements BottomSheetObserver
{ | 27 private static class TestBottomSheetObserver implements BottomSheetObserver
{ |
| 23 /** A {@link CallbackHelper} that can wait for the bottom sheet to be cl
osed. */ | 28 /** A {@link CallbackHelper} that can wait for the bottom sheet to be cl
osed. */ |
| 24 private final CallbackHelper mClosedCallbackHelper = new CallbackHelper(
); | 29 private final CallbackHelper mClosedCallbackHelper = new CallbackHelper(
); |
| 25 | 30 |
| 26 /** A {@link CallbackHelper} that can wait for the bottom sheet to be op
ened. */ | 31 /** A {@link CallbackHelper} that can wait for the bottom sheet to be op
ened. */ |
| 27 private final CallbackHelper mOpenedCallbackHelper = new CallbackHelper(
); | 32 private final CallbackHelper mOpenedCallbackHelper = new CallbackHelper(
); |
| 28 | 33 |
| 29 /** A {@link CallbackHelper} that can wait for the onTransitionPeekToHal
f event. */ | 34 /** A {@link CallbackHelper} that can wait for the onTransitionPeekToHal
f event. */ |
| 30 private final CallbackHelper mPeekToHalfCallbackHelper = new CallbackHel
per(); | 35 private final CallbackHelper mPeekToHalfCallbackHelper = new CallbackHel
per(); |
| 31 | 36 |
| 32 /** A {@link CallbackHelper} that can wait for the onOffsetChanged event
. */ | 37 /** A {@link CallbackHelper} that can wait for the onOffsetChanged event
. */ |
| 33 private final CallbackHelper mOffsetChangedCallbackHelper = new Callback
Helper(); | 38 private final CallbackHelper mOffsetChangedCallbackHelper = new Callback
Helper(); |
| 34 | 39 |
| 40 /** A {@link CallbackHelper} that can wait for the onSheetContentChanged
event. */ |
| 41 private final CallbackHelper mContentChangedCallbackHelper = new Callbac
kHelper(); |
| 42 |
| 35 /** The last value that the onTransitionPeekToHalf event sent. */ | 43 /** The last value that the onTransitionPeekToHalf event sent. */ |
| 36 private float mLastPeekToHalfValue; | 44 private float mLastPeekToHalfValue; |
| 37 | 45 |
| 38 /** The last value that the onOffsetChanged event sent. */ | 46 /** The last value that the onOffsetChanged event sent. */ |
| 39 private float mLastOffsetChangedValue; | 47 private float mLastOffsetChangedValue; |
| 40 | 48 |
| 41 @Override | 49 @Override |
| 42 public void onTransitionPeekToHalf(float fraction) { | 50 public void onTransitionPeekToHalf(float fraction) { |
| 43 mLastPeekToHalfValue = fraction; | 51 mLastPeekToHalfValue = fraction; |
| 44 mPeekToHalfCallbackHelper.notifyCalled(); | 52 mPeekToHalfCallbackHelper.notifyCalled(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 60 mClosedCallbackHelper.notifyCalled(); | 68 mClosedCallbackHelper.notifyCalled(); |
| 61 } | 69 } |
| 62 | 70 |
| 63 @Override | 71 @Override |
| 64 public void onLoadUrl(String url) {} | 72 public void onLoadUrl(String url) {} |
| 65 | 73 |
| 66 @Override | 74 @Override |
| 67 public void onSheetStateChanged(int newState) {} | 75 public void onSheetStateChanged(int newState) {} |
| 68 | 76 |
| 69 @Override | 77 @Override |
| 70 public void onSheetContentChanged(BottomSheetContent newContent) {} | 78 public void onSheetContentChanged(BottomSheetContent newContent) { |
| 79 mContentChangedCallbackHelper.notifyCalled(); |
| 80 } |
| 71 } | 81 } |
| 72 | 82 |
| 73 @Override | 83 @Override |
| 74 protected void setUp() throws Exception { | 84 protected void setUp() throws Exception { |
| 75 super.setUp(); | 85 super.setUp(); |
| 76 | 86 |
| 77 mObserver = new TestBottomSheetObserver(); | 87 mObserver = new TestBottomSheetObserver(); |
| 78 mBottomSheet.addObserver(mObserver); | 88 mBottomSheet.addObserver(mObserver); |
| 79 } | 89 } |
| 80 | 90 |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 setSheetOffsetFromBottom(midPeekHalf); | 235 setSheetOffsetFromBottom(midPeekHalf); |
| 226 callbackHelper.waitForCallback(callbackCount, 1); | 236 callbackHelper.waitForCallback(callbackCount, 1); |
| 227 assertEquals(0.5f, mObserver.mLastPeekToHalfValue, MathUtils.EPSILON); | 237 assertEquals(0.5f, mObserver.mLastPeekToHalfValue, MathUtils.EPSILON); |
| 228 | 238 |
| 229 // At the half state the event should send 1. | 239 // At the half state the event should send 1. |
| 230 callbackCount = callbackHelper.getCallCount(); | 240 callbackCount = callbackHelper.getCallCount(); |
| 231 setSheetOffsetFromBottom(halfHeight); | 241 setSheetOffsetFromBottom(halfHeight); |
| 232 callbackHelper.waitForCallback(callbackCount, 1); | 242 callbackHelper.waitForCallback(callbackCount, 1); |
| 233 assertEquals(1f, mObserver.mLastPeekToHalfValue, MathUtils.EPSILON); | 243 assertEquals(1f, mObserver.mLastPeekToHalfValue, MathUtils.EPSILON); |
| 234 } | 244 } |
| 245 |
| 246 /** |
| 247 * Test the onSheetContentChanged event. |
| 248 */ |
| 249 @MediumTest |
| 250 public void testSheetContentChanged() throws InterruptedException, TimeoutEx
ception { |
| 251 CallbackHelper callbackHelper = mObserver.mContentChangedCallbackHelper; |
| 252 |
| 253 int callbackCount = callbackHelper.getCallCount(); |
| 254 selectBottomSheetContent(R.id.action_bookmarks); |
| 255 callbackHelper.waitForCallback(callbackCount, 1); |
| 256 assertTrue(getBottomSheetContent() instanceof BookmarkSheetContent); |
| 257 |
| 258 callbackCount++; |
| 259 selectBottomSheetContent(R.id.action_history); |
| 260 callbackHelper.waitForCallback(callbackCount, 1); |
| 261 assertTrue(getBottomSheetContent() instanceof HistorySheetContent); |
| 262 |
| 263 callbackCount++; |
| 264 selectBottomSheetContent(R.id.action_downloads); |
| 265 callbackHelper.waitForCallback(callbackCount, 1); |
| 266 assertTrue(getBottomSheetContent() instanceof DownloadSheetContent); |
| 267 |
| 268 callbackCount++; |
| 269 selectBottomSheetContent(R.id.action_home); |
| 270 callbackHelper.waitForCallback(callbackCount, 1); |
| 271 assertTrue(getBottomSheetContent() instanceof SuggestionsBottomSheetCont
ent); |
| 272 } |
| 235 } | 273 } |
| OLD | NEW |