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