PartnerTest.java revision 91051468dbc273da8d7e7608640e833a71d011e2
1/* 2 * Copyright (C) 2015 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 17package com.android.setupwizardlib.test; 18 19import android.content.Context; 20import android.content.ContextWrapper; 21import android.content.Intent; 22import android.content.pm.ActivityInfo; 23import android.content.pm.ApplicationInfo; 24import android.content.pm.PackageManager; 25import android.content.pm.ResolveInfo; 26import android.content.res.Resources; 27import android.test.InstrumentationTestCase; 28import android.test.mock.MockPackageManager; 29import android.test.mock.MockResources; 30import android.test.suitebuilder.annotation.SmallTest; 31import android.util.SparseArray; 32 33import com.android.setupwizardlib.util.Partner; 34import com.android.setupwizardlib.util.Partner.ResourceEntry; 35 36import java.util.ArrayList; 37import java.util.Arrays; 38import java.util.HashMap; 39import java.util.List; 40import java.util.Map; 41 42public class PartnerTest extends InstrumentationTestCase { 43 44 private TestContext mTestContext; 45 46 @Override 47 protected void setUp() throws Exception { 48 super.setUp(); 49 mTestContext = new TestContext(getInstrumentation().getTargetContext()); 50 Partner.resetForTesting(); 51 } 52 53 @SmallTest 54 public void testLoadPartner() { 55 mTestContext.partnerList = Arrays.asList( 56 createResolveInfo("hocus.pocus", false), 57 createResolveInfo("com.android.setupwizardlib.test", true) 58 ); 59 60 Partner partner = Partner.get(mTestContext); 61 assertNotNull("Partner should not be null", partner); 62 } 63 64 @SmallTest 65 public void testLoadNoPartner() { 66 mTestContext.partnerList = new ArrayList<>(); 67 68 Partner partner = Partner.get(mTestContext); 69 assertNull("Partner should be null", partner); 70 } 71 72 @SmallTest 73 public void testLoadNonSystemPartner() { 74 mTestContext.partnerList = Arrays.asList( 75 createResolveInfo("hocus.pocus", false), 76 createResolveInfo("com.android.setupwizardlib.test", false) 77 ); 78 79 Partner partner = Partner.get(mTestContext); 80 assertNull("Partner should be null", partner); 81 } 82 83 @SmallTest 84 public void testLoadPartnerValue() { 85 mTestContext.partnerList = Arrays.asList( 86 createResolveInfo("hocus.pocus", false), 87 createResolveInfo("com.android.setupwizardlib.test", true) 88 ); 89 90 ResourceEntry entry = 91 Partner.getResourceEntry(mTestContext, R.integer.suwTransitionDuration); 92 int partnerValue = entry.resources.getInteger(entry.id); 93 assertEquals("Partner value should be overlaid to 5000", 5000, partnerValue); 94 assertTrue("Partner value should come from overlay", entry.isOverlay); 95 } 96 97 @SmallTest 98 public void testLoadDefaultValue() { 99 mTestContext.partnerList = Arrays.asList( 100 createResolveInfo("hocus.pocus", false), 101 createResolveInfo("com.android.setupwizardlib.test", true) 102 ); 103 104 ResourceEntry entry = 105 Partner.getResourceEntry(mTestContext, R.color.suw_color_accent_dark); 106 int partnerValue = entry.resources.getColor(entry.id); 107 assertEquals("Partner value should default to 0xff448aff", 0xff448aff, partnerValue); 108 assertFalse("Partner value should come from fallback", entry.isOverlay); 109 } 110 111 private ResolveInfo createResolveInfo(String packageName, boolean isSystem) { 112 ResolveInfo info = new ResolveInfo(); 113 info.resolvePackageName = packageName; 114 ActivityInfo activityInfo = new ActivityInfo(); 115 ApplicationInfo appInfo = new ApplicationInfo(); 116 appInfo.flags = isSystem ? ApplicationInfo.FLAG_SYSTEM : 0; 117 appInfo.packageName = packageName; 118 activityInfo.applicationInfo = appInfo; 119 activityInfo.packageName = packageName; 120 activityInfo.name = packageName; 121 info.activityInfo = activityInfo; 122 return info; 123 } 124 125 private static class TestResources extends MockResources { 126 127 private static final Map<String, Integer> TEST_RESOURCE_IDS = new HashMap<>(); 128 private static final SparseArray<Object> TEST_RESOURCES = new SparseArray<>(); 129 130 private static void addItem(String name, int id, Object value) { 131 TEST_RESOURCE_IDS.put(name, id); 132 TEST_RESOURCES.put(id, value); 133 } 134 135 static { 136 addItem("integer/suwTransitionDuration", 0x7f010000, 5000); 137 } 138 139 @Override 140 public int getIdentifier(String name, String defType, String defPackage) { 141 String key = defType + "/" + name; 142 if (TEST_RESOURCE_IDS.containsKey(key)) { 143 return TEST_RESOURCE_IDS.get(key); 144 } 145 return 0; 146 } 147 148 @Override 149 public int getInteger(int id) throws NotFoundException { 150 if (TEST_RESOURCES.indexOfKey(id) >= 0) { 151 return (int) TEST_RESOURCES.get(id); 152 } else { 153 throw new NotFoundException(); 154 } 155 } 156 157 @Override 158 public int getColor(int id) throws NotFoundException { 159 if (TEST_RESOURCES.indexOfKey(id) >= 0) { 160 return (int) TEST_RESOURCES.get(id); 161 } else { 162 throw new NotFoundException(); 163 } 164 } 165 } 166 167 private static class TestPackageManager extends MockPackageManager { 168 169 private Context mTestContext; 170 private Resources mTestResources; 171 172 public TestPackageManager(Context testContext) { 173 mTestContext = testContext; 174 mTestResources = new TestResources(); 175 } 176 177 @Override 178 public Resources getResourcesForApplication(ApplicationInfo app) { 179 if (app != null && "com.android.setupwizardlib.test".equals(app.packageName)) { 180 return mTestResources; 181 } else { 182 return super.getResourcesForApplication(app); 183 } 184 } 185 186 @Override 187 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) { 188 if ("com.android.setupwizard.action.PARTNER_CUSTOMIZATION".equals(intent.getAction())) { 189 return ((TestContext) mTestContext).partnerList; 190 } else { 191 return super.queryBroadcastReceivers(intent, flags); 192 } 193 } 194 } 195 196 private static class TestContext extends ContextWrapper { 197 198 public List<ResolveInfo> partnerList; 199 200 public TestContext(Context context) { 201 super(context); 202 } 203 204 @Override 205 public PackageManager getPackageManager() { 206 return new TestPackageManager(this); 207 } 208 } 209} 210