| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.base.test; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.content.ContextWrapper; | |
| 9 import android.content.SharedPreferences; | |
| 10 import android.content.pm.ApplicationInfo; | |
| 11 import android.content.pm.PackageManager; | |
| 12 | |
| 13 import org.chromium.base.Log; | |
| 14 import org.chromium.base.annotations.MainDex; | |
| 15 | |
| 16 import java.io.File; | |
| 17 import java.io.IOException; | |
| 18 import java.io.Serializable; | |
| 19 import java.lang.reflect.Field; | |
| 20 import java.util.Arrays; | |
| 21 import java.util.Comparator; | |
| 22 | |
| 23 /** | |
| 24 * Functionality common to the JUnit3 and JUnit4 runners. | |
| 25 */ | |
| 26 @MainDex | |
| 27 class BaseChromiumRunnerCommon { | |
| 28 private static final String TAG = "base_test"; | |
| 29 | |
| 30 /** | |
| 31 * A ContextWrapper that allows multidex test APKs to extract secondary dex
es into | |
| 32 * the APK under test's data directory. | |
| 33 */ | |
| 34 @MainDex | |
| 35 static class MultiDexContextWrapper extends ContextWrapper { | |
| 36 private Context mAppContext; | |
| 37 | |
| 38 MultiDexContextWrapper(Context instrContext, Context appContext) { | |
| 39 super(instrContext); | |
| 40 mAppContext = appContext; | |
| 41 } | |
| 42 | |
| 43 @Override | |
| 44 public File getFilesDir() { | |
| 45 return mAppContext.getFilesDir(); | |
| 46 } | |
| 47 | |
| 48 @Override | |
| 49 public SharedPreferences getSharedPreferences(String name, int mode) { | |
| 50 return mAppContext.getSharedPreferences(name, mode); | |
| 51 } | |
| 52 | |
| 53 @Override | |
| 54 public PackageManager getPackageManager() { | |
| 55 return new PackageManagerWrapper(super.getPackageManager()) { | |
| 56 @Override | |
| 57 public ApplicationInfo getApplicationInfo(String packageName, in
t flags) { | |
| 58 try { | |
| 59 ApplicationInfo ai = super.getApplicationInfo(packageNam
e, flags); | |
| 60 if (packageName.equals(getPackageName())) { | |
| 61 ApplicationInfo appAi = | |
| 62 super.getApplicationInfo(mAppContext.getPack
ageName(), flags); | |
| 63 File dataDir = new File(appAi.dataDir, "test-multide
x"); | |
| 64 if (!dataDir.exists() && !dataDir.mkdirs()) { | |
| 65 throw new IOException(String.format( | |
| 66 "Unable to create test multidex director
y \"%s\"", | |
| 67 dataDir.getPath())); | |
| 68 } | |
| 69 ai.dataDir = dataDir.getPath(); | |
| 70 } | |
| 71 return ai; | |
| 72 } catch (Exception e) { | |
| 73 Log.e(TAG, "Failed to get application info for %s", pack
ageName, e); | |
| 74 } | |
| 75 return null; | |
| 76 } | |
| 77 }; | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 /** | |
| 82 * Ensure all test dex entries precede app dex entries. | |
| 83 * | |
| 84 * @param cl ClassLoader to modify. Assumed to be a derivative of | |
| 85 * {@link dalvik.system.BaseDexClassLoader}. If this isn't | |
| 86 * the case, reordering will fail. | |
| 87 */ | |
| 88 static void reorderDexPathElements(ClassLoader cl, Context context, Context
targetContext) { | |
| 89 try { | |
| 90 Log.i(TAG, | |
| 91 "Reordering dex files. If you're building a multidex test AP
K and see a " | |
| 92 + "class resolving to an unexpected implementation,
this may be why."); | |
| 93 Field pathListField = findField(cl, "pathList"); | |
| 94 Object dexPathList = pathListField.get(cl); | |
| 95 Field dexElementsField = findField(dexPathList, "dexElements"); | |
| 96 Object[] dexElementsList = (Object[]) dexElementsField.get(dexPathLi
st); | |
| 97 Arrays.sort(dexElementsList, | |
| 98 new DexListReorderingComparator( | |
| 99 context.getPackageName(), targetContext.getPackageNa
me())); | |
| 100 dexElementsField.set(dexPathList, dexElementsList); | |
| 101 } catch (Exception e) { | |
| 102 Log.e(TAG, "Failed to reorder dex elements for testing.", e); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 /** | |
| 107 * Comparator for sorting dex list entries. | |
| 108 * | |
| 109 * Using this to sort a list of dex list entries will result in the followi
ng order: | |
| 110 * - Strings that contain neither the test package nor the app package in
lexicographical | |
| 111 * order. | |
| 112 * - Strings that contain the test package in lexicographical order. | |
| 113 * - Strings that contain the app package but not the test package in lexi
cographical order. | |
| 114 */ | |
| 115 private static class DexListReorderingComparator implements Comparator<Objec
t>, Serializable { | |
| 116 private String mTestPackage; | |
| 117 private String mAppPackage; | |
| 118 | |
| 119 public DexListReorderingComparator(String testPackage, String appPackage
) { | |
| 120 mTestPackage = testPackage; | |
| 121 mAppPackage = appPackage; | |
| 122 } | |
| 123 | |
| 124 @Override | |
| 125 public int compare(Object o1, Object o2) { | |
| 126 String s1 = o1.toString(); | |
| 127 String s2 = o2.toString(); | |
| 128 if (s1.contains(mTestPackage)) { | |
| 129 if (!s2.contains(mTestPackage)) { | |
| 130 if (s2.contains(mAppPackage)) { | |
| 131 return -1; | |
| 132 } else { | |
| 133 return 1; | |
| 134 } | |
| 135 } | |
| 136 } else if (s1.contains(mAppPackage)) { | |
| 137 if (s2.contains(mTestPackage)) { | |
| 138 return 1; | |
| 139 } else if (!s2.contains(mAppPackage)) { | |
| 140 return 1; | |
| 141 } | |
| 142 } else if (s2.contains(mTestPackage) || s2.contains(mAppPackage)) { | |
| 143 return -1; | |
| 144 } | |
| 145 return s1.compareTo(s2); | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 private static Field findField(Object instance, String name) throws NoSuchFi
eldException { | |
| 150 for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.
getSuperclass()) { | |
| 151 try { | |
| 152 Field f = clazz.getDeclaredField(name); | |
| 153 f.setAccessible(true); | |
| 154 return f; | |
| 155 } catch (NoSuchFieldException e) { | |
| 156 } | |
| 157 } | |
| 158 throw new NoSuchFieldException( | |
| 159 "Unable to find field " + name + " in " + instance.getClass()); | |
| 160 } | |
| 161 } | |
| OLD | NEW |