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.service.notification.StatusBarNotification; 23 import android.support.test.uiautomator.UiDevice; 24 import android.test.InstrumentationTestCase; 25 import android.test.suitebuilder.annotation.MediumTest; 26 import android.util.Log; 27 28 public class NotificationSecurityTests extends InstrumentationTestCase { 29 30 private static final String LOG_TAG = NotificationSecurityTests.class.getSimpleName(); 31 private static final int NOTIFICATION_ID_PUBLIC = 1; 32 private NotificationManager mNotificationManager = null; 33 private UiDevice mDevice = null; 34 private Context mContext; 35 private NotificationHelper mHelper; 36 37 @Override 38 public void setUp() throws Exception { 39 super.setUp(); 40 mDevice = UiDevice.getInstance(getInstrumentation()); 41 mContext = getInstrumentation().getContext(); 42 mNotificationManager = (NotificationManager) mContext 43 .getSystemService(Context.NOTIFICATION_SERVICE); 44 Log.i(LOG_TAG, "set up notification..."); 45 mHelper = new NotificationHelper(mDevice, getInstrumentation(), mNotificationManager); 46 mDevice.freezeRotation(); 47 mHelper.sleepAndWakeUpDevice(); 48 } 49 50 @Override 51 public void tearDown() throws Exception { 52 mNotificationManager.cancelAll(); 53 mHelper.swipeUp(); 54 super.tearDown(); 55 } 56 57 @MediumTest 58 public void testVisibilityPublic() throws Exception { 59 mHelper.enableNotificationViaAdb(true); 60 String title = "Public Notification"; 61 Log.i(LOG_TAG, "Begin test visibility equals VISIBILITY_PUBLIC "); 62 mHelper.sendNotification(NOTIFICATION_ID_PUBLIC, Notification.VISIBILITY_PUBLIC, title); 63 if (mHelper.checkNotificationExistence(NOTIFICATION_ID_PUBLIC, false)) { 64 fail("couldn't find posted notification id=" + NOTIFICATION_ID_PUBLIC); 65 } 66 StatusBarNotification[] sbns = mNotificationManager.getActiveNotifications(); 67 for (StatusBarNotification sbn : sbns) { 68 Log.i(LOG_TAG, sbn.getNotification().extras.getString(Notification.EXTRA_TITLE)); 69 if (sbn.getId() == NOTIFICATION_ID_PUBLIC) { 70 String sentTitle = sbn.getNotification().extras.getString(Notification.EXTRA_TITLE); 71 assertEquals(sentTitle, title); 72 } 73 } 74 } 75 } 76