aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/android/AndroidResourceClassWriterTest.java
blob: 72eac32a7fa924befd1fe514c809255fefe92828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
// Copyright 2017 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.android;

import static com.google.common.truth.Truth.assertAbout;
import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.jimfs.Jimfs;
import com.google.common.truth.FailureStrategy;
import com.google.common.truth.SubjectFactory;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Tests for {@link AndroidResourceClassWriter}.
 */
@RunWith(JUnit4.class)
public class AndroidResourceClassWriterTest {

  private FileSystem fs;

  @Rule
  public final ExpectedException thrown = ExpectedException.none();

  private static final AndroidFrameworkAttrIdProvider mockAndroidFrameworkIds =
      new MockAndroidFrameworkAttrIdProvider(ImmutableMap.<String, Integer>of());

  @Before
  public void createCleanEnvironment() {
    fs = Jimfs.newFileSystem();
  }

  @Test
  public void simpleIdFromLayout() throws Exception {
    Path target = fs.getPath("target");
    Path source = fs.getPath("source");
    AndroidResourceClassWriter resourceClassWriter =
        AndroidResourceClassWriter.of(mockAndroidFrameworkIds, target, "com.carroll.lewis");
    ParsedAndroidData direct =
        AndroidDataBuilder.of(source)
            .addResource(
                "layout/some_layout.xml",
                AndroidDataBuilder.ResourceType.LAYOUT,
                "<TextView android:id=\"@+id/HelloView\"",
                "          android:text=\"Hello World!\"",
                "          android:layout_width=\"wrap_content\"",
                "          android:layout_height=\"wrap_content\" />",
                "<Button android:id=\"@+id/AdiosButton\"",
                "        android:text=\"Adios!\"",
                "        android:layout_width=\"wrap_content\"",
                "        android:layout_height=\"wrap_content\" />")
            .createManifest("AndroidManifest.xml", "com.carroll.lewis", "")
            .buildParsed();
    UnwrittenMergedAndroidData unwrittenMergedAndroidData =
        UnwrittenMergedAndroidData.of(
            source.resolve("AndroidManifest.xml"), direct, ParsedAndroidDataBuilder.empty());
    unwrittenMergedAndroidData
        .writeResourceClass(resourceClassWriter);

    assertAbout(paths)
        .that(target.resolve("com/carroll/lewis/R.java"))
        .javaContentsIsEqualTo(
            "package com.carroll.lewis;",
            "public final class R {",
            "public static final class id {",
            "public static int AdiosButton = 0x7f030000;",
            "public static int HelloView = 0x7f030001;",
            "}",
            "public static final class layout {",
            "public static int some_layout = 0x7f020000;",
            "}",
            "}"
        );
    assertAbout(paths)
        .that(target)
        .withClass("com.carroll.lewis.R$id")
        .classContentsIsEqualTo(
            ImmutableMap.of(
                "AdiosButton", 0x7f030000,
                "HelloView", 0x7f030001),
            ImmutableMap.<String, List<Integer>>of(),
            false
        );
    assertAbout(paths)
        .that(target)
        .withClass("com.carroll.lewis.R$layout")
        .classContentsIsEqualTo(
            ImmutableMap.of("some_layout", 0x7f020000),
            ImmutableMap.<String, List<Integer>>of(),
            false
        );
  }

  @Test
  public void ninePatchFieldNames() throws Exception {
    Path target = fs.getPath("target");
    Path source = fs.getPath("source");
    String drawable = "drawable/light.png";
    String ninePatch = "drawable/patchface.9.png";
    AndroidResourceClassWriter resourceClassWriter =
        AndroidResourceClassWriter.of(mockAndroidFrameworkIds, target, "com.boop");
    ParsedAndroidData direct =
        AndroidDataBuilder.of(source)
            .addResourceBinary(
                drawable,
                Files.createFile(fs.getPath("lightbringer.png")))
            .addResourceBinary(
                ninePatch,
                Files.createFile(fs.getPath("patchface.9.png")))
            .createManifest("AndroidManifest.xml", "com.boop", "")
            .buildParsed();
    UnwrittenMergedAndroidData unwrittenMergedAndroidData =
        UnwrittenMergedAndroidData.of(
            source.resolve("AndroidManifest.xml"), direct, ParsedAndroidDataBuilder.empty());
    unwrittenMergedAndroidData
        .writeResourceClass(resourceClassWriter);
    assertAbout(paths)
        .that(target.resolve("com/boop/R.java"))
        .javaContentsIsEqualTo(
            "package com.boop;",
            "public final class R {",
            "public static final class drawable {",
            "public static int light = 0x7f020000;",
            "public static int patchface = 0x7f020001;",
            "}",
            "}"
        );
    assertAbout(paths)
        .that(target)
        .withClass("com.boop.R$drawable")
        .classContentsIsEqualTo(
            ImmutableMap.of(
                "light", 0x7f020000,
                "patchface", 0x7f020001),
            ImmutableMap.<String, List<Integer>>of(),
            false
        );
  }

