Home | History | Annotate | Download | only in settings
      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.settings;
     18 
     19 import android.annotation.NonNull;
     20 
     21 import org.junit.runners.model.InitializationError;
     22 import org.robolectric.RobolectricTestRunner;
     23 import org.robolectric.annotation.Config;
     24 import org.robolectric.manifest.AndroidManifest;
     25 import org.robolectric.res.Fs;
     26 import org.robolectric.res.ResourcePath;
     27 
     28 import java.net.MalformedURLException;
     29 import java.net.URL;
     30 import java.util.List;
     31 
     32 /**
     33  * Custom test runner. This is needed because the
     34  * default behavior for robolectric is just to grab the resource directory in the target package.
     35  * We want to override this to add several spanning different projects.
     36  */
     37 public class TvSettingsRobolectricTestRunner extends RobolectricTestRunner {
     38 
     39     public TvSettingsRobolectricTestRunner(Class<?> testClass) throws InitializationError {
     40         super(testClass);
     41     }
     42 
     43     /**
     44      * We are going to create our own custom manifest so we can add multiple resource paths to it.
     45      */
     46     @Override
     47     protected AndroidManifest getAppManifest(Config config) {
     48         try {
     49             // Using the manifest file's relative path, we can figure out the application directory.
     50             final URL appRoot = new URL("file:packages/apps/TvSettings/Settings/tests/robotests");
     51             final URL manifestPath = new URL(appRoot, "AndroidManifest.xml");
     52             final URL resDir = new URL(appRoot, "res");
     53             final URL assetsDir = new URL(appRoot, "assets");
     54 
     55             return new AndroidManifest(Fs.fromURL(manifestPath), Fs.fromURL(resDir),
     56                     Fs.fromURL(assetsDir), "com.android.tv.settings") {
     57                 @Override
     58                 public List<ResourcePath> getIncludedResourcePaths() {
     59                     final List<ResourcePath> paths = super.getIncludedResourcePaths();
     60                     paths.add(resourcePath("file:frameworks/base/core/res/res"));
     61                     paths.add(resourcePath("file:packages/apps/TvSettings/Settings/res"));
     62                     paths.add(resourcePath("file:frameworks/base/packages/SettingsLib/res"));
     63                     paths.add(resourcePath("file:frameworks/support/leanback/res"));
     64                     paths.add(resourcePath("file:frameworks/support/v7/preference/res"));
     65                     return paths;
     66                 }
     67             };
     68         } catch (MalformedURLException e) {
     69             throw new RuntimeException("TvSettingsRobolectricTestRunner failure", e);
     70         }
     71     }
     72 
     73     private static ResourcePath resourcePath(@NonNull String spec) {
     74         try {
     75             return new ResourcePath(null, Fs.fromURL(new URL(spec)), null);
     76         } catch (MalformedURLException e) {
     77             throw new RuntimeException("TvSettingsRobolectricTestRunner failure", e);
     78         }
     79     }
     80 
     81 }
     82