Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Unified Diff: chrome/android/junit/src/org/chromium/chrome/browser/media/ui/MediaNotificationTitleUpdatedTest.java

Issue 2831223002: [Media>UI] Migrate media notification tests to JUnit tests (Closed)
Patch Set: fixed tests Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/junit/src/org/chromium/chrome/browser/media/ui/MediaNotificationTitleUpdatedTest.java
diff --git a/chrome/android/junit/src/org/chromium/chrome/browser/media/ui/MediaNotificationTitleUpdatedTest.java b/chrome/android/junit/src/org/chromium/chrome/browser/media/ui/MediaNotificationTitleUpdatedTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..2cf8e614dd618c9acd24d0d08c94ac913a3fbf05
--- /dev/null
+++ b/chrome/android/junit/src/org/chromium/chrome/browser/media/ui/MediaNotificationTitleUpdatedTest.java
@@ -0,0 +1,201 @@
+// Copyright 2017 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.chrome.browser.media.ui;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.doCallRealMethod;
+
+import android.app.Notification;
+import android.content.Intent;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.Shadows;
+import org.robolectric.annotation.Config;
+import org.robolectric.shadows.ShadowNotification;
+
+import org.chromium.base.BaseChromiumApplication;
+import org.chromium.chrome.R;
+import org.chromium.chrome.browser.media.ui.MediaNotificationManager.ListenerService;
+import org.chromium.content_public.common.MediaMetadata;
+import org.chromium.testing.local.LocalRobolectricTestRunner;
+
+/**
+ * Test of media notifications to see whether the text updates when the tab title changes or the
+ * MediaMetadata gets updated.
+ */
+@RunWith(LocalRobolectricTestRunner.class)
+@Config(manifest = Config.NONE, application = BaseChromiumApplication.class,
+ shadows = {MediaNotificationTestShadowResources.class,
+ MediaNotificationTestShadowNotificationManager.class})
+public class MediaNotificationTitleUpdatedTest extends MediaNotificationManagerTestBase {
+ private static final int TAB_ID_1 = 1;
+ private static final int TAB_ID_2 = 2;
+ private static final int HIDE_NOTIFICATION_DELAY_MILLIS =
+ MediaSessionTabHelper.HIDE_NOTIFICATION_DELAY_MILLIS;
+
+ private MediaNotificationTestTabHolder mTabHolder;
+
+ @Before
+ @Override
+ public void setUp() {
+ super.setUp();
+
+ doCallRealMethod().when(getManager()).onServiceStarted(any(ListenerService.class));
+ doCallRealMethod().when(mMockAppHooks).startForegroundService(any(Intent.class));
+ mTabHolder = new MediaNotificationTestTabHolder(TAB_ID_1, "about:blank", "title1");
+ }
+
+ @Test
+ public void testSessionStatePlaying() {
+ mTabHolder.simulateMediaSessionStateChanged(true, false);
+ assertEquals("title1", getDisplayedTitle());
+ mTabHolder.simulateTitleUpdated("title2");
+ assertEquals("title2", getDisplayedTitle());
+ }
+
+ @Test
+ public void testSessionStatePausedAfterPlaying() {
+ mTabHolder.simulateMediaSessionStateChanged(true, false);
+ mTabHolder.simulateMediaSessionStateChanged(true, true);
+ assertEquals("title1", getDisplayedTitle());
+ mTabHolder.simulateTitleUpdated("title2");
+ assertEquals("title2", getDisplayedTitle());
+ }
+
+ @Test
+ public void testSessionStateNewlyPaused() {
+ mTabHolder.simulateMediaSessionStateChanged(true, true);
+ assertNull(getManager().mNotificationBuilder);
+
+ mTabHolder.simulateTitleUpdated("title2");
+ assertNull(getManager().mNotificationBuilder);
+ }
+
+ @Test
+ public void testSessionStateUncontrollable() {
+ mTabHolder.simulateMediaSessionStateChanged(true, false);
+ assertEquals("title1", getDisplayedTitle());
+
+ mTabHolder.simulateMediaSessionStateChanged(false, false);
+ advanceTimeByMillis(HIDE_NOTIFICATION_DELAY_MILLIS);
+ mTabHolder.simulateTitleUpdated("title2");
+ assertNull(getManager().mMediaNotificationInfo);
+ }
+
+ @Test
+ public void testMediaMetadataSetsTitle() {
+ mTabHolder.simulateMediaSessionStateChanged(true, false);
+ mTabHolder.simulateMediaSessionMetadataChanged(new MediaMetadata("title2", "", ""));
+ assertEquals("title2", getDisplayedTitle());
+ }
+
+ @Test
+ public void testMediaMetadataOverridesTitle() {
+ mTabHolder.simulateMediaSessionStateChanged(true, false);
+ mTabHolder.simulateMediaSessionMetadataChanged(new MediaMetadata("title2", "", ""));
+ assertEquals("title2", getDisplayedTitle());
+
+ mTabHolder.simulateTitleUpdated("title2");
+ assertEquals("title2", getDisplayedTitle());
+ }
+
+ /**
+ * Test if a notification accepts the title update from another tab, using the following steps:
+ * 1. set the title of mTab, start the media session, a notification should show up;
+ * 2. stop the media session of mTab, the notification shall hide;
+ * 3. create newTab, start the media session of newTab, a notification should show up,
+ * set the title of newTab, the notification title should match newTab;
+ * 4. change the title of newTab and then mTab to different names,
+ * the notification should have the title of newTab.
+ */
+ @Test
+ public void testMultipleTabs() {
+ mTabHolder.simulateMediaSessionStateChanged(true, false);
+ assertEquals("title1", getDisplayedTitle());
+ mTabHolder.simulateMediaSessionStateChanged(false, false);
+
+ MediaNotificationTestTabHolder newTabHolder =
+ new MediaNotificationTestTabHolder(TAB_ID_2, "about:blank", "title2");
+
+ newTabHolder.simulateMediaSessionStateChanged(true, false);
+ newTabHolder.simulateTitleUpdated("title3");
+ mTabHolder.simulateTitleUpdated("title2");
+ assertEquals("title3", getDisplayedTitle());
+ }
+
+ /**
+ * Test for the notification should not update for a paused tab if it mismatches the current tab
+ * id:
+ * 1. set the title of mTab, start the media session, a notification should show up;
+ * 2. create newTab, start the media session of newTab, a notification should show up,
+ * set the title of newTab, the notification will match the title;
+ * 3. stop the media session of mTab, and change its title, the notification should not
+ * change.
+ */
+ @Test
+ public void testPreferLastActiveTab() {
+ mTabHolder.simulateMediaSessionStateChanged(true, false);
+ assertEquals("title1", getDisplayedTitle());
+
+ MediaNotificationTestTabHolder newTabHolder =
+ new MediaNotificationTestTabHolder(TAB_ID_2, "about:blank", "title2");
+
+ newTabHolder.simulateMediaSessionStateChanged(true, false);
+ newTabHolder.simulateTitleUpdated("title3");
+
+ mTabHolder.simulateMediaSessionStateChanged(false, false);
+ mTabHolder.simulateTitleUpdated("title2");
+ assertEquals("title3", getDisplayedTitle());
+ }
+
+ @Test
+ public void testMediaMetadataResetsAfterNavigation() {
+ mTabHolder.simulateNavigation("https://example.com/", false);
+ mTabHolder.simulateMediaSessionStateChanged(true, false);
+ mTabHolder.simulateMediaSessionMetadataChanged(new MediaMetadata("title2", "", ""));
+ assertEquals("title2", getDisplayedTitle());
+
+ mTabHolder.simulateNavigation("https://example1.com/", false);
+ assertEquals("title1", getDisplayedTitle());
+ }
+
+ @Test
+ public void testMediaMetadataResetsAfterSameOriginNavigation() throws Throwable {
+ mTabHolder.simulateNavigation("https://example.com/", false);
+ mTabHolder.simulateMediaSessionStateChanged(true, false);
+ mTabHolder.simulateMediaSessionMetadataChanged(new MediaMetadata("title2", "", ""));
+ assertEquals("title2", getDisplayedTitle());
+
+ mTabHolder.simulateNavigation("https://example.com/foo.html", false);
+ assertEquals("title1", getDisplayedTitle());
+ }
+
+ @Test
+ public void testMediaMetadataPersistsAfterSamePageNavigation() throws Throwable {
+ mTabHolder.simulateNavigation("https://example.com/", false);
+ mTabHolder.simulateMediaSessionStateChanged(true, false);
+ mTabHolder.simulateMediaSessionMetadataChanged(new MediaMetadata("title2", "", ""));
+ assertEquals("title2", getDisplayedTitle());
+
+ mTabHolder.simulateNavigation("https://example.com/#1", true);
+ assertEquals("title2", getDisplayedTitle());
+ }
+
+ private CharSequence getDisplayedTitle() {
+ Notification notification = getManager().mNotificationBuilder.build();
+ ShadowNotification shadowNotification = Shadows.shadowOf(notification);
+
+ return shadowNotification.getContentTitle();
+ }
+
+ @Override
+ int getNotificationId() {
+ return R.id.media_playback_notification;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698