Chromium Code Reviews| 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.content.Context; | |
| 8 import android.os.Vibrator; | |
| 9 import android.test.suitebuilder.annotation.MediumTest; | |
| 10 | |
| 11 import org.chromium.base.test.util.Feature; | |
| 12 import org.chromium.base.test.util.UrlUtils; | |
| 13 import org.chromium.content.browser.test.util.Criteria; | |
| 14 import org.chromium.content.browser.test.util.CriteriaHelper; | |
| 15 import org.chromium.content_shell_apk.ContentShellTestBase; | |
| 16 import org.chromium.device.vibration.VibrationManagerImpl; | |
| 17 | |
| 18 /** | |
| 19 * Tests java implementation of VibrationManager mojo service on android. | |
| 20 */ | |
| 21 public class VibrationManagerImplTest extends ContentShellTestBase { | |
| 22 private static final String URL_VIBRATOR_VIBRATE = UrlUtils.encodeHtmlDataUr i("<html><body>" | |
| 23 + " <script type=\"text/javascript\">" | |
| 24 + " navigator.vibrate(3000);" | |
| 25 + " </script>" | |
| 26 + "</body></html>"); | |
| 27 private static final String URL_VIBRATOR_CANCEL = UrlUtils.encodeHtmlDataUri ("<html><body>" | |
| 28 + " <script type=\"text/javascript\">" | |
| 29 + " navigator.vibrate(10000);" | |
| 30 + " navigator.vibrate(0);" | |
| 31 + " </script>" | |
| 32 + "</body></html>"); | |
| 33 | |
| 34 // Override AndroidVibratorWrapper API to record the calling. | |
| 35 private static class FakeAndroidVibratorWrapper | |
| 36 extends VibrationManagerImpl.AndroidVibratorWrapper { | |
| 37 // Record the parameters of vibrate() and cancel(). | |
| 38 public long mMilliSeconds; | |
| 39 public boolean mCancelled; | |
| 40 | |
| 41 protected FakeAndroidVibratorWrapper(Vibrator vibrator) { | |
| 42 super(vibrator); | |
| 43 mMilliSeconds = -1; | |
| 44 mCancelled = false; | |
| 45 } | |
| 46 | |
| 47 @Override | |
| 48 public void vibrate(long milliseconds) { | |
| 49 super.vibrate(milliseconds); | |
| 50 mMilliSeconds = milliseconds; | |
| 51 } | |
| 52 | |
| 53 @Override | |
| 54 public void cancel() { | |
| 55 super.cancel(); | |
| 56 mCancelled = true; | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 @Override | |
| 61 protected void setUp() throws Exception { | |
| 62 super.setUp(); | |
| 63 launchContentShellWithUrl("about:blank"); | |
| 64 assertTrue("Page failed to load", waitForActiveShellToBeDoneLoading()); | |
| 65 } | |
| 66 | |
| 67 /** | |
| 68 * Inject our fake wrapper into VibrationManagerImpl, | |
| 69 * load the webpage which will request vibrate for 3000 milliseconds, | |
| 70 * the fake wrapper vibrate() should be called and 3000 milliseconds should be recorded | |
| 71 * correctly. | |
| 72 */ | |
| 73 @MediumTest | |
| 74 @Feature({"VibrationManagerImpl"}) | |
|
timvolodine
2015/09/11 16:40:15
@Feature({"Vibration"}) would probably be more mea
leonhsl(Using Gerrit)
2015/09/22 09:56:19
Done.
| |
| 75 public void testVibrate() throws Throwable { | |
| 76 final FakeAndroidVibratorWrapper fakeWrapper = new FakeAndroidVibratorWr apper( | |
| 77 (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVI CE)); | |
| 78 VibrationManagerImpl.setVibratorWrapperForTesting(fakeWrapper); | |
| 79 | |
| 80 assertEquals(fakeWrapper.mMilliSeconds, -1); | |
| 81 assertFalse(fakeWrapper.mCancelled); | |
| 82 | |
| 83 loadNewShell(URL_VIBRATOR_VIBRATE); | |
| 84 // Waits until VibrationManagerImpl.Vibrate() got called. | |
| 85 assertTrue(CriteriaHelper.pollForUIThreadCriteria(new Criteria() { | |
| 86 @Override | |
| 87 public boolean isSatisfied() { | |
| 88 return fakeWrapper.mMilliSeconds != -1; | |
| 89 } | |
| 90 })); | |
| 91 | |
| 92 assertEquals( | |
| 93 "Did not get vibrate mMilliSeconds correctly", fakeWrapper.mMill iSeconds, 3000); | |
|
timvolodine
2015/09/11 16:40:15
put 3000 before fakeWrapper.mMilliSeconds, that wa
leonhsl(Using Gerrit)
2015/09/22 09:56:19
Done.
| |
| 94 } | |
| 95 | |
| 96 /** | |
| 97 * Inject our fake wrapper into VibrationManagerImpl, | |
| 98 * load the webpage which will request vibrate and then request cancel, | |
| 99 * the fake wrapper cancel() should be called. | |
| 100 */ | |
| 101 @MediumTest | |
| 102 @Feature({"VibrationManagerImpl"}) | |
| 103 public void testCancel() throws Throwable { | |
| 104 final FakeAndroidVibratorWrapper fakeWrapper = new FakeAndroidVibratorWr apper( | |
| 105 (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVI CE)); | |
| 106 VibrationManagerImpl.SetVibratorWrapperForTesting(fakeWrapper); | |
| 107 | |
| 108 assertEquals(fakeWrapper.mMilliSeconds, -1); | |
| 109 assertFalse(fakeWrapper.mCancelled); | |
| 110 | |
| 111 loadNewShell(URL_VIBRATOR_CANCEL); | |
| 112 // Waits until VibrationManagerImpl.Cancel() got called. | |
| 113 assertTrue(CriteriaHelper.pollForUIThreadCriteria(new Criteria() { | |
| 114 @Override | |
| 115 public boolean isSatisfied() { | |
| 116 return fakeWrapper.mCancelled; | |
| 117 } | |
| 118 })); | |
| 119 | |
| 120 assertTrue("Did not get cancelled", fakeWrapper.mCancelled); | |
| 121 } | |
| 122 } | |
| OLD | NEW |