  @Test
  public void unionOfResourcesInConfigurations() throws Exception {
    // See what happens if there are some configuration specific resources
    // (selection guarded by checks at runtime?).
    Path target = fs.getPath("target");
    Path source = fs.getPath("source");
    String drawable = "drawable/light.png";
    String drawableV18 = "drawable-v18/light18.png";
    String drawableV19 = "drawable-xxhdpi-v19/light19.png";
    String drawableV20 = "drawable-ldltr-v20/light20.png";
    Path stubImage = Files.createFile(fs.getPath("stub.png"));
    AndroidResourceClassWriter resourceClassWriter =
        AndroidResourceClassWriter.of(mockAndroidFrameworkIds, target, "com.boop");
    ParsedAndroidData direct =
        AndroidDataBuilder.of(source)
            .addResourceBinary(drawable, stubImage)
            .addResourceBinary(drawableV18, stubImage)
            .addResourceBinary(drawableV19, stubImage)
            .addResourceBinary(drawableV20, stubImage)
            .createManifest("AndroidManifest.xml", "com.boop", "")
            .buildParsed();
    UnwrittenMergedAndroidData unwrittenMergedAndroidData =
        UnwrittenMergedAndroidData.of(
            source.resolve("AndroidManifest.xml"), direct, ParsedAndroidDataBuilder.empty());
    unwrittenMergedAndroidData
        .writeResourceClass(resourceClassWriter);
    assertAbout(paths)
        .that(target.resolve("com/boop/R.java"))
        .javaContentsIsEqualTo(
            "package com.boop;",
            "public final class R {",
            "public static final class drawable {",
            "public static int light = 0x7f020000;",
            "public static int light18 = 0x7f020001;",
            "public static int light19 = 0x7f020002;",
            "public static int light20 = 0x7f020003;",
            "}",
            "}"
        );
    assertAbout(paths)
        .that(target)
        .withClass("com.boop.R$drawable")
        .classContentsIsEqualTo(
            ImmutableMap.of(
                "light", 0x7f020000,
                "light18", 0x7f020001,
                "light19", 0x7f020002,
                "light20", 0x7f020003),
            ImmutableMap.<String, List<Integer>>of(),
            false
        );
  }

