aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/refactorings/core/RenameResourceParticipantTest.java
blob: 3f029b93b4a97af11c1bedddee8f05545bf99aa1 (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
/*
 * 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.common.resources.ResourceUrl;
import com.android.ide.eclipse.adt.internal.project.BaseProjectHelper;
import com.android.resources.ResourceType;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.internal.corext.refactoring.rename.RenameFieldProcessor;
import org.eclipse.ltk.core.refactoring.participants.RenameProcessor;
import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring;

@SuppressWarnings({"javadoc", "restriction"})
public class RenameResourceParticipantTest extends RefactoringTestBase {
    public void testRefactor1() throws Exception {
        renameResource(
                TEST_PROJECT,
                "@string/app_name",
                true /*updateReferences*/,
                "myname",

                "CHANGES:\n" +
                "-------\n" +
                "[x] strings.xml - /testRefactor1/res/values/strings.xml\n" +
                "  @@ -4 +4\n" +
                "  -     <string name=\"app_name\">RefactoringTest</string>\n" +
                "  +     <string name=\"myname\">RefactoringTest</string>\n" +
                "\n" +
                "\n" +
                "[ ] R.java - /testRefactor1/gen/com/example/refactoringtest/R.java\n" +
                "  @@ -29 +29\n" +
                "  -         public static final int app_name=0x7f040000;\n" +
                "  +         public static final int myname=0x7f040000;\n" +
                "\n" +
                "\n" +
                "[x] AndroidManifest.xml - /testRefactor1/AndroidManifest.xml\n" +
                "  @@ -13 +13\n" +
                "  -         android:label=\"@string/app_name\"\n" +
                "  +         android:label=\"@string/myname\"\n" +
                "  @@ -17 +17\n" +
                "  -             android:label=\"@string/app_name\" >\n" +
                "  +             android:label=\"@string/myname\" >");
    }

    public void testRefactor2() throws Exception {
        renameResource(
                TEST_PROJECT,
                "@+id/menu_settings",
                true /*updateReferences*/,
                "new_id_for_the_action_bar",

                "CHANGES:\n" +
                "-------\n" +
                "[x] activity_main.xml - /testRefactor2/res/menu/activity_main.xml\n" +
                "  @@ -4 +4\n" +
                "  -         android:id=\"@+id/menu_settings\"\n" +
                "  +         android:id=\"@+id/new_id_for_the_action_bar\"\n" +
                "\n" +
                "\n" +
                "[ ] R.java - /testRefactor2/gen/com/example/refactoringtest/R.java\n" +
                "  @@ -19 +19\n" +
                "  -         public static final int menu_settings=0x7f070003;\n" +
                "  +         public static final int new_id_for_the_action_bar=0x7f070003;");
    }

    public void testRefactor3() throws Exception {
        renameResource(
                TEST_PROJECT,
                "@+id/textView1",
                true /*updateReferences*/,
                "output",

                "CHANGES:\n" +
                "-------\n" +
                "[x] activity_main.xml - /testRefactor3/res/layout/activity_main.xml\n" +
                "  @@ -8 +8\n" +
                "  -         android:id=\"@+id/textView1\"\n" +
                "  +         android:id=\"@+id/output\"\n" +
                "  @@ -19 +19\n" +
                "  -         android:layout_alignLeft=\"@+id/textView1\"\n" +
                "  -         android:layout_below=\"@+id/textView1\"\n" +
                "  +         android:layout_alignLeft=\"@+id/output\"\n" +
                "  +         android:layout_below=\"@+id/output\"\n" +
                "\n" +
                "\n" +
                "[x] MainActivity.java - /testRefactor3/src/com/example/refactoringtest/MainActivity.java\n" +
                "  @@ -14 +14\n" +
                "  -         View view1 = findViewById(R.id.textView1);\n" +
                "  +         View view1 = findViewById(R.id.output);\n" +
                "\n" +
                "\n" +
                "[ ] R.java - /testRefactor3/gen/com/example/refactoringtest/R.java\n" +
                "  @@ -20 +20\n" +
                "  -         public static final int textView1=0x7f070000;\n" +
                "  +         public static final int output=0x7f070000;");
    }

    public void testRefactor4() throws Exception {
        renameResource(
                TEST_PROJECT,
                // same as testRefactor3, but use @id rather than @+id even though @+id is in file
                "@id/textView1",
                true /*updateReferences*/,
                "output",

                "CHANGES:\n" +
                "-------\n" +
                "[x] activity_main.xml - /testRefactor4/res/layout/activity_main.xml\n" +
                "  @@ -8 +8\n" +
                "  -         android:id=\"@+id/textView1\"\n" +
                "  +         android:id=\"@+id/output\"\n" +
                "  @@ -19 +19\n" +
                "  -         android:layout_alignLeft=\"@+id/textView1\"\n" +
                "  -         android:layout_below=\"@+id/textView1\"\n" +
                "  +         android:layout_alignLeft=\"@+id/output\"\n" +
                "  +         android:layout_below=\"@+id/output\"\n" +
                "\n" +
                "\n" +
                "[x] MainActivity.java - /testRefactor4/src/com/example/refactoringtest/MainActivity.java\n" +
                "  @@ -14 +14\n" +
                "  -         View view1 = findViewById(R.id.textView1);\n" +
                "  +         View view1 = findViewById(R.id.output);\n" +
                "\n" +
                "\n" +
                "[ ] R.java - /testRefactor4/gen/com/example/refactoringtest/R.java\n" +
                "  @@ -20 +20\n" +
                "  -         public static final int textView1=0x7f070000;\n" +
                "  +         public static final int output=0x7f070000;");
    }

    public void testRefactor5() throws Exception {
        renameResource(
                TEST_PROJECT,
                "@layout/activity_main",
                true /*updateReferences*/,
                "newlayout",

                "CHANGES:\n" +
                "-------\n" +
                "[x] MainActivity.java - /testRefactor5/src/com/example/refactoringtest/MainActivity.java\n" +
                "  @@ -13 +13\n" +
                "  -         setContentView(R.layout.activity_main);\n" +
                "  +         setContentView(R.layout.newlayout);\n" +
                "\n" +
                "\n" +
                "[ ] R.java - /testRefactor5/gen/com/example/refactoringtest/R.java\n" +
                "  @@ -23 +23\n" +
                "  -         public static final int activity_main=0x7f030000;\n" +
                "  +         public static final int newlayout=0x7f030000;\n" +
                "\n" +
                "\n" +
                "[x] Rename 'testRefactor5/res/layout/activity_main.xml' to 'newlayout.xml'\n" +
                "\n" +
                "[x] Rename 'testRefactor5/res/layout-land/activity_main.xml' to 'newlayout.xml'");
    }

    public void testRefactor6() throws Exception {
        renameResource(
                TEST_PROJECT,
                "@drawable/ic_launcher",
                true /*updateReferences*/,
                "newlauncher",

                "CHANGES:\n" +
                "-------\n" +
                "[ ] R.java - /testRefactor6/gen/com/example/refactoringtest/R.java\n" +
                "  @@ -14 +14\n" +
                "  -         public static final int ic_launcher=0x7f020000;\n" +
                "  +         public static final int newlauncher=0x7f020000;\n" +
                "\n" +
                "\n" +
                "[x] Rename 'testRefactor6/res/drawable-xhdpi/ic_launcher.png' to 'newlauncher.png'\n" +
                "\n" +
                "[x] Rename 'testRefactor6/res/drawable-mdpi/ic_launcher.png' to 'newlauncher.png'\n" +
                "\n" +
                "[x] Rename 'testRefactor6/res/drawable-ldpi/ic_launcher.png' to 'newlauncher.png'\n" +
                "\n" +
                "[x] Rename 'testRefactor6/res/drawable-hdpi/ic_launcher.png' to 'newlauncher.png'\n" +
                "\n" +
                "[x] AndroidManifest.xml - /testRefactor6/AndroidManifest.xml\n" +
                "  @@ -12 +12\n" +
                "  -         android:icon=\"@drawable/ic_launcher\"\n" +
                "  +         android:icon=\"@drawable/newlauncher\"");
    }

    public void testRefactor7() throws Exception {
        // Test refactoring initiated on a file rename
        IProject project = createProject(TEST_PROJECT);
        IFile file = project.getFile("res/layout/activity_main.xml");
        renameResource(
                project,
                file,
                true /*updateReferences*/,
                "newlayout",

                "CHANGES:\n" +
                "-------\n" +
                "[x] MainActivity.java - /testRefactor7/src/com/example/refactoringtest/MainActivity.java\n" +
                "  @@ -13 +13\n" +
                "  -         setContentView(R.layout.activity_main);\n" +
                "  +         setContentView(R.layout.newlayout);\n" +
                "\n" +
                "\n" +
                "[ ] R.java - /testRefactor7/gen/com/example/refactoringtest/R.java\n" +
                "  @@ -23 +23\n" +
                "  -         public static final int activity_main=0x7f030000;\n" +
                "  +         public static final int newlayout=0x7f030000;\n" +
                "\n" +
                "\n" +
                "[x] Rename 'testRefactor7/res/layout-land/activity_main.xml' to 'newlayout.xml'\n" +
                "\n" +
                "[x] Rename 'testRefactor7/res/layout/activity_main.xml' to 'newlayout.xml'",
                null);
    }

    public void testRefactor8() throws Exception {
        // Test refactoring initiated on a Java field rename
        IProject project = createProject(TEST_PROJECT);
        IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);
        assertNotNull(javaProject);
        IType type = javaProject.findType("com.example.refactoringtest.R.layout");
        if (type == null || !type.exists()) {
            type = javaProject.findType("com.example.refactoringtest.R$layout");
            System.out.println("Had to switch to $ notation");
        }
        assertNotNull(type);
        assertTrue(type.exists());
        IField field = type.getField("activity_main");
        assertNotNull(field);
        assertTrue(field.exists());

        renameResource(
                project,
                field,
                true /*updateReferences*/,
                "newlauncher",

                "CHANGES:\n" +
                "-------\n" +
                "[x] Rename 'testRefactor8/res/layout/activity_main.xml' to 'newlauncher.xml'\n" +
                "\n" +
                "[x] Rename 'testRefactor8/res/layout-land/activity_main.xml' to 'newlauncher.xml'\n" +
                "\n" +
                "[x] MainActivity.java - /testRefactor8/src/com/example/refactoringtest/MainActivity.java\n" +
                "  @@ -13 +13\n" +
                "  -         setContentView(R.layout.activity_main);\n" +
                "  +         setContentView(R.layout.newlauncher);\n" +
                "\n" +
                "\n" +
                "[ ] R.java - /testRefactor8/gen/com/example/refactoringtest/R.java\n" +
                "  @@ -23 +23\n" +
                "  -         public static final int activity_main=0x7f030000;\n" +
                "  +         public static final int newlauncher=0x7f030000;",
                null);
    }

    public void testInvalidName() throws Exception {
        renameResource(
                TEST_PROJECT,
                "@drawable/ic_launcher",
                true /*updateReferences*/,
                "Newlauncher",

                "",
                "<ERROR\n" +
                "\t\n" +
                "ERROR: File-based resource names must start with a lowercase letter.\n" +
                "Context: <Unspecified context>\n" +
                "code: none\n" +
                "Data: null\n" +
                ">");
    }

    public void testRefactor9() throws Exception {
        // same as testRefactor4, but not updating references
        renameResource(
                TEST_PROJECT,
                "@id/textView1",
                false /*updateReferences*/,
                "output",

                "CHANGES:\n" +
                "-------\n" +
                "[x] activity_main.xml - /testRefactor9/res/layout/activity_main.xml\n" +
                "  @@ -8 +8\n" +
                "  -         android:id=\"@+id/textView1\"\n" +
                "  +         android:id=\"@+id/output\"\n" +
                "\n" +
                "\n" +
                "[ ] R.java - /testRefactor9/gen/com/example/refactoringtest/R.java\n" +
                "  @@ -20 +20\n" +
                "  -         public static final int textView1=0x7f070000;\n" +
                "  +         public static final int output=0x7f070000;");
    }

    public void testRefactor10() throws Exception {
        // Check updating tools: attributes
        renameResource(
                TEST_PROJECT,
                "@layout/preview",
                true /*updateReferences*/,
                "newlayout",

                "CHANGES:\n" +
                "-------\n" +
                "[x] activity_main.xml - /testRefactor10/res/layout-land/activity_main.xml\n" +
                "  @@ -10 +10\n" +
                "  -         tools:listitem=\"@layout/preview\" >\n" +
                "  +         tools:listitem=\"@layout/newlayout\" >\n" +
                "  @@ -17 +17\n" +
                "  -         tools:layout=\"@layout/preview\" />\n" +
                "  +         tools:layout=\"@layout/newlayout\" />");
    }

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

    protected void renameResource(
            @NonNull Object[] testData,
            @NonNull Object resource,
            boolean updateReferences,
            @NonNull String newName,
            @NonNull String expected) throws Exception {
        renameResource(testData, resource, updateReferences, newName, expected, null);
    }

    protected void renameResource(
            @NonNull Object[] testData,
            @NonNull Object resource,
            boolean updateReferences,
            @NonNull String newName,
            @NonNull String expected,
            @NonNull String expectedWarnings) throws Exception {
        IProject project = createProject(testData);
        renameResource(project, resource, updateReferences, newName, expected, expectedWarnings);
    }

    protected void renameResource(
            @NonNull IProject project,
            @NonNull Object resource,
            boolean updateReferences,
            @NonNull String newName,
            @NonNull String expected,
            @NonNull String expectedWarnings) throws Exception {
        RenameProcessor processor = null;
        if (resource instanceof String) {
            String url = (String) resource;
            assert url.startsWith("@") : resource;

            ResourceUrl parsedUrl = ResourceUrl.parse(url);
            assertNotNull(url, parsedUrl);
            assert parsedUrl != null; // null analysis doesn't recognize assertNotNull
            ResourceType type = parsedUrl.type;
            String currentName = parsedUrl.name;
            RenameResourceProcessor p;
            p = new RenameResourceProcessor(project, type, currentName, newName);
            p.setUpdateReferences(updateReferences);
            processor = p;
        } else if (resource instanceof IResource) {
            IResource r = (IResource) resource;
            org.eclipse.ltk.internal.core.refactoring.resource.RenameResourceProcessor p;
            p = new org.eclipse.ltk.internal.core.refactoring.resource.RenameResourceProcessor(r);
            String fileName = r.getName();
            int dot = fileName.indexOf('.');
            String extension = (dot != -1) ? fileName.substring(dot) : "";
            p.setNewResourceName(newName + extension);
            p.setUpdateReferences(updateReferences);
            processor = p;
        } else if (resource instanceof IField) {
            RenameFieldProcessor p = new RenameFieldProcessor((IField) resource);
            p.setNewElementName(newName);
            p.setUpdateReferences(updateReferences);
            processor = p;
        } else {
            fail("Unsupported resource element in tests: " + resource);
        }

        assertNotNull(processor);

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