upload android base code part4

This commit is contained in:
August 2018-08-08 17:00:29 +08:00
parent b9e30e05b1
commit 78ea2404cd
23455 changed files with 5250148 additions and 0 deletions

View file

@ -0,0 +1,42 @@
# Copyright (C) 2014 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.
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
# Don't include this package in any target
LOCAL_MODULE_TAGS := tests
# When built, explicitly put it in the data partition.
LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
LOCAL_DEX_PREOPT := false
LOCAL_PROGUARD_ENABLED := disabled
LOCAL_STATIC_JAVA_LIBRARIES := \
compatibility-device-util \
android-support-test \
legacy-android-test
LOCAL_SRC_FILES := $(call all-java-files-under, src)
# Tag this module as a cts test artifact
LOCAL_COMPATIBILITY_SUITE := cts vts general-tests
LOCAL_PACKAGE_NAME := CtsSampleDeviceTestCases
LOCAL_SDK_VERSION := current
include $(BUILD_PACKAGE)

View file

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
* Copyright (C) 2014 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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.sample.cts">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application>
<uses-library android:name="android.test.runner" />
<activity android:name="android.sample.SampleDeviceActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!-- self-instrumenting test package. -->
<instrumentation
android:name="android.support.test.runner.AndroidJUnitRunner"
android:label="CTS sample tests"
android:targetPackage="android.sample.cts" >
</instrumentation>
</manifest>

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2015 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.
-->
<configuration description="Config for CTS Sample test cases">
<option name="config-descriptor:metadata" key="component" value="misc" />
<target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
<option name="cleanup-apks" value="true" />
<option name="test-file-name" value="CtsSampleDeviceTestCases.apk" />
</target_preparer>
<test class="com.android.tradefed.testtype.AndroidJUnitTest" >
<option name="package" value="android.sample.cts" />
</test>
</configuration>

View file

@ -0,0 +1,78 @@
/*
* Copyright (C) 2014 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 android.sample;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import java.lang.Override;
/**
* A simple activity for using the SharedPreferences API.
*/
public class SampleDeviceActivity extends Activity {
private SharedPreferences mPreferences;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Get a reference to this context's shared preference.
mPreferences = getPreferences(Context.MODE_PRIVATE);
}
/**
* Saves the given key value pair to the shared preferences.
*
* @param key
* @param value
*/
public void savePreference(String key, String value) {
// Get an editor to modify the preferences.
Editor editor = mPreferences.edit();
// Insert the key value pair.
editor.putString(key, value);
// Commit the changes - important.
editor.commit();
}
/**
* Looks up the given key in the shared preferences.
*
* @param key
* @return
*/
public String getPreference(String key) {
return mPreferences.getString(key, null);
}
/**
* Deletes all entries in the shared preferences.
*/
public void clearPreferences() {
// Get an editor to modify the preferences.
Editor editor = mPreferences.edit();
// Delete all entries.
editor.clear();
// Commit the changes - important.
editor.commit();
}
}

View file

