зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1357639 - part4 : add Fennec test for media control and audio focus. r=sebastian
Add robocop tests and mochitest chrome. MozReview-Commit-ID: JofkKRSNdB5 --HG-- extra : rebase_source : f8a5315927cf7e10a96337468fa2dec9871b7147
This commit is contained in:
Родитель
59a4e31527
Коммит
af1774e465
Двоичный файл не отображается.
|
@ -1,6 +1,7 @@
|
|||
[DEFAULT]
|
||||
skip-if = os != 'android'
|
||||
support-files =
|
||||
audio.ogg
|
||||
basic_article.html
|
||||
basic_article_mobile.html
|
||||
basic_article_mobile_2x.html
|
||||
|
@ -10,6 +11,7 @@ support-files =
|
|||
devicesearch.xml
|
||||
head.js
|
||||
head_search.js
|
||||
media_playback.html
|
||||
session_formdata_sample.html
|
||||
simpleservice.xml
|
||||
video_controls.html
|
||||
|
@ -34,6 +36,7 @@ skip-if = debug
|
|||
[test_identity_mode.html]
|
||||
[test_java_addons.html]
|
||||
[test_jni.html]
|
||||
[test_media_playback.html]
|
||||
[test_migrate_ui.html]
|
||||
[test_network_manager.html]
|
||||
[test_offline_page.html]
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
<!DOCTYPE html>
|
||||
<audio id="testAudio" src="audio.ogg" loop></audio>
|
||||
<script type="text/javascript">
|
||||
var audio = document.getElementById("testAudio");
|
||||
audio.oncanplay = function() {
|
||||
audio.oncanplay = null;
|
||||
audio.play();
|
||||
};
|
||||
</script>
|
|
@ -0,0 +1,154 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Test for media playback</title>
|
||||
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
|
||||
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
|
||||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
|
||||
<script type="application/javascript" src="head.js"></script>
|
||||
<script type="application/javascript">
|
||||
|
||||
"use strict";
|
||||
|
||||
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
|
||||
|
||||
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
||||
Cu.import("resource://gre/modules/Services.jsm");
|
||||
Cu.import("resource://gre/modules/Task.jsm");
|
||||
|
||||
let gChromeWin;
|
||||
let gBrowserApp;
|
||||
|
||||
const URL = "http://example.org/chrome/mobile/android/tests/browser/chrome/media_playback.html";
|
||||
|
||||
// Waiting for a tab to load or restore can be slow on the emulator.
|
||||
SimpleTest.requestLongerTimeout(2);
|
||||
|
||||
setup_browser();
|
||||
|
||||
function getAudio(browser) {
|
||||
return browser.contentWindow.document.getElementById("testAudio");
|
||||
}
|
||||
|
||||
function playAudio(browser) {
|
||||
let audio = getAudio(browser);
|
||||
if (!audio) {
|
||||
ok(false, "can't get the audio!")
|
||||
}
|
||||
return audio.play();
|
||||
}
|
||||
|
||||
function stopAudio(browser) {
|
||||
let audio = getAudio(browser);
|
||||
if (!audio) {
|
||||
ok(false, "can't get the audio!")
|
||||
}
|
||||
audio.pause();
|
||||
}
|
||||
|
||||
function isAudioStarted(browser) {
|
||||
let audio = getAudio(browser);
|
||||
if (!audio) {
|
||||
ok(false, "can't get the audio!")
|
||||
}
|
||||
return !audio.paused;
|
||||
}
|
||||
|
||||
function setup_browser() {
|
||||
gChromeWin = Services.wm.getMostRecentWindow("navigator:browser");
|
||||
gBrowserApp = gChromeWin.BrowserApp;
|
||||
}
|
||||
|
||||
add_task(function* test_media_control() {
|
||||
info("- open a new tab -");
|
||||
let tab = gBrowserApp.addTab(URL);
|
||||
let browser = tab.browser;
|
||||
|
||||
info("- wait for loading tab's content -");
|
||||
yield promiseBrowserEvent(browser, "load");
|
||||
|
||||
info("- check whether audio starts playing -");
|
||||
yield promiseTabEvent(browser, "DOMAudioPlaybackStarted");
|
||||
ok(isAudioStarted(browser), "audio has started playing.");
|
||||
ok(tab.playingAudio, "tab is playing audio.");
|
||||
|
||||
info("- pause from control -");
|
||||
Services.obs.notifyObservers(browser, "mediaControl", "mediaControlPaused");
|
||||
|
||||
info("- check whether audio stops playing -");
|
||||
yield promiseTabEvent(browser, "DOMAudioPlaybackStopped");
|
||||
ok(!isAudioStarted(browser), "audio has stopped playing.");
|
||||
ok(!tab.playingAudio, "tab isn't playing audio.");
|
||||
|
||||
info("- resume from control -");
|
||||
Services.obs.notifyObservers(browser, "mediaControl", "resumeMedia");
|
||||
|
||||
info("- check whether audio starts playing -");
|
||||
yield promiseTabEvent(browser, "DOMAudioPlaybackStarted");
|
||||
ok(isAudioStarted(browser), "audio has started playing.");
|
||||
ok(tab.playingAudio, "tab is playing audio.");
|
||||
|
||||
info("- stop from control -");
|
||||
Services.obs.notifyObservers(browser, "mediaControl", "mediaControlStopped");
|
||||
|
||||
info("- check whether audio stops playing -");
|
||||
yield promiseTabEvent(browser, "DOMAudioPlaybackStopped");
|
||||
ok(!isAudioStarted(browser), "audio has stopped playing.");
|
||||
ok(!tab.playingAudio, "tab isn't playing audio.");
|
||||
|
||||
info("- remove tab -");
|
||||
gBrowserApp.closeTab(tab);
|
||||
});
|
||||
|
||||
add_task(function* test_audio_focus() {
|
||||
info("- open a new tab -");
|
||||
let tab = gBrowserApp.addTab(URL);
|
||||
let browser = tab.browser;
|
||||
|
||||
info("- wait for loading tab's content -");
|
||||
yield promiseBrowserEvent(browser, "load");
|
||||
|
||||
info("- check whether audio starts playing -");
|
||||
yield promiseTabEvent(browser, "DOMAudioPlaybackStarted");
|
||||
ok(isAudioStarted(browser), "audio has started playing.");
|
||||
ok(tab.playingAudio, "tab is playing audio.");
|
||||
|
||||
info("- pause when transiently lossing audio focus -");
|
||||
Services.obs.notifyObservers(browser, "audioFocusChanged", "lostAudioFocusTransiently");
|
||||
|
||||
info("- check whether audio stops playing -");
|
||||
yield promiseTabEvent(browser, "DOMAudioPlaybackStopped");
|
||||
ok(!isAudioStarted(browser), "audio has stopped playing.");
|
||||
ok(!tab.playingAudio, "tab isn't playing audio.");
|
||||
|
||||
info("- resume when gain audio focus again -");
|
||||
Services.obs.notifyObservers(browser, "audioFocusChanged", "gainAudioFocus");
|
||||
|
||||
info("- check whether audio starts playing -");
|
||||
yield promiseTabEvent(browser, "DOMAudioPlaybackStarted");
|
||||
ok(isAudioStarted(browser), "audio has started playing.");
|
||||
ok(tab.playingAudio, "tab is playing audio.");
|
||||
|
||||
info("- pause when lossing audio focus -");
|
||||
Services.obs.notifyObservers(browser, "audioFocusChanged", "lostAudioFocus");
|
||||
|
||||
info("- check whether audio stops playing -");
|
||||
yield promiseTabEvent(browser, "DOMAudioPlaybackStopped");
|
||||
ok(!isAudioStarted(browser), "audio has stopped playing.");
|
||||
ok(!tab.playingAudio, "tab isn't playing audio.");
|
||||
|
||||
info("- remove tab -");
|
||||
gBrowserApp.closeTab(tab);
|
||||
});
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
</div>
|
||||
<pre id="test">
|
||||
</pre>
|
||||
</body>
|
||||
</html>
|
Двоичный файл не отображается.
|
@ -14,6 +14,9 @@ skip-if = android_version == "18"
|
|||
# disabled on 4.3, bug 1146420
|
||||
skip-if = android_version == "18"
|
||||
[src/org/mozilla/gecko/tests/testANRReporter.java]
|
||||
[src/org/mozilla/gecko/tests/testAudioFocus.java]
|
||||
[src/org/mozilla/gecko/tests/testMediaControl.java]
|
||||
skip-if = android_version < "23"
|
||||
[src/org/mozilla/gecko/tests/testAxisLocking.java]
|
||||
# [src/org/mozilla/gecko/tests/testBookmark.java] # see bug 915350
|
||||
[src/org/mozilla/gecko/tests/testBookmarksPanel.java]
|
||||
|
|
|
@ -1 +1,4 @@
|
|||
[testAdobeFlash]
|
||||
[testAudioFocus]
|
||||
[testMediaControl]
|
||||
skip-if = android_version < "23"
|
|
@ -0,0 +1,9 @@
|
|||
<!DOCTYPE html>
|
||||
<audio id="testAudio" src="audio.ogg"></audio>
|
||||
<script type="text/javascript">
|
||||
var audio = document.getElementById("testAudio");
|
||||
audio.oncanplay = function() {
|
||||
audio.oncanplay = null;
|
||||
audio.play();
|
||||
};
|
||||
</script>
|
|
@ -0,0 +1,9 @@
|
|||
<!DOCTYPE html>
|
||||
<audio id="testAudio" src="audio.ogg" loop></audio>
|
||||
<script type="text/javascript">
|
||||
var audio = document.getElementById("testAudio");
|
||||
audio.oncanplay = function() {
|
||||
audio.oncanplay = null;
|
||||
audio.play();
|
||||
};
|
||||
</script>
|
|
@ -611,6 +611,10 @@ abstract class BaseTest extends BaseRobocopTest {
|
|||
}
|
||||
}
|
||||
|
||||
public void closeAllTabs() {
|
||||
Tabs.getInstance().closeAll();
|
||||
}
|
||||
|
||||
public final void runOnUiThreadSync(Runnable runnable) {
|
||||
RobocopUtils.runOnUiThreadSync(getActivity(), runnable);
|
||||
}
|
||||
|
|
|
@ -85,6 +85,8 @@ public class StringHelper {
|
|||
public final String ROBOCOP_INPUT_URL = "/robocop/robocop_input.html";
|
||||
public final String ROBOCOP_READER_MODE_BASIC_ARTICLE = "/robocop/reader_mode_pages/basic_article.html";
|
||||
public final String ROBOCOP_LINK_TO_SLOW_LOADING = "/robocop/robocop_link_to_slow_loading.html";
|
||||
public final String ROBOCOP_MEDIA_PLAYBACK_URL = "/robocop/robocop_media_playback.html";
|
||||
public final String ROBOCOP_MEDIA_PLAYBACK_LOOP_URL = "/robocop/robocop_media_playback_loop.html";
|
||||
|
||||
private final String ROBOCOP_JS_HARNESS_URL = "/robocop/robocop_javascript.html";
|
||||
|
||||
|
|
|
@ -0,0 +1,212 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.gecko.tests;
|
||||
|
||||
import org.mozilla.gecko.Actions;
|
||||
import org.mozilla.gecko.Tab;
|
||||
import org.mozilla.gecko.Tabs;
|
||||
import org.mozilla.gecko.media.AudioFocusAgent;
|
||||
import org.mozilla.gecko.media.AudioFocusAgent.State;
|
||||
|
||||
import android.media.AudioManager;
|
||||
|
||||
import com.robotium.solo.Condition;
|
||||
|
||||
public class testAudioFocus extends BaseTest {
|
||||
private boolean mPrevTabAudioPlaying = false;
|
||||
|
||||
public void testAudioFocus() {
|
||||
info("- wait for gecko ready -");
|
||||
blockForGeckoReady();
|
||||
|
||||
info("- check audio focus in the beginning -");
|
||||
mAsserter.is(getAudioFocusAgent().getAudioFocusState(),
|
||||
State.LOST_FOCUS,
|
||||
"Should not own audio focus.");
|
||||
|
||||
info("- request audio focus -");
|
||||
requestAudioFocus();
|
||||
mAsserter.ok(true,
|
||||
"Check audio focus state",
|
||||
"Should own audio focus.");
|
||||
|
||||
info("- simulate losing audio focus transiently -");
|
||||
getAudioFocusAgent().changeAudioFocus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT);
|
||||
mAsserter.is(getAudioFocusAgent().getAudioFocusState(),
|
||||
State.LOST_FOCUS_TRANSIENT,
|
||||
"Should lose audio focus.");
|
||||
|
||||
info("- simulate gaining audio focus again -");
|
||||
getAudioFocusAgent().changeAudioFocus(AudioManager.AUDIOFOCUS_GAIN);
|
||||
mAsserter.is(getAudioFocusAgent().getAudioFocusState(),
|
||||
State.OWN_FOCUS,
|
||||
"Should own audio focus.");
|
||||
|
||||
info("- simulate losing audio focus and can duck -");
|
||||
getAudioFocusAgent().changeAudioFocus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK);
|
||||
mAsserter.is(getAudioFocusAgent().getAudioFocusState(),
|
||||
State.LOST_FOCUS_TRANSIENT_CAN_DUCK,
|
||||
"Should lose audio focus and can duck.");
|
||||
|
||||
info("- simulate gaining audio focus again -");
|
||||
getAudioFocusAgent().changeAudioFocus(AudioManager.AUDIOFOCUS_GAIN);
|
||||
mAsserter.is(getAudioFocusAgent().getAudioFocusState(),
|
||||
State.OWN_FOCUS,
|
||||
"Should own audio focus.");
|
||||
|
||||
info("- simulate losing audio focus -");
|
||||
getAudioFocusAgent().changeAudioFocus(AudioManager.AUDIOFOCUS_LOSS);
|
||||
mAsserter.is(getAudioFocusAgent().getAudioFocusState(),
|
||||
State.LOST_FOCUS,
|
||||
"Should lose audio focus.");
|
||||
|
||||
info("- request audio focus -");
|
||||
requestAudioFocus();
|
||||
mAsserter.is(getAudioFocusAgent().getAudioFocusState(),
|
||||
State.OWN_FOCUS,
|
||||
"Should own audio focus.");
|
||||
|
||||
info("- abandon audio focus -");
|
||||
getAudioFocusAgent().notifyStoppedPlaying();
|
||||
mAsserter.is(getAudioFocusAgent().getAudioFocusState(),
|
||||
State.LOST_FOCUS,
|
||||
"Should lose audio focus.");
|
||||
|
||||
info("- run next test : testAudioFocusLoadTab -");
|
||||
testAudioFocusLoadTab();
|
||||
}
|
||||
|
||||
private void testAudioFocusLoadTab() {
|
||||
info("- check audio focus in the beginning -");
|
||||
mAsserter.is(getAudioFocusAgent().getAudioFocusState(),
|
||||
State.LOST_FOCUS,
|
||||
"Should not request audio focus before media starts.");
|
||||
|
||||
info("- load URL with non-looping audio file -");
|
||||
final String MEDIA_URL = getAbsoluteUrl(mStringHelper.ROBOCOP_MEDIA_PLAYBACK_URL);
|
||||
loadUrlAndWait(MEDIA_URL);
|
||||
|
||||
info("- wait audio starts playing -");
|
||||
waitUntilTabAudioPlayingStateChanged();
|
||||
mAsserter.is(getAudioFocusAgent().getAudioFocusState(),
|
||||
State.OWN_FOCUS,
|
||||
"Should request audio focus after media started playing.");
|
||||
|
||||
info("- simulate losing audio focus -");
|
||||
waitUntilTabAudioPlayingStateChanged();
|
||||
mAsserter.is(getAudioFocusAgent().getAudioFocusState(),
|
||||
State.LOST_FOCUS,
|
||||
"Should abandon audio focus after media stopped playing.");
|
||||
|
||||
info("- close tab -");
|
||||
closeAllTabs();
|
||||
|
||||
info("- run next test : testAudioFocusChanged -");
|
||||
testAudioFocusChanged();
|
||||
}
|
||||
|
||||
private void testAudioFocusChanged() {
|
||||
info("- check audio focus in the beginning -");
|
||||
mAsserter.is(getAudioFocusAgent().getAudioFocusState(),
|
||||
State.LOST_FOCUS,
|
||||
"Should not request audio focus before media starts.");
|
||||
|
||||
info("- load URL with looping audio file -");
|
||||
final String MEDIA_URL = getAbsoluteUrl(mStringHelper.ROBOCOP_MEDIA_PLAYBACK_LOOP_URL);
|
||||
loadUrlAndWait(MEDIA_URL);
|
||||
|
||||
info("- wait audio starts playing -");
|
||||
waitUntilTabAudioPlayingStateChanged();
|
||||
mAsserter.is(getAudioFocusAgent().getAudioFocusState(),
|
||||
State.OWN_FOCUS,
|
||||
"Should request audio focus after media started playing.");
|
||||
|
||||
info("- simulate losing audio focus transiently -");
|
||||
getAudioFocusAgent().changeAudioFocus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT);
|
||||
waitUntilTabAudioPlayingStateChanged();
|
||||
mAsserter.is(getAudioFocusAgent().getAudioFocusState(),
|
||||
State.LOST_FOCUS_TRANSIENT,
|
||||
"Should lose audio focus.");
|
||||
|
||||
info("- simulate gaining audio focus again -");
|
||||
getAudioFocusAgent().changeAudioFocus(AudioManager.AUDIOFOCUS_GAIN);
|
||||
waitUntilTabAudioPlayingStateChanged();
|
||||
mAsserter.is(getAudioFocusAgent().getAudioFocusState(),
|
||||
State.OWN_FOCUS,
|
||||
"Should own audio focus.");
|
||||
|
||||
info("- simulate losing audio focus -");
|
||||
getAudioFocusAgent().changeAudioFocus(AudioManager.AUDIOFOCUS_LOSS);
|
||||
waitUntilTabAudioPlayingStateChanged();
|
||||
mAsserter.is(getAudioFocusAgent().getAudioFocusState(),
|
||||
State.LOST_FOCUS,
|
||||
"Should abandon audio focus after media stopped playing.");
|
||||
|
||||
info("- close tab -");
|
||||
closeAllTabs();
|
||||
}
|
||||
|
||||
private void testSwitchTab() {
|
||||
info("- load URL -");
|
||||
final String MEDIA_URL = getAbsoluteUrl(mStringHelper.ROBOCOP_MEDIA_PLAYBACK_LOOP_URL);
|
||||
loadUrlAndWait(MEDIA_URL);
|
||||
|
||||
info("- check whether audio starts playing -");
|
||||
waitUntilTabAudioPlayingStateChanged();
|
||||
|
||||
info("- switch to the another tab -");
|
||||
final String BLANK_URL = getAbsoluteUrl(mStringHelper.ROBOCOP_BLANK_PAGE_01_URL);
|
||||
addTab(BLANK_URL);
|
||||
|
||||
info("- should still own the audio focus -");
|
||||
mAsserter.is(getAudioFocusAgent().getAudioFocusState(),
|
||||
State.OWN_FOCUS,
|
||||
"Should own audio focus.");
|
||||
|
||||
info("- close tab -");
|
||||
closeAllTabs();
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing tool functions
|
||||
*/
|
||||
private void info(String msg) {
|
||||
mAsserter.dumpLog(msg);
|
||||
}
|
||||
|
||||
private AudioFocusAgent getAudioFocusAgent() {
|
||||
return AudioFocusAgent.getInstance();
|
||||
}
|
||||
|
||||
private void requestAudioFocus() {
|
||||
getAudioFocusAgent().notifyStartedPlaying();
|
||||
if (getAudioFocusAgent().getAudioFocusState() == State.OWN_FOCUS) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Request audio focus might fail, depend on the andriod's audio mode.
|
||||
waitForCondition(new Condition() {
|
||||
@Override
|
||||
public boolean isSatisfied() {
|
||||
getAudioFocusAgent().notifyStartedPlaying();
|
||||
return getAudioFocusAgent().getAudioFocusState() == State.OWN_FOCUS;
|
||||
}
|
||||
}, MAX_WAIT_MS);
|
||||
}
|
||||
|
||||
private void waitUntilTabAudioPlayingStateChanged() {
|
||||
final Tab tab = Tabs.getInstance().getSelectedTab();
|
||||
waitForCondition(new Condition() {
|
||||
@Override
|
||||
public boolean isSatisfied() {
|
||||
if (tab.isAudioPlaying() != mPrevTabAudioPlaying) {
|
||||
mPrevTabAudioPlaying = tab.isAudioPlaying();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}, MAX_WAIT_MS);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,273 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.gecko.tests;
|
||||
|
||||
import org.mozilla.gecko.Actions;
|
||||
import org.mozilla.gecko.R;
|
||||
import org.mozilla.gecko.Tab;
|
||||
import org.mozilla.gecko.Tabs;
|
||||
import org.mozilla.gecko.media.AudioFocusAgent;
|
||||
import org.mozilla.gecko.media.AudioFocusAgent.State;
|
||||
import org.mozilla.gecko.media.MediaControlService;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.Context;
|
||||
import android.media.AudioManager;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.os.Build;
|
||||
import android.service.notification.StatusBarNotification;
|
||||
|
||||
import com.robotium.solo.Condition;
|
||||
|
||||
public class testMediaControl extends BaseTest {
|
||||
private Context mContext;
|
||||
private int mPrevIcon = 0;
|
||||
private boolean mPrevTabAudioPlaying = false;
|
||||
|
||||
public void testMediaControl() {
|
||||
// The API to check system notification is available after version 23.
|
||||
if (Build.VERSION.SDK_INT < 23) {
|
||||
return;
|
||||
}
|
||||
|
||||
info("- wait for gecko ready -");
|
||||
blockForGeckoReady();
|
||||
|
||||
info("- setup testing memeber variable -");
|
||||
setupForTesting();
|
||||
|
||||
info("- load URL -");
|
||||
final String MEDIA_URL = getAbsoluteUrl(mStringHelper.ROBOCOP_MEDIA_PLAYBACK_LOOP_URL);
|
||||
loadUrlAndWait(MEDIA_URL);
|
||||
|
||||
info("- check whether audio starts playing -");
|
||||
checkIfMediaPlayingSuccess(true /* playing */);
|
||||
|
||||
info("- simulate media control pause -");
|
||||
notifyMediaControlService(MediaControlService.ACTION_PAUSE);
|
||||
checkIfMediaPlayingSuccess(false /* paused */);
|
||||
|
||||
info("- simulate media control resume -");
|
||||
notifyMediaControlService(MediaControlService.ACTION_RESUME);
|
||||
checkIfMediaPlayingSuccess(true /* playing */);
|
||||
|
||||
info("- simulate media control stop -");
|
||||
notifyMediaControlService(MediaControlService.ACTION_STOP);
|
||||
checkIfMediaPlayingSuccess(false /* paused */, true /* clear notification */);
|
||||
|
||||
info("- close tab -");
|
||||
closeAllTabs();
|
||||
|
||||
info("- run next test : testNavigateOutThePage -");
|
||||
testNavigateOutThePage();
|
||||
}
|
||||
|
||||
private void testNavigateOutThePage() {
|
||||
info("- load URL -");
|
||||
final String MEDIA_URL = getAbsoluteUrl(mStringHelper.ROBOCOP_MEDIA_PLAYBACK_LOOP_URL);
|
||||
loadUrlAndWait(MEDIA_URL);
|
||||
|
||||
info("- check whether audio starts playing -");
|
||||
checkIfMediaPlayingSuccess(true /* playing */);
|
||||
|
||||
info("- navigate out the present page -");
|
||||
final String BLANK_URL = getAbsoluteUrl(mStringHelper.ROBOCOP_BLANK_PAGE_01_URL);
|
||||
loadUrlAndWait(BLANK_URL);
|
||||
checkIfMediaPlayingSuccess(false /* paused */, true /* clear notification */);
|
||||
|
||||
info("- close tab -");
|
||||
closeAllTabs();
|
||||
|
||||
info("- run next test : testAudioFocusChanged -");
|
||||
testAudioFocusChanged();
|
||||
}
|
||||
|
||||
private void testAudioFocusChanged() {
|
||||
info("- load URL -");
|
||||
final String MEDIA_URL = getAbsoluteUrl(mStringHelper.ROBOCOP_MEDIA_PLAYBACK_LOOP_URL);
|
||||
loadUrlAndWait(MEDIA_URL);
|
||||
|
||||
info("- check whether audio starts playing -");
|
||||
checkIfMediaPlayingSuccess(true /* playing */);
|
||||
|
||||
info("- simulate lose audio focus transiently -");
|
||||
getAudioFocusAgent().changeAudioFocus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT);
|
||||
checkIfMediaPlayingSuccess(false /* paused */);
|
||||
|
||||
info("- simulate gain audio focus again -");
|
||||
getAudioFocusAgent().changeAudioFocus(AudioManager.AUDIOFOCUS_GAIN);
|
||||
checkIfMediaPlayingSuccess(true /* playing */);
|
||||
|
||||
info("- simulate lose audio focus -");
|
||||
getAudioFocusAgent().changeAudioFocus(AudioManager.AUDIOFOCUS_LOSS);
|
||||
checkIfMediaPlayingSuccess(false /* paused */);
|
||||
|
||||
info("- close tab -");
|
||||
closeAllTabs();
|
||||
|
||||
info("- run next test : testSwitchTab -");
|
||||
testSwitchTab();
|
||||
}
|
||||
|
||||
private void testSwitchTab() {
|
||||
info("- load URL -");
|
||||
final String MEDIA_URL = getAbsoluteUrl(mStringHelper.ROBOCOP_MEDIA_PLAYBACK_LOOP_URL);
|
||||
loadUrlAndWait(MEDIA_URL);
|
||||
|
||||
info("- check whether audio starts playing -");
|
||||
checkIfMediaPlayingSuccess(true /* playing */);
|
||||
|
||||
info("- switch to the another tab -");
|
||||
final String BLANK_URL = getAbsoluteUrl(mStringHelper.ROBOCOP_BLANK_PAGE_01_URL);
|
||||
addTab(BLANK_URL);
|
||||
|
||||
info("- the media control should still be displayed in status bar -");
|
||||
checkDisplayedNotificationStates(true /* playing */);
|
||||
|
||||
info("- close tab -");
|
||||
closeAllTabs();
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing tool functions
|
||||
*/
|
||||
private void info(String msg) {
|
||||
mAsserter.dumpLog(msg);
|
||||
}
|
||||
|
||||
private void setupForTesting() {
|
||||
mContext = getInstrumentation().getTargetContext();
|
||||
}
|
||||
|
||||
private void waitUntilNotificationUIChanged() {
|
||||
waitForCondition(new Condition() {
|
||||
@Override
|
||||
public boolean isSatisfied() {
|
||||
NotificationManager notificationManager = (NotificationManager)
|
||||
mContext.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
StatusBarNotification[] sbns = notificationManager.getActiveNotifications();
|
||||
if (sbns.length == 1 &&
|
||||
sbns[0].getNotification().actions.length == 1) {
|
||||
// Ensure the UI has been changed.
|
||||
if (sbns[0].getNotification().actions[0].icon != mPrevIcon) {
|
||||
mPrevIcon = sbns[0].getNotification().actions[0].icon ;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}, MAX_WAIT_MS);
|
||||
}
|
||||
|
||||
private void waitUntilTabAudioPlayingStateChanged() {
|
||||
final Tab tab = Tabs.getInstance().getSelectedTab();
|
||||
waitForCondition(new Condition() {
|
||||
@Override
|
||||
public boolean isSatisfied() {
|
||||
if (tab.isAudioPlaying() != mPrevTabAudioPlaying) {
|
||||
mPrevTabAudioPlaying = tab.isAudioPlaying();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}, MAX_WAIT_MS);
|
||||
}
|
||||
|
||||
private void notifyMediaControlService(String action) {
|
||||
Intent intent = new Intent(mContext, MediaControlService.class);
|
||||
intent.setAction(action);
|
||||
mContext.startService(intent);
|
||||
}
|
||||
|
||||
private void checkIfMediaPlayingSuccess(boolean isTabPlaying) {
|
||||
checkIfMediaPlayingSuccess(isTabPlaying, false);
|
||||
}
|
||||
|
||||
private void checkIfMediaPlayingSuccess(boolean isTabPlaying,
|
||||
boolean clearNotification) {
|
||||
checkAudioFocusStateChanged(isTabPlaying);
|
||||
checkMediaNotificationStatesChanged(isTabPlaying, clearNotification);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is used to check whether notification states are correct or
|
||||
* not after notification UI changed.
|
||||
*/
|
||||
private void checkMediaNotificationStatesChanged(boolean isTabPlaying,
|
||||
boolean clearNotification) {
|
||||
waitUntilNotificationUIChanged();
|
||||
|
||||
final Tab tab = Tabs.getInstance().getSelectedTab();
|
||||
mAsserter.ok(isTabPlaying == tab.isMediaPlaying(),
|
||||
"Checking the media playing state of tab, isTabPlaying = " + isTabPlaying,
|
||||
"Tab's media playing state is correct.");
|
||||
|
||||
if (clearNotification) {
|
||||
checkIfNotificationBeCleared();
|
||||
} else {
|
||||
checkDisplayedNotificationStates(isTabPlaying);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkDisplayedNotificationStates(boolean isTabPlaying) {
|
||||
NotificationManager notificationManager = (NotificationManager)
|
||||
mContext.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
|
||||
StatusBarNotification[] sbns = notificationManager.getActiveNotifications();
|
||||
mAsserter.is(sbns.length, 1,
|
||||
"Should only have one notification in system's status bar.");
|
||||
|
||||
Notification notification = sbns[0].getNotification();
|
||||
mAsserter.is(notification.actions.length, 1,
|
||||
"Only has one action in notification.");
|
||||
|
||||
mAsserter.is(notification.actions[0].title,
|
||||
mContext.getString(isTabPlaying ? R.string.media_pause : R.string.media_play),
|
||||
"Action has correct title.");
|
||||
mAsserter.is(notification.actions[0].icon,
|
||||
isTabPlaying ? R.drawable.ic_media_pause : R.drawable.ic_media_play,
|
||||
"Action has correct icon.");
|
||||
}
|
||||
|
||||
private void checkIfNotificationBeCleared() {
|
||||
NotificationManager notificationManager = (NotificationManager)
|
||||
mContext.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
StatusBarNotification[] sbns = notificationManager.getActiveNotifications();
|
||||
mAsserter.is(sbns.length, 0,
|
||||
"Should not have notification in system's status bar.");
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is used to check whether audio focus state are correct or
|
||||
* not after tab's audio playing state changed.
|
||||
*/
|
||||
private void checkAudioFocusStateChanged(boolean isTabPlaying) {
|
||||
waitUntilTabAudioPlayingStateChanged();
|
||||
|
||||
final Tab tab = Tabs.getInstance().getSelectedTab();
|
||||
mAsserter.ok(isTabPlaying == tab.isAudioPlaying(),
|
||||
"Checking the audio playing state of tab, isTabPlaying = " + isTabPlaying,
|
||||
"Tab's audio playing state is correct.");
|
||||
|
||||
if (isTabPlaying) {
|
||||
mAsserter.is(AudioFocusAgent.getInstance().getAudioFocusState(),
|
||||
State.OWN_FOCUS,
|
||||
"Audio focus state is correct.");
|
||||
} else {
|
||||
boolean isLostFocus =
|
||||
AudioFocusAgent.getInstance().getAudioFocusState().equals(State.LOST_FOCUS) ||
|
||||
AudioFocusAgent.getInstance().getAudioFocusState().equals(State.LOST_FOCUS_TRANSIENT);
|
||||
mAsserter.ok(isLostFocus,
|
||||
"Checking the audio focus when the tab is not playing",
|
||||
"Audio focus state is correct.");
|
||||
}
|
||||
}
|
||||
|
||||
private AudioFocusAgent getAudioFocusAgent() {
|
||||
return AudioFocusAgent.getInstance();
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче