@@ -3,6 +3,7 @@ import { Yok } from "../../lib/common/yok";
3
3
import * as stubs from "../stubs" ;
4
4
import { assert } from "chai" ;
5
5
import * as sinon from "sinon" ;
6
+ import * as path from "path" ;
6
7
import { GradleCommandService } from "../../lib/services/android/gradle-command-service" ;
7
8
import { GradleBuildService } from "../../lib/services/android/gradle-build-service" ;
8
9
import { GradleBuildArgsService } from "../../lib/services/android/gradle-build-args-service" ;
@@ -42,7 +43,7 @@ const createTestInjector = (): IInjector => {
42
43
testInjector . register ( "gradleBuildService" , GradleBuildService ) ;
43
44
testInjector . register ( "gradleBuildArgsService" , GradleBuildArgsService ) ;
44
45
testInjector . register ( "analyticsService" , stubs . AnalyticsService ) ;
45
- testInjector . register ( "staticConfig" , { TRACK_FEATURE_USAGE_SETTING_NAME : "TrackFeatureUsage" } ) ;
46
+ testInjector . register ( "staticConfig" , { TRACK_FEATURE_USAGE_SETTING_NAME : "TrackFeatureUsage" } ) ;
46
47
return testInjector ;
47
48
} ;
48
49
@@ -59,7 +60,7 @@ const getDefautlBuildConfig = (): IBuildConfig => {
59
60
} ;
60
61
} ;
61
62
62
- describe ( "androidDeviceDebugService " , ( ) => {
63
+ describe ( "androidProjectService " , ( ) => {
63
64
let injector : IInjector ;
64
65
let androidProjectService : IPlatformProjectService ;
65
66
let sandbox : sinon . SinonSandbox = null ;
@@ -74,7 +75,7 @@ describe("androidDeviceDebugService", () => {
74
75
sandbox . restore ( ) ;
75
76
} ) ;
76
77
77
- describe ( "buildPlatform " , ( ) => {
78
+ describe ( "buildProject " , ( ) => {
78
79
let projectData : IProjectData ;
79
80
let childProcess : stubs . ChildProcessStub ;
80
81
@@ -138,4 +139,182 @@ describe("androidDeviceDebugService", () => {
138
139
assert . include ( childProcess . lastCommandArgs , "bundleDebug" ) ;
139
140
} ) ;
140
141
} ) ;
142
+
143
+ describe ( "prepareAppResources" , ( ) => {
144
+ const projectDir = "testDir" ;
145
+ const pathToAppResourcesDir = path . join ( projectDir , "app" , "App_Resources" ) ;
146
+ const pathToAppResourcesAndroid = path . join ( pathToAppResourcesDir , "Android" ) ;
147
+ const pathToPlatformsAndroid = path . join ( projectDir , "platforms" , "android" ) ;
148
+ const pathToResDirInPlatforms = path . join ( pathToPlatformsAndroid , "app" , "src" , "main" , "res" ) ;
149
+ const valuesV27Path = path . join ( pathToResDirInPlatforms , "values-v27" ) ;
150
+ const valuesV28Path = path . join ( pathToResDirInPlatforms , "values-v28" ) ;
151
+ const libsPath = path . join ( pathToResDirInPlatforms , "libs" ) ;
152
+ const drawableHdpiPath = path . join ( pathToResDirInPlatforms , "drawable-hdpi" ) ;
153
+ const drawableLdpiPath = path . join ( pathToResDirInPlatforms , "drawable-ldpi" ) ;
154
+ let deletedDirs : string [ ] = [ ] ;
155
+ let copiedFiles : { sourceFileName : string , destinationFileName : string } [ ] = [ ] ;
156
+ let readDirectoryResults : IDictionary < string [ ] > = { } ;
157
+ let fs : IFileSystem = null ;
158
+ let projectData : IProjectData = null ;
159
+ let compileSdkVersion = 29 ;
160
+
161
+ beforeEach ( ( ) => {
162
+ projectData = injector . resolve ( "projectData" ) ;
163
+ projectData . projectDir = projectDir ;
164
+ projectData . appResourcesDirectoryPath = pathToAppResourcesDir ;
165
+
166
+ deletedDirs = [ ] ;
167
+ copiedFiles = [ ] ;
168
+ readDirectoryResults = { } ;
169
+
170
+ fs = injector . resolve < IFileSystem > ( "fs" ) ;
171
+ fs . deleteDirectory = ( directory : string ) : void => {
172
+ deletedDirs . push ( directory ) ;
173
+ } ;
174
+ fs . copyFile = ( sourceFileName : string , destinationFileName : string ) : void => {
175
+ copiedFiles . push ( { sourceFileName, destinationFileName } ) ;
176
+ } ;
177
+ fs . readDirectory = ( dir : string ) : string [ ] => {
178
+ return readDirectoryResults [ dir ] || [ ] ;
179
+ } ;
180
+
181
+ compileSdkVersion = 29 ;
182
+
183
+ const androidToolsInfo = injector . resolve < IAndroidToolsInfo > ( "androidToolsInfo" ) ;
184
+ androidToolsInfo . getToolsInfo = ( config ?: IProjectDir ) : IAndroidToolsInfoData => {
185
+ return < any > {
186
+ compileSdkVersion
187
+ } ;
188
+ } ;
189
+
190
+ } ) ;
191
+
192
+ describe ( "when new Android App_Resources structure is detected (post {N} 4.0 structure)" , ( ) => {
193
+ const pathToSrcDirInAppResources = path . join ( pathToAppResourcesAndroid , "src" ) ;
194
+ beforeEach ( ( ) => {
195
+ const androidResourcesMigrationService = injector . resolve < IAndroidResourcesMigrationService > ( "androidResourcesMigrationService" ) ;
196
+ androidResourcesMigrationService . hasMigrated = ( ) => true ;
197
+ } ) ;
198
+
199
+ it ( "copies everything from App_Resources/Android/src to correct location in platforms" , async ( ) => {
200
+ await androidProjectService . prepareAppResources ( projectData ) ;
201
+
202
+ assert . deepEqual ( copiedFiles , [ { sourceFileName : path . join ( pathToSrcDirInAppResources , "*" ) , destinationFileName : path . join ( projectData . projectDir , "platforms" , "android" , "app" , "src" ) } ] ) ;
203
+ } ) ;
204
+
205
+ it ( "deletes correct values-<sdk> directories based on the compileSdk" , async ( ) => {
206
+ readDirectoryResults = {
207
+ [ `${ pathToResDirInPlatforms } ` ] : [
208
+ "values" ,
209
+ "values-v21" ,
210
+ "values-v26" ,
211
+ "values-v27" ,
212
+ "values-v28"
213
+ ]
214
+ } ;
215
+
216
+ compileSdkVersion = 26 ;
217
+ await androidProjectService . prepareAppResources ( projectData ) ;
218
+ assert . deepEqual ( deletedDirs , [
219
+ valuesV27Path ,
220
+ valuesV28Path
221
+ ] ) ;
222
+ } ) ;
223
+
224
+ it ( "deletes drawable directories when they've been previously prepared" , async ( ) => {
225
+ readDirectoryResults = {
226
+ [ path . join ( pathToSrcDirInAppResources , "main" , "res" ) ] : [
227
+ "drawable-hdpi" ,
228
+ "drawable-ldpi" ,
229
+ "values" ,
230
+ "values-v21" ,
231
+ "values-v29"
232
+ ] ,
233
+ [ `${ pathToResDirInPlatforms } ` ] : [
234
+ "drawable-hdpi" ,
235
+ "drawable-ldpi" ,
236
+ "drawable-mdpi" ,
237
+ "values" ,
238
+ "values-v21" ,
239
+ "values-v29"
240
+ ]
241
+ } ;
242
+
243
+ await androidProjectService . prepareAppResources ( projectData ) ;
244
+
245
+ // NOTE: Currently the drawable-mdpi directory is not deleted from prepared App_Resources as it does not exist in the currently prepared App_Resources
246
+ // This is not correct behavior and it should be fixed in a later point.
247
+ assert . deepEqual ( deletedDirs , [
248
+ drawableHdpiPath ,
249
+ drawableLdpiPath
250
+ ] ) ;
251
+ } ) ;
252
+ } ) ;
253
+
254
+ describe ( "when old Android App_Resources structure is detected (post {N} 4.0 structure)" , ( ) => {
255
+ beforeEach ( ( ) => {
256
+ const androidResourcesMigrationService = injector . resolve < IAndroidResourcesMigrationService > ( "androidResourcesMigrationService" ) ;
257
+ androidResourcesMigrationService . hasMigrated = ( ) => false ;
258
+ } ) ;
259
+
260
+ it ( "copies everything from App_Resources/Android to correct location in platforms" , async ( ) => {
261
+ await androidProjectService . prepareAppResources ( projectData ) ;
262
+
263
+ assert . deepEqual ( copiedFiles , [ { sourceFileName : path . join ( pathToAppResourcesAndroid , "*" ) , destinationFileName : pathToResDirInPlatforms } ] ) ;
264
+ } ) ;
265
+
266
+ it ( "deletes correct values-<sdk> directories based on the compileSdk" , async ( ) => {
267
+ readDirectoryResults = {
268
+ [ `${ pathToResDirInPlatforms } ` ] : [
269
+ "values" ,
270
+ "values-v21" ,
271
+ "values-v26" ,
272
+ "values-v27" ,
273
+ "values-v28"
274
+ ]
275
+ } ;
276
+
277
+ compileSdkVersion = 26 ;
278
+
279
+ await androidProjectService . prepareAppResources ( projectData ) ;
280
+
281
+ // During preparation of old App_Resources, CLI copies all of them in platforms and after that deletes the libs directory.
282
+ assert . deepEqual ( deletedDirs , [
283
+ libsPath ,
284
+ valuesV27Path ,
285
+ valuesV28Path
286
+ ] ) ;
287
+ } ) ;
288
+
289
+ it ( "deletes drawable directories when they've been previously prepared" , async ( ) => {
290
+ readDirectoryResults = {
291
+ [ `${ pathToAppResourcesAndroid } ` ] : [
292
+ "drawable-hdpi" ,
293
+ "drawable-ldpi" ,
294
+ "values" ,
295
+ "values-v21" ,
296
+ "values-v29"
297
+ ] ,
298
+ [ `${ pathToResDirInPlatforms } ` ] : [
299
+ "drawable-hdpi" ,
300
+ "drawable-ldpi" ,
301
+ "drawable-mdpi" ,
302
+ "values" ,
303
+ "values-v21" ,
304
+ "values-v29"
305
+ ]
306
+ } ;
307
+
308
+ await androidProjectService . prepareAppResources ( projectData ) ;
309
+ // NOTE: Currently the drawable-mdpi directory is not deleted from prepared App_Resources as it does not exist in the currently prepared App_Resources
310
+ // This is not correct behavior and it should be fixed in a later point.
311
+ // During preparation of old App_Resources, CLI copies all of them in platforms and after that deletes the libs directory.
312
+ assert . deepEqual ( deletedDirs , [
313
+ drawableHdpiPath ,
314
+ drawableLdpiPath ,
315
+ libsPath
316
+ ] ) ;
317
+ } ) ;
318
+ } ) ;
319
+ } ) ;
141
320
} ) ;
0 commit comments