aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/refactorings/core/AndroidTypeRenameParticipantTest.java
blob: f65124a2c8f8fc8451d7b258ed8d31a63c273e50 (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
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
 *
 * 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.android.ide.eclipse.adt.internal.refactorings.core;

import com.android.annotations.NonNull;
import com.android.ide.eclipse.adt.internal.project.BaseProjectHelper;

import org.eclipse.core.resources.IProject;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.internal.corext.refactoring.rename.RenameTypeProcessor;
import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring;


@SuppressWarnings({"javadoc", "restriction"})
public class AndroidTypeRenameParticipantTest extends RefactoringTestBase {
    public void testRefactor1() throws Exception {
        renameType(
                TEST_PROJECT,
                "com.example.refactoringtest.MainActivity",
                true /*updateReferences*/,
                "NewActivityName",

                "CHANGES:\n" +
                "-------\n" +
                "[x] Rename compilation unit 'MainActivity.java' to 'NewActivityName.java'\n" +
                "\n" +
                "[x] activity_main.xml - /testRefactor1/res/layout/activity_main.xml\n" +
                "  @@ -5 +5\n" +
                "  -     tools:context=\".MainActivity\" >\n" +
                "  +     tools:context=\".NewActivityName\" >\n" +
                "\n" +
                "\n" +
                "[x] activity_main.xml - /testRefactor1/res/layout-land/activity_main.xml\n" +
                "  @@ -5 +5\n" +
                "  -     tools:context=\".MainActivity\" >\n" +
                "  +     tools:context=\".NewActivityName\" >\n" +
                "\n" +
                "\n" +
                "[x] AndroidManifest.xml - /testRefactor1/AndroidManifest.xml\n" +
                "  @@ -16 +16\n" +
                "  -             android:name=\"com.example.refactoringtest.MainActivity\"\n" +
                "  +             android:name=\"com.example.refactoringtest.NewActivityName\"");
    }

    public void testRefactor1b() throws Exception {
        renameType(
                TEST_PROJECT,
                "com.example.refactoringtest.MainActivity2",
                true /*updateReferences*/,
                "NewActivityName",

                "CHANGES:\n" +
                "-------\n" +
                "[x] Rename compilation unit 'MainActivity2.java' to 'NewActivityName.java'\n" +
                "\n" +
                "[x] AndroidManifest.xml - /testRefactor1b/AndroidManifest.xml\n" +
                "  @@ -25 +25\n" +
                "  -             android:name=\".MainActivity2\"\n" +
                "  +             android:name=\".NewActivityName\"");
    }

    public void testRefactor1_noreferences() throws Exception {
        renameType(
                TEST_PROJECT,
                "com.example.refactoringtest.MainActivity",
                false /*updateReferences*/,
                "NewActivityName",

                "CHANGES:\n" +
                "-------\n" +
                "[x] Rename compilation unit 'MainActivity.java' to 'NewActivityName.java'");
    }

    public void testRefactor2() throws Exception {
        renameType(
                TEST_PROJECT2,
                "com.example.refactoringtest.CustomView1",
                true /*updateReferences*/,
                "NewCustomViewName",

                "CHANGES:\n" +
                "-------\n" +
                "[x] Rename compilation unit 'CustomView1.java' to 'NewCustomViewName.java'\n" +
                "\n" +
                "[x] attrs_custom_view.xml - /testRefactor2/res/values/attrs_custom_view.xml\n" +
                "  @@ -4 +4\n" +
                "  -     <declare-styleable name=\"CustomView1\">\n" +
                "  +     <declare-styleable name=\"NewCustomViewName\">\n" +
                "\n" +
                "\n" +
                "[x] customviews.xml - /testRefactor2/res/layout/customviews.xml\n" +
                "  @@ -9 +9\n" +
                "  -     <com.example.refactoringtest.CustomView1\n" +
                "  +     <com.example.refactoringtest.NewCustomViewName\n" +
                "\n" +
                "\n" +
                "[x] customviews.xml - /testRefactor2/res/layout-land/customviews.xml\n" +
                "  @@ -9 +9\n" +
                "  -     <com.example.refactoringtest.CustomView1\n" +
                "  +     <com.example.refactoringtest.NewCustomViewName");
    }

    public void testRefactorFragment() throws Exception {
        renameType(
            TEST_PROJECT2,
            "com.example.refactoringtest.MyFragment",
            true /*updateReferences*/,
            "NewFragmentName",

            "CHANGES:\n" +
            "-------\n" +
            "[x] Rename compilation unit 'MyFragment.java' to 'NewFragmentName.java'\n" +
            "\n" +
            "[x] activity_main.xml - /testRefactorFragment/res/layout/activity_main.xml\n" +
            "  @@ -33 +33\n" +
            "  -     <fragment android:name=\"com.example.refactoringtest.MyFragment\"/>\n" +
            "  +     <fragment android:name=\"com.example.refactoringtest.NewFragmentName\"/>");
    }

    // ---- Test infrastructure ----

    protected void renameType(
            @NonNull Object[] testData,
            @NonNull String typeFqcn,
            boolean updateReferences,
            @NonNull String newName,
            @NonNull String expected) throws Exception {
        IProject project = createProject(testData);
        IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);
        assertNotNull(javaProject);
        IType type = javaProject.findType(typeFqcn);
        assertNotNull(typeFqcn, type);
        assertTrue(typeFqcn, type.exists());
        RenameTypeProcessor processor = new RenameTypeProcessor(type);
        processor.setNewElementName(newName);
        processor.setUpdateQualifiedNames(true);
        processor.setUpdateSimilarDeclarations(false);
        //processor.setMatchStrategy(?);
        //processor.setFilePatterns(patterns);
        processor.setUpdateReferences(updateReferences);
        assertNotNull(processor);

        RenameRefactoring refactoring = new RenameRefactoring(processor);
        checkRefactoring(refactoring, expected);
    }
}