summaryrefslogtreecommitdiff
path: root/android/testSrc/org/jetbrains/android/AndroidRenameTest.java
blob: 81b46637bef7afc5389c871e5d96ce56b6507338 (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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 *  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 org.jetbrains.android;

import com.android.SdkConstants;
import com.intellij.codeInsight.TargetElementUtilBase;
import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.refactoring.PackageWrapper;
import com.intellij.refactoring.actions.RenameElementAction;
import com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesProcessor;
import com.intellij.refactoring.move.moveClassesOrPackages.SingleSourceRootMoveDestination;
import com.intellij.refactoring.rename.*;
import com.intellij.testFramework.TestActionEvent;
import com.intellij.testFramework.fixtures.IdeaProjectTestFixture;
import com.intellij.testFramework.fixtures.TestFixtureBuilder;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Eugene.Kudelevsky
 * Date: Aug 9, 2009
 * Time: 8:50:20 PM
 * To change this template use File | Settings | File Templates.
 */
public class AndroidRenameTest extends AndroidTestCase {
  private static final String BASE_PATH = "/rename/";
  private static final String R_JAVA_PATH = "gen/p1/p2/R.java";

  public AndroidRenameTest() {
    super(false);
  }

  @Override
  public void setUp() throws Exception {
    super.setUp();
    AndroidResourceRenameResourceProcessor.ASK = false;
  }

  public void testXmlReferenceToFileResource() throws Throwable {
    createManifest();
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "layout1.xml", "res/layout/layout1.xml");
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.copyFileToProject(BASE_PATH + "pic.png", "res/drawable/pic.png");
    myFixture.copyFileToProject(BASE_PATH + "styles.xml", "res/values/styles.xml");
    myFixture.copyFileToProject("R.java", R_JAVA_PATH);
    myFixture.copyFileToProject(BASE_PATH + "RefR2.java", "src/p1/p2/RefR2.java");
    renameElementWithTextOccurences("pic1.png");
    myFixture.checkResultByFile(BASE_PATH + "layout_file_after.xml");
    myFixture.checkResultByFile(R_JAVA_PATH, "R.java", true);
    myFixture.checkResultByFile("res/values/styles.xml", BASE_PATH + "styles_after.xml", true);
    myFixture.checkResultByFile("src/p1/p2/RefR2.java", BASE_PATH + "RefR2_after.java", true);
    assertNotNull(myFixture.findFileInTempDir("res/drawable/pic1.png"));
  }

  public void testXmlReferenceToFileResource1() throws Throwable {
    createManifest();
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "layout1.xml", "res/layout/layout1.xml");
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.copyFileToProject(BASE_PATH + "pic.png", "res/drawable/pic.9.png");
    myFixture.copyFileToProject(BASE_PATH + "styles.xml", "res/values/styles.xml");
    myFixture.copyFileToProject("R.java", R_JAVA_PATH);
    myFixture.copyFileToProject(BASE_PATH + "RefR2.java", "src/p1/p2/RefR2.java");
    renameElementWithTextOccurences("pic1.9.png");
    myFixture.checkResultByFile(BASE_PATH + "layout_file_after.xml");
    myFixture.checkResultByFile(R_JAVA_PATH, "R.java", true);
    myFixture.checkResultByFile("res/values/styles.xml", BASE_PATH + "styles_after.xml", true);
    myFixture.checkResultByFile("src/p1/p2/RefR2.java", BASE_PATH + "RefR2_after.java", true);
    assertNotNull(myFixture.findFileInTempDir("res/drawable/pic1.9.png"));
  }

  public void testMoveApplicationClass() throws Throwable {
    deleteManifest();
    myFixture.copyFileToProject(BASE_PATH + "MyApplication.java", "src/p1/p2/MyApplication.java");
    VirtualFile f = myFixture.copyFileToProject(BASE_PATH + getTestName(true) + ".xml", "AndroidManifest.xml");
    myFixture.configureFromExistingVirtualFile(f);
    moveClass("p1.p2.MyApplication", "p1");
    myFixture.checkResultByFile(BASE_PATH + getTestName(true) + "_after.xml");
  }

  private void renameElementWithTextOccurences(final String newName) throws Throwable {
    new WriteCommandAction.Simple(myFixture.getProject()) {
      @Override
      protected void run() throws Throwable {
        Editor editor = myFixture.getEditor();
        PsiFile file = myFixture.getFile();
        Editor completionEditor = InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(editor, file);
        PsiElement element = TargetElementUtilBase.findTargetElement(completionEditor, TargetElementUtilBase.REFERENCED_ELEMENT_ACCEPTED |
                                                                                       TargetElementUtilBase.ELEMENT_NAME_ACCEPTED);
        assert element != null;
        final PsiElement substitution = RenamePsiElementProcessor.forElement(element).substituteElementToRename(element, editor);
        new RenameProcessor(myFixture.getProject(), substitution, newName, false, true).run();
      }
    }.execute().throwException();
  }

  private void moveClass(final String className, final String newPackageName) throws Throwable {
    new WriteCommandAction.Simple(myFixture.getProject()) {
      @Override
      protected void run() throws Throwable {
        PsiClass aClass = JavaPsiFacade.getInstance(getProject()).findClass(className, GlobalSearchScope.projectScope(getProject()));
        PsiPackage aPackage = JavaPsiFacade.getInstance(getProject()).findPackage(newPackageName);

        final PsiDirectory[] dirs = aPackage.getDirectories();
        assertEquals(dirs.length, 1);

        new MoveClassesOrPackagesProcessor(getProject(), new PsiElement[]{aClass}, new SingleSourceRootMoveDestination(
          PackageWrapper.create(JavaDirectoryService.getInstance().getPackage(dirs[0])), dirs[0]), true, true, null).run();
      }
    }.execute().throwException();
  }

  public void testXmlReferenceToValueResource() throws Throwable {
    createManifest();
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "layout2.xml", "res/layout/layout2.xml");
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.copyFileToProject(BASE_PATH + "strings.xml", "res/values/strings.xml");
    myFixture.copyFileToProject("R.java", R_JAVA_PATH);
    myFixture.copyFileToProject(BASE_PATH + "RefR1.java", "src/p1/p2/RefR1.java");
    checkAndRename("str1");
    myFixture.checkResultByFile(BASE_PATH + "layout_value_after.xml");
    myFixture.checkResultByFile(R_JAVA_PATH, "R.java", true);
    myFixture.checkResultByFile("src/p1/p2/RefR1.java", BASE_PATH + "RefR1_after.java", true);
    myFixture.checkResultByFile("res/values/strings.xml", BASE_PATH + "strings_after.xml", true);
  }

  public void testValueResource1() throws Throwable {
    doTestStringRename("strings1.xml");
  }

  public void testValueResource2() throws Throwable {
    doTestStringRename("strings2.xml");
  }

  public void testValueResource3() throws Throwable {
    doTestStringRename("strings3.xml");
  }

  public void testValueResource4() throws Throwable {
    doTestStringRename("strings4.xml");
  }

  public void testValueResource5() throws Throwable {
    String before = "strings5.xml";
    String after = "strings5_after.xml";

    createManifest();
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + before, "res/values/strings.xml");
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.renameElementAtCaretUsingHandler("str1");
    myFixture.checkResultByFile(BASE_PATH + after);
  }

  public void testStyleInheritance() throws Throwable {
    doTestStyleInheritance("styles1.xml", "styles1_after.xml");
  }

  public void testStyleInheritance1() throws Throwable {
    doTestStyleInheritance("styles2.xml", "styles2_after.xml");
  }

  public void testStyleInheritance2() throws Throwable {
    doTestStyleInheritance("styles3.xml", "styles3_after.xml");
  }

  public void testStyleInheritance3() throws Throwable {
    doTestStyleInheritance("styles4.xml", "styles4_after.xml");
  }

  public void testStyleInheritance4() throws Throwable {
    doTestStyleInheritance("styles5.xml", "styles5_after.xml", "Dilimiter.Horisontal");
  }

  public void testStyleInheritance5() throws Throwable {
    myFixture.copyFileToProject(BASE_PATH + "styles6_1.xml", "res/values-en/styles6.xml");
    myFixture.copyFileToProject(BASE_PATH + "styles6_2.xml", "res/values-ru/styles6.xml");
    doTestStyleInheritance("styles6.xml", "styles6_after.xml");
  }

  public void testStyleInheritance6() throws Throwable {
    doTestStyleInheritance("styles7.xml", "styles7_after.xml");
  }

  public void testStyleInheritance7() throws Throwable {
    myFixture.copyFileToProject(BASE_PATH + "styles8_1.xml", "res/values-en/styles8.xml");
    myFixture.copyFileToProject(BASE_PATH + "styles8_2.xml", "res/values-ru/styles8.xml");
    doTestStyleInheritance("styles8.xml", "styles8_after.xml");
  }

  public void testStyleInheritance8() throws Throwable {
    doTestStyleInheritance("styles9.xml", "styles9_after.xml");
  }

  public void testStyleInheritance9() throws Throwable {
    doTestStyleInheritance("styles10.xml", "styles10_after.xml");
  }

  private void doTestStyleInheritance(String before, String after) throws IOException {
    doTestStyleInheritance(before, after, "newStyle");
  }

  private void doTestStyleInheritance(String before, String after, String newName) throws IOException {
    createManifest();
    final VirtualFile file = myFixture.copyFileToProject(BASE_PATH + before, "res/values/" + before);
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.renameElementAtCaretUsingHandler(newName);
    myFixture.checkResultByFile(BASE_PATH + after);
  }

  private void doTestStringRename(String fileName) throws IOException {
    createManifest();
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + fileName, "res/values/strings.xml");
    myFixture.configureFromExistingVirtualFile(file);

    myFixture.copyFileToProject(BASE_PATH + "layoutStrUsage.xml", "res/layout/layoutStrUsage.xml");
    myFixture.copyFileToProject("R.java", R_JAVA_PATH);
    myFixture.renameElementAtCaretUsingHandler("str1");

    myFixture.checkResultByFile(BASE_PATH + "strings_after.xml");
    myFixture.checkResultByFile(R_JAVA_PATH, "R.java", true);
    myFixture.checkResultByFile("res/layout/layoutStrUsage.xml", BASE_PATH + "layoutStrUsage_after.xml", true);
  }

  public void testJavaReferenceToFileResource() throws Throwable {
    createManifest();
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "RefR3.java", "src/p1/p2/RefR3.java");
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.copyFileToProject("R.java", R_JAVA_PATH);
    myFixture.copyFileToProject(BASE_PATH + "layout3.xml", "res/layout/layout3.xml");
    myFixture.copyFileToProject(BASE_PATH + "pic.png", "res/drawable/pic.png");
    checkAndRename("pic1");
    myFixture.checkResultByFile(BASE_PATH + "RefR3_after.java", true);
    myFixture.checkResultByFile("res/layout/layout3.xml", BASE_PATH + "layout_file_after.xml", true);
    assertNotNull(myFixture.findFileInTempDir("res/drawable/pic1.png"));
  }

  public void testJavaReferenceToValueResource() throws Throwable {
    createManifest();
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "RefR4.java", "src/p1/p2/RefR4.java");
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.copyFileToProject("R.java", R_JAVA_PATH);
    myFixture.copyFileToProject(BASE_PATH + "layout4.xml", "res/layout/layout4.xml");
    myFixture.copyFileToProject(BASE_PATH + "strings.xml", "res/values/strings.xml");
    checkAndRename("str1");
    myFixture.checkResultByFile(BASE_PATH + "RefR4_after.java", true);
    myFixture.checkResultByFile("res/layout/layout4.xml", BASE_PATH + "layout_value_after.xml", true);
    myFixture.checkResultByFile("res/values/strings.xml", BASE_PATH + "strings_after.xml", true);
  }

  public void testXmlReferenceToId() throws Throwable {
    createManifest();
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "layout5.xml", "res/layout/layout5.xml");
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.copyFileToProject("R.java", R_JAVA_PATH);
    checkAndRename("@+id/anchor1");
    myFixture.checkResultByFile(BASE_PATH + "layout_id_after.xml");
    myFixture.checkResultByFile(R_JAVA_PATH, "R.java", true);
  }

  public void testIdDeclaration() throws Throwable {
    createManifest();
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "layout6.xml", "res/layout/layout6.xml");
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.copyFileToProject("R.java", R_JAVA_PATH);
    checkAndRename("@+id/anchor1");
    myFixture.checkResultByFile(BASE_PATH + "layout_id_after.xml");
    myFixture.checkResultByFile(R_JAVA_PATH, "R.java", true);
  }

  public void testJavaReferenceToId() throws Throwable {
    createManifest();
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "RefR7.java", "src/p1/p2/RefR7.java");
    myFixture.copyFileToProject("R.java", R_JAVA_PATH);
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.copyFileToProject(BASE_PATH + "layout7.xml", "res/layout/layout7.xml");
    checkAndRename("anchor1");
    myFixture.checkResultByFile(BASE_PATH + "RefR7_after.java", true);
    myFixture.checkResultByFile("res/layout/layout7.xml", BASE_PATH + "layout_id_after.xml", true);
  }

  public void testJavaReferenceToId1() throws Throwable {
    createManifest();
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "RefR7.java", "src/p1/p2/RefR7.java");
    myFixture.copyFileToProject("R.java", R_JAVA_PATH);
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.copyFileToProject(BASE_PATH + "layout7.xml", "res/layout/l1.xml");
    myFixture.copyFileToProject(BASE_PATH + "layout7.xml", "res/layout/l2.xml");
    checkAndRename("anchor1");
    myFixture.checkResultByFile(BASE_PATH + "RefR7_after.java", true);
    myFixture.checkResultByFile("res/layout/l1.xml", BASE_PATH + "layout_id_after.xml", true);
    myFixture.checkResultByFile("res/layout/l2.xml", BASE_PATH + "layout_id_after.xml", true);
  }

  public void testStyleable() throws Throwable {
    createManifest();
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "RefR8.java", "src/p1/p2/RefR8.java");
    myFixture.copyFileToProject("R.java", R_JAVA_PATH);
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.copyFileToProject(BASE_PATH + "attrs8.xml", "res/values/attrs8.xml");
    checkAndRename("LabelView1");
    myFixture.checkResultByFile(BASE_PATH + "RefR8_after.java", true);
    myFixture.checkResultByFile("res/values/attrs8.xml", BASE_PATH + "attrs8_after.xml", true);
  }

  public void testAttr() throws Throwable {
    createManifest();
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "RefR9.java", "src/p1/p2/RefR9.java");
    myFixture.copyFileToProject("R.java", R_JAVA_PATH);
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.copyFileToProject(BASE_PATH + "attrs9.xml", "res/values/attrs9.xml");
    checkAndRename("attr1");
    myFixture.checkResultByFile(BASE_PATH + "RefR9_after.java", true);
    myFixture.checkResultByFile("res/values/attrs9.xml", BASE_PATH + "attrs9_after.xml", true);
  }

  public void testRenameDeclareStyleableFromJava() throws Throwable {
    // Renaming an R styleable field should update the declare styleable declaration, as well as
    // any field references, including those for the attributes
    createManifest();
    myFixture.copyFileToProject(BASE_PATH + "attrs10.xml", "res/values/attrs10.xml");
    myFixture.copyFileToProject(BASE_PATH + "R_MyView.java", "src/p1/p2/R.java");

    final VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "MyView1.java", "src/p1/p2/MyView.java");
    myFixture.configureFromExistingVirtualFile(file);
    checkAndRename("NewName");
    myFixture.checkResultByFile(BASE_PATH + "MyView1_after.java", true);
    myFixture.checkResultByFile("res/values/attrs10.xml", BASE_PATH + "attrs10_after.xml", true);
  }

  public void testRenameDeclareStyleableFromXml() throws Throwable {
    // Like testRenameDeclareStyleableFromJava, but the rename request originates from
    // the XML declare-styleable reference rather than a Java field reference.
    createManifest();
    myFixture.copyFileToProject("R.java", R_JAVA_PATH);
    myFixture.copyFileToProject(BASE_PATH + "MyView4.java", "src/p1/p2/MyView.java");
    final VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "attrs13.xml", "res/values/attrs13.xml");
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.renameElementAtCaretUsingHandler("NewName");
    myFixture.checkResultByFile(BASE_PATH + "attrs13_after.xml", true);
    myFixture.checkResultByFile("src/p1/p2/MyView.java", BASE_PATH + "MyView4_after.java", true);
  }

  public void testRenameDeclareStyleableAttrFromJava() throws Throwable {
    // Renaming a styleable field should update the attrs.xml and field references
    createManifest();
    myFixture.copyFileToProject(BASE_PATH + "attrs11.xml", "res/values/attrs11.xml");
    myFixture.copyFileToProject(BASE_PATH + "R_MyView.java", "src/p1/p2/R.java");
    final VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "MyView2.java", "src/p1/p2/MyView.java");
    myFixture.configureFromExistingVirtualFile(file);
    checkAndRename("newname");
    myFixture.checkResultByFile(BASE_PATH + "MyView2_after.java", true);
    myFixture.checkResultByFile("res/values/attrs11.xml", BASE_PATH + "attrs11_after.xml", true);
  }

  public void testRenameDeclareStyleableAttrFromXml() throws Throwable {
    createManifest();
    myFixture.copyFileToProject("R.java", R_JAVA_PATH);
    myFixture.copyFileToProject(BASE_PATH + "MyView5.java", "src/p1/p2/MyView.java");
    final VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "attrs14.xml", "res/values/attrs14.xml");
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.renameElementAtCaretUsingHandler("newname");
    myFixture.checkResultByFile(BASE_PATH + "attrs14_after.xml", true);
    myFixture.checkResultByFile("src/p1/p2/MyView.java", BASE_PATH + "MyView5_after.java", true);
  }

  public void testRenameCustomView() throws Throwable {
    // Make sure renaming a custom view causes the styleable references to be updated as well
    createManifest();
    myFixture.copyFileToProject(BASE_PATH + "attrs12.xml", "res/values/attrs12.xml");
    myFixture.copyFileToProject(BASE_PATH + "R_MyView.java", "src/p1/p2/R.java");

    final VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "MyView3.java", "src/p1/p2/MyView.java");
    myFixture.configureFromExistingVirtualFile(file);
    checkAndRename("NewName");
    myFixture.checkResultByFile(BASE_PATH + "MyView3_after.java", true);
    myFixture.checkResultByFile("res/values/attrs12.xml", BASE_PATH + "attrs12_after.xml", true);
  }

  public void testFileResourceAliases1() throws Throwable {
    // Rename aliases involving drawables; ensure that they are handled correctly.
    // This tests that both the <item name="<name>"> and @layout/<name> references are
    // updated when name.xml is updated
    createManifest();
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "RefR10.java", "src/p1/p2/RefR10.java");
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.copyFileToProject("R.java", R_JAVA_PATH);
    myFixture.copyFileToProject(BASE_PATH + "pic.png", "res/drawable/pic.png");
    myFixture.copyFileToProject(BASE_PATH + "pic.png", "res/drawable/pic2.png");
    myFixture.copyFileToProject(BASE_PATH + "aliases.xml", "res/values-sw600dp/aliases.xml");
    checkAndRename("newpic");
    myFixture.checkResultByFile(BASE_PATH + "RefR10_after.java", true);
    myFixture.checkResultByFile("res/values-sw600dp/aliases.xml", BASE_PATH + "aliases_after.xml", true);
    assertNotNull(myFixture.findFileInTempDir("res/drawable/newpic.png"));
  }

  public void testFileResourceAliases2() throws Throwable {
    // Rename aliases involving layouts; this test checks that a layout reference like @layout/foo is
    // updated when foo.xml is renamed
    createManifest();
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "RefR11.java", "src/p1/p2/RefR11.java");
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.copyFileToProject("R.java", R_JAVA_PATH);
    myFixture.copyFileToProject(BASE_PATH + "layout3.xml", "res/layout/mainlayout.xml");
    myFixture.copyFileToProject(BASE_PATH + "layout3.xml", "res/layout/layout3.xml");
    //myFixture.copyFileToProject(BASE_PATH + "aliases2.xml", "res/values-sw600dp/aliases.xml");
    myFixture.copyFileToProject(BASE_PATH + "aliases2.xml", "res/values-land/aliases.xml");
    checkAndRename("newlayout");
    myFixture.checkResultByFile(BASE_PATH + "RefR11_after.java", true);
    myFixture.checkResultByFile("res/values-land/aliases.xml", BASE_PATH + "aliases2_after.xml", true);
    assertNotNull(myFixture.findFileInTempDir("res/layout/newlayout.xml"));
  }

  public void testFileResourceAliases3() throws Throwable {
    // Rename aliases involving layouts; this test checks that a resource alias' name declaration (<item name="foo" type="layout">)
    // is updated when foo.xml is renamed
    createManifest();
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "RefR11.java", "src/p1/p2/RefR11.java");
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.copyFileToProject("R.java", R_JAVA_PATH);
    myFixture.copyFileToProject(BASE_PATH + "layout3.xml", "res/layout/mainlayout.xml");
    myFixture.copyFileToProject(BASE_PATH + "layout3.xml", "res/layout/layout3.xml");
    myFixture.copyFileToProject(BASE_PATH + "aliases3.xml", "res/values-land/aliases.xml");
    checkAndRename("newlayout");
    myFixture.checkResultByFile(BASE_PATH + "RefR11_after.java", true);
    myFixture.checkResultByFile("res/values-land/aliases.xml", BASE_PATH + "aliases3_after.xml", true);
    assertNotNull(myFixture.findFileInTempDir("res/layout/newlayout.xml"));
  }

  public void testRenameComponent() throws Throwable {
    doRenameComponentTest("MyActivity1");
  }

  public void testRenameComponent2() throws Throwable {
    myFixture.copyFileToProject(BASE_PATH + "ChildActivity.java", "src/p1/p2/ChildActivity.java");
    doRenameComponentTest("MyActivity1");
  }

  @Override
  protected void configureAdditionalModules(@NotNull TestFixtureBuilder<IdeaProjectTestFixture> projectBuilder,
                                            @NotNull List<MyAdditionalModuleData> modules) {
    if ("testRenamePackageFromTestModule".equals(getName())) {
      addModuleWithAndroidFacet(projectBuilder, modules, "module1", false);
    }
  }

  public void testRenamePackage() throws Throwable {
    doRenameComponentTest("p10");
  }

  public void testRenamePackage1() throws Throwable {
    doRenameComponentTest("p20");
  }

  public void testRenamePackage2() throws Throwable {
    doRenameComponentTest("p1.p2");
  }

  public void testRenamePackage3() throws Throwable {
    doRenameComponentTest("p1");
  }

  public void testRenamePackageFromTestModule() throws Throwable {
    doRenameComponentTest("p1.p3");
    myFixture.checkResultByFile("additionalModules/module1/AndroidManifest.xml", BASE_PATH + getTestName(false) + "_module1_after.xml",
                                true);
  }

  public void testMovePackage() throws Throwable {
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p1/p2/p3");
    myFixture.copyFileToProject(BASE_PATH + "MyActivity1.java", "src/p1/p2/p3/MyActivity.java");
    doMovePackageTest("p1.p2.p3", "p1");
  }

  public void testMovePackage1() throws Throwable {
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p1/p2/p3");
    myFixture.copyFileToProject(BASE_PATH + "MyActivity1.java", "src/p1/p2/p3/MyActivity.java");
    doMovePackageTest("p1.p2.p3", "p1");
  }

  public void testMovePackage2() throws Throwable {
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p1/p2/p3");
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p1/p3");
    myFixture.copyFileToProject(BASE_PATH + "MyActivity2.java", "src/p1/p3/MyActivity.java");
    doMovePackageTest("p1.p3", "p1.p2");
  }

  public void testMovePackage3() throws Throwable {
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p1/p2/p3");
    myFixture.copyFileToProject(BASE_PATH + "MyActivity1.java", "src/p1/p2/p3/MyActivity.java");
    doMovePackageTest("p1.p2.p3", "p1");
  }

  public void testMovePackage4() throws Throwable {
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p1/p2/p3");
    myFixture.copyFileToProject(BASE_PATH + "MyActivity1.java", "src/p1/p2/p3/MyActivity.java");
    doMovePackageTest("p1.p2.p3", "p1");
  }

  public void testMovePackage5() throws Throwable {
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p1/p2/p3");
    myFixture.copyFileToProject(BASE_PATH + "MyActivity1.java", "src/p1/p2/p3/MyActivity.java");
    doMovePackageTest("p1.p2.p3", "p1");
  }

  public void testMovePackage6() throws Throwable {
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p1/p2/p4/p3");
    myFixture.copyFileToProject(BASE_PATH + "MyActivity3.java", "src/p1/p2/p4/p3/MyActivity.java");
    doMovePackageTest("p1.p2.p4", "p1");
  }

  public void testMovePackage7() throws Throwable {
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p1/p2");
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p33");
    myFixture.copyFileToProject(BASE_PATH + "MyActivity.java", "src/p1/p2/MyActivity.java");
    doMovePackageTest("p1.p2", "p33");
  }

  public void testMoveClass1() throws Throwable {
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p1/p2");
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p3");
    myFixture.copyFileToProject(BASE_PATH + "MyActivity.java", "src/p1/p2/MyActivity.java");
    doMoveClass("p1.p2.MyActivity", "p3");
  }

  public void testMoveClass2() throws Throwable {
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p1/p2");
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p1/p3");
    myFixture.copyFileToProject(BASE_PATH + "MyActivity.java", "src/p1/p2/MyActivity.java");
    doMoveClass("p1.p2.MyActivity", "p1.p3");
  }

  public void testRenameJavaPackage1() throws Throwable {
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p1/p2");
    myFixture.copyFileToProject(BASE_PATH + "MyActivity.java", "src/p1/p2/MyActivity.java");
    VirtualFile manifestFile = myFixture.copyFileToProject(BASE_PATH + getTestName(false) + ".xml", SdkConstants.FN_ANDROID_MANIFEST_XML);
    myFixture.configureFromExistingVirtualFile(manifestFile);
    doRenameJavaPackage("p1.p2", "p3");
    myFixture.checkResultByFile(BASE_PATH + getTestName(false) + "_after.xml");
  }

  public void testRenameJavaPackage2() throws Throwable {
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p1/p2");
    myFixture.copyFileToProject(BASE_PATH + "MyActivity.java", "src/p1/p2/MyActivity.java");
    VirtualFile manifestFile = myFixture.copyFileToProject(BASE_PATH + getTestName(false) + ".xml", SdkConstants.FN_ANDROID_MANIFEST_XML);
    myFixture.configureFromExistingVirtualFile(manifestFile);
    doRenameJavaPackage("p1.p2", "p3");
    myFixture.checkResultByFile(BASE_PATH + getTestName(false) + "_after.xml");
  }

  public void testRenameJavaPackage3() throws Throwable {
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p1/p2");
    myFixture.copyFileToProject(BASE_PATH + "MyActivity.java", "src/p1/p2/MyActivity.java");
    VirtualFile manifestFile = myFixture.copyFileToProject(BASE_PATH + getTestName(false) + ".xml", SdkConstants.FN_ANDROID_MANIFEST_XML);
    myFixture.configureFromExistingVirtualFile(manifestFile);
    doRenameJavaPackage("p1", "p3");
    myFixture.checkResultByFile(BASE_PATH + getTestName(false) + "_after.xml");
  }

  public void testRenameJavaPackage4() throws Throwable {
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p1/p2");
    myFixture.copyFileToProject(BASE_PATH + "MyActivity.java", "src/p1/p2/MyActivity.java");
    VirtualFile manifestFile = myFixture.copyFileToProject(BASE_PATH + getTestName(false) + ".xml", SdkConstants.FN_ANDROID_MANIFEST_XML);
    myFixture.configureFromExistingVirtualFile(manifestFile);
    doRenameJavaPackage("p1.p2", "p3");
    myFixture.checkResultByFile(BASE_PATH + getTestName(false) + "_after.xml");
  }

  public void testRenameJavaPackage5() throws Throwable {
    myFixture.copyDirectoryToProject(BASE_PATH + "empty", "src/p1/p2");
    myFixture.copyFileToProject(BASE_PATH + "MyActivity.java", "src/p1/p2/MyActivity.java");
    VirtualFile manifestFile = myFixture.copyFileToProject(BASE_PATH + getTestName(false) + ".xml", SdkConstants.FN_ANDROID_MANIFEST_XML);
    myFixture.configureFromExistingVirtualFile(manifestFile);
    doRenameJavaPackage("p1", "p3");
    myFixture.checkResultByFile(BASE_PATH + getTestName(false) + "_after.xml");
  }

  public void testRenameWidget() throws Throwable {
    createManifest();
    myFixture.copyFileToProject(BASE_PATH + "MyWidget.java", "src/p1/p2/MyWidget.java");
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "layout_widget.xml", "res/layout/layout_widget.xml");
    myFixture.configureFromExistingVirtualFile(file);
    checkAndRename("MyWidget1");
    myFixture.checkResultByFile(BASE_PATH + "layout_widget_after.xml");
  }

  public void testRenameWidget1() throws Throwable {
    createManifest();
    myFixture.copyFileToProject(BASE_PATH + "MyWidget.java", "src/p1/p2/MyWidget.java");
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "layout_widget.xml", "res/layout/layout_widget.xml");
    myFixture.configureFromExistingVirtualFile(file);
    checkAndRename("MyWidget1");
    myFixture.checkResultByFile(BASE_PATH + "layout_widget_after.xml");
  }

  public void testRenameWidgetPackage1() throws Throwable {
    createManifest();
    myFixture.copyFileToProject(BASE_PATH + "MyWidget.java", "src/p1/p2/MyWidget.java");
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "layout_widget1.xml", "res/layout/layout_widget1.xml");
    myFixture.configureFromExistingVirtualFile(file);
    checkAndRename("newPackage");
    myFixture.checkResultByFile(BASE_PATH + "layout_widget1_after.xml");
  }

  public void testMoveWidgetPackage1() throws Throwable {
    createManifest();
    myFixture.copyFileToProject(BASE_PATH + "Dummy.java", "src/p1/newp/Dummy.java");
    myFixture.copyFileToProject(BASE_PATH + "MyWidget.java", "src/p1/p2/MyWidget.java");
    myFixture.copyFileToProject(BASE_PATH + "MyPreference.java", "src/p1/p2/MyPreference.java");
    final VirtualFile f = myFixture.copyFileToProject(BASE_PATH + "layout_widget2.xml", "res/layout/layout_widget2.xml");
    myFixture.configureFromExistingVirtualFile(f);
    myFixture.copyFileToProject(BASE_PATH + "custom_pref.xml", "res/xml/custom_pref.xml");
    doMovePackage("p1.p2", "p1.newp");
    myFixture.checkResultByFile("res/layout/layout_widget2.xml", BASE_PATH + "layout_widget2_after.xml", false);
    myFixture.checkResultByFile("res/xml/custom_pref.xml", BASE_PATH + "custom_pref_after.xml", false);
  }

  public void testRenameInlineIdDeclarations() throws Throwable {
    createManifest();
    VirtualFile file = myFixture.copyFileToProject(BASE_PATH + "layout15.xml", "res/layout/layout15.xml");
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.copyFileToProject("R.java", R_JAVA_PATH);
    checkAndRename("newname");
    myFixture.checkResultByFile(BASE_PATH + "layout15_after.xml");
  }

  private void doMovePackageTest(String packageName, String newPackageName) throws Exception {
    VirtualFile manifestFile = myFixture.copyFileToProject(BASE_PATH + getTestName(false) + ".xml", SdkConstants.FN_ANDROID_MANIFEST_XML);
    myFixture.configureFromExistingVirtualFile(manifestFile);
    doMovePackage(packageName, newPackageName);
    myFixture.checkResultByFile(BASE_PATH + getTestName(false) + "_after.xml");
  }

  private void doRenameComponentTest(String newName) {
    myFixture.copyFileToProject(BASE_PATH + "MyActivity.java", "src/p1/p2/MyActivity.java");
    VirtualFile manifestFile = myFixture.copyFileToProject(BASE_PATH + getTestName(false) + ".xml", SdkConstants.FN_ANDROID_MANIFEST_XML);
    myFixture.configureFromExistingVirtualFile(manifestFile);
    checkAndRename(newName);
    myFixture.checkResultByFile(BASE_PATH + getTestName(false) + "_after.xml");
  }

  private void doMovePackage(String packageName, String newPackageName) throws Exception {
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
    final PsiPackage aPackage = facade.findPackage(packageName);
    final PsiPackage newParentPackage = facade.findPackage(newPackageName);

    assertNotNull(newParentPackage);
    final PsiDirectory[] dirs = newParentPackage.getDirectories();
    assertEquals(dirs.length, 1);

    new MoveClassesOrPackagesProcessor(getProject(), new PsiElement[]{aPackage},
                                       new SingleSourceRootMoveDestination(PackageWrapper.create(newParentPackage), dirs[0]),
                                       true, false, null).run();
    FileDocumentManager.getInstance().saveAllDocuments();
  }

  private void doMoveClass(String className, String newParentPackageName) throws Exception {
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
    final PsiClass psiClass = facade.findClass(className, GlobalSearchScope.projectScope(getProject()));
    final PsiPackage newParentPackage = facade.findPackage(newParentPackageName);

    assertNotNull(newParentPackage);
    final PsiDirectory[] dirs = newParentPackage.getDirectories();
    assertEquals(dirs.length, 1);

    new MoveClassesOrPackagesProcessor(getProject(), new PsiElement[]{psiClass},
                                       new SingleSourceRootMoveDestination(PackageWrapper.create(newParentPackage), dirs[0]),
                                       true, false, null).run();
    FileDocumentManager.getInstance().saveAllDocuments();
  }

  private void doRenameJavaPackage(String packageName, String newPackageName) throws Exception {
    final PsiPackage aPackage = JavaPsiFacade.getInstance(getProject()).findPackage(packageName);
    assertNotNull("Package " + packageName + " not found", aPackage);
    new RenameProcessor(getProject(), aPackage, newPackageName, true, true).run();
    FileDocumentManager.getInstance().saveAllDocuments();
  }

  private void checkAndRename(String newName) {
    final RenameElementAction action = new RenameElementAction();
    final AnActionEvent e = new TestActionEvent(DataManager.getInstance().getDataContext(myFixture.getEditor().getComponent()), action);
    action.update(e);
    assertTrue(e.getPresentation().isEnabled() && e.getPresentation().isVisible());
    // Note: This fails when trying to rename XML attribute values: Use myFixture.renameElementAtCaretUsingHandler() instead!
    myFixture.renameElementAtCaret(newName);
  }
}