Bug 1357639 - part10 : check notification id. draft
authorAlastor Wu <alwu@mozilla.com>
Tue, 09 May 2017 10:51:27 +0800
changeset 574618 394ed038ce22ea763d3285cdd0fb7f18c2052a5a
parent 573971 b406b449d986f8384823b525c723b7de54ca0a56
child 574619 086669e9846ced2b7ac96762d59446201e1ba43f
push id57772
push useralwu@mozilla.com
push dateTue, 09 May 2017 05:56:20 +0000
bugs1357639
milestone55.0a1
Bug 1357639 - part10 : check notification id. Use notification id to check whether the notification is correct is more stable than just checking the number of system notification. MozReview-Commit-ID: BVYdMrBsSLR
mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
mobile/android/base/resources/values/ids.xml
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/MediaPlaybackTest.java
--- a/mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
+++ b/mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
@@ -41,17 +41,16 @@ public class MediaControlService extends
     public static final String ACTION_INIT           = "action_init";
     public static final String ACTION_RESUME         = "action_resume";
     public static final String ACTION_PAUSE          = "action_pause";
     public static final String ACTION_STOP           = "action_stop";
     public static final String ACTION_RESUME_BY_AUDIO_FOCUS = "action_resume_audio_focus";
     public static final String ACTION_PAUSE_BY_AUDIO_FOCUS  = "action_pause_audio_focus";
     public static final String ACTION_START_AUDIO_DUCK      = "action_start_audio_duck";
     public static final String ACTION_STOP_AUDIO_DUCK       = "action_stop_audio_duck";
-    private static final int MEDIA_CONTROL_ID = 1;
     private static final String MEDIA_CONTROL_PREF = "dom.audiochannel.mediaControl";
 
     // This is maximum volume level difference when audio ducking. The number is arbitrary.
     private static final int AUDIO_DUCK_MAX_STEPS = 3;
     private enum AudioDucking { START, STOP };
     private boolean mSupportsDucking = false;
     private int mAudioDuckCurrentSteps = 0;
 
@@ -361,17 +360,17 @@ public class MediaControlService extends
         if (!mInitialize) {
             return;
         }
 
         Log.d(LOGTAG, "onStateChanged, state = " + mMediaState);
 
         if (isNeedToRemoveControlInterface(mMediaState)) {
             stopForeground(false);
-            NotificationManagerCompat.from(this).cancel(MEDIA_CONTROL_ID);
+            NotificationManagerCompat.from(this).cancel(R.id.mediaControlNotification);
             return;
         }
 
         if (!mIsMediaControlPrefOn) {
             return;
         }
 
         final Tab tab = mTabReference.get();
