release-request-94bbded7-c270-40fa-9a74-fedecfd046c6-for-git_oc-release-4034177 snap-temp-L73200000066785187

Change-Id: I18533523ade1d2b6520183dd7fd1b4b5dfb441bc
diff --git a/src/com/android/managedprovisioning/preprovisioning/EncryptionController.java b/src/com/android/managedprovisioning/preprovisioning/EncryptionController.java
index 032e664..30239b2 100644
--- a/src/com/android/managedprovisioning/preprovisioning/EncryptionController.java
+++ b/src/com/android/managedprovisioning/preprovisioning/EncryptionController.java
@@ -19,6 +19,7 @@
 import static com.android.internal.util.Preconditions.checkNotNull;
 
 import android.app.Notification;
+import android.app.NotificationChannel;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.ComponentName;
@@ -29,9 +30,9 @@
 import android.os.UserHandle;
 
 import com.android.internal.annotations.VisibleForTesting;
-import com.android.managedprovisioning.common.ProvisionLogger;
 import com.android.managedprovisioning.R;
 import com.android.managedprovisioning.common.Globals;
+import com.android.managedprovisioning.common.ProvisionLogger;
 import com.android.managedprovisioning.common.SettingsFacade;
 import com.android.managedprovisioning.common.Utils;
 import com.android.managedprovisioning.model.ProvisioningParams;
@@ -48,7 +49,10 @@
  */
 public class EncryptionController {
 
-    private static final int NOTIFICATION_ID = 1;
+    @VisibleForTesting
+    public static final String CHANNEL_ID = "encrypt";
+    @VisibleForTesting
+    public static final int NOTIFICATION_ID = 1;
 
     private final Context mContext;
     private final Utils mUtils;
@@ -199,9 +203,14 @@
         public void showResumeNotification(Intent intent) {
             NotificationManager notificationManager = (NotificationManager)
                     mContext.getSystemService(Context.NOTIFICATION_SERVICE);
+            NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
+                    mContext.getString(R.string.encrypt), NotificationManager.IMPORTANCE_HIGH);
+            notificationManager.createNotificationChannel(channel);
+
             final PendingIntent resumePendingIntent = PendingIntent.getActivity(
                     mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
             final Notification.Builder notify = new Notification.Builder(mContext)
+                    .setChannel(CHANNEL_ID)
                     .setContentIntent(resumePendingIntent)
                     .setContentTitle(mContext
                             .getString(R.string.continue_provisioning_notify_title))
diff --git a/tests/instrumentation/src/com/android/managedprovisioning/preprovisioning/ResumeNotificationHelperTest.java b/tests/instrumentation/src/com/android/managedprovisioning/preprovisioning/ResumeNotificationHelperTest.java
new file mode 100644
index 0000000..c9e2dbf
--- /dev/null
+++ b/tests/instrumentation/src/com/android/managedprovisioning/preprovisioning/ResumeNotificationHelperTest.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.managedprovisioning.preprovisioning;
+
+import static com.android.managedprovisioning.preprovisioning.EncryptionController.CHANNEL_ID;
+import static com.android.managedprovisioning.preprovisioning.EncryptionController.NOTIFICATION_ID;
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertEquals;
+
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.content.Context;
+import android.content.Intent;
+import android.os.SystemClock;
+import android.service.notification.StatusBarNotification;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.filters.SmallTest;
+import com.android.managedprovisioning.R;
+import com.android.managedprovisioning.common.Globals;
+import com.android.managedprovisioning.preprovisioning.EncryptionController.ResumeNotificationHelper;
+import org.hamcrest.MatcherAssert;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+@SmallTest
+public class ResumeNotificationHelperTest {
+
+    private static final int NOTIFICATION_TIMEOUT_MS = 5000;
+
+    private ResumeNotificationHelper mResumeNotificationHelper;
+    private NotificationManager mNotificationManager;
+    private Context mContext;
+
+    @Before
+    public void setUp() {
+        mContext = InstrumentationRegistry.getTargetContext();
+        mResumeNotificationHelper = new ResumeNotificationHelper(mContext);
+        mNotificationManager = mContext.getSystemService(NotificationManager.class);
+        removeAllNotifications();
+    }
+
+    @After
+    public void tearDown() {
+        removeAllNotifications();
+    }
+
+    @Test
+    public void testShowResumeNotification() throws Exception {
+        MatcherAssert.assertThat(mNotificationManager.getActiveNotifications().length,
+                equalTo(0));
+
+        Intent intent = new Intent(Globals.ACTION_RESUME_PROVISIONING);
+        mResumeNotificationHelper.showResumeNotification(intent);
+
+        waitForNotification();
+        StatusBarNotification[] notifications = mNotificationManager.getActiveNotifications();
+        MatcherAssert.assertThat(notifications.length, equalTo(1));
+        StatusBarNotification notification = notifications[0];
+        assertEquals(notification.getId(), NOTIFICATION_ID);
+        assertEquals(notification.getNotification().getChannel(), CHANNEL_ID);
+        assertEquals(notification.getNotification().extras.getString(Notification.EXTRA_TITLE),
+                mContext.getString(R.string.continue_provisioning_notify_title));
+    }
+
+    private void waitForNotification() throws InterruptedException {
+        long elapsed = SystemClock.elapsedRealtime();
+        while(SystemClock.elapsedRealtime() - elapsed < NOTIFICATION_TIMEOUT_MS
+                && mNotificationManager.getActiveNotifications().length == 0) {
+            Thread.sleep(10);
+        }
+    }
+
+    private void removeAllNotifications() {
+        mNotificationManager.cancelAll();
+    }
+}