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 | |
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
| |
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({"Vibration"}) | |
75 public void testVibrate() throws Throwable { | |
76 final FakeAndroidVibratorWrapper fakeWrapper = new FakeAndroidVibratorWr apper( | |
77 (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVI CE)); | |
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.
| |
78 VibrationManagerImpl.setVibratorWrapperForTesting(fakeWrapper); | |
79 | |
80 assertEquals(-1, fakeWrapper.mMilliSeconds); | |
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", 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
| |
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(-1, fakeWrapper.mMilliSeconds); | |
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 |