aboutsummaryrefslogtreecommitdiff
path: root/android/WALT/app/src/test/java/org/chromium/latency/walt
diff options
context:
space:
mode:
authorAndrew Lehmer <alehmer@google.com>2017-04-26 14:58:59 -0700
committerAndrew Lehmer <alehmer@google.com>2017-04-26 14:58:59 -0700
commite76dcf96b0c451e46cddfa695de8feeb92533937 (patch)
treeed9a45d409f988f517e6c3f3a685cbf81ac45a5a /android/WALT/app/src/test/java/org/chromium/latency/walt
parentbcf013dda8ffac9fd76937be6441b44bb9f3586f (diff)
downloadwalt-e76dcf96b0c451e46cddfa695de8feeb92533937.tar.gz
Import google/walt
Cloned from https://github.com/google/walt.git without modification. Bug: 36896528 Test: N/A
Diffstat (limited to 'android/WALT/app/src/test/java/org/chromium/latency/walt')
-rw-r--r--android/WALT/app/src/test/java/org/chromium/latency/walt/HistogramChartTest.java81
-rw-r--r--android/WALT/app/src/test/java/org/chromium/latency/walt/TraceLoggerTest.java46
-rw-r--r--android/WALT/app/src/test/java/org/chromium/latency/walt/UtilsTest.java162
3 files changed, 289 insertions, 0 deletions
diff --git a/android/WALT/app/src/test/java/org/chromium/latency/walt/HistogramChartTest.java b/android/WALT/app/src/test/java/org/chromium/latency/walt/HistogramChartTest.java
new file mode 100644
index 0000000..8365719
--- /dev/null
+++ b/android/WALT/app/src/test/java/org/chromium/latency/walt/HistogramChartTest.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.chromium.latency.walt;
+
+import com.github.mikephil.charting.data.BarData;
+import com.github.mikephil.charting.data.BarDataSet;
+import com.github.mikephil.charting.data.BarEntry;
+import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import java.util.ArrayList;
+
+import static junit.framework.Assert.assertEquals;
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Mockito.when;
+import static org.powermock.api.mockito.PowerMockito.mockStatic;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(android.graphics.Color.class)
+public class HistogramChartTest {
+
+ private BarData barData;
+ private HistogramChart.HistogramData data;
+
+ @Before
+ public void setUp() {
+ mockStatic(android.graphics.Color.class);
+ when(android.graphics.Color.rgb(anyInt(), anyInt(), anyInt())).thenReturn(0);
+ barData = new BarData();
+ barData.setBarWidth((1f - HistogramChart.GROUP_SPACE)/1);
+ barData.addDataSet(new BarDataSet(new ArrayList<BarEntry>(), "SomeLabel"));
+ data = new HistogramChart.HistogramData(1, 5f);
+ data.addEntry(barData, 0, 12);
+ data.addEntry(barData, 0, 14);
+ data.addEntry(barData, 0, 16);
+ data.addEntry(barData, 0, 21);
+ }
+
+ @Test
+ public void testBinHeights() {
+ final IBarDataSet barDataSet = barData.getDataSetByIndex(0);
+ assertEquals(3, barDataSet.getEntryCount());
+ assertEquals(2d, barDataSet.getEntryForIndex(0).getY(), 0.000001);
+ assertEquals(1d, barDataSet.getEntryForIndex(1).getY(), 0.000001);
+ assertEquals(1d, barDataSet.getEntryForIndex(2).getY(), 0.000001);
+ }
+
+ @Test
+ public void testBinXPositions() {
+ final IBarDataSet barDataSet = barData.getDataSetByIndex(0);
+ assertEquals(3, barDataSet.getEntryCount());
+ assertEquals(0d + 0.05d + 0.45d, barDataSet.getEntryForIndex(0).getX(), 0.000001);
+ assertEquals(1d + 0.05d + 0.45d, barDataSet.getEntryForIndex(1).getX(), 0.000001);
+ assertEquals(2d + 0.05d + 0.45d, barDataSet.getEntryForIndex(2).getX(), 0.000001);
+ }
+
+ @Test
+ public void testDisplayValue() {
+ assertEquals(10d, data.getMinBin(), 0.000001);
+ assertEquals(15d, data.getDisplayValue(1), 0.000001);
+ }
+}
diff --git a/android/WALT/app/src/test/java/org/chromium/latency/walt/TraceLoggerTest.java b/android/WALT/app/src/test/java/org/chromium/latency/walt/TraceLoggerTest.java
new file mode 100644
index 0000000..ed617cb
--- /dev/null
+++ b/android/WALT/app/src/test/java/org/chromium/latency/walt/TraceLoggerTest.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.chromium.latency.walt;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import static junit.framework.Assert.assertTrue;
+import static org.mockito.Mockito.when;
+import static org.powermock.api.mockito.PowerMockito.mockStatic;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(android.os.Process.class)
+public class TraceLoggerTest {
+
+ @Test
+ public void testLogText() {
+ final TraceLogger traceLogger = TraceLogger.getInstance();
+ traceLogger.log(30012345, 30045678, "SomeTitle", "Some description here");
+ traceLogger.log(40012345, 40045678, "AnotherTitle", "Another description here");
+ mockStatic(android.os.Process.class);
+ when(android.os.Process.myPid()).thenReturn(42);
+ String expected =
+ "WALTThread-[0-9]+ \\(42\\) \\[[0-9]+] .{4} 30\\.012345: tracing_mark_write: B\\|42\\|SomeTitle\\|description=Some description here\\|WALT\n" +
+ "WALTThread-[0-9]+ \\(42\\) \\[[0-9]+] .{4} 30\\.045678: tracing_mark_write: E\\|42\\|SomeTitle\\|\\|WALT\n" +
+ "WALTThread-[0-9]+ \\(42\\) \\[[0-9]+] .{4} 40\\.012345: tracing_mark_write: B\\|42\\|AnotherTitle\\|description=Another description here\\|WALT\n" +
+ "WALTThread-[0-9]+ \\(42\\) \\[[0-9]+] .{4} 40\\.045678: tracing_mark_write: E\\|42\\|AnotherTitle\\|\\|WALT\n";
+ assertTrue(traceLogger.getLogText().matches(expected));
+ }
+}
diff --git a/android/WALT/app/src/test/java/org/chromium/latency/walt/UtilsTest.java b/android/WALT/app/src/test/java/org/chromium/latency/walt/UtilsTest.java
new file mode 100644
index 0000000..bf77e05
--- /dev/null
+++ b/android/WALT/app/src/test/java/org/chromium/latency/walt/UtilsTest.java
@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.chromium.latency.walt;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Random;
+
+import static java.lang.Double.NaN;
+import static junit.framework.Assert.assertEquals;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+public class UtilsTest {
+
+ @Test
+ public void testMedian_singleNumber() {
+ ArrayList<Double> arr = new ArrayList<>();
+ arr.add(0d);
+ assertThat(Utils.median(arr), is(0d));
+ }
+
+ @Test
+ public void testMedian_evenSize() {
+ ArrayList<Double> arr = new ArrayList<>();
+ arr.add(1d); arr.add(2d); arr.add(3d); arr.add(4d);
+ assertThat(Utils.median(arr), is(2.5d));
+ }
+
+ @Test
+ public void testMedian_oddSize() {
+ ArrayList<Double> arr = new ArrayList<>();
+ arr.add(1d); arr.add(2d); arr.add(3d); arr.add(4d); arr.add(5d);
+ assertThat(Utils.median(arr), is(3d));
+ }
+
+ @Test
+ public void testMean() {
+ assertThat(Utils.mean(new double[]{-1,1,2,3}), is(1.25d));
+ }
+
+ @Test
+ public void testMean_singleNumber() {
+ assertThat(Utils.mean(new double[]{0}), is(0d));
+ }
+
+ @Test
+ public void testMean_empty() {
+ assertThat(Utils.mean(new double[]{}), is(NaN));
+ }
+
+ @Test
+ public void testMean_repeatedNumbers() {
+ assertThat(Utils.mean(new double[]{5,5,5,5}), is(5d));
+ }
+
+ @Test
+ public void testInterp() {
+ assertThat(Utils.interp(new double[]{5,6,16,17}, new double[]{0, 10, 12, 18},
+ new double[]{35, 50, 75, 93}), is(new double[]{42.5, 44, 87, 90}));
+ }
+
+ @Test
+ public void testInterp_singleNumber() {
+ assertThat(Utils.interp(new double[]{5}, new double[]{0, 10},
+ new double[]{35, 50}), is(new double[]{42.5}));
+ }
+
+ @Test
+ public void testInterp_twoNumbers() {
+ assertThat(Utils.interp(new double[]{0}, new double[]{0, 10},
+ new double[]{35, 50}), is(new double[]{35}));
+ }
+
+ @Test
+ public void testInterp_numberContained() {
+ assertThat(Utils.interp(new double[]{5, 10}, new double[]{0, 5, 10},
+ new double[]{35, 19, 50}), is(new double[]{19, 50}));
+ }
+
+ @Test
+ public void testStdev() {
+ assertThat(Utils.stdev(new double[]{10,12,14,18}), is(Math.sqrt(8.75)));
+ }
+
+ @Test
+ public void testStdev_empty() {
+ assertThat(Utils.stdev(new double[]{}), is(NaN));
+ }
+
+ @Test
+ public void testStdev_singleNumber() {
+ assertThat(Utils.stdev(new double[]{42}), is(0d));
+ }
+
+ @Test
+ public void testStdev_manyNumbers() {
+ assertThat(Utils.stdev(new double[]{-1,0,1}), is(Math.sqrt(2d/3d)));
+ }
+
+ @Test
+ public void testExtract() {
+ assertThat(Utils.extract(new int[]{1, 2, 2, 1, 2, 2, 1, 2, 2}, 1,
+ new double[]{1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}),
+ is(new double[]{1.5, 4.5, 7.5}));
+ }
+
+ @Test
+ public void testExtract_empty() {
+ assertThat(Utils.extract(new int[]{}, 1, new double[]{}), is(new double[]{}));
+ }
+
+ @Test
+ public void testArgmin() {
+ assertThat(Utils.argmin(new double[]{5, 2, 1, -10, -20, 5, 19, 100}), is(4));
+ }
+
+ @Test
+ public void testArgmin_empty() {
+ assertThat(Utils.argmin(new double[]{}), is(0));
+ }
+
+ @Test
+ public void testFindBestShift() {
+ Random rand = new Random(42);
+ double latency = 12.34;
+ double[] touchTimes = new double[4000];
+ for (int i = 0; i < touchTimes.length; i++) {
+ // touch events every millisecond with some jitter
+ touchTimes[i] = i + rand.nextDouble()*0.2 - 0.1;
+ }
+ double[] touchY = new double[touchTimes.length];
+ for (int i = 0; i < touchY.length; i++) {
+ // sine wave will oscillate 1 time
+ touchY[i] = 1000*Math.cos((touchTimes[i] - latency) * Math.PI/500) + rand.nextDouble()*0.02 - 0.01;
+ }
+ double[] laserTimes = new double[4];
+ int i = 0;
+ for (int root = 0; root < 1000; root+=1000) {
+ laserTimes[i++] = root + 250 - 10;
+ laserTimes[i++] = root + 250 + 10;
+ laserTimes[i++] = root + 750 - 10;
+ laserTimes[i++] = root + 750 + 10;
+ }
+ assertEquals(latency, Utils.findBestShift(laserTimes, touchTimes, touchY), 1e-6);
+ }
+}