• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.NotificationManager;
20 import android.content.Context;
21 import android.service.notification.StatusBarNotification;
22 import android.support.test.uiautomator.By;
23 import android.support.test.uiautomator.Direction;
24 import android.support.test.uiautomator.UiDevice;
25 import android.support.test.uiautomator.UiObject2;
26 import android.support.test.uiautomator.Until;
27 import android.test.InstrumentationTestCase;
28 import android.test.suitebuilder.annotation.MediumTest;
29 import android.util.Log;
30 
31 import java.util.HashMap;
32 import java.util.Map;
33 
34 public class NotificationInteractionTests extends InstrumentationTestCase {
35     private static final String LOG_TAG = NotificationInteractionTests.class.getSimpleName();
36     private static final int LONG_TIMEOUT = 2000;
37     private final boolean DEBUG = false;
38     private NotificationManager mNotificationManager;
39     private UiDevice mDevice = null;
40     private Context mContext;
41     private NotificationHelper mHelper;
42     private static final int CUSTOM_NOTIFICATION_ID = 1;
43     private static final int NOTIFICATIONS_COUNT = 3;
44 
45     @Override
setUp()46     public void setUp() throws Exception {
47         super.setUp();
48         mDevice = UiDevice.getInstance(getInstrumentation());
49         mContext = getInstrumentation().getContext();
50         mNotificationManager = (NotificationManager) mContext
51                 .getSystemService(Context.NOTIFICATION_SERVICE);
52         mHelper = new NotificationHelper(mDevice, getInstrumentation(), mNotificationManager);
53         mDevice.setOrientationNatural();
54         mNotificationManager.cancelAll();
55     }
56 
57     @Override
tearDown()58     public void tearDown() throws Exception {
59         super.tearDown();
60         mDevice.unfreezeRotation();
61         mDevice.pressHome();
62         mNotificationManager.cancelAll();
63     }
64 
65     @MediumTest
testNonDismissNotification()66     public void testNonDismissNotification() throws Exception {
67         String text = "USB debugging connected";
68         mDevice.openNotification();
69         Thread.sleep(LONG_TIMEOUT);
70         UiObject2 obj = findByText(text);
71         assertNotNull(String.format("Couldn't find %s notification", text), obj);
72         obj.swipe(Direction.LEFT, 1.0f);
73         Thread.sleep(LONG_TIMEOUT);
74         obj = mDevice.wait(Until.findObject(By.text(text)),
75                 LONG_TIMEOUT);
76         assertNotNull("USB debugging notification has been dismissed", obj);
77     }
78 
79     /** send out multiple notifications in order to test CLEAR ALL function */
80     @MediumTest
testDismissAll()81     public void testDismissAll() throws Exception {
82         String text = "CLEAR ALL";
83         Map<Integer, String> lists = new HashMap<Integer, String>();
84         StatusBarNotification[] sbns = mNotificationManager.getActiveNotifications();
85         int currentSbns = sbns.length;
86         for (int i = 0; i < NOTIFICATIONS_COUNT; i++) {
87             lists.put(CUSTOM_NOTIFICATION_ID + i, Integer.toString(CUSTOM_NOTIFICATION_ID + i));
88         }
89         mHelper.sendNotifications(lists);
90         if (DEBUG) {
91             Log.d(LOG_TAG,
92                     String.format("posted %s notifications, here they are: ", NOTIFICATIONS_COUNT));
93             sbns = mNotificationManager.getActiveNotifications();
94             for (StatusBarNotification sbn : sbns) {
95                 Log.d(LOG_TAG, "  " + sbn);
96             }
97         }
98         if (mDevice.openNotification()) {
99             Thread.sleep(LONG_TIMEOUT);
100             UiObject2 clearAll = findByText(text);
101             clearAll.click();
102         }
103         Thread.sleep(LONG_TIMEOUT);
104         sbns = mNotificationManager.getActiveNotifications();
105         assertTrue(String.format("%s notifications have not been cleared", sbns.length),
106                 sbns.length == currentSbns);
107     }
108 
findByText(String text)109     private UiObject2 findByText(String text) throws Exception {
110         int maxAttempt = 5;
111         UiObject2 item = null;
112         while (maxAttempt-- > 0) {
113             item = mDevice.wait(Until.findObject(By.text(text)), LONG_TIMEOUT);
114             if (item == null) {
115                 mDevice.swipe(mDevice.getDisplayWidth() / 2, mDevice.getDisplayHeight() / 2,
116                         mDevice.getDisplayWidth() / 2, 0, 30);
117             } else {
118                 return item;
119             }
120         }
121         return null;
122     }
123 }
124