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.settings; 17 18 import android.app.Fragment; 19 import android.content.Intent; 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 import org.robolectric.util.ActivityController; 28 import org.robolectric.util.ReflectionHelpers; 29 30 import java.util.List; 31 32 import static com.android.settings.SettingsActivity.EXTRA_SHOW_FRAGMENT; 33 import static org.robolectric.Robolectric.getShadowsAdapter; 34 35 /** 36 * Custom test runner for the testing of BluetoothPairingDialogs. This is needed because the 37 * default behavior for robolectric is just to grab the resource directory in the target package. 38 * We want to override this to add several spanning different projects. 39 */ 40 public class SettingsRobolectricTestRunner extends RobolectricTestRunner { 41 42 /** 43 * We don't actually want to change this behavior, so we just call super. 44 */ 45 public SettingsRobolectricTestRunner(Class<?> testClass) throws InitializationError { 46 super(testClass); 47 } 48 49 /** 50 * We are going to create our own custom manifest so that we can add multiple resource 51 * paths to it. This lets us access resources in both Settings and SettingsLib in our tests. 52 */ 53 @Override 54 protected AndroidManifest getAppManifest(Config config) { 55 // Using the manifest file's relative path, we can figure out the application directory. 56 final String appRoot = "packages/apps/Settings"; 57 final String manifestPath = appRoot + "/AndroidManifest.xml"; 58 final String resDir = appRoot + "/tests/robotests/res"; 59 final String assetsDir = appRoot + config.assetDir(); 60 61 // By adding any resources from libraries we need the AndroidManifest, we can access 62 // them from within the parallel universe's resource loader. 63 final AndroidManifest manifest = new AndroidManifest(Fs.fileFromPath(manifestPath), 64 Fs.fileFromPath(resDir), Fs.fileFromPath(assetsDir)) { 65 @Override 66 public List<ResourcePath> getIncludedResourcePaths() { 67 List<ResourcePath> paths = super.getIncludedResourcePaths(); 68 paths.add(new ResourcePath( 69 getPackageName(), 70 Fs.fileFromPath("./packages/apps/Settings/res"), 71 null)); 72 paths.add(new ResourcePath( 73 getPackageName(), 74 Fs.fileFromPath("./frameworks/base/packages/SettingsLib/res"), 75 null)); 76 paths.add(new ResourcePath( 77 getPackageName(), 78 Fs.fileFromPath("./frameworks/base/core/res/res"), 79 null)); 80 paths.add(new ResourcePath( 81 getPackageName(), 82 Fs.fileFromPath("./frameworks/opt/setupwizard/library/main/res"), 83 null)); 84 paths.add(new ResourcePath( 85 getPackageName(), 86 Fs.fileFromPath("./frameworks/opt/setupwizard/library/eclair-mr1/res"), 87 null)); 88 paths.add(new ResourcePath( 89 getPackageName(), 90 Fs.fileFromPath("./frameworks/opt/setupwizard/library/full-support/res"), 91 null)); 92 paths.add(new ResourcePath( 93 getPackageName(), 94 Fs.fileFromPath("./frameworks/support/v7/appcompat/res"), 95 null)); 96 return paths; 97 } 98 }; 99 100 // Set the package name to the renamed one 101 manifest.setPackageName("com.android.settings"); 102 return manifest; 103 } 104 105 // A simple utility class to start a Settings fragment with an intent. The code here is almost 106 // the same as FragmentTestUtil.startFragment except that it starts an activity with an intent. 107 public static void startSettingsFragment( 108 Fragment fragment, Class<? extends SettingsActivity> activityClass) { 109 Intent intent = new Intent().putExtra(EXTRA_SHOW_FRAGMENT, fragment.getClass().getName()); 110 SettingsActivity activity = ActivityController.of( 111 getShadowsAdapter(), ReflectionHelpers.callConstructor(activityClass), intent) 112 .setup().get(); 113 activity.getFragmentManager().beginTransaction().add(fragment, null).commit(); 114 } 115 } 116