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      */
getAppManifest(Config config)47     protected AndroidManifest getAppManifest(Config config) {
48         final String packageName = "com.android.tv";
49 
50         // By adding any resources from libraries we need the AndroidManifest, we can access
51         // them from within the parallel universe's resource loader.
52         return new AndroidManifest(
53                 Fs.fileFromPath(config.manifest()),
54                 Fs.fileFromPath(config.resourceDir()),
55                 Fs.fileFromPath(config.assetDir()),
56                 packageName) {
57             @Override
58             public List<ResourcePath> getIncludedResourcePaths() {
59                 List<ResourcePath> paths = super.getIncludedResourcePaths();
60                 TvRobolectricTestRunner.getIncludedResourcePaths(paths);
61                 return paths;
62             }
63         };
64     }
65 
66     public static void getIncludedResourcePaths(List<ResourcePath> paths) {
67         paths.add(
68                 new ResourcePath(
69                         null,
70                         Fs.fileFromPath("./packages/apps/TV/res"),
71                         null));
72         paths.add(
73                 new ResourcePath(
74                         null,
75                         Fs.fileFromPath("./packages/apps/TV/common/res"),
76                         null));
77         paths.add(
78                 new ResourcePath(
79                         null,
80                         Fs.fileFromPath("./packages/apps/TV/material_res"),
81                         null));
82 	paths.add(
83                 new ResourcePath(
84                         null,
85                         Fs.fileFromPath("./prebuilts/sdk/current/support/v17/leanback/res"),
86                         null));
87     }
88 }
89