1 // Copyright 2013 Google Inc. All Rights Reserved.
2 
3 package com.android.cts.verifier.deskclock;
4 
5 import android.content.Intent;
6 import android.database.DataSetObserver;
7 import android.os.Bundle;
8 import android.provider.AlarmClock;
9 
10 import com.android.cts.verifier.ArrayTestListAdapter;
11 import com.android.cts.verifier.IntentDrivenTestActivity;
12 import com.android.cts.verifier.PassFailButtons;
13 import com.android.cts.verifier.R;
14 import com.android.cts.verifier.TestListAdapter.TestListItem;
15 import com.android.cts.verifier.IntentDrivenTestActivity.ButtonInfo;
16 import com.android.cts.verifier.IntentDrivenTestActivity.IntentFactory;
17 import com.android.cts.verifier.IntentDrivenTestActivity.TestInfo;
18 
19 import java.util.ArrayList;
20 import java.util.Calendar;
21 
22 /**
23  * Activity that lists all the DeskClock tests.
24  */
25 public class DeskClockTestsActivity extends PassFailButtons.TestListActivity {
26 
27     private static final String SHOW_ALARMS_TEST = "SHOW_ALARMS";
28     public static final String SET_ALARM_WITH_UI_TEST = "SET_ALARM_WITH_UI";
29     public static final String START_ALARM_TEST = "START_ALARM";
30     public static final String CREATE_ALARM_TEST = "CREATE_ALARM";
31     public static final String SET_TIMER_WITH_UI_TEST = "SET_TIMER_WITH_UI";
32     public static final String START_TIMER = "START_TIMER";
33     public static final String START_TIMER_WITH_UI = "START_TIMER_WITH_UI";
34 
35     private static final ArrayList<Integer> DAYS = new ArrayList<Integer>();
36 
37     private static final Intent CREATE_ALARM_INTENT = new Intent(AlarmClock.ACTION_SET_ALARM)
38             .putExtra(AlarmClock.EXTRA_MESSAGE, "Create Alarm Test")
39             .putExtra(AlarmClock.EXTRA_SKIP_UI, false)
40             .putExtra(AlarmClock.EXTRA_VIBRATE, true)
41             .putExtra(AlarmClock.EXTRA_RINGTONE, AlarmClock.VALUE_RINGTONE_SILENT)
42             .putExtra(AlarmClock.EXTRA_HOUR, 1)
43             .putExtra(AlarmClock.EXTRA_MINUTES, 23)
44             .putExtra(AlarmClock.EXTRA_DAYS, DAYS);
45 
46     static {
47         DAYS.add(Calendar.MONDAY);
48         DAYS.add(Calendar.WEDNESDAY);
49     }
50 
51     private static final Intent SHOW__ALARMS_INTENT = new Intent(AlarmClock.ACTION_SHOW_ALARMS);
52 
53     private static final Intent SET_ALARM_WITH_UI_INTENT = new Intent(AlarmClock.ACTION_SET_ALARM)
54             .putExtra(AlarmClock.EXTRA_SKIP_UI, false);
55 
56     private static final Intent SET_TIMER_WITH_UI_INTENT = new Intent(AlarmClock.ACTION_SET_TIMER)
57              .putExtra(AlarmClock.EXTRA_SKIP_UI, false);
58 
59     private static final Intent START_TIMER_INTENT = new Intent(AlarmClock.ACTION_SET_TIMER)
60     .putExtra(AlarmClock.EXTRA_SKIP_UI, true)
61     .putExtra(AlarmClock.EXTRA_MESSAGE, "Start Timer Test")
62     .putExtra(AlarmClock.EXTRA_LENGTH, 30);
63 
64     private static final Intent START_TIMER_WITH_UI_INTENT = new Intent(AlarmClock.ACTION_SET_TIMER)
65     .putExtra(AlarmClock.EXTRA_SKIP_UI, false)
66     .putExtra(AlarmClock.EXTRA_MESSAGE, "Start Timer Test")
67     .putExtra(AlarmClock.EXTRA_LENGTH, 30);
68 
69     private static final TestInfo[] ALARM_TESTS = new TestInfo[] {
70             new TestInfo(
71                     SHOW_ALARMS_TEST,
72                     R.string.dc_show_alarms_test,
73                     R.string.dc_show_alarms_test_info,
74                     new ButtonInfo(
75                             R.string.dc_show_alarms_button,
76                             SHOW__ALARMS_INTENT)),
77             new TestInfo(
78                     SET_ALARM_WITH_UI_TEST,
79                     R.string.dc_set_alarm_with_ui_test,
80                     R.string.dc_set_alarm_with_ui_test_info,
81                     new ButtonInfo(
82                             R.string.dc_set_alarm_button,
83                             SET_ALARM_WITH_UI_INTENT)),
84             new TestInfo(
85                     START_ALARM_TEST,
86                     R.string.dc_start_alarm_test,
87                     R.string.dc_start_alarm_test_info,
88                     new ButtonInfo(
89                             R.string.dc_set_alarm_button,
90                             DeskClockIntentFactory.class.getName()),
91                     new ButtonInfo(
92                             R.string.dc_set_alarm_verify_button,
93                             SHOW__ALARMS_INTENT)),
94             new TestInfo(
95                     CREATE_ALARM_TEST,
96                     R.string.dc_full_alarm_test,
97                     R.string.dc_full_alarm_test_info,
98                     new ButtonInfo(
99                             R.string.dc_full_alarm_button,
100                             CREATE_ALARM_INTENT)),
101     };
102 
103     private static final TestInfo[] TIMER_TESTS = new TestInfo[] {
104             new TestInfo(
105                     SET_TIMER_WITH_UI_TEST,
106                     R.string.dc_set_timer_with_ui_test,
107                     R.string.dc_set_timer_with_ui_test_info,
108                     new ButtonInfo(
109                             R.string.dc_set_timer_with_ui_button,
110                             SET_TIMER_WITH_UI_INTENT)),
111             new TestInfo(
112                     START_TIMER,
113                     R.string.dc_start_timer_test,
114                     R.string.dc_start_timer_test_info,
115                     new ButtonInfo(
116                             R.string.dc_start_timer_button,
117                             START_TIMER_INTENT)),
118             new TestInfo(
119                     START_TIMER_WITH_UI,
120                     R.string.dc_start_timer_with_ui_test,
121                     R.string.dc_start_timer_with_ui_test_info,
122                     new ButtonInfo(
123                             R.string.dc_start_timer_button,
124                             START_TIMER_WITH_UI_INTENT)),
125   };
126 
127     @Override
onCreate(Bundle savedInstanceState)128     protected void onCreate(Bundle savedInstanceState) {
129         super.onCreate(savedInstanceState);
130         setContentView(R.layout.pass_fail_list);
131         setInfoResources(R.string.deskclock_tests, R.string.deskclock_tests_info, 0);
132         setPassFailButtonClickListeners();
133 
134         getPassButton().setEnabled(false);
135 
136         final ArrayTestListAdapter adapter = new ArrayTestListAdapter(this);
137 
138         adapter.add(TestListItem.newCategory(this, R.string.deskclock_group_alarms));
139         addTests(adapter, ALARM_TESTS);
140 
141         adapter.add(TestListItem.newCategory(this, R.string.deskclock_group_timers));
142         addTests(adapter, TIMER_TESTS);
143 
144         adapter.registerDataSetObserver(new DataSetObserver() {
145             @Override
146             public void onChanged() {
147                 updatePassButton();
148             }
149         });
150 
151         setTestListAdapter(adapter);
152     }
153 
addTests(ArrayTestListAdapter adapter, TestInfo[] tests)154     private void addTests(ArrayTestListAdapter adapter, TestInfo[] tests) {
155         for (TestInfo info : tests) {
156 
157             final int title = info.getTitle();
158             adapter.add(TestListItem.newTest(this, title, info.getTestId(),
159             new Intent(this, IntentDrivenTestActivity.class)
160                     .putExtra(IntentDrivenTestActivity.EXTRA_ID, info.getTestId())
161                     .putExtra(IntentDrivenTestActivity.EXTRA_TITLE, title)
162                     .putExtra(IntentDrivenTestActivity.EXTRA_INFO, info.getInfoText())
163                     .putExtra(IntentDrivenTestActivity.EXTRA_BUTTONS, info.getButtons()),
164                     null));
165         }
166     }
167 
168     public static class DeskClockIntentFactory implements IntentFactory {
169         @Override
createIntents(String testId, int buttonText)170         public Intent[] createIntents(String testId, int buttonText) {
171             if (testId.equals(START_ALARM_TEST)) {
172                 // Alarm should go off 2 minutes from now
173                 final Calendar cal = Calendar.getInstance();
174                 cal.setTimeInMillis(cal.getTimeInMillis() + 120000);
175                 return new Intent[] {
176                         new Intent(AlarmClock.ACTION_SET_ALARM)
177                                 .putExtra(AlarmClock.EXTRA_MESSAGE, "Start Alarm Test")
178                         .putExtra(AlarmClock.EXTRA_SKIP_UI, true)
179                         .putExtra(AlarmClock.EXTRA_VIBRATE, true)
180                         .putExtra(AlarmClock.EXTRA_RINGTONE, AlarmClock.VALUE_RINGTONE_SILENT)
181                         .putExtra(AlarmClock.EXTRA_HOUR, cal.get(Calendar.HOUR_OF_DAY))
182                         .putExtra(AlarmClock.EXTRA_MINUTES, cal.get(Calendar.MINUTE)),
183                 };
184             } else {
185                 throw new IllegalArgumentException("Unknown test: " + testId);
186             }
187         }
188     }
189 }
190