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.test.suitebuilder.annotation.MediumTest; | |
8 | |
9 import org.chromium.base.test.util.Feature; | |
10 import org.chromium.base.test.util.UrlUtils; | |
11 import org.chromium.content.browser.test.util.Criteria; | |
12 import org.chromium.content.browser.test.util.CriteriaHelper; | |
13 import org.chromium.content_shell_apk.ContentShellTestBase; | |
14 import org.chromium.device.vibration.VibrationManagerImpl; | |
15 | |
16 /** | |
17 * Tests java implementation of VibrationManager mojo service on android. | |
18 */ | |
19 public class VibrationManagerImplTest extends ContentShellTestBase { | |
20 private static final String URL_VIBRATOR_VIBRATE = UrlUtils.encodeHtmlDataUr i("<html><body>" | |
21 + " <script type=\"text/javascript\">" | |
22 + " navigator.vibrate(3000);" | |
23 + " </script>" | |
24 + "</body></html>"); | |
25 private static final String URL_VIBRATOR_CANCEL = UrlUtils.encodeHtmlDataUri ("<html><body>" | |
26 + " <script type=\"text/javascript\">" | |
27 + " navigator.vibrate(10000);" | |
28 + " navigator.vibrate(0);" | |
29 + " </script>" | |
30 + "</body></html>"); | |
31 | |
32 private final FakeAndroidVibratorWrapper mFakeWrapper = new FakeAndroidVibra torWrapper(); | |
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() { | |
42 // No need to provide real system Vibrator for testing, just pass nu ll. | |
43 super(null); | |
44 mMilliSeconds = -1; | |
45 mCancelled = false; | |
46 } | |
47 | |
48 @Override | |
49 public void vibrate(long milliseconds) { | |
50 mMilliSeconds = milliseconds; | |
51 } | |
52 | |
53 @Override | |
54 public void cancel() { | |
55 mCancelled = true; | |
56 } | |
57 } | |
58 | |
59 @Override | |
60 protected void setUp() throws Exception { | |
61 super.setUp(); | |
62 launchContentShellWithUrl("about:blank"); | |
63 assertTrue("Page failed to load", waitForActiveShellToBeDoneLoading()); | |
64 | |
65 VibrationManagerImpl.setVibratorWrapperForTesting(mFakeWrapper); | |
timvolodine
2015/10/22 11:26:13
hmm does that actually work? probably better to cr
leonhsl(Using Gerrit)
2015/10/25 05:55:49
Done.
| |
66 assertEquals(-1, mFakeWrapper.mMilliSeconds); | |
67 assertFalse(mFakeWrapper.mCancelled); | |
68 } | |
69 | |
70 /** | |
71 * Inject our fake wrapper into VibrationManagerImpl, | |
72 * load the webpage which will request vibrate for 3000 milliseconds, | |
73 * the fake wrapper vibrate() should be called and 3000 milliseconds should be recorded | |
74 * correctly. | |
75 */ | |
76 @MediumTest | |
77 @Feature({"Vibration"}) | |
78 public void testVibrate() throws Throwable { | |
79 loadNewShell(URL_VIBRATOR_VIBRATE); | |
80 | |
81 // Waits until VibrationManagerImpl.Vibrate() got called. | |
82 assertTrue(CriteriaHelper.pollForUIThreadCriteria(new Criteria() { | |
83 @Override | |
84 public boolean isSatisfied() { | |
85 return mFakeWrapper.mMilliSeconds != -1; | |
86 } | |
87 })); | |
88 | |
89 assertEquals( | |
90 "Did not get vibrate mMilliSeconds correctly", 3000, mFakeWrappe r.mMilliSeconds); | |
91 } | |
92 | |
93 /** | |
94 * Inject our fake wrapper into VibrationManagerImpl, | |
95 * load the webpage which will request vibrate and then request cancel, | |
96 * the fake wrapper cancel() should be called. | |
97 */ | |
98 @MediumTest | |
99 @Feature({"Vibration"}) | |
100 public void testCancel() throws Throwable { | |
101 loadNewShell(URL_VIBRATOR_CANCEL); | |
102 | |
103 // Waits until VibrationManagerImpl.Cancel() got called. | |
104 assertTrue(CriteriaHelper.pollForUIThreadCriteria(new Criteria() { | |
105 @Override | |
106 public boolean isSatisfied() { | |
107 return mFakeWrapper.mCancelled; | |
108 } | |
109 })); | |
110 | |
111 assertTrue("Did not get cancelled", mFakeWrapper.mCancelled); | |
112 } | |
113 } | |
OLD | NEW |