PersistentDataStoreTest.java revision eef0e13f018f33bf3db2f8311bbb429369e13394
1/*
2 * Copyright 2017 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
17package com.android.server.display;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertNotNull;
21import static org.junit.Assert.assertNull;
22import static org.junit.Assert.assertTrue;
23
24import android.hardware.display.BrightnessConfiguration;
25import android.support.test.filters.SmallTest;
26import android.support.test.runner.AndroidJUnit4;
27import android.util.AtomicFile;
28import android.util.Pair;
29
30import org.junit.Before;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33
34import java.io.FileInputStream;
35import java.io.ByteArrayInputStream;
36import java.io.ByteArrayOutputStream;
37import java.io.FileNotFoundException;
38import java.io.InputStream;
39import java.io.IOException;
40import java.io.OutputStream;
41import java.io.PrintWriter;
42import java.nio.charset.StandardCharsets;
43
44@SmallTest
45@RunWith(AndroidJUnit4.class)
46public class PersistentDataStoreTest {
47    private PersistentDataStore mDataStore;
48    private TestInjector mInjector;
49
50    @Before
51    public void setUp() {
52        mInjector = new TestInjector();
53        mDataStore = new PersistentDataStore(mInjector);
54    }
55
56    @Test
57    public void testLoadingBrightnessConfigurations() {
58        String contents = "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n"
59                + "<display-manager-state>\n"
60                + "  <brightness-configurations>\n"
61                + "    <brightness-configuration user-serial=\"1\">\n"
62                + "      <brightness-curve>\n"
63                + "        <brightness-point lux=\"0\" nits=\"13.25\"/>\n"
64                + "        <brightness-point lux=\"25\" nits=\"35.94\"/>\n"
65                + "      </brightness-curve>\n"
66                + "    </brightness-configuration>\n"
67                + "    <brightness-configuration user-serial=\"3\">\n"
68                + "      <brightness-curve>\n"
69                + "        <brightness-point lux=\"0\" nits=\"13.25\"/>\n"
70                + "        <brightness-point lux=\"10.2\" nits=\"15\"/>\n"
71                + "      </brightness-curve>\n"
72                + "    </brightness-configuration>\n"
73                + "  </brightness-configurations>\n"
74                + "</display-manager-state>\n";
75        InputStream is = new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8));
76        mInjector.setReadStream(is);
77        mDataStore.loadIfNeeded();
78        BrightnessConfiguration config = mDataStore.getBrightnessConfiguration(1 /*userSerial*/);
79        Pair<float[], float[]> curve = config.getCurve();
80        float[] expectedLux = { 0f, 25f };
81        float[] expectedNits = { 13.25f, 35.94f };
82        assertArrayEquals(expectedLux, curve.first, "lux");
83        assertArrayEquals(expectedNits, curve.second, "nits");
84
85        config = mDataStore.getBrightnessConfiguration(3 /*userSerial*/);
86        curve = config.getCurve();
87        expectedLux = new float[] { 0f, 10.2f };
88        expectedNits = new float[] { 13.25f, 15f };
89        assertArrayEquals(expectedLux, curve.first, "lux");
90        assertArrayEquals(expectedNits, curve.second, "nits");
91    }
92
93    @Test
94    public void testBrightnessConfigWithInvalidCurveIsIgnored() {
95        String contents = "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n"
96                + "<display-manager-state>\n"
97                + "  <brightness-configurations>\n"
98                + "    <brightness-configuration user-serial=\"0\">\n"
99                + "      <brightness-curve>\n"
100                + "        <brightness-point lux=\"1\" nits=\"13.25\"/>\n"
101                + "        <brightness-point lux=\"25\" nits=\"35.94\"/>\n"
102                + "      </brightness-curve>\n"
103                + "    </brightness-configuration>\n"
104                + "  </brightness-configurations>\n"
105                + "</display-manager-state>\n";
106        InputStream is = new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8));
107        mInjector.setReadStream(is);
108        mDataStore.loadIfNeeded();
109        assertNull(mDataStore.getBrightnessConfiguration(0 /*userSerial*/));
110    }
111
112    @Test
113    public void testBrightnessConfigWithInvalidFloatsIsIgnored() {
114        String contents = "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n"
115                + "<display-manager-state>\n"
116                + "  <brightness-configurations>\n"
117                + "    <brightness-configuration user-serial=\"0\">\n"
118                + "      <brightness-curve>\n"
119                + "        <brightness-point lux=\"0\" nits=\"13.25\"/>\n"
120                + "        <brightness-point lux=\"0xFF\" nits=\"foo\"/>\n"
121                + "      </brightness-curve>\n"
122                + "    </brightness-configuration>\n"
123                + "  </brightness-configurations>\n"
124                + "</display-manager-state>\n";
125        InputStream is = new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8));
126        mInjector.setReadStream(is);
127        mDataStore.loadIfNeeded();
128        assertNull(mDataStore.getBrightnessConfiguration(0 /*userSerial*/));
129    }
130
131    @Test
132    public void testEmptyBrightnessConfigurationsDoesntCrash() {
133        String contents = "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n"
134                + "<display-manager-state>\n"
135                + "  <brightness-configurations />\n"
136                + "</display-manager-state>\n";
137        InputStream is = new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8));
138        mInjector.setReadStream(is);
139        mDataStore.loadIfNeeded();
140        assertNull(mDataStore.getBrightnessConfiguration(0 /*userSerial*/));
141    }
142
143    @Test
144    public void testStoreAndReloadOfBrightnessConfigurations() {
145        final float[] lux = { 0f, 10f };
146        final float[] nits = {1f, 100f };
147        final BrightnessConfiguration config = new BrightnessConfiguration.Builder()
148                .setCurve(lux, nits)
149                .build();
150        mDataStore.loadIfNeeded();
151        assertNull(mDataStore.getBrightnessConfiguration(0 /*userSerial*/));
152        mDataStore.setBrightnessConfigurationForUser(config, 0);
153
154        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
155        mInjector.setWriteStream(baos);
156        mDataStore.saveIfNeeded();
157        assertTrue(mInjector.wasWriteSuccessful());
158
159        TestInjector newInjector = new TestInjector();
160        PersistentDataStore newDataStore = new PersistentDataStore(newInjector);
161        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
162        newInjector.setReadStream(bais);
163        newDataStore.loadIfNeeded();
164        assertNotNull(newDataStore.getBrightnessConfiguration(0 /*userSerial*/));
165        assertEquals(mDataStore.getBrightnessConfiguration(0 /*userSerial*/),
166                newDataStore.getBrightnessConfiguration(0 /*userSerial*/));
167    }
168
169    public class TestInjector extends PersistentDataStore.Injector {
170        private InputStream mReadStream;
171        private OutputStream mWriteStream;
172
173        private boolean mWasSuccessful;
174
175        @Override
176        public InputStream openRead() throws FileNotFoundException {
177            if (mReadStream != null) {
178                return mReadStream;
179            } else {
180                throw new FileNotFoundException();
181            }
182        }
183
184        @Override
185        public OutputStream startWrite() {
186            return mWriteStream;
187        }
188
189        @Override
190        public void finishWrite(OutputStream os, boolean success) {
191            mWasSuccessful = success;
192            try {
193                os.close();
194            } catch (IOException e) {
195                // This method can't throw IOException since the super implementation doesn't, so
196                // we just wrap it in a RuntimeException so we end up crashing the test all the
197                // same.
198                throw new RuntimeException(e);
199            }
200        }
201
202        public void setReadStream(InputStream is) {
203            mReadStream = is;
204        }
205
206        public void setWriteStream(OutputStream os) {
207            mWriteStream = os;
208        }
209
210        public boolean wasWriteSuccessful() {
211            return mWasSuccessful;
212        }
213    }
214
215    private static void assertArrayEquals(float[] expected, float[] actual, String name) {
216        assertEquals("Expected " + name + " arrays to be the same length!",
217                expected.length, actual.length);
218        for (int i = 0; i < expected.length; i++) {
219            assertEquals("Expected " + name + " arrays to be equivalent when value " + i
220                    + "differs", expected[i], actual[i], 0.01 /*tolerance*/);
221        }
222    }
223}
224