Skip to content

Commit

Permalink
Add browsertests for VibrationManager java impl on android.
Browse files Browse the repository at this point in the history
On android VibrationManager mojo service is implemented directly with Java,
this CL adds browsertests to confirm VibrationManagerImpl behaviors.

BUG=
TEST=ContentShellTest instrumentation test PASS on android device.

Review URL: https://codereview.chromium.org/1324853004

Cr-Commit-Position: refs/heads/master@{#358286}
  • Loading branch information
leon.han authored and Commit bot committed Nov 6, 2015
1 parent 5fcf9a3 commit ce83630
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 2 deletions.
2 changes: 2 additions & 0 deletions content/public/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ android_library("content_javatests") {
"//content/shell/android:content_shell_java_resources",
"//content/shell/android:content_shell_apk_java",
"//content/shell/android:content_shell_test_java",
"//device/vibration/android:vibration_manager_android",
"//device/vibration:mojo_bindings_java",
"//media/base/android:media_java",
"//mojo/android:system_java",
"//net/android:net_java",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// 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.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>");

private FakeAndroidVibratorWrapper mFakeWrapper;

// Override AndroidVibratorWrapper API to record the calling.
private static class FakeAndroidVibratorWrapper
extends VibrationManagerImpl.AndroidVibratorWrapper {
// Record the parameters of vibrate() and cancel().
public long mMilliSeconds;
public boolean mCancelled;

protected FakeAndroidVibratorWrapper() {
super();
mMilliSeconds = -1;
mCancelled = false;
}

@Override
public void vibrate(Vibrator vibrator, long milliseconds) {
mMilliSeconds = milliseconds;
}

@Override
public void cancel(Vibrator vibrator) {
mCancelled = true;
}
}

@Override
protected void setUp() throws Exception {
super.setUp();
launchContentShellWithUrl("about:blank");
assertTrue("Page failed to load", waitForActiveShellToBeDoneLoading());

mFakeWrapper = new FakeAndroidVibratorWrapper();
VibrationManagerImpl.setVibratorWrapperForTesting(mFakeWrapper);
assertEquals(-1, mFakeWrapper.mMilliSeconds);
assertFalse(mFakeWrapper.mCancelled);
}

/**
* 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 {
loadNewShell(URL_VIBRATOR_VIBRATE);

// Waits until VibrationManagerImpl.Vibrate() got called.
assertTrue(CriteriaHelper.pollForUIThreadCriteria(new Criteria() {
@Override
public boolean isSatisfied() {
return mFakeWrapper.mMilliSeconds != -1;
}
}));

assertEquals(
"Did not get vibrate mMilliSeconds correctly", 3000, mFakeWrapper.mMilliSeconds);
}

/**
* 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({"Vibration"})
public void testCancel() throws Throwable {
loadNewShell(URL_VIBRATOR_CANCEL);

// Waits until VibrationManagerImpl.Cancel() got called.
assertTrue(CriteriaHelper.pollForUIThreadCriteria(new Criteria() {
@Override
public boolean isSatisfied() {
return mFakeWrapper.mCancelled;
}
}));

assertTrue("Did not get cancelled", mFakeWrapper.mCancelled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.os.Vibrator;
import android.util.Log;

import org.chromium.base.VisibleForTesting;
import org.chromium.mojo.system.MojoException;
import org.chromium.mojom.device.VibrationManager;

Expand All @@ -27,9 +28,35 @@ public class VibrationManagerImpl implements VibrationManager {
private final Vibrator mVibrator;
private final boolean mHasVibratePermission;

private static AndroidVibratorWrapper sVibratorWrapper;

/**
* Android Vibrator wrapper class provided to test code to extend.
*/
@VisibleForTesting
public static class AndroidVibratorWrapper {
protected AndroidVibratorWrapper() {}

public void vibrate(Vibrator vibrator, long milliseconds) {
vibrator.vibrate(milliseconds);
}

public void cancel(Vibrator vibrator) {
vibrator.cancel();
}
}

// Test code can use this function to inject other wrapper for testing.
public static void setVibratorWrapperForTesting(AndroidVibratorWrapper wrapper) {
sVibratorWrapper = wrapper;
}

public VibrationManagerImpl(Context context) {
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (sVibratorWrapper == null) {
sVibratorWrapper = new AndroidVibratorWrapper();
}
mHasVibratePermission =
context.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
== PackageManager.PERMISSION_GRANTED;
Expand All @@ -53,12 +80,12 @@ public void vibrate(long milliseconds) {

if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT
&& mHasVibratePermission) {
mVibrator.vibrate(sanitizedMilliseconds);
sVibratorWrapper.vibrate(mVibrator, sanitizedMilliseconds);
}
}

@Override
public void cancel() {
if (mHasVibratePermission) mVibrator.cancel();
if (mHasVibratePermission) sVibratorWrapper.cancel(mVibrator);
}
}

0 comments on commit ce83630

Please sign in to comment.