• 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.preprovisioning;
17 
18 import static com.android.managedprovisioning.preprovisioning.EncryptionController.CHANNEL_ID;
19 import static com.android.managedprovisioning.preprovisioning.EncryptionController.NOTIFICATION_ID;
20 import static org.hamcrest.Matchers.equalTo;
21 import static org.junit.Assert.assertEquals;
22 
23 import android.app.Notification;
24 import android.app.NotificationManager;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.os.SystemClock;
28 import android.service.notification.StatusBarNotification;
29 import android.support.test.InstrumentationRegistry;
30 import android.support.test.filters.SmallTest;
31 import com.android.managedprovisioning.R;
32 import com.android.managedprovisioning.common.Globals;
33 import com.android.managedprovisioning.preprovisioning.EncryptionController.ResumeNotificationHelper;
34 import org.hamcrest.MatcherAssert;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 
39 @SmallTest
40 public class ResumeNotificationHelperTest {
41 
42     private static final int NOTIFICATION_TIMEOUT_MS = 5000;
43 
44     private ResumeNotificationHelper mResumeNotificationHelper;
45     private NotificationManager mNotificationManager;
46     private Context mContext;
47 
48     @Before
setUp()49     public void setUp() {
50         mContext = InstrumentationRegistry.getTargetContext();
51         mResumeNotificationHelper = new ResumeNotificationHelper(mContext);
52         mNotificationManager = mContext.getSystemService(NotificationManager.class);
53         removeAllNotifications();
54     }
55 
56     @After
tearDown()57     public void tearDown() {
58         removeAllNotifications();
59     }
60 
61     @Test
testShowResumeNotification()62     public void testShowResumeNotification() throws Exception {
63         MatcherAssert.assertThat(mNotificationManager.getActiveNotifications().length,
64                 equalTo(0));
65 
66         Intent intent = new Intent(Globals.ACTION_RESUME_PROVISIONING);
67         mResumeNotificationHelper.showResumeNotification(intent);
68 
69         waitForNotification();
70         StatusBarNotification[] notifications = mNotificationManager.getActiveNotifications();
71         MatcherAssert.assertThat(notifications.length, equalTo(1));
72         StatusBarNotification notification = notifications[0];
73         assertEquals(notification.getId(), NOTIFICATION_ID);
74         assertEquals(notification.getNotification().getChannel(), CHANNEL_ID);
75         assertEquals(notification.getNotification().extras.getString(Notification.EXTRA_TITLE),
76                 mContext.getString(R.string.continue_provisioning_notify_title));
77     }
78 
waitForNotification()79     private void waitForNotification() throws InterruptedException {
80         long elapsed = SystemClock.elapsedRealtime();
81         while(SystemClock.elapsedRealtime() - elapsed < NOTIFICATION_TIMEOUT_MS
82                 && mNotificationManager.getActiveNotifications().length == 0) {
83             Thread.sleep(10);
84         }
85     }
86 
removeAllNotifications()87     private void removeAllNotifications() {
88         mNotificationManager.cancelAll();
89     }
90 }
91