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 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNull;
21 
22 import android.Manifest;
23 import android.annotation.ColorRes;
24 import android.app.Activity;
25 import android.content.res.ColorStateList;
26 import android.graphics.BlendMode;
27 import android.graphics.Color;
28 import android.graphics.drawable.Icon;
29 import android.util.AttributeSet;
30 import android.util.Xml;
31 import android.view.View;
32 import android.widget.AnalogClock;
33 
34 import androidx.test.filters.SmallTest;
35 import androidx.test.rule.ActivityTestRule;
36 import androidx.test.runner.AndroidJUnit4;
37 
38 import com.android.compatibility.common.util.AdoptShellPermissionsRule;
39 
40 import org.junit.Before;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.xmlpull.v1.XmlPullParser;
45 
46 @SmallTest
47 @RunWith(AndroidJUnit4.class)
48 public class AnalogClockTest {
49     private static final String TIME_ZONE_NEW_YORK = "America/New_York";
50     private static final String TIME_ZONE_LOS_ANGELES = "America/Los_Angeles";
51 
52     private AttributeSet mAttrSet;
53     private Activity mActivity;
54     private AnalogClock mClock;
55     private AnalogClock mClockWithAttrs;
56 
57     @Rule(order = 0)
58     public AdoptShellPermissionsRule mAdoptShellPermissionsRule = new AdoptShellPermissionsRule(
59             androidx.test.platform.app.InstrumentationRegistry
60                     .getInstrumentation().getUiAutomation(),
61             Manifest.permission.START_ACTIVITIES_FROM_SDK_SANDBOX);
62 
63     @Rule(order = 1)
64     public ActivityTestRule<FrameLayoutCtsActivity> mActivityRule =
65             new ActivityTestRule<>(FrameLayoutCtsActivity.class);
66 
67     @Before
setup()68     public void setup() throws Exception {
69         mActivity = mActivityRule.getActivity();
70         XmlPullParser parser = mActivity.getResources().getXml(R.layout.analogclock_layout);
71         mAttrSet = Xml.asAttributeSet(parser);
72 
73         View layout = mActivity.getLayoutInflater().inflate(R.layout.analogclock_layout, null);
74         mClock = layout.findViewById(R.id.clock);
75         mClockWithAttrs = layout.findViewById(R.id.clock_with_attrs);
76     }
77 
78     @Test
testConstructor()79     public void testConstructor() {
80         new AnalogClock(mActivity);
81         new AnalogClock(mActivity, mAttrSet);
82         new AnalogClock(mActivity, mAttrSet, 0);
83     }
84 
85     @Test(expected = NullPointerException.class)
testConstructorWithNullContext1()86     public void testConstructorWithNullContext1() {
87         new AnalogClock(null);
88     }
89 
90     @Test(expected = NullPointerException.class)
testConstructorWithNullContext2()91     public void testConstructorWithNullContext2() {
92         new AnalogClock(null, null);
93     }
94 
95     @Test(expected = NullPointerException.class)
testConstructorWithNullContext3()96     public void testConstructorWithNullContext3() {
97         new AnalogClock(null, null, -1);
98     }
99 
100     @Test
testSetDial()101     public void testSetDial() {
102         Icon icon = Icon.createWithResource(mActivity, R.drawable.magenta_fill);
103         mClock.setDial(icon);
104         mClockWithAttrs.setDial(icon);
105     }
106 
107     @Test(expected = NullPointerException.class)
testSetDialWithNull()108     public void testSetDialWithNull() {
109         mClock.setDial(null);
110     }
111 
112     @Test
testSetHourHand()113     public void testSetHourHand() {
114         Icon icon = Icon.createWithResource(mActivity, R.drawable.magenta_fill);
115         mClock.setHourHand(icon);
116         mClockWithAttrs.setHourHand(icon);
117     }
118 
119     @Test(expected = NullPointerException.class)
testSetHourHandWithNull()120     public void testSetHourHandWithNull() {
121         mClock.setHourHand(null);
122     }
123 
124     @Test
testSetMinuteHand()125     public void testSetMinuteHand() {
126         Icon icon = Icon.createWithResource(mActivity, R.drawable.magenta_fill);
127         mClock.setMinuteHand(icon);
128         mClockWithAttrs.setMinuteHand(icon);
129     }
130 
131     @Test(expected = NullPointerException.class)
testSetMinuteHandWithNull()132     public void testSetMinuteHandWithNull() {
133         mClock.setMinuteHand(null);
134     }
135 
136     @Test
testSetSecondHand()137     public void testSetSecondHand() {
138         Icon icon = Icon.createWithResource(mActivity, R.drawable.magenta_fill);
139         mClock.setSecondHand(icon);
140         mClockWithAttrs.setSecondHand(icon);
141     }
142 
143     @Test
testSetSecondHandWithNull()144     public void testSetSecondHandWithNull() {
145         mClock.setSecondHand(null);
146         mClockWithAttrs.setSecondHand(null);
147     }
148 
149     @Test
testTimeZone()150     public void testTimeZone() {
151         assertNull(mClock.getTimeZone());
152         assertEquals(TIME_ZONE_NEW_YORK, mClockWithAttrs.getTimeZone());
153 
154         mClock.setTimeZone(TIME_ZONE_NEW_YORK);
155         assertEquals(TIME_ZONE_NEW_YORK, mClock.getTimeZone());
156 
157         mClock.setTimeZone(TIME_ZONE_LOS_ANGELES);
158         assertEquals(TIME_ZONE_LOS_ANGELES, mClock.getTimeZone());
159 
160         mClock.setTimeZone("Some/Invalid_time_zone");
161         assertNull(mClock.getTimeZone());
162 
163         mClock.setTimeZone(TIME_ZONE_NEW_YORK);
164         assertEquals(TIME_ZONE_NEW_YORK, mClock.getTimeZone());
165 
166         mClock.setTimeZone(null);
167         assertNull(mClock.getTimeZone());
168     }
169 
170     @Test
testSetDialTintList()171     public void testSetDialTintList() {
172         assertNull(mClock.getDialTintList());
173         assertEquals(
174                 getColorStateList(R.color.testcolorstatelist1),
175                 mClockWithAttrs.getDialTintList());
176 
177         ColorStateList tintList = new ColorStateList(
178                 new int[][] { {android.R.attr.state_checked}, {}},
179                 new int[] {Color.RED, Color.BLUE});
180         mClock.setDialTintList(tintList);
181         assertEquals(tintList, mClock.getDialTintList());
182 
183         mClock.setDialTintList(null);
184         assertNull(mClock.getDialTintList());
185     }
186 
187     @Test
testSetDialTintBlendMode()188     public void testSetDialTintBlendMode() {
189         assertNull(mClock.getDialTintBlendMode());
190         assertEquals(BlendMode.SRC_IN, mClockWithAttrs.getDialTintBlendMode());
191 
192         mClock.setDialTintBlendMode(BlendMode.COLOR);
193         mClockWithAttrs.setDialTintBlendMode(BlendMode.COLOR);
194         assertEquals(BlendMode.COLOR, mClock.getDialTintBlendMode());
195         assertEquals(BlendMode.COLOR, mClockWithAttrs.getDialTintBlendMode());
196 
197         mClock.setDialTintBlendMode(null);
198         mClockWithAttrs.setDialTintBlendMode(null);
199         assertNull(mClock.getDialTintBlendMode());
200         assertNull(mClockWithAttrs.getDialTintBlendMode());
201     }
202 
203     @Test
testSetHourHandTintList()204     public void testSetHourHandTintList() {
205         assertNull(mClock.getHourHandTintList());
206         assertEquals(
207                 getColorStateList(R.color.testcolor1),
208                 mClockWithAttrs.getHourHandTintList());
209 
210         ColorStateList tintList = new ColorStateList(
211                 new int[][] { {android.R.attr.state_checked}, {}},
212                 new int[] {Color.BLACK, Color.WHITE});
213         mClock.setHourHandTintList(tintList);
214         assertEquals(tintList, mClock.getHourHandTintList());
215 
216         mClock.setHourHandTintList(null);
217         assertNull(mClock.getHourHandTintList());
218     }
219 
220     @Test
testSetHourHandTintBlendMode()221     public void testSetHourHandTintBlendMode() {
222         assertNull(mClock.getHourHandTintBlendMode());
223         assertEquals(BlendMode.SRC_OVER, mClockWithAttrs.getHourHandTintBlendMode());
224 
225         mClock.setHourHandTintBlendMode(BlendMode.COLOR_BURN);
226         mClockWithAttrs.setHourHandTintBlendMode(BlendMode.COLOR_BURN);
227         assertEquals(BlendMode.COLOR_BURN, mClock.getHourHandTintBlendMode());
228         assertEquals(BlendMode.COLOR_BURN, mClockWithAttrs.getHourHandTintBlendMode());
229 
230         mClock.setHourHandTintBlendMode(null);
231         mClockWithAttrs.setHourHandTintBlendMode(null);
232         assertNull(mClock.getHourHandTintBlendMode());
233         assertNull(mClockWithAttrs.getHourHandTintBlendMode());
234     }
235 
236     @Test
testSetMinuteHandTintList()237     public void testSetMinuteHandTintList() {
238         assertNull(mClock.getMinuteHandTintList());
239         assertEquals(
240                 getColorStateList(R.color.testcolor2),
241                 mClockWithAttrs.getMinuteHandTintList());
242 
243         ColorStateList tintList = new ColorStateList(
244                 new int[][] { {android.R.attr.state_active}, {}},
245                 new int[] {Color.CYAN, Color.BLUE});
246         mClock.setMinuteHandTintList(tintList);
247         assertEquals(tintList, mClock.getMinuteHandTintList());
248 
249         mClock.setMinuteHandTintList(null);
250         assertNull(mClock.getMinuteHandTintList());
251     }
252 
253     @Test
testSetMinuteHandTintBlendMode()254     public void testSetMinuteHandTintBlendMode() {
255         assertNull(mClock.getMinuteHandTintBlendMode());
256         assertEquals(BlendMode.SCREEN, mClockWithAttrs.getMinuteHandTintBlendMode());
257 
258         mClock.setMinuteHandTintBlendMode(BlendMode.COLOR_DODGE);
259         mClockWithAttrs.setMinuteHandTintBlendMode(BlendMode.COLOR_DODGE);
260         assertEquals(BlendMode.COLOR_DODGE, mClock.getMinuteHandTintBlendMode());
261         assertEquals(BlendMode.COLOR_DODGE, mClockWithAttrs.getMinuteHandTintBlendMode());
262 
263         mClock.setMinuteHandTintBlendMode(null);
264         mClockWithAttrs.setMinuteHandTintBlendMode(null);
265         assertNull(mClock.getMinuteHandTintBlendMode());
266         assertNull(mClockWithAttrs.getMinuteHandTintBlendMode());
267     }
268 
269     @Test
testSetSecondHandTintList()270     public void testSetSecondHandTintList() {
271         assertNull(mClock.getSecondHandTintList());
272         assertEquals(
273                 getColorStateList(R.color.testcolor3),
274                 mClockWithAttrs.getSecondHandTintList());
275 
276         ColorStateList tintList = new ColorStateList(
277                 new int[][] { {android.R.attr.state_checked}, {}},
278                 new int[] {Color.GREEN, Color.BLUE});
279         mClock.setSecondHandTintList(tintList);
280         assertEquals(tintList, mClock.getSecondHandTintList());
281 
282         mClock.setSecondHandTintList(null);
283         assertNull(mClock.getSecondHandTintList());
284     }
285 
286     @Test
testSetSecondHandTintBlendMode()287     public void testSetSecondHandTintBlendMode() {
288         assertNull(mClock.getSecondHandTintBlendMode());
289         assertEquals(BlendMode.PLUS, mClockWithAttrs.getSecondHandTintBlendMode());
290 
291         mClock.setSecondHandTintBlendMode(BlendMode.DARKEN);
292         mClockWithAttrs.setSecondHandTintBlendMode(BlendMode.DARKEN);
293         assertEquals(BlendMode.DARKEN, mClock.getSecondHandTintBlendMode());
294         assertEquals(BlendMode.DARKEN, mClockWithAttrs.getSecondHandTintBlendMode());
295 
296         mClock.setSecondHandTintBlendMode(null);
297         mClockWithAttrs.setSecondHandTintBlendMode(null);
298         assertNull(mClock.getSecondHandTintBlendMode());
299         assertNull(mClockWithAttrs.getSecondHandTintBlendMode());
300     }
301 
getColorStateList(@olorRes int resId)302     private ColorStateList getColorStateList(@ColorRes int resId) {
303         return mClock.getContext().getColorStateList(resId);
304     }
305 }
306