• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 package com.android.managedprovisioning.common;
17 
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 import static com.google.common.truth.Truth.assertThat;
23 
24 import android.app.Notification;
25 import android.app.NotificationManager;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.os.SystemClock;
29 import android.service.notification.StatusBarNotification;
30 
31 import androidx.test.InstrumentationRegistry;
32 import androidx.test.filters.SmallTest;
33 
34 import com.android.managedprovisioning.R;
35 
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 
40 @SmallTest
41 public class NotificationHelperTest {
42 
43     private static final int NOTIFICATION_TIMEOUT_MS = 5000;
44 
45     private NotificationHelper mNotificationHelper;
46     private NotificationManager mNotificationManager;
47     private Context mContext;
48 
49     @Before
setUp()50     public void setUp() {
51         mContext = InstrumentationRegistry.getTargetContext();
52         mNotificationHelper = new NotificationHelper(mContext);
53         mNotificationManager = mContext.getSystemService(NotificationManager.class);
54         removeAllNotifications();
55     }
56 
57     @After
tearDown()58     public void tearDown() {
59         removeAllNotifications();
60     }
61 
62     @Test
testShowResumeNotification()63     public void testShowResumeNotification() throws Exception {
64         assertThat(mNotificationManager.getActiveNotifications().length).isEqualTo(0);
65 
66         Intent intent = new Intent(Globals.ACTION_RESUME_PROVISIONING);
67         mNotificationHelper.showResumeNotification(intent);
68 
69         waitForNotification();
70         StatusBarNotification[] notifications = mNotificationManager.getActiveNotifications();
71         assertThat(notifications.length).isEqualTo(1);
72         StatusBarNotification notification = notifications[0];
73         assertThat(notification.getId()).isEqualTo(ENCRYPTION_NOTIFICATION_ID);
74         assertThat(notification.getNotification().getChannel()).isEqualTo(CHANNEL_ID);
75         assertThat(notification.getNotification().extras.getString(Notification.EXTRA_TITLE))
76                 .isEqualTo(mContext.getString(R.string.continue_provisioning_notify_title));
77     }
78 
79     @Test
testShowPrivacyReminderNotification()80     public void testShowPrivacyReminderNotification() throws Exception {
81         assertThat(mNotificationManager.getActiveNotifications().length).isEqualTo(0);
82 
83         mNotificationHelper.showPrivacyReminderNotification(
84                 mContext, NotificationManager.IMPORTANCE_DEFAULT);
85 
86         waitForNotification();
87         StatusBarNotification[] notifications = mNotificationManager.getActiveNotifications();
88         assertThat(notifications.length).isEqualTo(1);
89         StatusBarNotification notification = notifications[0];
90         assertThat(notification.getId()).isEqualTo(PRIVACY_REMINDER_NOTIFICATION_ID);
91         assertThat(notification.getNotification().getChannel()).isEqualTo(CHANNEL_ID);
92         assertThat(notification.getNotification().extras.getString(Notification.EXTRA_TITLE))
93                 .isEqualTo(mContext.getString(
94                         R.string.fully_managed_device_provisioning_privacy_title));
95     }
96 
waitForNotification()97     private void waitForNotification() throws InterruptedException {
98         long elapsed = SystemClock.elapsedRealtime();
99         while(SystemClock.elapsedRealtime() - elapsed < NOTIFICATION_TIMEOUT_MS
100                 && mNotificationManager.getActiveNotifications().length == 0) {
101             Thread.sleep(10);
102         }
103     }
104 
removeAllNotifications()105     private void removeAllNotifications() {
106         mNotificationManager.cancelAll();
107     }
108 }
109