Home | History | Annotate | Download | only in testing
      1 /*
      2  * Copyright (C) 2016 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 package com.android.storagemanager.testing;
     17 
     18 import java.util.List;
     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 /**
     27  * Custom test runner for Robolectric UI testing. This adds additional resources needed to run.
     28  */
     29 public class StorageManagerRobolectricTestRunner extends RobolectricTestRunner {
     30 
     31     /**
     32      * We don't actually want to change this behavior, so we just call super.
     33      */
     34     public StorageManagerRobolectricTestRunner(Class<?> testClass) throws InitializationError {
     35         super(testClass);
     36     }
     37 
     38     /**
     39      * We are going to create our own custom manifest so that we can add multiple resource
     40      * paths to it. This lets us access resources in both StorageManager and SettingsLib in our
     41      * tests.
     42      */
     43     @Override
     44     protected AndroidManifest getAppManifest(Config config) {
     45         // Using the manifest file's relative path, we can figure out the application directory.
     46         final String appRoot = "packages/apps/StorageManager";
     47         final String manifestPath = appRoot + "/AndroidManifest.xml";
     48         final String resDir = appRoot + "/res";
     49         final String assetsDir = appRoot + "/assets";
     50 
     51         // By adding any resources from libraries we need to the AndroidManifest, we can access
     52         // them from within the parallel universe's resource loader.
     53         return new AndroidManifest(Fs.fileFromPath(manifestPath), Fs.fileFromPath(resDir),
     54             Fs.fileFromPath(assetsDir), "com.android.storagemanager") {
     55             @Override
     56             public List<ResourcePath> getIncludedResourcePaths() {
     57                 List<ResourcePath> paths = super.getIncludedResourcePaths();
     58                 paths.add(new ResourcePath(
     59                     null,
     60                     Fs.fileFromPath("./packages/apps/StorageManager/res"),
     61                     null));
     62                 paths.add(new ResourcePath(
     63                     null,
     64                     Fs.fileFromPath("./frameworks/base/packages/SettingsLib/res"),
     65                     null));
     66                 paths.add(new ResourcePath(
     67                     null,
     68                     Fs.fileFromPath("./frameworks/base/core/res/res"),
     69                     null));
     70                 return paths;
     71             }
     72         };
     73     }
     74 }