@@ -409,21 +408,21 @@ public class MediaControlService extends
             .addAction(createNotificationAction())
             .setOngoing(isPlaying)
             .setShowWhen(false)
             .setWhen(0)
             .setVisibility(visibility)
             .build();
 
         if (isPlaying) {
-            startForeground(MEDIA_CONTROL_ID, notification);
+            startForeground(R.id.mediaControlNotification, notification);
         } else {
             stopForeground(false);
             NotificationManagerCompat.from(this)
-                .notify(MEDIA_CONTROL_ID, notification);
+                .notify(R.id.mediaControlNotification, notification);
         }
     }
 
     private Notification.Action createNotificationAction() {
         final Intent intent = createIntentUponState(mMediaState);
         boolean isPlayAction = intent.getAction().equals(ACTION_RESUME);
 
         int icon = isPlayAction ? R.drawable.ic_media_play : R.drawable.ic_media_pause;
--- a/mobile/android/base/resources/values/ids.xml
+++ b/mobile/android/base/resources/values/ids.xml
@@ -17,10 +17,11 @@
     <item type="id" name="pref_header_privacy"/>
     <item type="id" name="pref_header_search"/>
     <item type="id" name="updateServicePermissionNotification" />
     <item type="id" name="websiteContentNotification" />
     <item type="id" name="foregroundNotification" />
     <item type="id" name="actionbar"/>
     <item type="id" name="action_button"/>
     <item type="id" name="page_progress"/>
+    <item type="id" name="mediaControlNotification"/>
 
 </resources>
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/MediaPlaybackTest.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/MediaPlaybackTest.java
@@ -48,25 +48,31 @@ abstract class MediaPlaybackTest extends
         }
         waitForCondition(new Condition() {
             @Override
             public boolean isSatisfied() {
                 NotificationManager notificationManager = (NotificationManager)
                     getContext().getSystemService(Context.NOTIFICATION_SERVICE);
                 StatusBarNotification[] sbns = notificationManager.getActiveNotifications();
                 // Ensure the UI has been changed.
-                if (sbns.length == 1 &&
-                    sbns[0].getNotification().actions.length == 1) {
-                    if (sbns[0].getNotification().actions[0].icon != mPrevIcon) {
-                        mPrevIcon = sbns[0].getNotification().actions[0].icon ;
+                boolean findCorrectNotification = false;
+                for (int idx = 0; idx < sbns.length; idx++) {
+                    if (sbns[idx].getId() != R.id.mediaControlNotification) {
+                       continue;
+                    }
+                    findCorrectNotification = true;
+                    if (sbns[idx].getNotification().actions.length == 1 &&
+                        sbns[idx].getNotification().actions[0].icon != mPrevIcon) {
+                        mPrevIcon = sbns[idx].getNotification().actions[0].icon;
                         return true;
                     }
                 }
+
                 // The notification was cleared.
-                else if (mPrevIcon != 0 && sbns.length == 0) {
+                if (!findCorrectNotification && mPrevIcon != 0) {
                     mPrevIcon = 0;
                     return true;
                 }
                 return false;
             }
         }, MAX_WAIT_MS);
     }
 
@@ -182,18 +188,26 @@ abstract class MediaPlaybackTest extends
                                                       final boolean isTabPlaying) {
         if (!isAvailableToCheckNotification()) {
             return;
         }
         NotificationManager notificationManager = (NotificationManager)
             getContext().getSystemService(Context.NOTIFICATION_SERVICE);
 
         StatusBarNotification[] sbns = notificationManager.getActiveNotifications();
-        mAsserter.is(sbns.length, 1,
-                     "Should only have one notification in system's status bar.");
+        boolean findCorrectNotification = false;
+        for (int idx = 0; idx < sbns.length; idx++) {
+            if (sbns[idx].getId() == R.id.mediaControlNotification) {
+                findCorrectNotification = true;
+                break;
+            }
+        }
+        mAsserter.ok(findCorrectNotification,
+                     "Showing correct notification in system's status bar.",
+                     "Check system notification");
 
         Notification notification = sbns[0].getNotification();
         mAsserter.is(notification.icon,
                      R.drawable.flat_icon,
                      "Notification shows correct small icon.");
         mAsserter.is(notification.extras.get(Notification.EXTRA_TITLE),
                      tab.getTitle(),
                      "Notification shows correct title.");
@@ -212,18 +226,27 @@ abstract class MediaPlaybackTest extends
 
     protected final void checkIfMediaNotificationBeCleared() {
         if (!isAvailableToCheckNotification()) {
             return;
         }
         NotificationManager notificationManager = (NotificationManager)
             getContext().getSystemService(Context.NOTIFICATION_SERVICE);
         StatusBarNotification[] sbns = notificationManager.getActiveNotifications();
-        mAsserter.is(sbns.length, 0,
-                     "Should not have notification in system's status bar.");
+
+        boolean findCorrectNotification = false;
+        for (int idx = 0; idx < sbns.length; idx++) {
+            if (sbns[idx].getId() == R.id.mediaControlNotification) {
+                findCorrectNotification = true;
+                break;
+            }
+        }
+        mAsserter.ok(!findCorrectNotification,
+                     "Should not have notification in system's status bar.",
+                     "Check system notification.");
     }
 
     /**
      * This method is used to check whether audio focus state are correct or
      * not after tab's audio playing state changed.
      */
     protected final void checkAudioFocusStateAfterChanged(boolean isTabPlaying) {
         if (isTabPlaying) {