SettingsLibRobolectricTestRunner.java revision 5c50dc11b857dfdc265a4e0aa80fd7d17a62e7f6
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 */ 16package com.android.settingslib; 17 18import android.annotation.NonNull; 19 20import org.junit.runners.model.InitializationError; 21import org.robolectric.RobolectricTestRunner; 22import org.robolectric.annotation.Config; 23import org.robolectric.manifest.AndroidManifest; 24import org.robolectric.res.Fs; 25import org.robolectric.res.ResourcePath; 26 27import java.net.MalformedURLException; 28import java.net.URL; 29import java.util.List; 30 31public class SettingsLibRobolectricTestRunner extends RobolectricTestRunner { 32 33 public SettingsLibRobolectricTestRunner(Class<?> testClass) throws InitializationError { 34 super(testClass); 35 } 36 37 /** 38 * We are going to create our own custom manifest so we can add multiple resource paths to it. 39 */ 40 @Override 41 protected AndroidManifest getAppManifest(Config config) { 42 try { 43 // Using the manifest file's relative path, we can figure out the application directory. 44 final URL appRoot = 45 new URL("file:frameworks/base/packages/SettingsLib/tests/robotests"); 46 final URL manifestPath = new URL(appRoot, "AndroidManifest.xml"); 47 final URL resDir = new URL(appRoot, "res"); 48 final URL assetsDir = new URL(appRoot, "assets"); 49 50 return new AndroidManifest(Fs.fromURL(manifestPath), Fs.fromURL(resDir), 51 Fs.fromURL(assetsDir), "com.android.settingslib") { 52 @Override 53 public List<ResourcePath> getIncludedResourcePaths() { 54 final List<ResourcePath> paths = super.getIncludedResourcePaths(); 55 paths.add(resourcePath("file:frameworks/base/packages/SettingsLib/res")); 56 paths.add(resourcePath("file:frameworks/base/core/res/res")); 57 paths.add(resourcePath("file:frameworks/support/v7/appcompat/res")); 58 return paths; 59 } 60 }; 61 } catch (MalformedURLException e) { 62 throw new RuntimeException("SettingsLibRobolectricTestRunner failure", e); 63 } 64 } 65 66 private static ResourcePath resourcePath(@NonNull String spec) { 67 try { 68 return new ResourcePath(null, Fs.fromURL(new URL(spec)), null); 69 } catch (MalformedURLException e) { 70 throw new RuntimeException("SettingsLibRobolectricTestRunner failure", e); 71 } 72 } 73} 74