| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.media.AudioManager; | 8 import android.media.AudioManager; |
| 9 import android.support.test.filters.MediumTest; | 9 import android.support.test.filters.MediumTest; |
| 10 import android.support.test.filters.SmallTest; | 10 import android.support.test.filters.SmallTest; |
| 11 | 11 |
| 12 import org.chromium.base.ThreadUtils; |
| 13 import org.chromium.base.annotations.SuppressFBWarnings; |
| 12 import org.chromium.base.test.util.CommandLineFlags; | 14 import org.chromium.base.test.util.CommandLineFlags; |
| 13 import org.chromium.base.test.util.DisabledTest; | 15 import org.chromium.base.test.util.DisabledTest; |
| 14 import org.chromium.base.test.util.Feature; | 16 import org.chromium.base.test.util.Feature; |
| 15 import org.chromium.base.test.util.Restriction; | 17 import org.chromium.base.test.util.Restriction; |
| 16 import org.chromium.base.test.util.RetryOnFailure; | 18 import org.chromium.base.test.util.RetryOnFailure; |
| 17 import org.chromium.content.browser.test.util.Criteria; | 19 import org.chromium.content.browser.test.util.Criteria; |
| 18 import org.chromium.content.browser.test.util.CriteriaHelper; | 20 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 19 import org.chromium.content.browser.test.util.DOMUtils; | 21 import org.chromium.content.browser.test.util.DOMUtils; |
| 20 import org.chromium.content.common.ContentSwitches; | 22 import org.chromium.content.common.ContentSwitches; |
| 23 import org.chromium.content_public.browser.MediaSession; |
| 24 import org.chromium.content_public.browser.MediaSessionObserver; |
| 21 import org.chromium.content_shell_apk.ContentShellTestBase; | 25 import org.chromium.content_shell_apk.ContentShellTestBase; |
| 22 | 26 |
| 27 import java.util.ArrayList; |
| 23 import java.util.concurrent.Callable; | 28 import java.util.concurrent.Callable; |
| 24 | 29 |
| 25 /** | 30 /** |
| 26 * Tests for MediaSession. | 31 * Tests for MediaSession. |
| 27 */ | 32 */ |
| 28 @RetryOnFailure | 33 @RetryOnFailure |
| 29 @CommandLineFlags.Add(ContentSwitches.DISABLE_GESTURE_REQUIREMENT_FOR_MEDIA_PLAY
BACK) | 34 @CommandLineFlags.Add(ContentSwitches.DISABLE_GESTURE_REQUIREMENT_FOR_MEDIA_PLAY
BACK) |
| 30 public class MediaSessionTest extends ContentShellTestBase { | 35 public class MediaSessionTest extends ContentShellTestBase { |
| 31 private static final String MEDIA_SESSION_TEST_URL = | 36 private static final String MEDIA_SESSION_TEST_URL = |
| 32 "content/test/data/media/session/media-session.html"; | 37 "content/test/data/media/session/media-session.html"; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 @Override | 82 @Override |
| 78 public Integer call() { | 83 public Integer call() { |
| 79 return getAudioFocusState(); | 84 return getAudioFocusState(); |
| 80 } | 85 } |
| 81 })); | 86 })); |
| 82 } | 87 } |
| 83 } | 88 } |
| 84 | 89 |
| 85 private MockAudioFocusChangeListener mAudioFocusChangeListener; | 90 private MockAudioFocusChangeListener mAudioFocusChangeListener; |
| 86 | 91 |
| 92 @SuppressFBWarnings("URF_UNREAD_FIELD") |
| 93 private MediaSessionObserver mObserver; |
| 94 |
| 95 private ArrayList<StateRecord> mStateRecords = new ArrayList<StateRecord>(); |
| 96 |
| 97 private static class StateRecord { |
| 98 public boolean isControllable; |
| 99 public boolean isSuspended; |
| 100 |
| 101 public StateRecord(boolean isControllable, boolean isSuspended) { |
| 102 this.isControllable = isControllable; |
| 103 this.isSuspended = isSuspended; |
| 104 } |
| 105 |
| 106 @Override |
| 107 public boolean equals(Object obj) { |
| 108 if (obj == this) return true; |
| 109 if (!(obj instanceof StateRecord)) return false; |
| 110 |
| 111 StateRecord other = (StateRecord) obj; |
| 112 return isControllable == other.isControllable && isSuspended == othe
r.isSuspended; |
| 113 } |
| 114 |
| 115 @Override |
| 116 public int hashCode() { |
| 117 return (isControllable ? 2 : 0) + (isSuspended ? 1 : 0); |
| 118 } |
| 119 } |
| 120 |
| 87 @Override | 121 @Override |
| 88 public void setUp() throws Exception { | 122 public void setUp() throws Exception { |
| 89 super.setUp(); | 123 super.setUp(); |
| 90 | 124 |
| 91 try { | 125 try { |
| 92 startActivityWithTestUrl(MEDIA_SESSION_TEST_URL); | 126 startActivityWithTestUrl(MEDIA_SESSION_TEST_URL); |
| 93 } catch (Throwable t) { | 127 } catch (Throwable t) { |
| 94 fail("Couldn't load test page"); | 128 fail("Couldn't load test page"); |
| 95 } | 129 } |
| 96 | 130 |
| 97 mAudioFocusChangeListener = new MockAudioFocusChangeListener(); | 131 mAudioFocusChangeListener = new MockAudioFocusChangeListener(); |
| 132 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 133 @Override |
| 134 public void run() { |
| 135 mObserver = |
| 136 new MediaSessionObserver(MediaSession.fromWebContents(ge
tWebContents())) { |
| 137 @Override |
| 138 public void mediaSessionStateChanged( |
| 139 boolean isControllable, boolean isSuspended)
{ |
| 140 mStateRecords.add(new StateRecord(isControllable
, isSuspended)); |
| 141 } |
| 142 }; |
| 143 } |
| 144 }); |
| 98 } | 145 } |
| 99 | 146 |
| 100 @Override | 147 @Override |
| 101 public void tearDown() throws Exception { | 148 public void tearDown() throws Exception { |
| 102 mAudioFocusChangeListener.abandonAudioFocus(); | 149 mAudioFocusChangeListener.abandonAudioFocus(); |
| 103 | 150 |
| 104 super.tearDown(); | 151 super.tearDown(); |
| 105 } | 152 } |
| 106 | 153 |
| 107 @SmallTest | 154 @SmallTest |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 mAudioFocusChangeListener.getAudioFocusState()); | 422 mAudioFocusChangeListener.getAudioFocusState()); |
| 376 | 423 |
| 377 DOMUtils.waitForMediaPauseBeforeEnd(getWebContents(), LONG_AUDIO); | 424 DOMUtils.waitForMediaPauseBeforeEnd(getWebContents(), LONG_AUDIO); |
| 378 DOMUtils.waitForMediaPauseBeforeEnd(getWebContents(), LONG_VIDEO); | 425 DOMUtils.waitForMediaPauseBeforeEnd(getWebContents(), LONG_VIDEO); |
| 379 | 426 |
| 380 mAudioFocusChangeListener.abandonAudioFocus(); | 427 mAudioFocusChangeListener.abandonAudioFocus(); |
| 381 | 428 |
| 382 DOMUtils.waitForMediaPlay(getWebContents(), LONG_AUDIO); | 429 DOMUtils.waitForMediaPlay(getWebContents(), LONG_AUDIO); |
| 383 DOMUtils.waitForMediaPlay(getWebContents(), LONG_VIDEO); | 430 DOMUtils.waitForMediaPlay(getWebContents(), LONG_VIDEO); |
| 384 } | 431 } |
| 432 |
| 433 @MediumTest |
| 434 @Feature({"MediaSession"}) |
| 435 @RetryOnFailure |
| 436 public void testSessionSuspendedAfterFocusLossWhenPlaying() throws Exception
{ |
| 437 ArrayList<StateRecord> expectedStates = new ArrayList<StateRecord>(); |
| 438 expectedStates.add(new StateRecord(true, false)); |
| 439 expectedStates.add(new StateRecord(true, true)); |
| 440 |
| 441 assertEquals(AudioManager.AUDIOFOCUS_LOSS, mAudioFocusChangeListener.get
AudioFocusState()); |
| 442 mAudioFocusChangeListener.requestAudioFocus(AudioManager.AUDIOFOCUS_GAIN
); |
| 443 assertEquals(AudioManager.AUDIOFOCUS_GAIN, mAudioFocusChangeListener.get
AudioFocusState()); |
| 444 |
| 445 DOMUtils.playMedia(getWebContents(), LONG_AUDIO); |
| 446 DOMUtils.waitForMediaPlay(getWebContents(), LONG_AUDIO); |
| 447 |
| 448 // Wait for the media to be really playing. |
| 449 mAudioFocusChangeListener.waitForFocusStateChange(AudioManager.AUDIOFOCU
S_LOSS); |
| 450 |
| 451 mAudioFocusChangeListener.requestAudioFocus(AudioManager.AUDIOFOCUS_GAIN
); |
| 452 assertEquals(AudioManager.AUDIOFOCUS_GAIN, mAudioFocusChangeListener.get
AudioFocusState()); |
| 453 |
| 454 DOMUtils.waitForMediaPauseBeforeEnd(getWebContents(), LONG_AUDIO); |
| 455 |
| 456 assertEquals(expectedStates, mStateRecords); |
| 457 } |
| 458 |
| 459 @MediumTest |
| 460 @Feature({"MediaSession"}) |
| 461 @RetryOnFailure |
| 462 public void testSessionSuspendedAfterFocusLossWhenPaused() throws Exception
{ |
| 463 ArrayList<StateRecord> expectedStates = new ArrayList<StateRecord>(); |
| 464 expectedStates.add(new StateRecord(true, false)); |
| 465 expectedStates.add(new StateRecord(true, true)); |
| 466 |
| 467 assertEquals(AudioManager.AUDIOFOCUS_LOSS, mAudioFocusChangeListener.get
AudioFocusState()); |
| 468 mAudioFocusChangeListener.requestAudioFocus(AudioManager.AUDIOFOCUS_GAIN
); |
| 469 assertEquals(AudioManager.AUDIOFOCUS_GAIN, mAudioFocusChangeListener.get
AudioFocusState()); |
| 470 |
| 471 DOMUtils.playMedia(getWebContents(), LONG_AUDIO); |
| 472 DOMUtils.waitForMediaPlay(getWebContents(), LONG_AUDIO); |
| 473 |
| 474 // Wait for the media to be really playing. |
| 475 mAudioFocusChangeListener.waitForFocusStateChange(AudioManager.AUDIOFOCU
S_LOSS); |
| 476 |
| 477 DOMUtils.pauseMedia(getWebContents(), LONG_AUDIO); |
| 478 DOMUtils.waitForMediaPauseBeforeEnd(getWebContents(), LONG_AUDIO); |
| 479 |
| 480 assertEquals(expectedStates, mStateRecords); |
| 481 |
| 482 mAudioFocusChangeListener.requestAudioFocus(AudioManager.AUDIOFOCUS_GAIN
); |
| 483 assertEquals(AudioManager.AUDIOFOCUS_GAIN, mAudioFocusChangeListener.get
AudioFocusState()); |
| 484 |
| 485 // Wait for 1 second before observing MediaSession state change. |
| 486 Thread.sleep(AUDIO_FOCUS_CHANGE_TIMEOUT); |
| 487 |
| 488 assertEquals(expectedStates, mStateRecords); |
| 489 } |
| 385 } | 490 } |
| OLD | NEW |