1 /* 2 * Copyright (C) 2017 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 17 package com.android.tv.testing; 18 19 import org.junit.runners.model.InitializationError; 20 import org.robolectric.RobolectricTestRunner; 21 import org.robolectric.annotation.Config; 22 import org.robolectric.manifest.AndroidManifest; 23 import org.robolectric.res.Fs; 24 import org.robolectric.res.ResourcePath; 25 26 import java.util.List; 27 28 /** 29 * Custom test runner TV. This is needed because the default behavior for robolectric is just to 30 * grab the resource directory in the target package. We want to override this to add several 31 * spanning different projects. 32 * 33 * <p><b>Note</b> copied from 34 * http://cs/android/packages/apps/Settings/tests/robotests/src/com/android/settings/testutils/SettingsRobolectricTestRunner.java 35 */ 36 public class TvRobolectricTestRunner extends RobolectricTestRunner { 37 38 /** We don't actually want to change this behavior, so we just call super. */ TvRobolectricTestRunner(Class<?> testClass)39 public TvRobolectricTestRunner(Class<?> testClass) throws InitializationError { 40 super(testClass); 41 } 42 43 /** 44 * We are going to create our own custom manifest so that we can add multiple resource paths to 45 * it. This lets us access resources in both Settings and SettingsLib in our tests. 46 */ 47 @Override getAppManifest(Config config)48 protected AndroidManifest getAppManifest(Config config) { 49 final String packageName = "com.android.tv"; 50 51 // By adding any resources from libraries we need the AndroidManifest, we can access 52 // them from within the parallel universe's resource loader. 53 return new AndroidManifest( 54 Fs.fileFromPath(config.manifest()), 55 Fs.fileFromPath(config.resourceDir()), 56 Fs.fileFromPath(config.assetDir()), 57 packageName) { 58 @Override 59 public List<ResourcePath> getIncludedResourcePaths() { 60 List<ResourcePath> paths = super.getIncludedResourcePaths(); 61 TvRobolectricTestRunner.getIncludedResourcePaths(paths); 62 return paths; 63 } 64 }; 65 } 66 67 public static void getIncludedResourcePaths(List<ResourcePath> paths) { 68 paths.add( 69 new ResourcePath( 70 null, 71 Fs.fileFromPath("./packages/apps/TV/res"), 72 null)); 73 paths.add( 74 new ResourcePath( 75 null, 76 Fs.fileFromPath("./packages/apps/TV/common/res"), 77 null)); 78 paths.add( 79 new ResourcePath( 80 null, 81 Fs.fileFromPath("./packages/apps/TV/material_res"), 82 null)); 83 paths.add( 84 new ResourcePath( 85 null, 86 Fs.fileFromPath("./prebuilts/sdk/current/support/v17/leanback/res"), 87 null)); 88 } 89 } 90