1 /* 2 * Copyright (C) 2019 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.platform.test.rule; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.mockito.Mockito.verify; 21 22 import android.app.Instrumentation; 23 import android.os.Bundle; 24 25 import org.junit.Test; 26 import org.junit.runner.Description; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.JUnit4; 29 import org.junit.runners.model.Statement; 30 import org.mockito.Mockito; 31 32 @RunWith(JUnit4.class) 33 public class StopwatchRuleTest { 34 35 private static final int SLEEP_TIME_MS = 200; 36 private static final int TIME_DELTA_MS = 20; 37 38 @Test testMeasurementIsCorrect()39 public void testMeasurementIsCorrect() throws Throwable { 40 StopwatchRule rule = new StopwatchRule(); 41 rule.apply( 42 new Statement() { 43 @Override 44 public void evaluate() throws Throwable { 45 Thread.sleep(SLEEP_TIME_MS); 46 } 47 }, 48 Description.createTestDescription("clzz", "method")) 49 .evaluate(); 50 51 Bundle metric = rule.getMetric(); 52 String metricKey = String.format(StopwatchRule.METRIC_FORMAT, "clzz", "method"); 53 long value = metric.getLong(metricKey); 54 // Assert if StopwatchRule correctly measures the test time. 55 assertEquals(SLEEP_TIME_MS, value, TIME_DELTA_MS); 56 } 57 58 @Test testMetricSendToInstr()59 public void testMetricSendToInstr() throws Throwable { 60 StopwatchRule rule = new StopwatchRule(); 61 Instrumentation instr = Mockito.mock(Instrumentation.class); 62 rule.setInstrumentation(instr); 63 rule.apply( 64 new Statement() { 65 @Override 66 public void evaluate() throws Throwable {} 67 }, 68 Description.EMPTY) 69 .evaluate(); 70 verify(instr).sendStatus(StopwatchRule.INST_STATUS_IN_PROGRESS, rule.getMetric()); 71 } 72 73 @Test testMetricKeyWithoutAppend()74 public void testMetricKeyWithoutAppend() throws Throwable { 75 Bundle args = new Bundle(); 76 args.putString(StopwatchRule.APPEND_TEST_NAME, "false"); 77 TestableStopwatchRule rule = new TestableStopwatchRule(args); 78 rule.apply( 79 new Statement() { 80 @Override 81 public void evaluate() throws Throwable { 82 Thread.sleep(SLEEP_TIME_MS); 83 } 84 }, 85 Description.createTestDescription("clzz", "method")) 86 .evaluate(); 87 88 Bundle metric = rule.getMetric(); 89 long value = metric.getLong(StopwatchRule.METRIC_BASE); 90 // Assert if StopwatchRule correctly measures the test time. 91 assertEquals(SLEEP_TIME_MS, value, TIME_DELTA_MS); 92 } 93 94 @Test testMetricKeyWithAppend()95 public void testMetricKeyWithAppend() throws Throwable { 96 Bundle args = new Bundle(); 97 args.putString(StopwatchRule.APPEND_TEST_NAME, "true"); 98 TestableStopwatchRule rule = new TestableStopwatchRule(args); 99 rule.apply( 100 new Statement() { 101 @Override 102 public void evaluate() throws Throwable { 103 Thread.sleep(SLEEP_TIME_MS); 104 } 105 }, 106 Description.createTestDescription("clzz", "method")) 107 .evaluate(); 108 109 Bundle metric = rule.getMetric(); 110 String metricKey = String.format(StopwatchRule.METRIC_FORMAT, "clzz", "method"); 111 long value = metric.getLong(metricKey); 112 // Assert if StopwatchRule correctly measures the test time. 113 assertEquals(SLEEP_TIME_MS, value, TIME_DELTA_MS); 114 } 115 116 private static class TestableStopwatchRule extends StopwatchRule { 117 private Bundle mBundle; 118 TestableStopwatchRule(Bundle bundle)119 public TestableStopwatchRule(Bundle bundle) { 120 mBundle = bundle; 121 } 122 123 @Override getArguments()124 protected Bundle getArguments() { 125 return mBundle; 126 } 127 } 128 } 129