blob: 45082aee9cfe6d66f5e6690d9d4f855465968eb5 [file] [log] [blame]
Alice Leef4aef2b2015-08-25 14:29:16 -07001/*
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
17package android.assist.cts;
18
19import android.assist.common.Utils;
20
21import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.database.DatabaseUtils;
26import android.graphics.Color;
27import android.os.Bundle;
28import android.util.Log;
29
30import junit.framework.Test;
31
32import java.lang.Exception;
33import java.lang.Override;
34import java.util.concurrent.CountDownLatch;
35import java.util.concurrent.TimeUnit;
36
37public 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;
Cameron Nealec77854e2015-09-04 10:09:27 -070043 private CountDownLatch mHasResumedLatch, mReadyLatch;
Alice Leef4aef2b2015-08-25 14:29:16 -070044
45 public ScreenshotTest() {
46 super();
47 }
48
49 @Override
50 protected void setUp() throws Exception {
51 super.setUp();
Cameron Nealec77854e2015-09-04 10:09:27 -070052 mReadyLatch = new CountDownLatch(1);
Alice Leef4aef2b2015-08-25 14:29:16 -070053 // set up receiver
54 mScreenshotActivityReceiver = new ScreenshotTestReceiver();
55 IntentFilter filter = new IntentFilter();
56 filter.addAction(Utils.ASSIST_RECEIVER_REGISTERED);
Alice Lee474d0e42015-09-02 15:14:00 -070057 filter.addAction(Utils.APP_3P_HASRESUMED);
Alice Leef4aef2b2015-08-25 14:29:16 -070058 mContext.registerReceiver(mScreenshotActivityReceiver, filter);
59
60 // start test start activity
61 startTestActivity(TEST_CASE_TYPE);
62 }
63
64 @Override
65 protected void tearDown() throws Exception {
66 if (mScreenshotActivityReceiver != null) {
67 mContext.unregisterReceiver(mScreenshotActivityReceiver);
68 }
69 super.tearDown();
70 }
71
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()");
Cameron Nealec77854e2015-09-04 10:09:27 -070076 waitForAssistantToBeReady(mReadyLatch);
Alice Leef4aef2b2015-08-25 14:29:16 -070077
78 waitForActivityResumeAndAssist(Color.RED);
79 verifyAssistDataNullness(false, false, false, false);
80 assertTrue(mScreenshotMatches);
81 }
82
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()");
Cameron Nealec77854e2015-09-04 10:09:27 -070087 waitForAssistantToBeReady(mReadyLatch);
Alice Leef4aef2b2015-08-25 14:29:16 -070088
89 waitForActivityResumeAndAssist(Color.GREEN);
90 verifyAssistDataNullness(false, false, false, false);
91 assertTrue(mScreenshotMatches);
92 }
93
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()");
Cameron Nealec77854e2015-09-04 10:09:27 -070098 waitForAssistantToBeReady(mReadyLatch);
Alice Leef4aef2b2015-08-25 14:29:16 -070099
100 waitForActivityResumeAndAssist(Color.BLUE);
101 verifyAssistDataNullness(false, false, false, false);
102 assertTrue(mScreenshotMatches);
103 }
104
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
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.");
Cameron Nealec77854e2015-09-04 10:09:27 -0700124 if (mReadyLatch != null) {
125 mReadyLatch.countDown();
126 }
Alice Lee474d0e42015-09-02 15:14:00 -0700127 } else if (action.equals(Utils.APP_3P_HASRESUMED)) {
Alice Leef4aef2b2015-08-25 14:29:16 -0700128 if (mHasResumedLatch != null) {
129 mHasResumedLatch.countDown();
130 }
131 }
132 }
133 }
Cameron Nealec77854e2015-09-04 10:09:27 -0700134}