Home | History | Annotate | Download | only in functional
      1 /*
      2  * Copyright (C) 2016 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.notification.functional;
     18 
     19 import android.app.Notification;
     20 import android.app.NotificationManager;
     21 import android.content.Context;
     22 import android.support.test.uiautomator.UiDevice;
     23 import android.support.test.uiautomator.By;
     24 import android.support.test.uiautomator.Until;
     25 import android.system.helpers.LockscreenHelper;
     26 import android.test.InstrumentationTestCase;
     27 import android.test.suitebuilder.annotation.LargeTest;
     28 import android.util.Log;
     29 
     30 public class NotificationSecurityLargeTests extends InstrumentationTestCase {
     31 
     32     private static final String LOG_TAG = NotificationSecurityLargeTests.class.getSimpleName();
     33     private static final int SHORT_TIMEOUT = 200;
     34     private static final int NOTIFICATION_ID_SECRET = 1;
     35     private static final int NOTIFICATION_ID_PUBLIC = 2;
     36     private static final String PIN = "1234";
     37     private NotificationManager mNotificationManager = null;
     38     private UiDevice mDevice = null;
     39     private Context mContext;
     40     private NotificationHelper mHelper;
     41     private LockscreenHelper mLockscreenHelper;
     42 
     43     @Override
     44     public void setUp() throws Exception {
     45         super.setUp();
     46         mDevice = UiDevice.getInstance(getInstrumentation());
     47         mContext = getInstrumentation().getContext();
     48         mNotificationManager = (NotificationManager) mContext
     49                 .getSystemService(Context.NOTIFICATION_SERVICE);
     50         Log.v(LOG_TAG, "set up notification...");
     51         mHelper = new NotificationHelper(mDevice, getInstrumentation(), mNotificationManager);
     52         mLockscreenHelper = new LockscreenHelper();
     53         mLockscreenHelper.setScreenLockViaShell(PIN, LockscreenHelper.MODE_PIN);
     54         mHelper.sleepAndWakeUpDevice();
     55     }
     56 
     57     @Override
     58     public void tearDown() throws Exception {
     59         mHelper.swipeUp();
     60         Thread.sleep(SHORT_TIMEOUT);
     61         mLockscreenHelper.removeScreenLockViaShell(PIN);
     62         mNotificationManager.cancelAll();
     63         super.tearDown();
     64     }
     65 
     66     @LargeTest
     67     public void testVisibilitySecret() throws Exception {
     68         String title = "Secret Title";
     69         Log.i(LOG_TAG, "Begin test visibility equals VISIBILITY_SECRET ");
     70         mHelper.sendNotification(NOTIFICATION_ID_SECRET, Notification.VISIBILITY_SECRET, title);
     71         if (!mHelper.checkNotificationExistence(NOTIFICATION_ID_SECRET, true)) {
     72             fail("couldn't find posted notification id=" + NOTIFICATION_ID_PUBLIC);
     73         }
     74         assertFalse(mDevice.wait(Until.hasObject(By.res("android:id/title").text(title)),
     75                 SHORT_TIMEOUT));
     76     }
     77 
     78     @LargeTest
     79     public void testVisibilityPrivate() throws Exception {
     80         Log.i(LOG_TAG, "Begin test visibility equals VISIBILITY_PRIVATE ");
     81         mHelper.sendNotification(NOTIFICATION_ID_PUBLIC, Notification.VISIBILITY_PRIVATE, "");
     82         if (!mHelper.checkNotificationExistence(NOTIFICATION_ID_PUBLIC, true)) {
     83             fail("couldn't find posted notification id=" + NOTIFICATION_ID_PUBLIC);
     84         }
     85         assertNotNull(
     86                 Until.findObject(By.res("android:id/title").text("Contents hidden")));
     87     }
     88 }
     89