  @Test
  public void normalizeStyleAndStyleableNames() throws Exception {
    // Style and Styleables can have dots in the name. In order for it to be a legal Java
    // identifier, the dots are converted to underscore.
    Path target = fs.getPath("target");
    Path source = fs.getPath("source");
    Path transitive = fs.getPath("transitive");

    AndroidResourceClassWriter resourceClassWriter =
        AndroidResourceClassWriter.of(mockAndroidFrameworkIds, target, "com.carroll.lewis");
    ParsedAndroidData direct =
        AndroidDataBuilder.of(source)
            .addResource(
                "values/attr.xml",
                AndroidDataBuilder.ResourceType.VALUE,
                "<attr name=\"y_color\" format=\"color\" />",
                "<attr name=\"z_color\" format=\"color\" />"
            )
            .addResource(
                "values/style.xml",
                AndroidDataBuilder.ResourceType.VALUE,
                "<style name=\"YStyle\">",
                "  <item name=\"y_color\">#FF00FF00</item>",
                "</style>",
                "<style name=\"ZStyle.ABC\" parent=\"YStyle\">",
                "  <item name=\"z_color\">#00FFFF00</item>",
                "</style>"
            )
            .addResource(
                "values/styleable.xml",
                AndroidDataBuilder.ResourceType.VALUE,
                "<declare-styleable name=\"com.google.android.Dots\">",
                "  <attr name=\"y_color\"/>",
                "  <attr name=\"z_color\"/>",
                "  <attr name=\"x_color\"/>",
                "</declare-styleable>"
            )
            .createManifest("AndroidManifest.xml", "com.carroll.lewis", "")
            .buildParsed();

    ParsedAndroidData transitiveDep =
        AndroidDataBuilder.of(transitive)
            .addResource(
                "values/attr.xml",
                AndroidDataBuilder.ResourceType.VALUE,
                "<attr name=\"x_color\" format=\"color\" />"
            )
            .addResource(
                "values/styleable.xml",
                AndroidDataBuilder.ResourceType.VALUE,
                "<declare-styleable name=\"com.google.android.Swirls.Fancy\">",
                "  <attr name=\"z_color\"/>",
                "  <attr name=\"x_color\"/>",
                "  <attr name=\"y_color\"/>",
                "</declare-styleable>"
            )
            .createManifest("AndroidManifest.xml", "com.library", "")
            .buildParsed();

    UnwrittenMergedAndroidData unwrittenMergedAndroidData =
        UnwrittenMergedAndroidData.of(
            source.resolve("AndroidManifest.xml"),
            direct,
            transitiveDep);
    unwrittenMergedAndroidData
        .writeResourceClass(resourceClassWriter);

    assertAbout(paths)
        .that(target.resolve("com/carroll/lewis/R.java"))
        .javaContentsIsEqualTo(
            "package com.carroll.lewis;",
            "public final class R {",
            "public static final class attr {",
            "public static int x_color = 0x7f010000;",
            "public static int y_color = 0x7f010001;",
            "public static int z_color = 0x7f010002;",
            "}",
            "public static final class style {",
            "public static int YStyle = 0x7f020000;",
            "public static int ZStyle_ABC = 0x7f020001;",
            "}",
            "public static final class styleable {",
            "public static int[] com_google_android_Dots = { 0x7f010000, 0x7f010001, 0x7f010002 };",
            "public static int com_google_android_Dots_x_color = 0x0;",
            "public static int com_google_android_Dots_y_color = 0x1;",
            "public static int com_google_android_Dots_z_color = 0x2;",
            "public static int[] com_google_android_Swirls_Fancy ="
                + " { 0x7f010000, 0x7f010001, 0x7f010002 };",
            "public static int com_google_android_Swirls_Fancy_x_color = 0x0;",
            "public static int com_google_android_Swirls_Fancy_y_color = 0x1;",
            "public static int com_google_android_Swirls_Fancy_z_color = 0x2;",
            "}",
            "}"
        );
    assertAbout(paths)
        .that(target)
        .withClass("com.carroll.lewis.R$attr")
        .classContentsIsEqualTo(
            ImmutableMap.of(
                "x_color", 0x7f010000,
                "y_color", 0x7f010001,
                "z_color", 0x7f010002),
            ImmutableMap.<String, List<Integer>>of(),
            false
        );
    assertAbout(paths)
        .that(target)
        .withClass("com.carroll.lewis.R$style")
        .classContentsIsEqualTo(
            ImmutableMap.of(
                "YStyle", 0x7f020000,
                "ZStyle_ABC", 0x7f020001),
            ImmutableMap.<String, List<Integer>>of(),
            false
        );
    assertAbout(paths)
        .that(target)
        .withClass("com.carroll.lewis.R$styleable")
        .classContentsIsEqualTo(
            ImmutableMap.<String, Integer>builder()
                .put("com_google_android_Dots_x_color", 0)
                .put("com_google_android_Dots_y_color", 1)
                .put("com_google_android_Dots_z_color", 2)
                .put("com_google_android_Swirls_Fancy_x_color", 0)
                .put("com_google_android_Swirls_Fancy_y_color", 1)
                .put("com_google_android_Swirls_Fancy_z_color", 2)
                .build(),
            ImmutableMap.<String, List<Integer>>of(
                "com_google_android_Dots",
                ImmutableList.of(0x7f010000, 0x7f010001, 0x7f010002),
                "com_google_android_Swirls_Fancy",
                ImmutableList.of(0x7f010000, 0x7f010001, 0x7f010002)
            ),
            false
        );
  }

