OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.content.browser; |
| 6 |
| 7 import android.os.Vibrator; |
| 8 import android.support.test.filters.MediumTest; |
| 9 |
| 10 import org.junit.Assert; |
| 11 import org.junit.Before; |
| 12 import org.junit.Rule; |
| 13 import org.junit.Test; |
| 14 import org.junit.runner.RunWith; |
| 15 |
| 16 import org.chromium.base.test.BaseJUnit4ClassRunner; |
| 17 import org.chromium.base.test.util.DisabledTest; |
| 18 import org.chromium.base.test.util.Feature; |
| 19 import org.chromium.base.test.util.UrlUtils; |
| 20 import org.chromium.content.browser.test.util.Criteria; |
| 21 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 22 import org.chromium.content_shell_apk.ContentShellActivityTestRule; |
| 23 import org.chromium.device.vibration.VibrationManagerImpl; |
| 24 |
| 25 /** |
| 26 * Tests java implementation of VibrationManager mojo service on android. |
| 27 */ |
| 28 @RunWith(BaseJUnit4ClassRunner.class) |
| 29 public class VibrationManagerImplTest { |
| 30 @Rule |
| 31 public ContentShellActivityTestRule mActivityTestRule = new ContentShellActi
vityTestRule(); |
| 32 |
| 33 private static final String URL_VIBRATOR_VIBRATE = UrlUtils.encodeHtmlDataUr
i("<html><body>" |
| 34 + " <script type=\"text/javascript\">" |
| 35 + " navigator.vibrate(3000);" |
| 36 + " </script>" |
| 37 + "</body></html>"); |
| 38 private static final String URL_VIBRATOR_CANCEL = UrlUtils.encodeHtmlDataUri
("<html><body>" |
| 39 + " <script type=\"text/javascript\">" |
| 40 + " navigator.vibrate(10000);" |
| 41 + " navigator.vibrate(0);" |
| 42 + " </script>" |
| 43 + "</body></html>"); |
| 44 |
| 45 private FakeAndroidVibratorWrapper mFakeWrapper; |
| 46 |
| 47 // Override AndroidVibratorWrapper API to record the calling. |
| 48 private static class FakeAndroidVibratorWrapper |
| 49 extends VibrationManagerImpl.AndroidVibratorWrapper { |
| 50 // Record the parameters of vibrate() and cancel(). |
| 51 public long mMilliSeconds; |
| 52 public boolean mCancelled; |
| 53 |
| 54 protected FakeAndroidVibratorWrapper() { |
| 55 super(); |
| 56 mMilliSeconds = -1; |
| 57 mCancelled = false; |
| 58 } |
| 59 |
| 60 @Override |
| 61 public void vibrate(Vibrator vibrator, long milliseconds) { |
| 62 mMilliSeconds = milliseconds; |
| 63 } |
| 64 |
| 65 @Override |
| 66 public void cancel(Vibrator vibrator) { |
| 67 mCancelled = true; |
| 68 } |
| 69 } |
| 70 |
| 71 @Before |
| 72 public void setUp() throws Exception { |
| 73 mActivityTestRule.launchContentShellWithUrl("about:blank"); |
| 74 mActivityTestRule.waitForActiveShellToBeDoneLoading(); |
| 75 |
| 76 mFakeWrapper = new FakeAndroidVibratorWrapper(); |
| 77 VibrationManagerImpl.setVibratorWrapperForTesting(mFakeWrapper); |
| 78 Assert.assertEquals(-1, mFakeWrapper.mMilliSeconds); |
| 79 Assert.assertFalse(mFakeWrapper.mCancelled); |
| 80 } |
| 81 |
| 82 /** |
| 83 * Inject our fake wrapper into VibrationManagerImpl, |
| 84 * load the webpage which will request vibrate for 3000 milliseconds, |
| 85 * the fake wrapper vibrate() should be called and 3000 milliseconds should
be recorded |
| 86 * correctly. |
| 87 */ |
| 88 // @MediumTest |
| 89 // @Feature({"Vibration"}) |
| 90 @Test |
| 91 @DisabledTest |
| 92 public void testVibrate() throws Throwable { |
| 93 mActivityTestRule.loadNewShell(URL_VIBRATOR_VIBRATE); |
| 94 |
| 95 // Waits until VibrationManagerImpl.Vibrate() got called. |
| 96 CriteriaHelper.pollUiThread(new Criteria() { |
| 97 @Override |
| 98 public boolean isSatisfied() { |
| 99 return mFakeWrapper.mMilliSeconds != -1; |
| 100 } |
| 101 }); |
| 102 |
| 103 Assert.assertEquals( |
| 104 "Did not get vibrate mMilliSeconds correctly", 3000, mFakeWrappe
r.mMilliSeconds); |
| 105 } |
| 106 |
| 107 /** |
| 108 * Inject our fake wrapper into VibrationManagerImpl, |
| 109 * load the webpage which will request vibrate and then request cancel, |
| 110 * the fake wrapper cancel() should be called. |
| 111 */ |
| 112 @Test |
| 113 @MediumTest |
| 114 @Feature({"Vibration"}) |
| 115 public void testCancel() throws Throwable { |
| 116 mActivityTestRule.loadNewShell(URL_VIBRATOR_CANCEL); |
| 117 |
| 118 // Waits until VibrationManagerImpl.Cancel() got called. |
| 119 CriteriaHelper.pollUiThread(new Criteria() { |
| 120 @Override |
| 121 public boolean isSatisfied() { |
| 122 return mFakeWrapper.mCancelled; |
| 123 } |
| 124 }); |
| 125 |
| 126 Assert.assertTrue("Did not get cancelled", mFakeWrapper.mCancelled); |
| 127 } |
| 128 } |
OLD | NEW |