@ -0,0 +1,125 @@
/*
* Copyright (C) 2014 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 android.sample.cts;
import android.sample.SampleDeviceActivity;
import android.test.ActivityInstrumentationTestCase2;
import com.android.compatibility.common.util.DeviceReportLog;
import com.android.compatibility.common.util.ResultType;
import com.android.compatibility.common.util.ResultUnit;
/**
* A simple compatibility test which includes results in the report.
*
* This class has 3 dummy tests that create report logs and log dummy metrics.
*/
public class SampleDeviceReportLogTest
extends ActivityInstrumentationTestCase2<SampleDeviceActivity> {
/**
* Name of the report log. Test metrics will be written out to ths report. The name must match
* the test module name.
*/
private static final String REPORT_LOG_NAME = "CtsSampleDeviceTestCases";
/**
* Sample numbers used by the sample tests.
*/
private static final int MULTIPLICATION_NUMBER_1 = 23;
private static final int MULTIPLICATION_NUMBER_2 = 97;
private static final int MULTIPLICATION_RESULT = 2231;
private static final int COUNT_START = 1;
private static final int COUNT_END = 1000;
private static final String EXPECTED_PRODUCT_TAG = "expected_product";
private static final String ACTUAL_PRODUCT_TAG = "actual_product";
private static final String START_TAG = "count_start";
private static final String END_TAG = "actual_end";
/**
* Constructor which passes the class of the activity to be instrumented.
*/
public SampleDeviceReportLogTest() {
super(SampleDeviceActivity.class);
}
/**
* Sample test that creates and logs test metrics into a report log.
*/
public void testMultiplication() {
// Perform test.
int product = MULTIPLICATION_NUMBER_1 * MULTIPLICATION_NUMBER_2;
assertTrue("Multiplication result do not match", product == MULTIPLICATION_RESULT);
// Log metrics from the test.
String streamName = "test_multiplication";
DeviceReportLog reportLog = new DeviceReportLog(REPORT_LOG_NAME, streamName);
reportLog.addValue(EXPECTED_PRODUCT_TAG, 1.0 * MULTIPLICATION_RESULT, ResultType.NEUTRAL,
ResultUnit.NONE);
reportLog.addValue(ACTUAL_PRODUCT_TAG, 1.0 * product, ResultType.NEUTRAL, ResultUnit.NONE);
reportLog.setSummary(ACTUAL_PRODUCT_TAG, 1.0 * product, ResultType.NEUTRAL, ResultUnit.NONE);
reportLog.submit(getInstrumentation());
}
/**
* Sample test to check counting up.
*/
public void testCountUp() {
String streamName = "test_count_up";
countHelper(1, streamName);
}
/**
* Sample test to check counting down.
*/
public void testCountDown() {
String streamName = "test_count_down";
countHelper(2, streamName);
}
/**
* Sample test function that counts up or down based on test parameter. It creates and logs test
* metrics into a report log.
* @param testParameter {@link String} parameter passed by caller test function.
* @param streamName {@link String} name of the report log stream retrieved from dynamic config.
*/
private void countHelper(int testParameter, String streamName) {
// Perform test.
int start;
int end;
if (testParameter == 1) {
start = COUNT_START;
end = COUNT_END;
for (int i = start; i <= end;) {
i++;
}
} else {
start = COUNT_END;
end = COUNT_START;
for (int i = start; i >= end;) {
i--;
}
}
// Log metrics.
DeviceReportLog reportLog = new DeviceReportLog(REPORT_LOG_NAME, streamName);
reportLog.addValue(START_TAG, 1.0 * start, ResultType.NEUTRAL, ResultUnit.NONE);
reportLog.addValue(END_TAG, 1.0 * end, ResultType.NEUTRAL, ResultUnit.NONE);
reportLog.setSummary(END_TAG, 1.0 * end, ResultType.NEUTRAL, ResultUnit.NONE);
reportLog.submit(getInstrumentation());
}
}

View file

@ -0,0 +1,119 @@
/*
* Copyright (C) 2014 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 android.sample.cts;
import android.sample.SampleDeviceActivity;
import android.test.ActivityInstrumentationTestCase2;
import com.android.compatibility.common.util.DeviceReportLog;
import com.android.compatibility.common.util.MeasureRun;
import com.android.compatibility.common.util.MeasureTime;
import com.android.compatibility.common.util.ResultType;
import com.android.compatibility.common.util.ResultUnit;
import com.android.compatibility.common.util.Stat;
import java.util.Arrays;
import java.util.Random;
/**
* A simple compatibility test which includes results in the report.
*
* This test measures the time taken to run a workload and adds in the report.
*/
public class SampleDeviceResultTest extends ActivityInstrumentationTestCase2<SampleDeviceActivity> {
/**
* Name of the report log to store test metrics.
*/
private static final String REPORT_LOG_NAME = "CtsSampleDeviceTestCases";
/**
* The number of times to repeat the test.
*/
private static final int REPEAT = 5;
/**
* A {@link Random} to generate random integers to test the sort.
*/
private static final Random random = new Random(12345);
/**
* Constructor which passes the class of the activity to be instrumented.
*/
public SampleDeviceResultTest() {
super(SampleDeviceActivity.class);
}
/**
* Measures the time taken to sort an array.
*/
public void testSort() throws Exception {
// MeasureTime runs the workload N times and records the time taken by each run.
double[] result = MeasureTime.measure(REPEAT, new MeasureRun() {
/**
* The size of the array to sort.
*/
private static final int ARRAY_SIZE = 100000;
private int[] array;
@Override
public void prepare(int i) throws Exception {
array = createArray(ARRAY_SIZE);
}
@Override
public void run(int i) throws Exception {
Arrays.sort(array);
assertTrue("Array not sorted", isSorted(array));
}
});
// Compute the stats.
Stat.StatResult stat = Stat.getStat(result);
// Create a new report to hold the metrics.
String streamName = "test_sort";
DeviceReportLog reportLog = new DeviceReportLog(REPORT_LOG_NAME, streamName);
// Add the results to the report.
reportLog.addValues("times", result, ResultType.LOWER_BETTER, ResultUnit.MS);
reportLog.addValue("min", stat.mMin, ResultType.LOWER_BETTER, ResultUnit.MS);
reportLog.addValue("max", stat.mMax, ResultType.LOWER_BETTER, ResultUnit.MS);
// Set a summary.
reportLog.setSummary("average", stat.mAverage, ResultType.LOWER_BETTER, ResultUnit.MS);
// Submit the report to the given instrumentation.
reportLog.submit(getInstrumentation());
}
/**
* Creates an array filled with random numbers of the given size.
*/
private static int[] createArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = random.nextInt();
}
return array;
}
/**
* Tests an array is sorted.
*/
private static boolean isSorted(int[] array) {
int len = array.length;
for (int i = 0, j = 1; j < len; i++, j++) {
if (array[i] > array[j]) {
return false;
}
}
return true;
}
}

