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