aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/android/tools/r8/naming/UseUniqueMemberNameTest.java
blob: 498f4dfecab2abedbcbca6e01737276e6d989c66 (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
// Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.naming;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import com.android.tools.r8.graph.DexField;
import com.android.tools.r8.graph.DexItemFactory;
import com.android.tools.r8.graph.DexMethod;
import com.android.tools.r8.graph.DexType;
import com.android.tools.r8.utils.ListUtils;
import com.android.tools.r8.utils.Timing;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class UseUniqueMemberNameTest extends NamingTestBase {

  public UseUniqueMemberNameTest(
      String test,
      List<String> keepRulesFiles,
      BiConsumer<DexItemFactory, NamingLens> inspection) {
    super(test, keepRulesFiles, inspection, new Timing("UseUniqueMemberNameTest"));
  }

  @Test
  public void minifierTest() throws Exception {
    NamingLens naming = runMinifier(ListUtils.map(keepRulesFiles, Paths::get));
    inspection.accept(dexItemFactory, naming);
  }

  @Parameters(name = "test: {0} keep: {1}")
  public static Collection<Object[]> data() {
    List<String> tests = Arrays.asList("uniquemembernames");

    Map<String, BiConsumer<DexItemFactory, NamingLens>> inspections = new HashMap<>();
    inspections.put("uniquemembernames:keep-rules-1.txt", UseUniqueMemberNameTest::test00_rule1);
    inspections.put("uniquemembernames:keep-rules-2.txt", UseUniqueMemberNameTest::test00_rule2);

    return createTests(tests, inspections);
  }

  private static void test00_rule1(DexItemFactory dexItemFactory, NamingLens naming) {
    DexType a = dexItemFactory.createType("Luniquemembernames/ClsA;");
    assertNotEquals("Luniquemembernames/ClsA;", naming.lookupDescriptor(a).toSourceString());

    DexMethod foo = dexItemFactory.createMethod(
        a, dexItemFactory.createProto(dexItemFactory.intType), "foo");
    String foo_renamed = naming.lookupName(foo).toSourceString();
    assertNotEquals("foo", foo_renamed);

    DexType aa = dexItemFactory.createType("Luniquemembernames/AnotherCls;");
    assertNotEquals("Luniquemembernames/AnotherCls;", naming.lookupDescriptor(aa).toSourceString());

    DexMethod another_foo = dexItemFactory.createMethod(
        aa, dexItemFactory.createProto(dexItemFactory.intType), "foo");
    String another_foo_renamed = naming.lookupName(another_foo).toSourceString();
    assertNotEquals("foo", another_foo_renamed);

    // BaseCls#a and AnotherCls#b are kept.
    // Due to BaseCls#a, BaseCls#foo should not be renamed to a.
    // On the other hand, AnotherCls#foo should be renamed to a.
    assertNotEquals(foo_renamed, another_foo_renamed);

    DexType base = dexItemFactory.createType("Luniquemembernames/BaseCls;");
    DexField f2 = dexItemFactory.createField(base, dexItemFactory.doubleType, "f2");
    DexField another_f2 = dexItemFactory.createField(aa, dexItemFactory.intType, "f2");
    // Fields f2's are only fields that are allowed to be renamed. Thus, they would be renamed to
    // the same name as long as R8 is deterministic.
    assertEquals(
        naming.lookupName(f2).toSourceString(),
        naming.lookupName(another_f2).toSourceString());
  }

  // -useuniqueclassmembernames
  private static void test00_rule2(DexItemFactory dexItemFactory, NamingLens naming) {
    DexType a = dexItemFactory.createType("Luniquemembernames/ClsA;");
    assertNotEquals("Luniquemembernames/ClsA;", naming.lookupDescriptor(a).toSourceString());

    DexMethod foo = dexItemFactory.createMethod(
        a, dexItemFactory.createProto(dexItemFactory.intType), "foo");
    String foo_renamed = naming.lookupName(foo).toSourceString();
    assertNotEquals("foo", foo_renamed);

    DexType aa = dexItemFactory.createType("Luniquemembernames/AnotherCls;");
    assertNotEquals("Luniquemembernames/AnotherCls;", naming.lookupDescriptor(aa).toSourceString());

    DexMethod another_foo = dexItemFactory.createMethod(
        aa, dexItemFactory.createProto(dexItemFactory.intType), "foo");
    String another_foo_renamed = naming.lookupName(another_foo).toSourceString();
    assertNotEquals("foo", another_foo_renamed);

    // With -useuniquemembernames, foo() with the same signature should be renamed to the same name.
    assertEquals(foo_renamed, another_foo_renamed);
    // But, those cannot be renamed to c and b, as those are _globally_ reserved.
    assertNotEquals("c", foo_renamed);
    assertNotEquals("c", another_foo_renamed);
    assertNotEquals("b", foo_renamed);
    assertNotEquals("b", another_foo_renamed);

    DexType base = dexItemFactory.createType("Luniquemembernames/BaseCls;");
    DexField f2 = dexItemFactory.createField(base, dexItemFactory.doubleType, "f2");
    DexField another_f2 = dexItemFactory.createField(aa, dexItemFactory.intType, "f2");
    // They should be renamed differently even w/ -useuniqueclassmembernames.
    assertNotEquals(
        naming.lookupName(f2).toSourceString(),
        naming.lookupName(another_f2).toSourceString());
  }

}