1
+ /*
2
+ * Copyright (c) 2012-2016 Algolia
3
+ * http://www.algolia.com/
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ * THE SOFTWARE.
22
+ */
23
+
24
+ package com .algolia .search .saas ;
25
+
26
+ import org .junit .runners .model .InitializationError ;
27
+ import org .robolectric .RobolectricTestRunner ;
28
+ import org .robolectric .annotation .Config ;
29
+ import org .robolectric .manifest .AndroidManifest ;
30
+ import org .robolectric .res .FileFsFile ;
31
+ import org .robolectric .util .Logger ;
32
+ import org .robolectric .util .ReflectionHelpers ;
33
+
34
+
35
+ /**
36
+ * Custom runner for Robolectric.
37
+ *
38
+ * **Note:** This is required to circumvent a bug in Robolectric when used in the following configuration:
39
+ *
40
+ * - Robolectric 3.0 -> 3.1.2 (at least)
41
+ * - Android Studio 2.2
42
+ * - Gradle 2.10
43
+ * - Build Tools 24.0.1 -> 24.0.3 (at least)
44
+ *
45
+ * Solution inspired by a reply to [Robolectric issue #1648](https://github.com/robolectric/robolectric/issues/1648).
46
+ */
47
+ public class CustomRunner extends RobolectricTestRunner {
48
+
49
+ private static final String BUILD_OUTPUT = "build/intermediates" ;
50
+
51
+ public CustomRunner (Class <?> klass ) throws InitializationError {
52
+ super (klass );
53
+ }
54
+
55
+ @ Override
56
+ protected AndroidManifest getAppManifest (Config config ) {
57
+ final String cwd = System .getProperty ("user.dir" );
58
+ Logger .debug ("Current working directory: " + cwd );
59
+
60
+ if (config .constants () == Void .class ) {
61
+ Logger .error ("Field 'constants' not specified in @Config annotation" );
62
+ Logger .error ("This is required when using RobolectricGradleTestRunner!" );
63
+ throw new RuntimeException ("No 'constants' field in @Config annotation!" );
64
+ }
65
+
66
+ final String type = getType (config );
67
+ final String flavor = getFlavor (config );
68
+ final String packageName = getPackageName (config );
69
+
70
+ final FileFsFile res ;
71
+ final FileFsFile assets ;
72
+ final FileFsFile manifest ;
73
+
74
+ if (FileFsFile .from (BUILD_OUTPUT , "res" ).exists ()) {
75
+ res = FileFsFile .from (BUILD_OUTPUT , "res" , flavor , type );
76
+ } else {
77
+ res = FileFsFile .from (BUILD_OUTPUT , "bundles" , flavor , type , "res" );
78
+ }
79
+
80
+ if (FileFsFile .from (BUILD_OUTPUT , "assets" ).exists ()) {
81
+ assets = FileFsFile .from (BUILD_OUTPUT , "assets" , flavor , type );
82
+ } else {
83
+ assets = FileFsFile .from (BUILD_OUTPUT , "bundles" , flavor , type , "assets" );
84
+ }
85
+
86
+ if (FileFsFile .from (BUILD_OUTPUT , "manifests" ).exists ()) {
87
+ // [CUSTOMIZED] The default implementation uses `full` instead of `aapt`.
88
+ manifest = FileFsFile .from (BUILD_OUTPUT , "manifests" , "aapt" , flavor , type , "AndroidManifest.xml" );
89
+ } else {
90
+ manifest = FileFsFile .from (BUILD_OUTPUT , "bundles" , flavor , type , "AndroidManifest.xml" );
91
+ }
92
+
93
+ Logger .debug ("Robolectric assets directory: " + assets .getPath ());
94
+ Logger .debug (" Robolectric res directory: " + res .getPath ());
95
+ Logger .debug (" Robolectric manifest path: " + manifest .getPath ());
96
+ Logger .debug (" Robolectric package name: " + packageName );
97
+ return new AndroidManifest (manifest , res , assets , packageName );
98
+ }
99
+
100
+ private String getType (Config config ) {
101
+ try {
102
+ return ReflectionHelpers .getStaticField (config .constants (), "BUILD_TYPE" );
103
+ } catch (Throwable e ) {
104
+ return null ;
105
+ }
106
+ }
107
+
108
+ private String getFlavor (Config config ) {
109
+ try {
110
+ return ReflectionHelpers .getStaticField (config .constants (), "FLAVOR" );
111
+ } catch (Throwable e ) {
112
+ return null ;
113
+ }
114
+ }
115
+
116
+ private String getPackageName (Config config ) {
117
+ try {
118
+ final String packageName = config .packageName ();
119
+ if (packageName != null && !packageName .isEmpty ()) {
120
+ return packageName ;
121
+ } else {
122
+ return ReflectionHelpers .getStaticField (config .constants (), "APPLICATION_ID" );
123
+ }
124
+ } catch (Throwable e ) {
125
+ return null ;
126
+ }
127
+ }
128
+ }
0 commit comments