-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved Notification Unit Tests (#947)
* move all notification tests to consistent package * Add some unit tests for GCM Message parsing * parameterize * more
- Loading branch information
1 parent
ac96cae
commit b43e027
Showing
14 changed files
with
210 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
android/src/test/java/com/thebluealliance/androidclient/di/MockNotificationComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.thebluealliance.androidclient.di; | ||
|
||
import com.thebluealliance.androidclient.gcm.GCMMessageHandlerWithMocks; | ||
|
||
import javax.inject.Singleton; | ||
|
||
import dagger.Component; | ||
|
||
@Singleton | ||
@Component( | ||
modules = {MockGceModule.class, MockRendererModule.class, MockConfigModule.class, MockGcmModule.class, MockAccountModule.class, MockAuthModule.class}, | ||
dependencies = MockApplicationComponent.class) | ||
public interface MockNotificationComponent { | ||
void inject(GCMMessageHandlerWithMocks handlerWithMocks); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
android/src/test/java/com/thebluealliance/androidclient/gcm/GCMMessageHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.thebluealliance.androidclient.gcm; | ||
|
||
import android.app.Notification; | ||
import android.app.NotificationManager; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
|
||
import com.google.gson.JsonObject; | ||
import com.thebluealliance.androidclient.datafeed.framework.ModelMaker; | ||
import com.thebluealliance.androidclient.gcm.notifications.BaseNotification; | ||
import com.thebluealliance.androidclient.gcm.notifications.NotificationTypes; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.experimental.runners.Enclosed; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.ParameterizedRobolectricTestRunner; | ||
import org.robolectric.Robolectric; | ||
import org.robolectric.Shadows; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import androidx.test.core.app.ApplicationProvider; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(Enclosed.class) | ||
public class GCMMessageHandlerTest { | ||
|
||
@RunWith(ParameterizedRobolectricTestRunner.class) | ||
public static class TestRenderSingleNotifications { | ||
|
||
private NotificationManager mNotificationManager; | ||
|
||
@ParameterizedRobolectricTestRunner.Parameter(0) | ||
public String mNotificationType; | ||
@ParameterizedRobolectricTestRunner.Parameter(1) | ||
public String mNotificationDataFileName; | ||
@ParameterizedRobolectricTestRunner.Parameter(2) | ||
public int mExpectedPriority; | ||
|
||
@Before | ||
public void setUp() { | ||
Context applicationContext = ApplicationProvider.getApplicationContext(); | ||
mNotificationManager = (NotificationManager) applicationContext.getSystemService(Context.NOTIFICATION_SERVICE); | ||
} | ||
|
||
@ParameterizedRobolectricTestRunner.Parameters(name = "NotificationType = {0}") | ||
public static Collection<Object[]> data() { | ||
return Arrays.asList(new Object[][]{ | ||
{NotificationTypes.ALLIANCE_SELECTION, "notification_alliance_selection", Notification.PRIORITY_HIGH}, | ||
{NotificationTypes.AWARDS, "notification_awards_posted", Notification.PRIORITY_HIGH}, | ||
{NotificationTypes.DISTRICT_POINTS_UPDATED, "notification_district_points_updated", Notification.PRIORITY_HIGH}, | ||
{NotificationTypes.EVENT_DOWN, "notification_event_down", Notification.PRIORITY_HIGH}, | ||
{NotificationTypes.LEVEL_STARTING, "notification_level_starting", Notification.PRIORITY_HIGH}, | ||
{NotificationTypes.MATCH_SCORE, "notification_match_score", Notification.PRIORITY_HIGH}, | ||
{NotificationTypes.PING, "notification_ping", Notification.PRIORITY_LOW}, | ||
{NotificationTypes.SCHEDULE_UPDATED, "notification_schedule_updated", Notification.PRIORITY_HIGH}, | ||
{NotificationTypes.UPCOMING_MATCH, "notification_upcoming_match", Notification.PRIORITY_HIGH}, | ||
}); | ||
} | ||
|
||
@Test | ||
public void testPostSingleNotification() { | ||
Intent intent = buildIntent(mNotificationType, mNotificationDataFileName); | ||
GCMMessageHandlerWithMocks service = Robolectric.setupService(GCMMessageHandlerWithMocks.class); | ||
service.onCreate(); | ||
service.onHandleWork(intent); | ||
|
||
List<Notification> notifications = Shadows.shadowOf(mNotificationManager).getAllNotifications(); | ||
assertEquals(1, notifications.size()); | ||
|
||
Notification notification = notifications.get(0); | ||
assertEquals(BaseNotification.NOTIFICATION_CHANNEL, notification.getChannelId()); | ||
assertEquals(mExpectedPriority, notification.priority); | ||
} | ||
} | ||
|
||
private static Intent buildIntent(String notificationType, String dataFileName) { | ||
JsonObject notificationData = ModelMaker.getModel(JsonObject.class, dataFileName); | ||
Intent intent = new Intent("com.google.android.c2dm.intent.RECEIVE"); | ||
intent.putExtra("notification_type", notificationType); | ||
intent.putExtra("message_data", notificationData.toString()); | ||
return intent; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
android/src/test/java/com/thebluealliance/androidclient/gcm/GCMMessageHandlerWithMocks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.thebluealliance.androidclient.gcm; | ||
|
||
import com.thebluealliance.androidclient.TestTbaAndroid; | ||
import com.thebluealliance.androidclient.database.DatabaseMocker; | ||
import com.thebluealliance.androidclient.di.DaggerMockNotificationComponent; | ||
import com.thebluealliance.androidclient.di.MockRendererModule; | ||
|
||
public class GCMMessageHandlerWithMocks extends GCMMessageHandler { | ||
|
||
/* | ||
* A version of the service that does its DI using mocks | ||
*/ | ||
|
||
@Override | ||
protected void inject() { | ||
TestTbaAndroid application = ((TestTbaAndroid) getApplication()); | ||
DaggerMockNotificationComponent.builder() | ||
.mockApplicationComponent(application.getMockComponent()) | ||
.mockDatafeedModule(application.getMockDatafeedModule()) | ||
.mockRendererModule(new MockRendererModule()) | ||
.mockAuthModule(application.getMockAuthModule()) | ||
.mockGcmModule(application.getMockGcmModule()) | ||
.build() | ||
.inject(this); | ||
initMocks(); | ||
} | ||
|
||
public void initMocks() { | ||
DatabaseMocker.mockMatchesTable(mDb); | ||
DatabaseMocker.mockEventsTable(mDb); | ||
DatabaseMocker.mockAwardsTable(mDb); | ||
DatabaseMocker.mockFavoritesTable(mDb); | ||
DatabaseMocker.mockSubscriptionsTable(mDb); | ||
DatabaseMocker.mockNotificationsTable(mDb); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.