  @Test
  public void handleAndroidFrameworkAttributes() throws Exception {
    // Attributes in the styleable array need to be sorted by integer ID value, so android
    // framework attributes need to come before application attributes.
    Path target = fs.getPath("target");
    Path source = fs.getPath("source");
    AndroidResourceClassWriter resourceClassWriter =
        AndroidResourceClassWriter.of(
            new MockAndroidFrameworkAttrIdProvider(
                ImmutableMap.of(
                    "textColor", 0x01000000,
                    "textColorSecondary", 0x01000006,
                    "textSize", 0x01000010)),
            target,
            "com.carroll.lewis");
    ParsedAndroidData direct =
        AndroidDataBuilder.of(source)
            .addResource(
                "values/attr.xml",
                AndroidDataBuilder.ResourceType.VALUE,
                "<attr name=\"aaa\" format=\"boolean\" />",
                "<attr name=\"zzz\" format=\"boolean\" />"
            )
            .addResource(
                "values/style.xml",
                AndroidDataBuilder.ResourceType.VALUE,
                "<style name=\"YStyle\">",
                "  <item name=\"android:textSize\">15sp</item>",
                "  <item name=\"android:textColor\">#ffffff</item>",
                "  <item name=\"android:textColorSecondary\">#ffffff</item>",
                "</style>")
            .addResource(
                "values/styleable.xml",
                AndroidDataBuilder.ResourceType.VALUE,
                "<declare-styleable name=\"com.google.android.Dots\">",
                "  <attr name=\"aaa\"/>",
                "  <attr name=\"zzz\"/>",
                // the android framework attr should be sorted first, even if it's alphabetically
                // after the "aaa" attribute.
                "  <attr name=\"android:textSize\"/>",
                "  <attr name=\"android:textColor\"/>",
                "</declare-styleable>"
            )
            .createManifest("AndroidManifest.xml", "com.carroll.lewis", "")
            .buildParsed();
    UnwrittenMergedAndroidData unwrittenMergedAndroidData =
        UnwrittenMergedAndroidData.of(
            source.resolve("AndroidManifest.xml"), direct, ParsedAndroidDataBuilder.empty());
    unwrittenMergedAndroidData
        .writeResourceClass(resourceClassWriter);

    assertAbout(paths)
        .that(target.resolve("com/carroll/lewis/R.java"))
        .javaContentsIsEqualTo(
            "package com.carroll.lewis;",
            "public final class R {",
            "public static final class attr {",
            "public static int aaa = 0x7f010000;",
            "public static int zzz = 0x7f010001;",
            "}",
            "public static final class style {",
            "public static int YStyle = 0x7f020000;",
            "}",
            "public static final class styleable {",
            "public static int[] com_google_android_Dots = "
                + "{ 0x1000000, 0x1000010, 0x7f010000, 0x7f010001 };",
            "public static int com_google_android_Dots_android_textColor = 0x0;",
            "public static int com_google_android_Dots_android_textSize = 0x1;",
            "public static int com_google_android_Dots_aaa = 0x2;",
            "public static int com_google_android_Dots_zzz = 0x3;",
            "}",
            "}"
        );
    assertAbout(paths)
        .that(target)
        .withClass("com.carroll.lewis.R$attr")
        .classContentsIsEqualTo(
            ImmutableMap.of(
                "aaa", 0x7f010000,
                "zzz", 0x7f010001),
            ImmutableMap.<String, List<Integer>>of(),
            false
        );
    assertAbout(paths)
        .that(target)
        .withClass("com.carroll.lewis.R$style")
        .classContentsIsEqualTo(
            ImmutableMap.of(
                "YStyle", 0x7f020000),
            ImmutableMap.<String, List<Integer>>of(),
            false
        );
    assertAbout(paths)
        .that(target)
        .withClass("com.carroll.lewis.R$styleable")
        .classContentsIsEqualTo(
            ImmutableMap.of(
                "com_google_android_Dots_android_textColor", 0,
                "com_google_android_Dots_android_textSize", 1,
                "com_google_android_Dots_aaa", 2,
                "com_google_android_Dots_zzz", 3
            ),
            ImmutableMap.<String, List<Integer>>of(
                "com_google_android_Dots",
                ImmutableList.of(0x01000000, 0x01000010, 0x7f010000, 0x7f010001)
            ),
            false
        );
  }

