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.content.Intent;
     22 import android.provider.AlarmClock;
     23 import android.support.test.uiautomator.By;
     24 import android.support.test.uiautomator.Direction;
     25 import android.support.test.uiautomator.UiDevice;
     26 import android.support.test.uiautomator.UiObject2;
     27 import android.support.test.uiautomator.Until;
     28 import android.test.InstrumentationTestCase;
     29 import android.test.suitebuilder.annotation.LargeTest;
     30 import android.test.suitebuilder.annotation.MediumTest;
     31 import android.view.inputmethod.InputMethodManager;
     32 
     33 import java.util.Calendar;
     34 import java.util.GregorianCalendar;
     35 
     36 public class HeadsUpNotificationTests extends InstrumentationTestCase {
     37     private static final int SHORT_TIMEOUT = 1000;
     38     private static final int LONG_TIMEOUT = 2000;
     39     private static final int NOTIFICATION_ID_1 = 1;
     40     private static final int NOTIFICATION_ID_2 = 2;
     41     private static final String NOTIFICATION_CONTENT_TEXT = "INLINE REPLY TEST";
     42     private NotificationManager mNotificationManager;
     43     private UiDevice mDevice = null;
     44     private Context mContext;
     45     private NotificationHelper mHelper;
     46 
     47     @Override
     48     public void setUp() throws Exception {
     49         super.setUp();
     50         mDevice = UiDevice.getInstance(getInstrumentation());
     51         mContext = getInstrumentation().getContext();
     52         mNotificationManager = (NotificationManager) mContext
     53                 .getSystemService(Context.NOTIFICATION_SERVICE);
     54         mHelper = new NotificationHelper(mDevice, getInstrumentation(), mNotificationManager);
     55         mDevice.setOrientationNatural();
     56         mHelper.unlockScreen();
     57         mDevice.pressHome();
     58         mNotificationManager.cancelAll();
     59     }
     60 
     61     @Override
     62     public void tearDown() throws Exception {
     63         mNotificationManager.cancelAll();
     64         mDevice.pressHome();
     65         mDevice.unfreezeRotation();
     66         super.tearDown();
     67     }
     68 
     69     @MediumTest
     70     public void testHeadsUpNotificationInlineReply() throws Exception {
     71         mHelper.sendNotificationsWithInlineReply(NOTIFICATION_ID_1, true);
     72         Thread.sleep(SHORT_TIMEOUT);
     73         mDevice.wait(Until.findObject(By.text("Reply")), LONG_TIMEOUT).click();
     74         try {
     75             UiObject2 replyBox = mDevice.wait(
     76                     Until.findObject(By.res("com.android.systemui:id/remote_input_send")),
     77                     LONG_TIMEOUT);
     78             InputMethodManager imm = (InputMethodManager) mContext
     79                     .getSystemService(Context.INPUT_METHOD_SERVICE);
     80             if (!imm.isAcceptingText()) {
     81                 assertNotNull("Keyboard for inline reply has not loaded correctly", replyBox);
     82             }
     83         } finally {
     84             mDevice.pressBack();
     85         }
     86     }
     87 
     88     @MediumTest
     89     public void testHeadsUpNotificationManualDismiss() throws Exception {
     90         mHelper.sendNotificationsWithInlineReply(NOTIFICATION_ID_1, true);
     91         Thread.sleep(SHORT_TIMEOUT);
     92         UiObject2 obj = mDevice.wait(Until.findObject(By.text(NOTIFICATION_CONTENT_TEXT)),
     93                 LONG_TIMEOUT);
     94         obj.swipe(Direction.LEFT, 1.0f);
     95         Thread.sleep(SHORT_TIMEOUT);
     96         if (mHelper.checkNotificationExistence(NOTIFICATION_ID_1, true)) {
     97             fail(String.format("Notification %s has not been auto dismissed", NOTIFICATION_ID_1));
     98         }
     99     }
    100 
    101     @LargeTest
    102     public void testHeadsUpNotificationAutoDismiss() throws Exception {
    103         mHelper.sendNotificationsWithInlineReply(NOTIFICATION_ID_1, true);
    104         Thread.sleep(LONG_TIMEOUT * 3);
    105         UiObject2 obj = mDevice.wait(Until.findObject(By.text(NOTIFICATION_CONTENT_TEXT)),
    106                 LONG_TIMEOUT);
    107         assertNull(String.format("Notification %s has not been auto dismissed", NOTIFICATION_ID_1),
    108                 obj);
    109     }
    110 
    111     @MediumTest
    112     public void testMultipleHeadsUpNotificationWithInlineReply() throws Exception {
    113         mHelper.sendNotificationsWithInlineReply(NOTIFICATION_ID_1, true);
    114         Thread.sleep(LONG_TIMEOUT);
    115         mDevice.wait(Until.findObject(By.text("Reply")), LONG_TIMEOUT).click();
    116         UiObject2 replyBox = mDevice.wait(
    117                 Until.findObject(By.res("com.android.systemui:id/remote_input_send")),
    118                 LONG_TIMEOUT);
    119         InputMethodManager imm = (InputMethodManager) mContext
    120                 .getSystemService(Context.INPUT_METHOD_SERVICE);
    121         if (!imm.isAcceptingText()) {
    122             assertNotNull("Keyboard for inline reply has not loaded correctly", replyBox);
    123         }
    124         mHelper.sendNotificationsWithInlineReply(NOTIFICATION_ID_2, true);
    125         Thread.sleep(LONG_TIMEOUT);
    126         UiObject2 obj = mDevice.wait(Until.findObject(By.text(NOTIFICATION_CONTENT_TEXT)),
    127                 LONG_TIMEOUT);
    128         if (obj == null) {
    129             assertNull(String.format("Notification %s can not be found", NOTIFICATION_ID_1),
    130                     obj);
    131         }
    132     }
    133 
    134     @LargeTest
    135     public void testAlarm() throws Exception {
    136         try {
    137             setAlarmNow();
    138             UiObject2 obj = mDevice.wait(Until.findObject(By.text("test")), 60000);
    139             if (obj == null) {
    140                 fail("Alarm heads up notifcation is not working");
    141             }
    142         } finally {
    143             mDevice.wait(Until.findObject(By.text("Dismiss")), LONG_TIMEOUT).click();
    144         }
    145     }
    146 
    147     private void setAlarmNow() throws InterruptedException {
    148         GregorianCalendar cal = new GregorianCalendar();
    149         cal.setTimeInMillis(System.currentTimeMillis());
    150         int hour = cal.get(Calendar.HOUR_OF_DAY);
    151         int minute = cal.get(Calendar.MINUTE) + 1;// to make sure it won't be set at the next day
    152         Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM);
    153         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    154         intent.putExtra(AlarmClock.EXTRA_HOUR, hour);
    155         intent.putExtra(AlarmClock.EXTRA_MINUTES, minute);
    156         intent.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
    157         intent.putExtra(AlarmClock.EXTRA_MESSAGE, "test");
    158         mContext.startActivity(intent);
    159         Thread.sleep(LONG_TIMEOUT * 2);
    160     }
    161 }
    162