Commit 56a9bbc9 authored by Anita Woodruff's avatar Anita Woodruff Committed by Commit Bot

[Android Unit Tests] Fix flaky media notification test

- Previously, MediaNotificationManagerServiceLifecycleTest had a
test which failed when run individually, despite passing
when all of the tests in that class were run, presumably due to the
custom shadow observer picking up on notifications shown by other tests.

- The only way I could get the test to always pass while still using the
custom shadow observer was to reset the observer at the start of the test,
and change the production code to notify via the non-compat
NotificationManager.

- However, turns out we can simply query Robolectric's built-in shadow of
the notification manager, which has a convenient accessor method for the
number of notifications it has shown, works for notifications posted to
NotificationManagerCompat, and doesn't appear to need manual resetting.

- Hence deleting the inferior MediaNotificationTestShadowNotificationManager

Bug: 867879
Change-Id: I14639648c8c1f3e0740da4bb1877b9aa21424e33
Reviewed-on: https://chromium-review.googlesource.com/1151304Reviewed-by: default avatarMounir Lamouri <mlamouri@chromium.org>
Commit-Queue: Anita Woodruff <awdf@chromium.org>
Cr-Commit-Position: refs/heads/master@{#578292}
parent 1dbc3a45
......@@ -2181,7 +2181,6 @@ chrome_junit_test_java_sources = [
"junit/src/org/chromium/chrome/browser/media/ui/MediaNotificationManagerServiceLifecycleTest.java",
"junit/src/org/chromium/chrome/browser/media/ui/MediaNotificationManagerTestBase.java",
"junit/src/org/chromium/chrome/browser/media/ui/MediaNotificationManagerThrottlerTest.java",
"junit/src/org/chromium/chrome/browser/media/ui/MediaNotificationTestShadowNotificationManager.java",
"junit/src/org/chromium/chrome/browser/media/ui/MediaNotificationTestShadowResources.java",
"junit/src/org/chromium/chrome/browser/media/ui/MediaNotificationTestTabHolder.java",
"junit/src/org/chromium/chrome/browser/media/ui/MediaNotificationTitleUpdatedTest.java",
......
......@@ -33,8 +33,7 @@ import java.util.Set;
// Remove this after updating to a version of Robolectric that supports
// notification channel creation. crbug.com/774315
sdk = Build.VERSION_CODES.N_MR1,
shadows = {MediaNotificationTestShadowResources.class,
MediaNotificationTestShadowNotificationManager.class})
shadows = {MediaNotificationTestShadowResources.class})
public class MediaNotificationActionsUpdatedTest extends MediaNotificationManagerTestBase {
private static final int TAB_ID_1 = 1;
private static final int TAB_ID_2 = 2;
......
......@@ -38,8 +38,7 @@ import org.chromium.chrome.browser.media.ui.MediaNotificationManager.ListenerSer
// Remove this after updating to a version of Robolectric that supports
// notification channel creation. crbug.com/774315
sdk = Build.VERSION_CODES.N_MR1,
shadows = {MediaNotificationTestShadowResources.class,
MediaNotificationTestShadowNotificationManager.class})
shadows = {MediaNotificationTestShadowResources.class})
public class MediaNotificationFaviconTest extends MediaNotificationManagerTestBase {
private static final int TAB_ID_1 = 1;
private static final String IS_LOW_END_DEVICE_SWITCH =
......
......@@ -29,8 +29,7 @@ import org.chromium.chrome.browser.media.ui.MediaNotificationManager.ListenerSer
*/
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE,
shadows = {MediaNotificationTestShadowResources.class,
MediaNotificationTestShadowNotificationManager.class})
shadows = {MediaNotificationTestShadowResources.class})
public class MediaNotificationManagerServiceActionsTest extends MediaNotificationManagerTestBase {
@Test
public void testProcessIntentWithNoAction() {
......
......@@ -21,8 +21,11 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.robolectric.Shadows.shadowOf;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
......@@ -30,6 +33,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowNotificationManager;
import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.chrome.browser.media.ui.MediaNotificationManager.ListenerService;
......@@ -48,8 +52,7 @@ import java.util.concurrent.TimeoutException;
// Remove this after updating to a version of Robolectric that supports
// notification channel creation. crbug.com/774315
sdk = Build.VERSION_CODES.N_MR1,
shadows = {MediaNotificationTestShadowResources.class,
MediaNotificationTestShadowNotificationManager.class})
shadows = {MediaNotificationTestShadowResources.class})
public class MediaNotificationManagerServiceLifecycleTest extends MediaNotificationManagerTestBase {
@Test
public void testServiceLifeCycle() {
......@@ -237,9 +240,7 @@ public class MediaNotificationManagerServiceLifecycleTest extends MediaNotificat
getManager().updateNotification(false);
verify(mService).stopForeground(false);
// One of the invocations comes from |setUpService()|.
verify(MediaNotificationTestShadowNotificationManager.sMockObserver, times(2))
.notify(eq(getNotificationId()), any(Notification.class));
assertEquals(1, getShadowNotificationManager().size());
}
@Test
......@@ -263,4 +264,10 @@ public class MediaNotificationManagerServiceLifecycleTest extends MediaNotificat
verify(mService).startForeground(eq(getNotificationId()), any(Notification.class));
}
private ShadowNotificationManager getShadowNotificationManager() {
NotificationManager notificationManager =
(NotificationManager) mMockContext.getSystemService(Context.NOTIFICATION_SERVICE);
return shadowOf(notificationManager);
}
}
// 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.mockito.Mockito.mock;
import android.app.Notification;
import android.app.NotificationManager;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadows.ShadowNotificationManager;
/**
* Dummy Robolectric shadow for Android NotificationManager for MediaNotification tests.
*/
@Implements(NotificationManager.class)
public class MediaNotificationTestShadowNotificationManager extends ShadowNotificationManager {
public static final NotificationManager sMockObserver;
static {
sMockObserver = mock(NotificationManager.class);
}
@Implementation
@Override
public void notify(int id, Notification notification) {
sMockObserver.notify(id, notification);
super.notify(id, notification);
}
}
......@@ -34,8 +34,7 @@ import org.chromium.content_public.common.MediaMetadata;
// Remove this after updating to a version of Robolectric that supports
// notification channel creation. crbug.com/774315
sdk = Build.VERSION_CODES.N_MR1,
shadows = {MediaNotificationTestShadowResources.class,
MediaNotificationTestShadowNotificationManager.class})
shadows = {MediaNotificationTestShadowResources.class})
public class MediaNotificationTitleUpdatedTest extends MediaNotificationManagerTestBase {
private static final int TAB_ID_1 = 1;
private static final int TAB_ID_2 = 2;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment