From 9526c2f1162ee4024dfdf93b121a95b0df0dfb53 Mon Sep 17 00:00:00 2001 From: Shraddha Basantwani Date: Thu, 7 Nov 2019 14:42:36 +0530 Subject: Enable robolectric build for tuner module Added make file for tuner/tests/robotests sources TODO: Add excluded tests Test: make RunTvTunerRoboTests Bug: 142106564 Change-Id: I144f36048dd09f19e2a0ee673e0140e08edab4ae --- tuner/tests/robotests/Android.mk | 68 ++++++++++++++++++++ .../testing/TvTunerRobolectricTestRunner.java | 74 ++++++++++++++++++++++ .../tuner/tvinput/TunerSessionWorkerExoV2Test.java | 6 +- .../tv/tuner/tvinput/TunerSessionWorkerTest.java | 4 +- 4 files changed, 147 insertions(+), 5 deletions(-) create mode 100644 tuner/tests/robotests/Android.mk create mode 100644 tuner/tests/robotests/javatests/com/android/tv/tuner/testing/TvTunerRobolectricTestRunner.java diff --git a/tuner/tests/robotests/Android.mk b/tuner/tests/robotests/Android.mk new file mode 100644 index 00000000..16af9e9c --- /dev/null +++ b/tuner/tests/robotests/Android.mk @@ -0,0 +1,68 @@ +############################################################# +# Tv Robolectric test target. # +############################################################# +LOCAL_PATH := $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE := TvTunerRoboTests +LOCAL_MODULE_CLASS := JAVA_LIBRARIES + +LOCAL_SRC_FILES := $(call all-java-files-under, javatests) + +LOCAL_JAVA_LIBRARIES := \ + Robolectric_all-target \ + mockito-robolectric-prebuilt \ + robolectric_android-all-stub \ + +LOCAL_STATIC_JAVA_LIBRARIES := \ + tv-lib-dagger + +LOCAL_STATIC_ANDROID_LIBRARIES := \ + androidx.test.core \ + tv-lib-dagger-android \ + tv-test-common \ + tv-test-common-robo \ + tv-tuner-testing \ + +LOCAL_ANNOTATION_PROCESSORS := \ + tv-lib-dagger-android-processor \ + tv-lib-dagger-compiler \ + +LOCAL_ANNOTATION_PROCESSOR_CLASSES := \ + dagger.internal.codegen.ComponentProcessor,dagger.android.processor.AndroidProcessor + +LOCAL_INSTRUMENTATION_FOR := LiveTv + +LOCAL_MODULE_TAGS := optional + +include $(BUILD_STATIC_JAVA_LIBRARY) + +############################################################# +# Tv runner target to run the previous target. # +############################################################# +include $(CLEAR_VARS) +LOCAL_MODULE := RunTvTunerRoboTests + +BASE_DIR = com/android/tv/tuner +EXCLUDE_FILES := \ + $(BASE_DIR)/dvb/DvbTunerHalTest.java \ + $(BASE_DIR)/exoplayer/tests/SampleSourceExtractorTest.java \ + +LOCAL_ROBOTEST_FILES := $(call find-files-in-subdirs,$(LOCAL_PATH)/javatests,*Test.java,.) +LOCAL_ROBOTEST_FILES := $(filter-out $(EXCLUDE_FILES),$(LOCAL_ROBOTEST_FILES)) + +LOCAL_JAVA_LIBRARIES := \ + Robolectric_all-target \ + TvTunerRoboTests \ + mockito-robolectric-prebuilt \ + robolectric_android-all-stub \ + tv-lib-truth \ + tv-test-common \ + tv-test-common-robo \ + tv-tuner-testing \ + +LOCAL_TEST_PACKAGE := LiveTv + +LOCAL_ROBOTEST_TIMEOUT := 36000 + +include external/robolectric-shadows/run_robotests.mk diff --git a/tuner/tests/robotests/javatests/com/android/tv/tuner/testing/TvTunerRobolectricTestRunner.java b/tuner/tests/robotests/javatests/com/android/tv/tuner/testing/TvTunerRobolectricTestRunner.java new file mode 100644 index 00000000..31dc25d5 --- /dev/null +++ b/tuner/tests/robotests/javatests/com/android/tv/tuner/testing/TvTunerRobolectricTestRunner.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2019 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.tv.tuner.testing; + +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; + +import java.util.List; + +/** + * Custom test runner TV tuner. 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. + * + *

