Add initial test scripts for SettingsIntelligence
Bug: 64691432
Test: make RunSettingsIntelligenceRoboTests
Change-Id: I588ed81289b269c7ce4ad83a8ce1aff18cedf5cf
diff --git a/tests/Android.mk b/tests/Android.mk
new file mode 100644
index 0000000..fd297e3
--- /dev/null
+++ b/tests/Android.mk
@@ -0,0 +1,5 @@
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+# Include all makefiles in subdirectories
+include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/tests/robotests/Android.mk b/tests/robotests/Android.mk
new file mode 100644
index 0000000..2ebc12f
--- /dev/null
+++ b/tests/robotests/Android.mk
@@ -0,0 +1,40 @@
+#############################################
+# Turbo Robolectric test target. #
+#############################################
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+# Include the testing libraries (JUnit4 + Robolectric libs).
+LOCAL_STATIC_JAVA_LIBRARIES := \
+ platform-system-robolectric \
+ truth-prebuilt
+
+LOCAL_JAVA_LIBRARIES := \
+ junit \
+ platform-robolectric-prebuilt \
+ sdk_vcurrent
+
+LOCAL_INSTRUMENTATION_FOR := SettingsIntelligence
+LOCAL_MODULE := SettingsIntelligenceRoboTests
+
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_STATIC_JAVA_LIBRARY)
+
+#############################################################
+# Turbo runner target to run the previous target. #
+#############################################################
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := RunSettingsIntelligenceRoboTests
+
+LOCAL_SDK_VERSION := system_current
+
+LOCAL_STATIC_JAVA_LIBRARIES := \
+ SettingsIntelligenceRoboTests
+
+LOCAL_TEST_PACKAGE := SettingsIntelligence
+
+include prebuilts/misc/common/robolectric/run_robotests.mk
diff --git a/tests/robotests/src/com/android/settings/intelligence/DummyTest.java b/tests/robotests/src/com/android/settings/intelligence/DummyTest.java
new file mode 100644
index 0000000..14cc0d5
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/intelligence/DummyTest.java
@@ -0,0 +1,31 @@
+/*
+ * 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 com.android.settings.intelligence;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.annotation.Config;
+
+@RunWith(SettingsIntelligenceRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class DummyTest {
+
+ @Test
+ public void testRun() {
+
+ }
+}
diff --git a/tests/robotests/src/com/android/settings/intelligence/SettingsIntelligenceRobolectricTestRunner.java b/tests/robotests/src/com/android/settings/intelligence/SettingsIntelligenceRobolectricTestRunner.java
new file mode 100644
index 0000000..e7f1d68
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/intelligence/SettingsIntelligenceRobolectricTestRunner.java
@@ -0,0 +1,83 @@
+/*
+ * 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 com.android.settings.intelligence;
+
+import java.util.List;
+import org.junit.runners.model.InitializationError;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.annotation.Config;
+import org.robolectric.manifest.AndroidManifest;
+import org.robolectric.res.Fs;
+import org.robolectric.res.ResourcePath;
+
+/**
+ * Custom test runner for dealing with resources from multiple sources. This is needed because the
+ * default behavior for robolectric is just to grab the resource directory in the target package. We
+ * want to override this to add several spanning different projects/libraries.
+ */
+public class SettingsIntelligenceRobolectricTestRunner extends RobolectricTestRunner {
+
+ /** We don't actually want to change this behavior, so we just call super. */
+ public SettingsIntelligenceRobolectricTestRunner(Class<?> testClass) throws InitializationError {
+ super(testClass);
+ }
+
+ /**
+ * We are going to create our own custom manifest so that we can add multiple resource paths to
+ * it. This lets us access resources in different projects in our tests as well as use a test
+ * res directory.
+ */
+ @Override
+ protected AndroidManifest getAppManifest(Config config) {
+ // Using the manifest file's relative path, we can figure out the application directory.
+ final String appRoot = "vendor/unbundled_google/packages/Turbo";
+ final String manifestPath = appRoot + "/AndroidManifest.xml";
+ final String resDir = appRoot + "tests/robotests/res";
+ final String assetsDir = appRoot + config.assetDir();
+
+ // By adding any resources from libraries we need to the AndroidManifest, we can access
+ // them from within the parallel universe's resource loader.
+ final AndroidManifest manifest =
+ new AndroidManifest(
+ Fs.fileFromPath(manifestPath),
+ Fs.fileFromPath(resDir),
+ Fs.fileFromPath(assetsDir)) {
+ @Override
+ public List<ResourcePath> getIncludedResourcePaths() {
+ List<ResourcePath> paths = super.getIncludedResourcePaths();
+ paths.add(
+ new ResourcePath(
+ getPackageName(),
+ Fs.fileFromPath(
+ "./vendor/unbundled_google/packages/"
+ + "Turbo/tests/robotests/res"),
+ null));
+ paths.add(
+ new ResourcePath(
+ getPackageName(),
+ Fs.fileFromPath(
+ "./vendor/unbundled_google/packages/Turbo/res"),
+ null));
+ return paths;
+ }
+ };
+
+ // Set the package name to the renamed one
+ manifest.setPackageName("com.google.android.apps.turbo");
+ return manifest;
+ }
+}
diff --git a/tests/robotests/src/com/android/settings/intelligence/TestConfig.java b/tests/robotests/src/com/android/settings/intelligence/TestConfig.java
new file mode 100644
index 0000000..0e182bb
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/intelligence/TestConfig.java
@@ -0,0 +1,26 @@
+/*
+ * 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 com.android.settings.intelligence;
+
+/**
+ * Constants for Robolectric config.
+ */
+public class TestConfig {
+ public static final int SDK_VERSION = 23;
+ public static final String MANIFEST_PATH =
+ "packages/apps/SettingsIntelligence/Turbo/AndroidManifest.xml";
+}