  @Test
  public void missingFrameworkAttribute() throws Exception {
    Path target = fs.getPath("target");
    Path source = fs.getPath("source");
    AndroidResourceClassWriter resourceClassWriter =
        AndroidResourceClassWriter.of(
            new MockAndroidFrameworkAttrIdProvider(ImmutableMap.<String, Integer>of()),
            target,
            "com.carroll.lewis");
    ParsedAndroidData direct =
        AndroidDataBuilder.of(source)
            .addResource(
                "values/attr.xml",
                AndroidDataBuilder.ResourceType.VALUE,
                "<attr name=\"aaazzz\" format=\"boolean\" />"
            )
            .addResource(
                "values/styleable.xml",
                AndroidDataBuilder.ResourceType.VALUE,
                "<declare-styleable name=\"com.google.android.Dots\">",
                "  <attr name=\"aaazzz\"/>",
                "  <attr name=\"android:aaazzz\"/>",
                "</declare-styleable>"
            )
            .createManifest("AndroidManifest.xml", "com.carroll.lewis", "")
            .buildParsed();
    UnwrittenMergedAndroidData unwrittenMergedAndroidData =
        UnwrittenMergedAndroidData.of(
            source.resolve("AndroidManifest.xml"), direct, ParsedAndroidDataBuilder.empty());
    thrown.expect(IOException.class);
    thrown.expectMessage("Android attribute not found: aaazzz");
    unwrittenMergedAndroidData
        .writeResourceClass(resourceClassWriter);
  }

  @Test
  public void missingAppAttribute() throws Exception {
    Path target = fs.getPath("target");
    Path source = fs.getPath("source");
    AndroidResourceClassWriter resourceClassWriter =
        AndroidResourceClassWriter.of(
            new MockAndroidFrameworkAttrIdProvider(ImmutableMap.<String, Integer>of()),
            target,
            "com.carroll.lewis");
    ParsedAndroidData direct =
        AndroidDataBuilder.of(source)
            .addResource(
                "values/styleable.xml",
                AndroidDataBuilder.ResourceType.VALUE,
                "<declare-styleable name=\"com.google.android.Dots\">",
                "  <attr name=\"aaazzz\"/>",
                "</declare-styleable>"
            )
            .createManifest("AndroidManifest.xml", "com.carroll.lewis", "")
            .buildParsed();
    UnwrittenMergedAndroidData unwrittenMergedAndroidData =
        UnwrittenMergedAndroidData.of(
            source.resolve("AndroidManifest.xml"), direct, ParsedAndroidDataBuilder.empty());
    thrown.expect(IOException.class);
    thrown.expectMessage("App attribute not found: aaazzz");
    unwrittenMergedAndroidData
        .writeResourceClass(resourceClassWriter);
  }

  /**
   * Test what happens if we try to create a field name that is not a valid Java identifier. Here,
   * we start the field name with a number, which is not legal according to {@link
   * Character#isJavaIdentifierStart}.
   *
   * See: {@link com.android.ide.common.res2.FileResourceNameValidator}, and {@link
   * com.android.ide.common.res2.ValueResourceNameValidator}.
   *
   * AAPT seems to miss out on checking this case (it only checks for [a-z0-9_.], but isn't
   * position-sensitive).
   */
  @Test
  public void illegalFileResFieldNamesStart() throws Exception {
    Path target = fs.getPath("target");
    Path source = fs.getPath("source");
    String drawable = "drawable/1.png";
    assertThat(Character.isJavaIdentifierStart('1')).isFalse();
    AndroidResourceClassWriter resourceClassWriter =
        AndroidResourceClassWriter.of(mockAndroidFrameworkIds, target, "com.boop");
    ParsedAndroidData direct =
        AndroidDataBuilder.of(source)
            .addResourceBinary(
                drawable,
                Files.createFile(fs.getPath("1.png")))
            .createManifest("AndroidManifest.xml", "com.boop", "")
            .buildParsed();
    UnwrittenMergedAndroidData unwrittenMergedAndroidData =
        UnwrittenMergedAndroidData.of(
            source.resolve("AndroidManifest.xml"), direct, ParsedAndroidDataBuilder.empty());
    unwrittenMergedAndroidData
        .writeResourceClass(resourceClassWriter);
    assertAbout(paths)
        .that(target.resolve("com/boop/R.java"))
        .javaContentsIsEqualTo(
            "package com.boop;",
            "public final class R {",
            "public static final class drawable {",
            "public static int 1 = 0x7f020000;",
            "}",
            "}"
        );
    assertAbout(paths)
        .that(target)
        .withClass("com.boop.R$drawable")
        .classContentsIsEqualTo(
            ImmutableMap.of("1", 0x7f020000),
            ImmutableMap.<String, List<Integer>>of(),
            false
        );
  }

