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         Log.i(TAG, "Starting screenshot test");
74         mTestActivity.startTest(TEST_CASE_TYPE);
75         Log.i(TAG, "start waitForAssistantToBeReady()");
76         waitForAssistantToBeReady(mReadyLatch);
77 
78         waitForActivityResumeAndAssist(Color.RED);
79         verifyAssistDataNullness(false, false, false, false);
80         assertTrue(mScreenshotMatches);
81     }
82 
testGreenScreenshot()83     public void testGreenScreenshot() throws Exception {
84         Log.i(TAG, "Starting screenshot test");
85         mTestActivity.startTest(TEST_CASE_TYPE);
86         Log.i(TAG, "start waitForAssistantToBeReady()");
87         waitForAssistantToBeReady(mReadyLatch);
88 
89         waitForActivityResumeAndAssist(Color.GREEN);
90         verifyAssistDataNullness(false, false, false, false);
91         assertTrue(mScreenshotMatches);
92     }
93 
testBlueScreenshot()94     public void testBlueScreenshot() throws Exception {
95         Log.i(TAG, "Starting screenshot test");
96         mTestActivity.startTest(TEST_CASE_TYPE);
97         Log.i(TAG, "start waitForAssistantToBeReady()");
98         waitForAssistantToBeReady(mReadyLatch);
99 
100         waitForActivityResumeAndAssist(Color.BLUE);
101         verifyAssistDataNullness(false, false, false, false);
102         assertTrue(mScreenshotMatches);
103     }
104 
waitForActivityResumeAndAssist(int color)105     private void waitForActivityResumeAndAssist(int color) throws Exception {
106         mHasResumedLatch = new CountDownLatch(1);
107         Bundle extras = new Bundle();
108         extras.putInt(Utils.SCREENSHOT_COLOR_KEY, color);
109         startSession(TEST_CASE_TYPE, extras);
110         Log.i(TAG, "waiting for onResume() before continuing.");
111         if (!mHasResumedLatch.await(Utils.ACTIVITY_ONRESUME_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
112             fail("activity failed to resume in " + Utils.ACTIVITY_ONRESUME_TIMEOUT_MS + "msec");
113         }
114         waitForContext();
115     }
116 
117     private class ScreenshotTestReceiver extends BroadcastReceiver {
118         @Override
onReceive(Context context, Intent intent)119         public void onReceive(Context context, Intent intent) {
120             String action = intent.getAction();
121             Log.i(ScreenshotTest.TAG, "Got some broadcast: " + action);
122             if (action.equals(Utils.ASSIST_RECEIVER_REGISTERED)) {
123                 Log.i(ScreenshotTest.TAG, "Received assist receiver is registered.");
124                 if (mReadyLatch != null) {
125                     mReadyLatch.countDown();
126                 }
127             } else if (action.equals(Utils.APP_3P_HASRESUMED)) {
128                 if (mHasResumedLatch != null) {
129                     mHasResumedLatch.countDown();
130                 }
131             }
132         }
133     }
134 }
135