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