  /**
   * Test embedding a character that doesn't satisfy Character#isJavaIdentifierPart. Do so in a file
   * resource. In this case, AAPT will actually complain, so we may not need to do earlier
   * validation.
   */
  @Test
  public void illegalFileResFieldNamesCharacters() throws Exception {
    Path target = fs.getPath("target");
    Path source = fs.getPath("source");
    String drawable = "drawable/c++.png";
    assertThat(Character.isJavaIdentifierStart('c')).isTrue();
    assertThat(Character.isJavaIdentifierPart('+')).isFalse();
    AndroidResourceClassWriter resourceClassWriter =
        AndroidResourceClassWriter.of(mockAndroidFrameworkIds, target, "com.boop");
    ParsedAndroidData direct =
        AndroidDataBuilder.of(source)
            .addResourceBinary(
                drawable,
                Files.createFile(fs.getPath("phone#.png")))
            .createManifest("AndroidManifest.xml", "com.boop", "")
            .buildParsed();
    UnwrittenMergedAndroidData unwrittenMergedAndroidData =
        UnwrittenMergedAndroidData.of(
            source.resolve("AndroidManifest.xml"), direct, ParsedAndroidDataBuilder.empty());
    unwrittenMergedAndroidData
        .writeResourceClass(resourceClassWriter);
    assertAbout(paths)
        .that(target.resolve("com/boop/R.java"))
        .javaContentsIsEqualTo(
            "package com.boop;",
            "public final class R {",
            "public static final class drawable {",
            "public static int c++ = 0x7f020000;",
            "}",
            "}"
        );
    assertAbout(paths)
        .that(target)
        .withClass("com.boop.R$drawable")
        .classContentsIsEqualTo(
            ImmutableMap.of("c++", 0x7f020000),
            ImmutableMap.<String, List<Integer>>of(),
            false
        );
  }

  /**
   * Test embedding a character that doesn't satisfy Character#isJavaIdentifierPart. Do so in a
   * value resource. This is a case that AAPT doesn't validate, so it may pass through to the java
   * compiler.
   */
  @Test
  public void illegalValueResFieldNamesCharacters() throws Exception {
    Path target = fs.getPath("target");
    Path source = fs.getPath("source");
    assertThat(Character.isJavaIdentifierStart('c')).isTrue();
    assertThat(Character.isJavaIdentifierPart('+')).isFalse();
    AndroidResourceClassWriter resourceClassWriter =
        AndroidResourceClassWriter.of(mockAndroidFrameworkIds, target, "com.boop");
    ParsedAndroidData direct =
        AndroidDataBuilder.of(source)
            .addResource(
                "values/integers.xml",
                AndroidDataBuilder.ResourceType.VALUE,
                "<integer name=\"c++\">0xd</integer>"
            )
            .createManifest("AndroidManifest.xml", "com.boop", "")
            .buildParsed();
    UnwrittenMergedAndroidData unwrittenMergedAndroidData =
        UnwrittenMergedAndroidData.of(
            source.resolve("AndroidManifest.xml"), direct, ParsedAndroidDataBuilder.empty());
    unwrittenMergedAndroidData
        .writeResourceClass(resourceClassWriter);
    assertAbout(paths)
        .that(target.resolve("com/boop/R.java"))
        .javaContentsIsEqualTo(
            "package com.boop;",
            "public final class R {",
            "public static final class integer {",
            "public static int c++ = 0x7f020000;",
            "}",
            "}"
        );
    assertAbout(paths)
        .that(target)
        .withClass("com.boop.R$integer")
        .classContentsIsEqualTo(
            ImmutableMap.of("c++", 0x7f020000),
            ImmutableMap.<String, List<Integer>>of(),
            false
        );
  }

  private static class MockAndroidFrameworkAttrIdProvider
      implements AndroidFrameworkAttrIdProvider {

    private final Map<String, Integer> mapToUse;

    MockAndroidFrameworkAttrIdProvider(Map<String, Integer> mapToUse) {
      this.mapToUse = mapToUse;
    }

    @Override
    public int getAttrId(String fieldName) throws AttrLookupException {
      if (mapToUse.containsKey(fieldName)) {
        return mapToUse.get(fieldName);
      }
      throw new AttrLookupException("Android attribute not found: " + fieldName);
    }
  }

  private static final SubjectFactory<ClassPathsSubject, Path> paths =
      new SubjectFactory<ClassPathsSubject, Path>() {
        @Override
        public ClassPathsSubject getSubject(FailureStrategy failureStrategy, Path path) {
          return new ClassPathsSubject(failureStrategy, path);
        }
      };
}