View file

@ -0,0 +1,76 @@
/*
* Copyright (C) 2014 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 android.sample.cts;
import android.sample.SampleDeviceActivity;
import android.test.ActivityInstrumentationTestCase2;
/**
* A simple compatibility test which tests the SharedPreferences API.
*
* This test uses {@link android.test.ActivityInstrumentationTestCase2} to instrument the
* {@link android.sample.SampleDeviceActivity}.
*/
public class SampleDeviceTest extends ActivityInstrumentationTestCase2<SampleDeviceActivity> {
private static final String KEY = "foo";
private static final String VALUE = "bar";
/**
* A reference to the activity whose shared preferences are being tested.
*/
private SampleDeviceActivity mActivity;
public SampleDeviceTest() {
super(SampleDeviceActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
// Start the activity and get a reference to it.
mActivity = getActivity();
// Wait for the UI Thread to become idle.
getInstrumentation().waitForIdleSync();
}
@Override
protected void tearDown() throws Exception {
// Scrub the activity so it can be freed. The next time the setUp will create a new activity
// rather than reusing the old one.
mActivity = null;
super.tearDown();
}
/**
* Tests the SharedPreferences API.
*
* This inserts the key value pair and assert they can be retrieved. Then it clears the
* preferences and asserts they can no longer be retrieved.
*
* @throws Exception
*/
public void testSharedPreferences() throws Exception {
// Save the key value pair to the preferences and assert they were saved.
mActivity.savePreference(KEY, VALUE);
assertEquals("Preferences were not saved", VALUE, mActivity.getPreference(KEY));
// Clear the shared preferences and assert the data was removed.
mActivity.clearPreferences();
assertNull("Preferences were not cleared", mActivity.getPreference(KEY));
}
}

View file

@ -0,0 +1,68 @@
/*
* Copyright (C) 2015 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 android.sample.cts;
import org.junit.Assert;
import org.junit.runner.RunWith;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import android.app.Activity;
import android.sample.SampleDeviceActivity;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
/**
* A simple compatibility test which tests the SharedPreferences API.
*
* This test uses {@link ActivityTestRule} to instrument the
* {@link android.sample.SampleDeviceActivity}.
*/
@RunWith(AndroidJUnit4.class)
public class SampleJUnit4DeviceTest {
private static final String KEY = "foo";
private static final String VALUE = "bar";
@Rule
public ActivityTestRule<SampleDeviceActivity> mActivityRule =
new ActivityTestRule(SampleDeviceActivity.class);
/**
* This inserts the key value pair and assert they can be retrieved. Then it clears the
* preferences and asserts they can no longer be retrieved.
*
* @throws Exception
*/
@Test
public void shouldSaveSharedPreferences() throws Exception {
// Save the key value pair to the preferences and assert they were saved.
mActivityRule.getActivity().savePreference(KEY, VALUE);
Assert.assertEquals("Preferences were not saved", VALUE,
mActivityRule.getActivity().getPreference(KEY));
// Clear the shared preferences and assert the data was removed.
mActivityRule.getActivity().clearPreferences();
Assert.assertNull("Preferences were not cleared",
mActivityRule.getActivity().getPreference(KEY));
}
}