OSDN Git Service

Add tests for NotificationCountService
authorutzcoz <utzcoz@outlook.com>
Sun, 11 Oct 2020 11:15:12 +0000 (19:15 +0800)
committerutzcoz <utzcoz@outlook.com>
Fri, 27 Nov 2020 14:05:01 +0000 (22:05 +0800)
Test: ./gradlew test

Signed-off-by: utzcoz <utzcoz@outlook.com>
app/src/main/java/com/farmerbb/taskbar/service/NotificationCountService.java
app/src/test/java/com/farmerbb/taskbar/service/NotificationCountServiceTest.java [new file with mode: 0644]

index 43e1731..b8e1657 100644 (file)
@@ -21,6 +21,7 @@ import android.content.Intent;
 import android.service.notification.NotificationListenerService;
 import android.service.notification.StatusBarNotification;
 
+import androidx.annotation.VisibleForTesting;
 import androidx.core.app.NotificationCompat;
 
 import com.farmerbb.taskbar.util.U;
@@ -79,7 +80,12 @@ public class NotificationCountService extends NotificationListenerService {
 
     private void broadcastNotificationCount(int count) {
         Intent intent = new Intent(ACTION_NOTIFICATION_COUNT_CHANGED);
-        intent.putExtra(EXTRA_COUNT, Math.min(count, 99));
+        intent.putExtra(EXTRA_COUNT, getValidCount(count));
         U.sendBroadcast(this, intent);
     }
+
+    @VisibleForTesting
+    int getValidCount(int count) {
+        return Math.min(count, 99);
+    }
 }
diff --git a/app/src/test/java/com/farmerbb/taskbar/service/NotificationCountServiceTest.java b/app/src/test/java/com/farmerbb/taskbar/service/NotificationCountServiceTest.java
new file mode 100644 (file)
index 0000000..1002919
--- /dev/null
@@ -0,0 +1,104 @@
+package com.farmerbb.taskbar.service;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+
+import androidx.localbroadcastmanager.content.LocalBroadcastManager;
+import androidx.test.core.app.ApplicationProvider;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+
+import static android.os.Looper.getMainLooper;
+import static com.farmerbb.taskbar.util.Constants.ACTION_NOTIFICATION_COUNT_CHANGED;
+import static com.farmerbb.taskbar.util.Constants.ACTION_REQUEST_NOTIFICATION_COUNT;
+import static com.farmerbb.taskbar.util.Constants.EXTRA_COUNT;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.robolectric.Shadows.shadowOf;
+
+@RunWith(RobolectricTestRunner.class)
+public class NotificationCountServiceTest {
+    private NotificationCountReceiver receiver = new NotificationCountReceiver();
+    private Context context;
+    private NotificationCountService service;
+
+    @Before
+    public void setUp() {
+        context = ApplicationProvider.getApplicationContext();
+        service = Robolectric.setupService(NotificationCountService.class);
+        LocalBroadcastManager
+                .getInstance(context)
+                .registerReceiver(receiver, new IntentFilter(ACTION_NOTIFICATION_COUNT_CHANGED));
+        receiver.reset();
+    }
+
+    @After
+    public void tearDown() {
+        LocalBroadcastManager.getInstance(context).unregisterReceiver(receiver);
+    }
+
+    @Test
+    public void testOnListenerConnected() {
+        service.onListenerConnected();
+        shadowOf(getMainLooper()).idle();
+        assertTrue(receiver.count >= 0);
+
+        receiver.reset();
+        Intent intent = new Intent();
+        intent.setAction(ACTION_REQUEST_NOTIFICATION_COUNT);
+        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
+        shadowOf(getMainLooper()).idle();
+        assertTrue(receiver.count >= 0);
+    }
+
+    @Test
+    public void testOnListenerDisconnected() {
+        receiver.count = Integer.MAX_VALUE;
+        service.onListenerDisconnected();
+        shadowOf(getMainLooper()).idle();
+        assertEquals(0, receiver.count);
+    }
+
+    @Test
+    public void testOnNotificationPosted() {
+        service.onNotificationPosted(null);
+        shadowOf(getMainLooper()).idle();
+        assertTrue(receiver.count >= 0);
+    }
+
+    @Test
+    public void testOnNotificationRemoved() {
+        service.onNotificationRemoved(null);
+        shadowOf(getMainLooper()).idle();
+        assertTrue(receiver.count >= 0);
+    }
+
+    @Test
+    public void testGetValidCount() {
+        assertEquals(0, service.getValidCount(0));
+        assertEquals(60, service.getValidCount(60));
+        assertEquals(99, service.getValidCount(99));
+        assertEquals(99, service.getValidCount(1000));
+        assertEquals(99, service.getValidCount(Integer.MAX_VALUE));
+    }
+
+    private static class NotificationCountReceiver extends BroadcastReceiver {
+        int count = -1;
+
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            count = intent.getIntExtra(EXTRA_COUNT, -1);
+        }
+
+        void reset() {
+            count = -1;
+        }
+    }
+}