• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 package android.sample.cts;
17 
18 import android.sample.SampleDeviceActivity;
19 
20 import androidx.test.platform.app.InstrumentationRegistry;
21 import androidx.test.rule.ActivityTestRule;
22 import androidx.test.runner.AndroidJUnit4;
23 
24 import org.junit.Assert;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 
29 import com.android.compatibility.common.util.DeviceReportLog;
30 import com.android.compatibility.common.util.ResultType;
31 import com.android.compatibility.common.util.ResultUnit;
32 
33 /**
34  * A simple compatibility test which includes results in the report.
35  *
36  * This class has 3 no-op tests that create report logs and log fake metrics.
37  */
38 @RunWith(AndroidJUnit4.class)
39 public class SampleDeviceReportLogTest {
40 
41     /**
42      * Name of the report log. Test metrics will be written out to ths report. The name must match
43      * the test module name.
44      */
45     private static final String REPORT_LOG_NAME = "CtsSampleDeviceTestCases";
46 
47     /**
48      * Sample numbers used by the sample tests.
49      */
50     private static final int MULTIPLICATION_NUMBER_1 = 23;
51     private static final int MULTIPLICATION_NUMBER_2 = 97;
52     private static final int MULTIPLICATION_RESULT = 2231;
53     private static final int COUNT_START = 1;
54     private static final int COUNT_END = 1000;
55 
56     private static final String EXPECTED_PRODUCT_TAG = "expected_product";
57     private static final String ACTUAL_PRODUCT_TAG = "actual_product";
58     private static final String START_TAG = "count_start";
59     private static final String END_TAG = "actual_end";
60 
61     /**
62      * Sample test that creates and logs test metrics into a report log.
63      */
64     @Test
testMultiplication()65     public void testMultiplication() {
66         // Perform test.
67         int product = MULTIPLICATION_NUMBER_1 * MULTIPLICATION_NUMBER_2;
68         Assert.assertTrue("Multiplication result do not match", product == MULTIPLICATION_RESULT);
69 
70         // Log metrics from the test.
71         String streamName = "test_multiplication";
72         DeviceReportLog reportLog = new DeviceReportLog(REPORT_LOG_NAME, streamName);
73         reportLog.addValue(EXPECTED_PRODUCT_TAG, 1.0 * MULTIPLICATION_RESULT, ResultType.NEUTRAL,
74                 ResultUnit.NONE);
75         reportLog.addValue(ACTUAL_PRODUCT_TAG, 1.0 * product, ResultType.NEUTRAL, ResultUnit.NONE);
76         reportLog.setSummary(ACTUAL_PRODUCT_TAG, 1.0 * product, ResultType.NEUTRAL, ResultUnit.NONE);
77         reportLog.submit(InstrumentationRegistry.getInstrumentation());
78     }
79 
80     /**
81      * Sample test to check counting up.
82      */
83     @Test
testCountUp()84     public void testCountUp() {
85         String streamName = "test_count_up";
86         countHelper(1, streamName);
87     }
88 
89     /**
90      * Sample test to check counting down.
91      */
92     @Test
testCountDown()93     public void testCountDown() {
94         String streamName = "test_count_down";
95         countHelper(2, streamName);
96     }
97 
98     /**
99      * Sample test function that counts up or down based on test parameter. It creates and logs test
100      * metrics into a report log.
101      * @param testParameter {@link String} parameter passed by caller test function.
102      * @param streamName {@link String} name of the report log stream retrieved from dynamic config.
103      */
countHelper(int testParameter, String streamName)104     private void countHelper(int testParameter, String streamName) {
105         // Perform test.
106         int start;
107         int end;
108         if (testParameter == 1) {
109             start = COUNT_START;
110             end = COUNT_END;
111             for (int i = start; i <= end;) {
112                 i++;
113             }
114         } else {
115             start = COUNT_END;
116             end = COUNT_START;
117             for (int i = start; i >= end;) {
118                 i--;
119             }
120         }
121 
122         // Log metrics.
123         DeviceReportLog reportLog = new DeviceReportLog(REPORT_LOG_NAME, streamName);
124         reportLog.addValue(START_TAG, 1.0 * start, ResultType.NEUTRAL, ResultUnit.NONE);
125         reportLog.addValue(END_TAG, 1.0 * end, ResultType.NEUTRAL, ResultUnit.NONE);
126         reportLog.setSummary(END_TAG, 1.0 * end, ResultType.NEUTRAL, ResultUnit.NONE);
127         reportLog.submit(InstrumentationRegistry.getInstrumentation());
128     }
129 }
130