1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.managedprovisioning.common;
18 
19 import static com.android.managedprovisioning.common.NotificationHelper.CHANNEL_ID;
20 import static com.android.managedprovisioning.common.NotificationHelper.ENCRYPTION_NOTIFICATION_ID;
21 import static com.android.managedprovisioning.common.NotificationHelper.PRIVACY_REMINDER_NOTIFICATION_ID;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.robolectric.Shadows.shadowOf;
26 
27 import android.app.Notification;
28 import android.app.NotificationManager;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.service.notification.StatusBarNotification;
32 
33 import com.android.managedprovisioning.R;
34 
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 
40 @RunWith(RobolectricTestRunner.class)
41 public class NotificationHelperTest {
42 
43     private Context mContext = RuntimeEnvironment.application;
44     private NotificationManager mNotificationManager =
45             mContext.getSystemService(NotificationManager.class);
46 
47     private NotificationHelper mNotificationHelper = new NotificationHelper(mContext);
48 
49     @Test
showResumeNotification_showsExpectedNotification()50     public void showResumeNotification_showsExpectedNotification() {
51         final Intent intent = new Intent(Globals.ACTION_RESUME_PROVISIONING);
52         mNotificationHelper.showResumeNotification(intent);
53 
54         assertThat(shadowOf(mNotificationManager).getActiveNotifications()).hasLength(1);
55         final StatusBarNotification notification =
56                 shadowOf(mNotificationManager).getActiveNotifications()[0];
57         assertThat(notification.getId()).isEqualTo(ENCRYPTION_NOTIFICATION_ID);
58         assertThat(notification.getNotification().getChannelId()).isEqualTo(CHANNEL_ID);
59         assertThat(notification.getNotification().extras.getString(Notification.EXTRA_TITLE))
60                 .isEqualTo(mContext.getString(R.string.continue_provisioning_notify_title));
61     }
62 
63     @Test
showPrivacyReminderNotification_showsExpectedNotification()64     public void showPrivacyReminderNotification_showsExpectedNotification() {
65         mNotificationHelper.showPrivacyReminderNotification(
66                 mContext, NotificationManager.IMPORTANCE_DEFAULT);
67 
68         assertThat(shadowOf(mNotificationManager).getActiveNotifications()).hasLength(1);
69         final StatusBarNotification notification =
70                 shadowOf(mNotificationManager).getActiveNotifications()[0];
71         assertThat(notification.getId()).isEqualTo(PRIVACY_REMINDER_NOTIFICATION_ID);
72         assertThat(notification.getNotification().getChannelId()).isEqualTo(CHANNEL_ID);
73         assertThat(notification.getNotification().extras.getString(Notification.EXTRA_TITLE))
74                 .isEqualTo(mContext.getString(
75                         R.string.fully_managed_device_provisioning_privacy_title));
76     }
77 }
78