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.NotificationManager;
     20 import android.content.Context;
     21 import android.os.RemoteException;
     22 import android.support.test.uiautomator.By;
     23 import android.support.test.uiautomator.UiDevice;
     24 import android.support.test.uiautomator.UiObject2;
     25 import android.support.test.uiautomator.Until;
     26 import android.test.InstrumentationTestCase;
     27 import android.test.suitebuilder.annotation.MediumTest;
     28 
     29 import java.util.ArrayList;
     30 import java.util.Arrays;
     31 import java.util.List;
     32 
     33 public class NotificationBundlingTests extends InstrumentationTestCase {
     34     private static final int SHORT_TIMEOUT = 200;
     35     private static final int LONG_TIMEOUT = 2000;
     36     private static final int GROUP_NOTIFICATION_ID = 1;
     37     private static final int CHILD_NOTIFICATION_ID = 500;
     38     private static final int SECOND_CHILD_NOTIFICATION_ID = 501;
     39     private static final String BUNDLE_GROUP_KEY = "group_key ";
     40     private NotificationManager mNotificationManager;
     41     private UiDevice mDevice = null;
     42     private Context mContext;
     43     private NotificationHelper mHelper;
     44 
     45     @Override
     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.pressHome();
     54         mNotificationManager.cancelAll();
     55         try {
     56             mDevice.setOrientationNatural();
     57         } catch (RemoteException e) {
     58             throw new RuntimeException("failed to freeze device orientaion", e);
     59         }
     60     }
     61 
     62     @Override
     63     public void tearDown() throws Exception {
     64         mNotificationManager.cancelAll();
     65         mDevice.pressHome();
     66         super.tearDown();
     67     }
     68 
     69     @MediumTest
     70     public void testBundlingNotification() throws Exception {
     71         List<Integer> lists = new ArrayList<Integer>(Arrays.asList(GROUP_NOTIFICATION_ID,
     72                 CHILD_NOTIFICATION_ID, SECOND_CHILD_NOTIFICATION_ID));
     73         mHelper.sendBundlingNotifications(lists, BUNDLE_GROUP_KEY);
     74         Thread.sleep(SHORT_TIMEOUT);
     75         mHelper.swipeDown();
     76         UiObject2 obj = mDevice.wait(
     77                 Until.findObject(By.res("com.android.systemui", "notification_title")),
     78                 LONG_TIMEOUT);
     79         int currentY = obj.getVisibleCenter().y;
     80         mDevice.wait(Until.findObject(By.res("android:id/expand_button")), LONG_TIMEOUT).click();
     81         obj = mDevice.wait(Until.findObject(By.textContains(lists.get(1).toString())),
     82                 LONG_TIMEOUT);
     83         assertFalse("The notifications have not been bundled",
     84                 obj.getVisibleCenter().y == currentY);
     85     }
     86 
     87     @MediumTest
     88     public void testDismissBundlingNotification() throws Exception {
     89         List<Integer> lists = new ArrayList<Integer>(Arrays.asList(GROUP_NOTIFICATION_ID,
     90                 CHILD_NOTIFICATION_ID, SECOND_CHILD_NOTIFICATION_ID));
     91         mHelper.sendBundlingNotifications(lists, BUNDLE_GROUP_KEY);
     92         mHelper.swipeDown();
     93         dismissObject(Integer.toString(CHILD_NOTIFICATION_ID));
     94         Thread.sleep(LONG_TIMEOUT);
     95         for (int n : lists) {
     96             if (mHelper.checkNotificationExistence(n, true)) {
     97                 fail(String.format("Notification %s has not been dismissed", n));
     98             }
     99         }
    100     }
    101 
    102     @MediumTest
    103     public void testDismissIndividualNotification() throws Exception {
    104         List<Integer> lists = new ArrayList<Integer>(Arrays.asList(GROUP_NOTIFICATION_ID,
    105                 CHILD_NOTIFICATION_ID, SECOND_CHILD_NOTIFICATION_ID));
    106         mHelper.sendBundlingNotifications(lists, BUNDLE_GROUP_KEY);
    107         Thread.sleep(SHORT_TIMEOUT);
    108         mDevice.openNotification();
    109         mDevice.wait(Until.findObject(By.res("android:id/expand_button")), LONG_TIMEOUT).click();
    110         dismissObject(Integer.toString(CHILD_NOTIFICATION_ID));
    111         Thread.sleep(LONG_TIMEOUT);
    112         if (mHelper.checkNotificationExistence(CHILD_NOTIFICATION_ID, true)) {
    113             fail(String.format("Notification %s has not been dismissed", CHILD_NOTIFICATION_ID));
    114         }
    115         if (mHelper.checkNotificationExistence(GROUP_NOTIFICATION_ID, false)) {
    116             fail(String.format("Notification %s has been dismissed ", GROUP_NOTIFICATION_ID));
    117         }
    118     }
    119 
    120     private void dismissObject(String text) {
    121         UiObject2 obj = mDevice.wait(
    122                 Until.findObject(By.textContains(text)),
    123                 LONG_TIMEOUT);
    124         int y = obj.getVisibleBounds().centerY();
    125         mDevice.swipe(0, y, mDevice.getDisplayWidth(),
    126                 y, 5);
    127     }
    128 }
    129