Vasu Nori | c397e8e | 2015-07-20 13:57:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.alarmclock.cts; |
| 18 | |
| 19 | import android.alarmclock.common.Utils; |
| 20 | import android.alarmclock.common.Utils.TestcaseType; |
| 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.ComponentName; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
Vasu Nori | aed9311 | 2015-09-08 18:28:03 -0700 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
| 27 | import android.provider.AlarmClock; |
Vasu Nori | c397e8e | 2015-07-20 13:57:30 -0700 | [diff] [blame] | 28 | import android.os.Bundle; |
| 29 | import android.test.ActivityInstrumentationTestCase2; |
| 30 | import android.util.Log; |
| 31 | |
| 32 | import java.util.concurrent.CountDownLatch; |
| 33 | import java.util.concurrent.TimeUnit; |
| 34 | |
| 35 | public class AlarmClockTestBase extends ActivityInstrumentationTestCase2<TestStartActivity> { |
| 36 | static final String TAG = "AlarmClockTestBase"; |
| 37 | protected static final int TIMEOUT_MS = 20 * 1000; |
| 38 | |
| 39 | private Context mContext; |
| 40 | private String mTestResult; |
| 41 | private CountDownLatch mLatch; |
| 42 | private ActivityDoneReceiver mActivityDoneReceiver = null; |
| 43 | private TestStartActivity mActivity; |
| 44 | private TestcaseType mTestCaseType; |
| 45 | |
| 46 | public AlarmClockTestBase() { |
| 47 | super(TestStartActivity.class); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | protected void setUp() throws Exception { |
| 52 | super.setUp(); |
| 53 | mContext = getInstrumentation().getTargetContext(); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | protected void tearDown() throws Exception { |
| 58 | mContext.unregisterReceiver(mActivityDoneReceiver); |
| 59 | super.tearDown(); |
| 60 | } |
| 61 | |
| 62 | private void registerBroadcastReceiver(TestcaseType testCaseType) throws Exception { |
| 63 | mTestCaseType = testCaseType; |
| 64 | mLatch = new CountDownLatch(1); |
| 65 | if (mActivityDoneReceiver != null) { |
| 66 | mContext.unregisterReceiver(mActivityDoneReceiver); |
| 67 | } |
| 68 | mActivityDoneReceiver = new ActivityDoneReceiver(); |
| 69 | mContext.registerReceiver(mActivityDoneReceiver, |
| 70 | new IntentFilter(Utils.BROADCAST_INTENT + testCaseType.toString())); |
| 71 | } |
| 72 | |
Vasu Nori | aed9311 | 2015-09-08 18:28:03 -0700 | [diff] [blame] | 73 | private boolean isIntentAupported(TestcaseType testCaseType) { |
| 74 | Intent intent; |
| 75 | switch (testCaseType) { |
| 76 | case DISMISS_ALARM: |
| 77 | intent = new Intent(AlarmClock.ACTION_DISMISS_ALARM); |
| 78 | break; |
| 79 | |
| 80 | case SET_ALARM: |
| 81 | case SET_ALARM_FOR_DISMISSAL: |
| 82 | intent = new Intent(AlarmClock.ACTION_SET_ALARM); |
| 83 | break; |
| 84 | |
| 85 | case SNOOZE_ALARM: |
| 86 | intent = new Intent(AlarmClock.ACTION_SNOOZE_ALARM); |
| 87 | break; |
| 88 | |
| 89 | default: |
| 90 | // shouldn't happen |
| 91 | return false; |
| 92 | } |
| 93 | final PackageManager manager = mContext.getPackageManager(); |
| 94 | assertNotNull(manager); |
| 95 | if (manager.resolveActivity(intent, 0) == null) { |
| 96 | Log.i(TAG, "No Voice Activity found for the intent: " + intent.getAction()); |
| 97 | return false; |
| 98 | } |
| 99 | return true; |
| 100 | } |
| 101 | |
Vasu Nori | c397e8e | 2015-07-20 13:57:30 -0700 | [diff] [blame] | 102 | protected String runTest(TestcaseType testCaseType) throws Exception { |
| 103 | Log.i(TAG, "Begin Testing: " + testCaseType); |
Vasu Nori | aed9311 | 2015-09-08 18:28:03 -0700 | [diff] [blame] | 104 | // Make sure the corresponding intent is supported by the platform, before testing. |
| 105 | if (!isIntentAupported(testCaseType)) return Utils.COMPLETION_RESULT; |
| 106 | |
Vasu Nori | c397e8e | 2015-07-20 13:57:30 -0700 | [diff] [blame] | 107 | if (!startTestActivity(testCaseType)) { |
| 108 | fail("test activity start failed for testcase = " + testCaseType); |
| 109 | return ""; |
| 110 | } |
| 111 | |
| 112 | registerBroadcastReceiver(testCaseType); |
| 113 | mActivity.startTest(testCaseType.toString()); |
| 114 | if (!mLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)) { |
| 115 | fail("Failed to receive broadcast in " + TIMEOUT_MS + "msec"); |
| 116 | return ""; |
| 117 | } |
| 118 | return mTestResult; |
| 119 | } |
| 120 | |
| 121 | private boolean startTestActivity(TestcaseType testCaseType) { |
| 122 | Log.i(TAG, "Starting test activity for test: " + testCaseType); |
| 123 | Intent intent = new Intent(); |
| 124 | intent.setAction("android.intent.action.TEST_START_ACTIVITY_" + testCaseType.toString()); |
| 125 | intent.setComponent(new ComponentName(getInstrumentation().getContext(), |
| 126 | TestStartActivity.class)); |
| 127 | setActivityIntent(intent); |
| 128 | mActivity = getActivity(); |
| 129 | return mActivity != null; |
| 130 | } |
| 131 | |
| 132 | class ActivityDoneReceiver extends BroadcastReceiver { |
| 133 | @Override |
| 134 | public void onReceive(Context context, Intent intent) { |
| 135 | if (intent.getAction().equals( |
| 136 | Utils.BROADCAST_INTENT + AlarmClockTestBase.this.mTestCaseType.toString())) { |
| 137 | AlarmClockTestBase.this.mTestResult = intent.getStringExtra(Utils.TEST_RESULT); |
| 138 | Log.i(TAG, "received_broadcast for testcase_type = " + |
| 139 | Utils.BROADCAST_INTENT + AlarmClockTestBase.this.mTestCaseType + |
| 140 | ", test_result = " + AlarmClockTestBase.this.mTestResult); |
| 141 | mLatch.countDown(); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |