Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(519)

Side by Side Diff: base/test/android/javatests/src/org/chromium/base/test/BaseChromiumRunnerCommon.java

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

Powered by Google App Engine
This is Rietveld 408576698