1 /*
2  * Copyright (C) 2014 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 android.assist.cts;
18 
19 import android.assist.common.Utils;
20 
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.database.DatabaseUtils;
26 import android.graphics.Color;
27 import android.os.Bundle;
28 import android.util.Log;
29 
30 import junit.framework.Test;
31 
32 import java.lang.Exception;
33 import java.lang.Override;
34 import java.util.concurrent.CountDownLatch;
35 import java.util.concurrent.TimeUnit;
36 
37 public class ScreenshotTest extends AssistTestBase {
38     static final String TAG = "ScreenshotTest";
39 
40     private static final String TEST_CASE_TYPE = Utils.SCREENSHOT;
41 
42     private BroadcastReceiver mScreenshotActivityReceiver;
43     private CountDownLatch mHasResumedLatch, mReadyLatch;
44 
ScreenshotTest()45     public ScreenshotTest() {
46         super();
47     }
48 
49     @Override
setUp()50     protected void setUp() throws Exception {
51         super.setUp();
52         mReadyLatch = new CountDownLatch(1);
53         // set up receiver
54         mScreenshotActivityReceiver = new ScreenshotTestReceiver();
55         IntentFilter filter = new IntentFilter();
56         filter.addAction(Utils.ASSIST_RECEIVER_REGISTERED);
57         filter.addAction(Utils.APP_3P_HASRESUMED);
58         mContext.registerReceiver(mScreenshotActivityReceiver, filter);
59 
60         // start test start activity
61         startTestActivity(TEST_CASE_TYPE);
62     }
63 
64     @Override
tearDown()65     protected void tearDown() throws Exception {
66         if (mScreenshotActivityReceiver != null) {
67             mContext.unregisterReceiver(mScreenshotActivityReceiver);
68         }
69         super.tearDown();
70     }
71 
testRedScreenshot()72     public void testRedScreenshot() throws Exception {
73         if (mActivityManager.isLowRamDevice()) {
74             Log.d(TAG, "Not running assist tests on low-RAM device.");
75             return;
76         }
77         Log.i(TAG, "Starting screenshot test");
78         mTestActivity.startTest(TEST_CASE_TYPE);
79         Log.i(TAG, "start waitForAssistantToBeReady()");
80         waitForAssistantToBeReady(mReadyLatch);
81 
82         waitForActivityResumeAndAssist(Color.RED);
83         verifyAssistDataNullness(false, false, false, false);
84         assertTrue(mScreenshotMatches);
85     }
86 
testGreenScreenshot()87     public void testGreenScreenshot() throws Exception {
88         if (mActivityManager.isLowRamDevice()) {
89             Log.d(TAG, "Not running assist tests on low-RAM device.");
90             return;
91         }
92         Log.i(TAG, "Starting screenshot test");
93         mTestActivity.startTest(TEST_CASE_TYPE);
94         Log.i(TAG, "start waitForAssistantToBeReady()");
95         waitForAssistantToBeReady(mReadyLatch);
96 
97         waitForActivityResumeAndAssist(Color.GREEN);
98         verifyAssistDataNullness(false, false, false, false);
99         assertTrue(mScreenshotMatches);
100     }
101 
testBlueScreenshot()102     public void testBlueScreenshot() throws Exception {
103         if (mActivityManager.isLowRamDevice()) {
104             Log.d(TAG, "Not running assist tests on low-RAM device.");
105             return;
106         }
107         Log.i(TAG, "Starting screenshot test");
108         mTestActivity.startTest(TEST_CASE_TYPE);
109         Log.i(TAG, "start waitForAssistantToBeReady()");
110         waitForAssistantToBeReady(mReadyLatch);
111 
112         waitForActivityResumeAndAssist(Color.BLUE);
113         verifyAssistDataNullness(false, false, false, false);
114         assertTrue(mScreenshotMatches);
115     }
116 
waitForActivityResumeAndAssist(int color)117     private void waitForActivityResumeAndAssist(int color) throws Exception {
118         mHasResumedLatch = new CountDownLatch(1);
119         Bundle extras = new Bundle();
120         extras.putInt(Utils.SCREENSHOT_COLOR_KEY, color);
121         startSession(TEST_CASE_TYPE, extras);
122         Log.i(TAG, "waiting for onResume() before continuing.");
123         if (!mHasResumedLatch.await(Utils.ACTIVITY_ONRESUME_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
124             fail("activity failed to resume in " + Utils.ACTIVITY_ONRESUME_TIMEOUT_MS + "msec");
125         }
126         waitForContext();
127     }
128 
129     private class ScreenshotTestReceiver extends BroadcastReceiver {
130         @Override
onReceive(Context context, Intent intent)131         public void onReceive(Context context, Intent intent) {
132             String action = intent.getAction();
133             Log.i(ScreenshotTest.TAG, "Got some broadcast: " + action);
134             if (action.equals(Utils.ASSIST_RECEIVER_REGISTERED)) {
135                 Log.i(ScreenshotTest.TAG, "Received assist receiver is registered.");
136                 if (mReadyLatch != null) {
137                     mReadyLatch.countDown();
138                 }
139             } else if (action.equals(Utils.APP_3P_HASRESUMED)) {
140                 if (mHasResumedLatch != null) {
141                     mHasResumedLatch.countDown();
142                 }
143             }
144         }
145     }
146 }
147