1 /*
2  * Copyright (C) 2008 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.widget.cts;
18 
19 
20 import android.app.Activity;
21 import android.app.Instrumentation;
22 import android.content.Context;
23 import android.os.Parcelable;
24 import android.test.ActivityInstrumentationTestCase2;
25 import android.util.AttributeSet;
26 import android.widget.TimePicker;
27 import android.widget.TimePicker.OnTimeChangedListener;
28 
29 /**
30  * Test {@link TimePicker}.
31  */
32 public class TimePickerTest extends ActivityInstrumentationTestCase2<CtsActivity> {
33     private TimePicker mTimePicker;
34 
35     private Activity mActivity;
36 
37     private Context mContext;
38 
39     private Instrumentation mInstrumentation;
40 
TimePickerTest()41     public TimePickerTest() {
42         super("android.widget.cts", CtsActivity.class);
43     }
44 
45     @Override
setUp()46     protected void setUp() throws Exception {
47         super.setUp();
48         mInstrumentation = getInstrumentation();
49         mContext = mInstrumentation.getTargetContext();
50         mActivity = getActivity();
51     }
52 
testConstructors()53     public void testConstructors() {
54         AttributeSet attrs =
55             mContext.getResources().getLayout(android.widget.cts.R.layout.timepicker);
56         assertNotNull(attrs);
57 
58         new TimePicker(mContext);
59         try {
60             new TimePicker(null);
61             fail("did not throw NullPointerException when param context is null.");
62         } catch (NullPointerException e) {
63             // expected
64         }
65 
66         new TimePicker(mContext, attrs);
67         try {
68             new TimePicker(null, attrs);
69             fail("did not throw NullPointerException when param context is null.");
70         } catch (NullPointerException e) {
71             // expected
72         }
73         new TimePicker(mContext, null);
74 
75         new TimePicker(mContext, attrs, 0);
76         try {
77             new TimePicker(null, attrs, 0);
78             fail("did not throw NullPointerException when param context is null.");
79         } catch (NullPointerException e) {
80             // expected
81         }
82         new TimePicker(mContext, null, 0);
83         new TimePicker(mContext, attrs, 0);
84         new TimePicker(mContext, attrs, Integer.MIN_VALUE);
85     }
86 
testSetEnabled()87     public void testSetEnabled() {
88         mTimePicker = new TimePicker(mContext);
89         assertTrue(mTimePicker.isEnabled());
90 
91         mTimePicker.setEnabled(false);
92         assertFalse(mTimePicker.isEnabled());
93 
94         mTimePicker.setEnabled(true);
95         assertTrue(mTimePicker.isEnabled());
96     }
97 
testSetOnTimeChangedListener()98     public void testSetOnTimeChangedListener() {
99         int initialHour = 13;
100         int initialMinute = 50;
101         mTimePicker = new TimePicker(mContext);
102 
103         MockOnTimeChangeListener listener = new MockOnTimeChangeListener();
104         mTimePicker.setOnTimeChangedListener(listener);
105         mTimePicker.setCurrentHour(Integer.valueOf(initialHour));
106         mTimePicker.setCurrentMinute(Integer.valueOf(initialMinute));
107         assertEquals(initialHour, listener.getNotifiedHourOfDay());
108         assertEquals(initialMinute, listener.getNotifiedMinute());
109 
110         // set the same hour as current
111         listener.reset();
112         mTimePicker.setCurrentHour(Integer.valueOf(initialHour));
113         assertFalse(listener.hasCalledOnTimeChanged());
114 
115         mTimePicker.setCurrentHour(Integer.valueOf(initialHour + 1));
116         assertTrue(listener.hasCalledOnTimeChanged());
117         assertEquals(initialHour + 1, listener.getNotifiedHourOfDay());
118         assertEquals(initialMinute, listener.getNotifiedMinute());
119         assertSame(mTimePicker, listener.getNotifiedView());
120 
121         // set the same minute as current
122         listener.reset();
123         mTimePicker.setCurrentMinute(initialMinute);
124         assertFalse(listener.hasCalledOnTimeChanged());
125 
126         listener.reset();
127         mTimePicker.setCurrentMinute(initialMinute + 1);
128         assertTrue(listener.hasCalledOnTimeChanged());
129         assertEquals(initialHour + 1, listener.getNotifiedHourOfDay());
130         assertEquals(initialMinute + 1, listener.getNotifiedMinute());
131         assertSame(mTimePicker, listener.getNotifiedView());
132 
133         // change time picker mode
134         listener.reset();
135         mTimePicker.setIs24HourView( !mTimePicker.is24HourView() );
136         assertFalse(listener.hasCalledOnTimeChanged());
137     }
138 
testAccessCurrentHour()139     public void testAccessCurrentHour() {
140         mTimePicker = new TimePicker(mContext);
141 
142         // AM/PM mode
143         mTimePicker.setIs24HourView(false);
144 
145         mTimePicker.setCurrentHour(0);
146         assertEquals(Integer.valueOf(0), mTimePicker.getCurrentHour());
147 
148         mTimePicker.setCurrentHour(12);
149         assertEquals(Integer.valueOf(12), mTimePicker.getCurrentHour());
150 
151         mTimePicker.setCurrentHour(13);
152         assertEquals(Integer.valueOf(13), mTimePicker.getCurrentHour());
153 
154         mTimePicker.setCurrentHour(23);
155         assertEquals(Integer.valueOf(23), mTimePicker.getCurrentHour());
156 
157         // for 24 hour mode
158         mTimePicker.setIs24HourView(true);
159 
160         mTimePicker.setCurrentHour(0);
161         assertEquals(Integer.valueOf(0), mTimePicker.getCurrentHour());
162 
163         mTimePicker.setCurrentHour(13);
164         assertEquals(Integer.valueOf(13), mTimePicker.getCurrentHour());
165 
166         mTimePicker.setCurrentHour(23);
167         assertEquals(Integer.valueOf(23), mTimePicker.getCurrentHour());
168     }
169 
testAccessHour()170     public void testAccessHour() {
171         mTimePicker = new TimePicker(mContext);
172 
173         // AM/PM mode
174         mTimePicker.setIs24HourView(false);
175 
176         mTimePicker.setHour(0);
177         assertEquals(0, mTimePicker.getHour());
178 
179         mTimePicker.setHour(12);
180         assertEquals(12, mTimePicker.getHour());
181 
182         mTimePicker.setHour(13);
183         assertEquals(13, mTimePicker.getHour());
184 
185         mTimePicker.setHour(23);
186         assertEquals(23, mTimePicker.getHour());
187 
188         // for 24 hour mode
189         mTimePicker.setIs24HourView(true);
190 
191         mTimePicker.setHour(0);
192         assertEquals(0, mTimePicker.getHour());
193 
194         mTimePicker.setHour(13);
195         assertEquals(13, mTimePicker.getHour());
196 
197         mTimePicker.setHour(23);
198         assertEquals(23, mTimePicker.getHour());
199     }
200 
testAccessIs24HourView()201     public void testAccessIs24HourView() {
202         mTimePicker = new TimePicker(mContext);
203         assertFalse(mTimePicker.is24HourView());
204 
205         mTimePicker.setIs24HourView(true);
206         assertTrue(mTimePicker.is24HourView());
207 
208         mTimePicker.setIs24HourView(false);
209         assertFalse(mTimePicker.is24HourView());
210     }
211 
testAccessCurrentMinute()212     public void testAccessCurrentMinute() {
213         mTimePicker = new TimePicker(mContext);
214 
215         mTimePicker.setCurrentMinute(0);
216         assertEquals(Integer.valueOf(0), mTimePicker.getCurrentMinute());
217 
218         mTimePicker.setCurrentMinute(12);
219         assertEquals(Integer.valueOf(12), mTimePicker.getCurrentMinute());
220 
221         mTimePicker.setCurrentMinute(33);
222         assertEquals(Integer.valueOf(33), mTimePicker.getCurrentMinute());
223 
224         mTimePicker.setCurrentMinute(59);
225         assertEquals(Integer.valueOf(59), mTimePicker.getCurrentMinute());
226     }
227 
testAccessMinute()228     public void testAccessMinute() {
229         mTimePicker = new TimePicker(mContext);
230 
231         mTimePicker.setMinute(0);
232         assertEquals(0, mTimePicker.getMinute());
233 
234         mTimePicker.setMinute(12);
235         assertEquals(12, mTimePicker.getMinute());
236 
237         mTimePicker.setMinute(33);
238         assertEquals(33, mTimePicker.getMinute());
239 
240         mTimePicker.setMinute(59);
241         assertEquals(59, mTimePicker.getMinute());
242     }
243 
testGetBaseline()244     public void testGetBaseline() {
245         mTimePicker = new TimePicker(mContext);
246         assertEquals(-1, mTimePicker.getBaseline());
247     }
248 
testOnSaveInstanceStateAndOnRestoreInstanceState()249     public void testOnSaveInstanceStateAndOnRestoreInstanceState() {
250         MyTimePicker source = new MyTimePicker(mContext);
251         MyTimePicker dest = new MyTimePicker(mContext);
252         int expectHour = (dest.getCurrentHour() + 10) % 24;
253         int expectMinute = (dest.getCurrentMinute() + 10) % 60;
254         source.setCurrentHour(expectHour);
255         source.setCurrentMinute(expectMinute);
256 
257         Parcelable p = source.onSaveInstanceState();
258         dest.onRestoreInstanceState(p);
259 
260         assertEquals(Integer.valueOf(expectHour), dest.getCurrentHour());
261         assertEquals(Integer.valueOf(expectMinute), dest.getCurrentMinute());
262     }
263 
264     private class MockOnTimeChangeListener implements OnTimeChangedListener {
265         private TimePicker mNotifiedView;
266 
267         private boolean mHasCalledOnTimeChanged;
268 
269         private int mNotifiedHourOfDay;
270 
271         private int mNotifiedMinute;;
272 
hasCalledOnTimeChanged()273         public boolean hasCalledOnTimeChanged() {
274             return mHasCalledOnTimeChanged;
275         }
276 
getNotifiedView()277         public TimePicker getNotifiedView() {
278             return mNotifiedView;
279         }
280 
getNotifiedHourOfDay()281         public int getNotifiedHourOfDay() {
282             return mNotifiedHourOfDay;
283         }
284 
getNotifiedMinute()285         public int getNotifiedMinute() {
286             return mNotifiedMinute;
287         }
288 
reset()289         public void reset() {
290             mNotifiedView = null;
291             mNotifiedHourOfDay = 0;
292             mNotifiedMinute = 0;
293             mHasCalledOnTimeChanged = false;
294         }
295 
onTimeChanged(TimePicker view, int hourOfDay, int minute)296         public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
297             mNotifiedView = view;
298             mNotifiedHourOfDay = hourOfDay;
299             mNotifiedMinute = minute;
300             mHasCalledOnTimeChanged = true;
301         }
302     }
303 
304     private class MyTimePicker extends TimePicker {
MyTimePicker(Context context)305         public MyTimePicker(Context context) {
306             super(context);
307         }
308 
309         @Override
onRestoreInstanceState(Parcelable state)310         protected void onRestoreInstanceState(Parcelable state) {
311             super.onRestoreInstanceState(state);
312         }
313 
314         @Override
onSaveInstanceState()315         protected Parcelable onSaveInstanceState() {
316             return super.onSaveInstanceState();
317         }
318     }
319 }
320