Index: content/public/android/javatests/src/org/chromium/content/browser/VibrationManagerImplTest.java |
diff --git a/content/public/android/javatests/src/org/chromium/content/browser/VibrationManagerImplTest.java b/content/public/android/javatests/src/org/chromium/content/browser/VibrationManagerImplTest.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7699eefc224c0ca2d39087fad9df91489cb1d59e |
--- /dev/null |
+++ b/content/public/android/javatests/src/org/chromium/content/browser/VibrationManagerImplTest.java |
@@ -0,0 +1,122 @@ |
+// Copyright 2015 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.content.browser; |
+ |
+import android.content.Context; |
+import android.os.Vibrator; |
+import android.test.suitebuilder.annotation.MediumTest; |
+ |
+import org.chromium.base.test.util.Feature; |
+import org.chromium.base.test.util.UrlUtils; |
+import org.chromium.content.browser.test.util.Criteria; |
+import org.chromium.content.browser.test.util.CriteriaHelper; |
+import org.chromium.content_shell_apk.ContentShellTestBase; |
+import org.chromium.device.vibration.VibrationManagerImpl; |
+ |
+/** |
+ * Tests java implementation of VibrationManager mojo service on android. |
+ */ |
+public class VibrationManagerImplTest extends ContentShellTestBase { |
+ private static final String URL_VIBRATOR_VIBRATE = UrlUtils.encodeHtmlDataUri("<html><body>" |
+ + " <script type=\"text/javascript\">" |
+ + " navigator.vibrate(3000);" |
+ + " </script>" |
+ + "</body></html>"); |
+ private static final String URL_VIBRATOR_CANCEL = UrlUtils.encodeHtmlDataUri("<html><body>" |
+ + " <script type=\"text/javascript\">" |
+ + " navigator.vibrate(10000);" |
+ + " navigator.vibrate(0);" |
+ + " </script>" |
+ + "</body></html>"); |
+ |
+ // Override AndroidVibratorWrapper API to record the calling. |
+ private static class FakeAndroidVibratorWrapper |
timvolodine
2015/10/16 17:41:24
can we simply inject a FakeVibrator which extends
leonhsl(Using Gerrit)
2015/10/19 10:27:37
Yeah I thought about this idea before but found th
timvolodine
2015/10/20 13:16:22
ah sorry didn't realize the Vibrator was not meant
|
+ extends VibrationManagerImpl.AndroidVibratorWrapper { |
+ // Record the parameters of vibrate() and cancel(). |
+ public long mMilliSeconds; |
+ public boolean mCancelled; |
+ |
+ protected FakeAndroidVibratorWrapper(Vibrator vibrator) { |
+ super(vibrator); |
+ mMilliSeconds = -1; |
+ mCancelled = false; |
+ } |
+ |
+ @Override |
+ public void vibrate(long milliseconds) { |
+ super.vibrate(milliseconds); |
+ mMilliSeconds = milliseconds; |
+ } |
+ |
+ @Override |
+ public void cancel() { |
+ super.cancel(); |
+ mCancelled = true; |
+ } |
+ } |
+ |
+ @Override |
+ protected void setUp() throws Exception { |
+ super.setUp(); |
+ launchContentShellWithUrl("about:blank"); |
+ assertTrue("Page failed to load", waitForActiveShellToBeDoneLoading()); |
+ } |
+ |
+ /** |
+ * Inject our fake wrapper into VibrationManagerImpl, |
+ * load the webpage which will request vibrate for 3000 milliseconds, |
+ * the fake wrapper vibrate() should be called and 3000 milliseconds should be recorded |
+ * correctly. |
+ */ |
+ @MediumTest |
+ @Feature({"Vibration"}) |
+ public void testVibrate() throws Throwable { |
+ final FakeAndroidVibratorWrapper fakeWrapper = new FakeAndroidVibratorWrapper( |
+ (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE)); |
timvolodine
2015/10/16 17:41:24
why the need to supply a real system vibrator for
leonhsl(Using Gerrit)
2015/10/19 10:27:37
Yeah no need for this, modified.
|
+ VibrationManagerImpl.setVibratorWrapperForTesting(fakeWrapper); |
+ |
+ assertEquals(-1, fakeWrapper.mMilliSeconds); |
+ assertFalse(fakeWrapper.mCancelled); |
+ |
+ loadNewShell(URL_VIBRATOR_VIBRATE); |
+ // Waits until VibrationManagerImpl.Vibrate() got called. |
+ assertTrue(CriteriaHelper.pollForUIThreadCriteria(new Criteria() { |
+ @Override |
+ public boolean isSatisfied() { |
+ return fakeWrapper.mMilliSeconds != -1; |
+ } |
+ })); |
+ |
+ assertEquals( |
+ "Did not get vibrate mMilliSeconds correctly", 3000, fakeWrapper.mMilliSeconds); |
timvolodine
2015/10/16 17:41:24
is this is correctly formatted?
leonhsl(Using Gerrit)
2015/10/19 10:27:37
It is formatted by 'git cl format', seems in this
|
+ } |
+ |
+ /** |
+ * Inject our fake wrapper into VibrationManagerImpl, |
+ * load the webpage which will request vibrate and then request cancel, |
+ * the fake wrapper cancel() should be called. |
+ */ |
+ @MediumTest |
+ @Feature({"VibrationManagerImpl"}) |
+ public void testCancel() throws Throwable { |
+ final FakeAndroidVibratorWrapper fakeWrapper = new FakeAndroidVibratorWrapper( |
+ (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE)); |
+ VibrationManagerImpl.SetVibratorWrapperForTesting(fakeWrapper); |
+ |
+ assertEquals(-1, fakeWrapper.mMilliSeconds); |
+ assertFalse(fakeWrapper.mCancelled); |
+ |
+ loadNewShell(URL_VIBRATOR_CANCEL); |
+ // Waits until VibrationManagerImpl.Cancel() got called. |
+ assertTrue(CriteriaHelper.pollForUIThreadCriteria(new Criteria() { |
+ @Override |
+ public boolean isSatisfied() { |
+ return fakeWrapper.mCancelled; |
+ } |
+ })); |
+ |
+ assertTrue("Did not get cancelled", fakeWrapper.mCancelled); |
+ } |
+} |