1cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal/* 2cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal * Copyright (C) 2018 The Android Open Source Project 3cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal * 4cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal * Licensed under the Apache License, Version 2.0 (the "License"); 5cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal * you may not use this file except in compliance with the License. 6cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal * You may obtain a copy of the License at 7cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal * 8cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal * http://www.apache.org/licenses/LICENSE-2.0 9cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal * 10cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal * Unless required by applicable law or agreed to in writing, software 11cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal * distributed under the License is distributed on an "AS IS" BASIS, 12cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal * See the License for the specific language governing permissions and 14cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal * limitations under the License. 15cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal */ 16cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal 17cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwalpackage android.hardware.display; 18cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal 19e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwalimport static org.junit.Assert.assertArrayEquals; 20cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwalimport static org.junit.Assert.assertEquals; 21cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwalimport static org.junit.Assert.assertNotEquals; 22e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwalimport static org.junit.Assert.assertTrue; 23e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwalimport static org.junit.Assert.fail; 24cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal 25cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwalimport android.os.Parcel; 26cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwalimport android.support.test.filters.SmallTest; 27cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwalimport android.support.test.runner.AndroidJUnit4; 28cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal 29cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwalimport org.junit.Test; 30cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwalimport org.junit.runner.RunWith; 31cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal 32cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwalimport java.time.LocalDate; 33e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwalimport java.util.Arrays; 34cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal 35cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal@SmallTest 36cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal@RunWith(AndroidJUnit4.class) 37cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwalpublic class AmbientBrightnessDayStatsTest { 38cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal 39e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal private static final LocalDate LOCAL_DATE = LocalDate.now(); 40e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal private static final float[] BUCKET_BOUNDARIES = {0, 1, 10, 100}; 41e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal private static final float[] STATS = {1.3f, 2.6f, 5.8f, 10}; 42e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal 43e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal @Test 44e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal public void testParamsMustNotBeNull() { 45e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertThrows(NullPointerException.class, 46e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal () -> new AmbientBrightnessDayStats(null, BUCKET_BOUNDARIES)); 47e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal 48e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertThrows(NullPointerException.class, 49e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal () -> new AmbientBrightnessDayStats(LOCAL_DATE, null)); 50e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal 51e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertThrows(NullPointerException.class, 52e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal () -> new AmbientBrightnessDayStats(null, BUCKET_BOUNDARIES, STATS)); 53e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal 54e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertThrows(NullPointerException.class, 55e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal () -> new AmbientBrightnessDayStats(LOCAL_DATE, null, STATS)); 56e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal } 57e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal 58e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal @Test(expected = IllegalArgumentException.class) 59e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal public void testBucketBoundariesMustNotBeEmpty() { 60e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal new AmbientBrightnessDayStats(LocalDate.now(), new float[]{}); 61e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal } 62e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal 63e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal @Test(expected = IllegalArgumentException.class) 64e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal public void testStatsAndBoundariesMustHaveSameLength() { 65e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal float[] stats = Arrays.copyOf(STATS, STATS.length + 1); 66e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal stats[stats.length - 1] = 0; 67e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal new AmbientBrightnessDayStats(LOCAL_DATE, BUCKET_BOUNDARIES, stats); 68e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal } 69e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal 70cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal @Test 71cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal public void testAmbientBrightnessDayStatsAdd() { 72e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal AmbientBrightnessDayStats dayStats = new AmbientBrightnessDayStats(LOCAL_DATE, 73e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal BUCKET_BOUNDARIES); 74cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal dayStats.log(0, 1); 75cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal dayStats.log(0.5f, 1.5f); 76cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal dayStats.log(50, 12.5f); 77cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal dayStats.log(2000, 1.24f); 78cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal dayStats.log(-10, 0.5f); 79e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertEquals(4, dayStats.getStats().length); 80cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal assertEquals(2.5f, dayStats.getStats()[0], 0); 81cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal assertEquals(0, dayStats.getStats()[1], 0); 82cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal assertEquals(12.5f, dayStats.getStats()[2], 0); 83cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal assertEquals(1.24f, dayStats.getStats()[3], 0); 84cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal } 85cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal 86cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal @Test 87e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal public void testGetters() { 88e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal AmbientBrightnessDayStats dayStats = new AmbientBrightnessDayStats(LOCAL_DATE, 89e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal BUCKET_BOUNDARIES, STATS); 90e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertEquals(LOCAL_DATE, dayStats.getLocalDate()); 91e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertArrayEquals(BUCKET_BOUNDARIES, dayStats.getBucketBoundaries(), 0); 92e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertArrayEquals(STATS, dayStats.getStats(), 0); 93cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal } 94cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal 95cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal @Test 96cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal public void testParcelUnparcelAmbientBrightnessDayStats() { 97cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal LocalDate today = LocalDate.now(); 98cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal AmbientBrightnessDayStats stats = new AmbientBrightnessDayStats(today, 99cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal new float[]{0, 1, 10, 100}, new float[]{1.3f, 2.6f, 5.8f, 10}); 100cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal // Parcel the data 101cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal Parcel parcel = Parcel.obtain(); 102cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal stats.writeToParcel(parcel, 0); 103cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal byte[] parceled = parcel.marshall(); 104cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal parcel.recycle(); 105cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal // Unparcel and check that it has not changed 106cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal parcel = Parcel.obtain(); 107cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal parcel.unmarshall(parceled, 0, parceled.length); 108cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal parcel.setDataPosition(0); 109cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal AmbientBrightnessDayStats statsAgain = AmbientBrightnessDayStats.CREATOR.createFromParcel( 110cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal parcel); 111cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal assertEquals(stats, statsAgain); 112cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal } 113e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal 114e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal @Test 115e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal public void testAmbientBrightnessDayStatsEquals() { 116e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal AmbientBrightnessDayStats emptyDayStats = new AmbientBrightnessDayStats(LOCAL_DATE, 117e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal BUCKET_BOUNDARIES); 118e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal AmbientBrightnessDayStats identicalEmptyDayStats = new AmbientBrightnessDayStats(LOCAL_DATE, 119e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal BUCKET_BOUNDARIES, new float[BUCKET_BOUNDARIES.length]); 120e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertEquals(emptyDayStats, identicalEmptyDayStats); 121e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertEquals(emptyDayStats.hashCode(), identicalEmptyDayStats.hashCode()); 122e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal 123e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal AmbientBrightnessDayStats dayStats = new AmbientBrightnessDayStats(LOCAL_DATE, 124e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal BUCKET_BOUNDARIES, STATS); 125e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal AmbientBrightnessDayStats identicalDayStats = new AmbientBrightnessDayStats(LOCAL_DATE, 126e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal BUCKET_BOUNDARIES, STATS); 127e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertEquals(dayStats, identicalDayStats); 128e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertEquals(dayStats.hashCode(), identicalDayStats.hashCode()); 129e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal 130e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertNotEquals(emptyDayStats, dayStats); 131e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertNotEquals(emptyDayStats.hashCode(), dayStats.hashCode()); 132e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal 133e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal AmbientBrightnessDayStats differentDateDayStats = new AmbientBrightnessDayStats( 134e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal LOCAL_DATE.plusDays(1), BUCKET_BOUNDARIES, STATS); 135e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertNotEquals(dayStats, differentDateDayStats); 136e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertNotEquals(dayStats.hashCode(), differentDateDayStats.hashCode()); 137e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal 138e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal float[] differentStats = Arrays.copyOf(STATS, STATS.length); 139e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal differentStats[differentStats.length - 1] += 5f; 140e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal AmbientBrightnessDayStats differentStatsDayStats = new AmbientBrightnessDayStats(LOCAL_DATE, 141e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal BUCKET_BOUNDARIES, differentStats); 142e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertNotEquals(dayStats, differentDateDayStats); 143e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertNotEquals(dayStats.hashCode(), differentStatsDayStats.hashCode()); 144e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal 145e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal float[] differentBucketBoundaries = Arrays.copyOf(BUCKET_BOUNDARIES, 146e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal BUCKET_BOUNDARIES.length); 147e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal differentBucketBoundaries[differentBucketBoundaries.length - 1] += 100f; 148e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal AmbientBrightnessDayStats differentBoundariesDayStats = new AmbientBrightnessDayStats( 149e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal LOCAL_DATE, differentBucketBoundaries, STATS); 150e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertNotEquals(dayStats, differentBoundariesDayStats); 151e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertNotEquals(dayStats.hashCode(), differentBoundariesDayStats.hashCode()); 152e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal } 153e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal 154e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal private interface ExceptionRunnable { 155e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal void run() throws Exception; 156e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal } 157e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal 158e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal private static void assertThrows(Class<? extends Throwable> exceptionClass, 159e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal ExceptionRunnable r) { 160e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal try { 161e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal r.run(); 162e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal } catch (Throwable e) { 163e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal assertTrue("Expected exception type " + exceptionClass.getName() + " but got " 164e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal + e.getClass().getName(), exceptionClass.isAssignableFrom(e.getClass())); 165e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal return; 166e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal } 167e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal fail("Expected exception type " + exceptionClass.getName() 168e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal + ", but no exception was thrown"); 169e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal } 170e3a08ab5e13e06c33ba868b50ba7ee64380188f4Peeyush Agarwal 171cc155ddc69efce0579118e873ae991cebc083ca6Peeyush Agarwal} 172