Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 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; |
Cameron Neale | c77854e | 2015-09-04 10:09:27 -0700 | [diff] [blame^] | 43 | private CountDownLatch mHasResumedLatch, mReadyLatch; |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 44 | |
| 45 | public ScreenshotTest() { |
| 46 | super(); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | protected void setUp() throws Exception { |
| 51 | super.setUp(); |
Cameron Neale | c77854e | 2015-09-04 10:09:27 -0700 | [diff] [blame^] | 52 | mReadyLatch = new CountDownLatch(1); |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 53 | // set up receiver |
| 54 | mScreenshotActivityReceiver = new ScreenshotTestReceiver(); |
| 55 | IntentFilter filter = new IntentFilter(); |
| 56 | filter.addAction(Utils.ASSIST_RECEIVER_REGISTERED); |
Alice Lee | 474d0e4 | 2015-09-02 15:14:00 -0700 | [diff] [blame] | 57 | filter.addAction(Utils.APP_3P_HASRESUMED); |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 58 | 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 Neale | c77854e | 2015-09-04 10:09:27 -0700 | [diff] [blame^] | 76 | waitForAssistantToBeReady(mReadyLatch); |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 77 | |
| 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 Neale | c77854e | 2015-09-04 10:09:27 -0700 | [diff] [blame^] | 87 | waitForAssistantToBeReady(mReadyLatch); |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 88 | |
| 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 Neale | c77854e | 2015-09-04 10:09:27 -0700 | [diff] [blame^] | 98 | waitForAssistantToBeReady(mReadyLatch); |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 99 | |
| 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 Neale | c77854e | 2015-09-04 10:09:27 -0700 | [diff] [blame^] | 124 | if (mReadyLatch != null) { |
| 125 | mReadyLatch.countDown(); |
| 126 | } |
Alice Lee | 474d0e4 | 2015-09-02 15:14:00 -0700 | [diff] [blame] | 127 | } else if (action.equals(Utils.APP_3P_HASRESUMED)) { |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 128 | if (mHasResumedLatch != null) { |
| 129 | mHasResumedLatch.countDown(); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
Cameron Neale | c77854e | 2015-09-04 10:09:27 -0700 | [diff] [blame^] | 134 | } |