Note copied from + * http://cs/android/packages/apps/Settings/tests/robotests/src/com/android/settings/testutils/SettingsRobolectricTestRunner.java + */ +public class TvTunerRobolectricTestRunner extends RobolectricTestRunner { + + /** We don't actually want to change this behavior, so we just call super. */ + public TvTunerRobolectricTestRunner(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. + */ + @Override + protected AndroidManifest getAppManifest(Config config) { + final String packageName = "com.android.tv.tuner"; + + // By adding any resources from libraries we need the AndroidManifest, we can access + // them from within the parallel universe's resource loader. + return new AndroidManifest( + Fs.fileFromPath(config.manifest()), + Fs.fileFromPath(config.resourceDir()), + Fs.fileFromPath(config.assetDir()), + packageName) { + @Override + public List getIncludedResourcePaths() { + List paths = super.getIncludedResourcePaths(); + TvTunerRobolectricTestRunner.getIncludedResourcePaths(paths); + return paths; + } + }; + } + + public static void getIncludedResourcePaths(List paths) { + paths.add( + new ResourcePath( + null, + Fs.fileFromPath("./packages/apps/TV/tuner/res"), + null)); + } +} diff --git a/tuner/tests/robotests/javatests/com/android/tv/tuner/tvinput/TunerSessionWorkerExoV2Test.java b/tuner/tests/robotests/javatests/com/android/tv/tuner/tvinput/TunerSessionWorkerExoV2Test.java index bb107f2b..24adbaa9 100644 --- a/tuner/tests/robotests/javatests/com/android/tv/tuner/tvinput/TunerSessionWorkerExoV2Test.java +++ b/tuner/tests/robotests/javatests/com/android/tv/tuner/tvinput/TunerSessionWorkerExoV2Test.java @@ -37,6 +37,7 @@ import com.android.tv.testing.constants.ConfigConstants; import com.android.tv.tuner.exoplayer.MpegTsPlayer; import com.android.tv.tuner.source.TsDataSourceManager; import com.android.tv.tuner.source.TunerTsStreamerManager; +import com.android.tv.tuner.testing.TvTunerRobolectricTestRunner; import com.android.tv.tuner.tvinput.datamanager.ChannelDataManager; import com.google.android.exoplayer.audio.AudioCapabilities; @@ -47,7 +48,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentMatchers; import org.mockito.Mockito; -import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; import org.robolectric.shadow.api.Shadow; @@ -57,8 +57,8 @@ import java.lang.reflect.Field; import javax.inject.Provider; -/** Tests for {@link TunerSessionWorker}. */ -@RunWith(RobolectricTestRunner.class) +/** Tests for {@link TunerSessionWorkerExoV2}. */ +@RunWith(TvTunerRobolectricTestRunner.class) @Config(sdk = ConfigConstants.SDK, application = TestSingletonApp.class) public class TunerSessionWorkerExoV2Test { diff --git a/tuner/tests/robotests/javatests/com/android/tv/tuner/tvinput/TunerSessionWorkerTest.java b/tuner/tests/robotests/javatests/com/android/tv/tuner/tvinput/TunerSessionWorkerTest.java index c1c90839..536af60c 100644 --- a/tuner/tests/robotests/javatests/com/android/tv/tuner/tvinput/TunerSessionWorkerTest.java +++ b/tuner/tests/robotests/javatests/com/android/tv/tuner/tvinput/TunerSessionWorkerTest.java @@ -37,6 +37,7 @@ import com.android.tv.testing.constants.ConfigConstants; import com.android.tv.tuner.exoplayer.MpegTsPlayer; import com.android.tv.tuner.source.TsDataSourceManager; import com.android.tv.tuner.source.TunerTsStreamerManager; +import com.android.tv.tuner.testing.TvTunerRobolectricTestRunner; import com.android.tv.tuner.tvinput.datamanager.ChannelDataManager; import com.google.android.exoplayer.audio.AudioCapabilities; @@ -47,7 +48,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentMatchers; import org.mockito.Mockito; -import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; import org.robolectric.shadow.api.Shadow; @@ -58,7 +58,7 @@ import java.lang.reflect.Field; import javax.inject.Provider; /** Tests for {@link TunerSessionWorker}. */ -@RunWith(RobolectricTestRunner.class) +@RunWith(TvTunerRobolectricTestRunner.class) @Config(sdk = ConfigConstants.SDK, application = TestSingletonApp.class) public class TunerSessionWorkerTest { -- cgit v1.2.3