1 /*
2  * Copyright (C) 2018 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.hardware.display;
18 
19 import static org.junit.Assert.assertArrayEquals;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotEquals;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 
25 import android.os.Parcel;
26 import android.support.test.filters.SmallTest;
27 import android.support.test.runner.AndroidJUnit4;
28 
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 import java.time.LocalDate;
33 import java.util.Arrays;
34 
35 @SmallTest
36 @RunWith(AndroidJUnit4.class)
37 public class AmbientBrightnessDayStatsTest {
38 
39     private static final LocalDate LOCAL_DATE = LocalDate.now();
40     private static final float[] BUCKET_BOUNDARIES = {0, 1, 10, 100};
41     private static final float[] STATS = {1.3f, 2.6f, 5.8f, 10};
42 
43     @Test
testParamsMustNotBeNull()44     public void testParamsMustNotBeNull() {
45         assertThrows(NullPointerException.class,
46                 () -> new AmbientBrightnessDayStats(null, BUCKET_BOUNDARIES));
47 
48         assertThrows(NullPointerException.class,
49                 () -> new AmbientBrightnessDayStats(LOCAL_DATE, null));
50 
51         assertThrows(NullPointerException.class,
52                 () -> new AmbientBrightnessDayStats(null, BUCKET_BOUNDARIES, STATS));
53 
54         assertThrows(NullPointerException.class,
55                 () -> new AmbientBrightnessDayStats(LOCAL_DATE, null, STATS));
56     }
57 
58     @Test(expected = IllegalArgumentException.class)
testBucketBoundariesMustNotBeEmpty()59     public void testBucketBoundariesMustNotBeEmpty() {
60         new AmbientBrightnessDayStats(LocalDate.now(), new float[]{});
61     }
62 
63     @Test(expected = IllegalArgumentException.class)
testStatsAndBoundariesMustHaveSameLength()64     public void testStatsAndBoundariesMustHaveSameLength() {
65         float[] stats = Arrays.copyOf(STATS, STATS.length + 1);
66         stats[stats.length - 1] = 0;
67         new AmbientBrightnessDayStats(LOCAL_DATE, BUCKET_BOUNDARIES, stats);
68     }
69 
70     @Test
testAmbientBrightnessDayStatsAdd()71     public void testAmbientBrightnessDayStatsAdd() {
72         AmbientBrightnessDayStats dayStats = new AmbientBrightnessDayStats(LOCAL_DATE,
73                 BUCKET_BOUNDARIES);
74         dayStats.log(0, 1);
75         dayStats.log(0.5f, 1.5f);
76         dayStats.log(50, 12.5f);
77         dayStats.log(2000, 1.24f);
78         dayStats.log(-10, 0.5f);
79         assertEquals(4, dayStats.getStats().length);
80         assertEquals(2.5f, dayStats.getStats()[0], 0);
81         assertEquals(0, dayStats.getStats()[1], 0);
82         assertEquals(12.5f, dayStats.getStats()[2], 0);
83         assertEquals(1.24f, dayStats.getStats()[3], 0);
84     }
85 
86     @Test
testGetters()87     public void testGetters() {
88         AmbientBrightnessDayStats dayStats = new AmbientBrightnessDayStats(LOCAL_DATE,
89                 BUCKET_BOUNDARIES, STATS);
90         assertEquals(LOCAL_DATE, dayStats.getLocalDate());
91         assertArrayEquals(BUCKET_BOUNDARIES, dayStats.getBucketBoundaries(), 0);
92         assertArrayEquals(STATS, dayStats.getStats(), 0);
93     }
94 
95     @Test
testParcelUnparcelAmbientBrightnessDayStats()96     public void testParcelUnparcelAmbientBrightnessDayStats() {
97         LocalDate today = LocalDate.now();
98         AmbientBrightnessDayStats stats = new AmbientBrightnessDayStats(today,
99                 new float[]{0, 1, 10, 100}, new float[]{1.3f, 2.6f, 5.8f, 10});
100         // Parcel the data
101         Parcel parcel = Parcel.obtain();
102         stats.writeToParcel(parcel, 0);
103         byte[] parceled = parcel.marshall();
104         parcel.recycle();
105         // Unparcel and check that it has not changed
106         parcel = Parcel.obtain();
107         parcel.unmarshall(parceled, 0, parceled.length);
108         parcel.setDataPosition(0);
109         AmbientBrightnessDayStats statsAgain = AmbientBrightnessDayStats.CREATOR.createFromParcel(
110                 parcel);
111         assertEquals(stats, statsAgain);
112     }
113 
114     @Test
testAmbientBrightnessDayStatsEquals()115     public void testAmbientBrightnessDayStatsEquals() {
116         AmbientBrightnessDayStats emptyDayStats = new AmbientBrightnessDayStats(LOCAL_DATE,
117                 BUCKET_BOUNDARIES);
118         AmbientBrightnessDayStats identicalEmptyDayStats = new AmbientBrightnessDayStats(LOCAL_DATE,
119                 BUCKET_BOUNDARIES, new float[BUCKET_BOUNDARIES.length]);
120         assertEquals(emptyDayStats, identicalEmptyDayStats);
121         assertEquals(emptyDayStats.hashCode(), identicalEmptyDayStats.hashCode());
122 
123         AmbientBrightnessDayStats dayStats = new AmbientBrightnessDayStats(LOCAL_DATE,
124                 BUCKET_BOUNDARIES, STATS);
125         AmbientBrightnessDayStats identicalDayStats = new AmbientBrightnessDayStats(LOCAL_DATE,
126                 BUCKET_BOUNDARIES, STATS);
127         assertEquals(dayStats, identicalDayStats);
128         assertEquals(dayStats.hashCode(), identicalDayStats.hashCode());
129 
130         assertNotEquals(emptyDayStats, dayStats);
131         assertNotEquals(emptyDayStats.hashCode(), dayStats.hashCode());
132 
133         AmbientBrightnessDayStats differentDateDayStats = new AmbientBrightnessDayStats(
134                 LOCAL_DATE.plusDays(1), BUCKET_BOUNDARIES, STATS);
135         assertNotEquals(dayStats, differentDateDayStats);
136         assertNotEquals(dayStats.hashCode(), differentDateDayStats.hashCode());
137 
138         float[] differentStats = Arrays.copyOf(STATS, STATS.length);
139         differentStats[differentStats.length - 1] += 5f;
140         AmbientBrightnessDayStats differentStatsDayStats = new AmbientBrightnessDayStats(LOCAL_DATE,
141                 BUCKET_BOUNDARIES, differentStats);
142         assertNotEquals(dayStats, differentDateDayStats);
143         assertNotEquals(dayStats.hashCode(), differentStatsDayStats.hashCode());
144 
145         float[] differentBucketBoundaries = Arrays.copyOf(BUCKET_BOUNDARIES,
146                 BUCKET_BOUNDARIES.length);
147         differentBucketBoundaries[differentBucketBoundaries.length - 1] += 100f;
148         AmbientBrightnessDayStats differentBoundariesDayStats = new AmbientBrightnessDayStats(
149                 LOCAL_DATE, differentBucketBoundaries, STATS);
150         assertNotEquals(dayStats, differentBoundariesDayStats);
151         assertNotEquals(dayStats.hashCode(), differentBoundariesDayStats.hashCode());
152     }
153 
154     private interface ExceptionRunnable {
run()155         void run() throws Exception;
156     }
157 
assertThrows(Class<? extends Throwable> exceptionClass, ExceptionRunnable r)158     private static void assertThrows(Class<? extends Throwable> exceptionClass,
159             ExceptionRunnable r) {
160         try {
161             r.run();
162         } catch (Throwable e) {
163             assertTrue("Expected exception type " + exceptionClass.getName() + " but got "
164                     + e.getClass().getName(), exceptionClass.isAssignableFrom(e.getClass()));
165             return;
166         }
167         fail("Expected exception type " + exceptionClass.getName()
168                 + ", but no exception was thrown");
169     }
170 
171 }
172