aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/refactorings/core/AndroidTypeMoveParticipantTest.java
blob: 262ea420e6c03bc3be9ba0398b39807ddfbac4b6 (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
/*
 * 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.AdtPlugin;
import com.android.ide.eclipse.adt.internal.project.BaseProjectHelper;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy;
import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaMoveProcessor;
import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgDestinationFactory;
import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgPolicyFactory;
import org.eclipse.jdt.internal.ui.refactoring.reorg.CreateTargetQueries;
import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgQueries;
import org.eclipse.ltk.core.refactoring.participants.MoveRefactoring;
import org.eclipse.swt.widgets.Shell;


@SuppressWarnings({"javadoc", "restriction"})
public class AndroidTypeMoveParticipantTest extends RefactoringTestBase {
    public void testRefactor1() throws Exception {
        moveType(
                TEST_PROJECT2,
                "com.example.refactoringtest.CustomView1",
                "src/com/example/refactoringtest/subpackage",
                true /*updateReferences*/,

                "CHANGES:\n" +
                "-------\n" +
                "[x] Move resource 'testRefactor1/src/com/example/refactoringtest/CustomView1.java' to 'subpackage'\n" +
                "\n" +
                "[x] Move resource 'testRefactor1/src/com/example/refactoringtest/CustomView1.java' to 'subpackage'\n" +
                "\n" +
                "[x] customviews.xml - /testRefactor1/res/layout/customviews.xml\n" +
                "  @@ -9 +9\n" +
                "  -     <com.example.refactoringtest.CustomView1\n" +
                "  +     <com.example.refactoringtest.subpackage.CustomView1\n" +
                "\n" +
                "\n" +
                "[x] customviews.xml - /testRefactor1/res/layout-land/customviews.xml\n" +
                "  @@ -9 +9\n" +
                "  -     <com.example.refactoringtest.CustomView1\n" +
                "  +     <com.example.refactoringtest.subpackage.CustomView1");
    }

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

                "CHANGES:\n" +
                "-------\n" +
                "[x] Move resource 'testRefactorFragment/src/com/example/refactoringtest/MyFragment.java' to 'subpackage'\n" +
                "\n" +
                "[x] Move resource 'testRefactorFragment/src/com/example/refactoringtest/MyFragment.java' to 'subpackage'\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.subpackage.MyFragment\"/>");
    }

    public void testRefactor1_norefs() throws Exception {
        moveType(
                TEST_PROJECT2,
                "com.example.refactoringtest.CustomView1",
                "src/com/example/refactoringtest/subpackage",
                false /*updateReferences*/,

                "CHANGES:\n" +
                "-------\n" +
                "[x] Move resource 'testRefactor1_norefs/src/com/example/refactoringtest/CustomView1.java' to 'subpackage'\n" +
                "\n" +
                "[x] Move resource 'testRefactor1_norefs/src/com/example/refactoringtest/CustomView1.java' to 'subpackage'");
    }

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

    protected void moveType(
            @NonNull Object[] testData,
            @NonNull String typeFqcn,
            @NonNull String destination,
            boolean updateReferences,
            @NonNull String expected) throws Exception {
        IProject project = createProject(testData);

        IFolder destinationFolder = project.getFolder(destination);

        IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);
        assertNotNull(javaProject);
        IType type = javaProject.findType(typeFqcn);
        assertNotNull(typeFqcn, type);
        assertTrue(typeFqcn, type.exists());
        IResource resource = type.getResource();
        assertNotNull(typeFqcn, resource);
        assertTrue(typeFqcn, resource.exists());

        IResource[] resources = new IResource[] { resource };
        IJavaElement[] elements = new IJavaElement[] { type };
        IMovePolicy policy = ReorgPolicyFactory.createMovePolicy(resources, elements);
        JavaMoveProcessor processor = new JavaMoveProcessor(policy);
        processor.setUpdateReferences(updateReferences);
        processor.setUpdateQualifiedNames(true);
        assertTrue(policy.canEnable());
        processor.setDestination(ReorgDestinationFactory.createDestination(destinationFolder));
        Shell parent = AdtPlugin.getShell();
        assertNotNull(parent);
        processor.setCreateTargetQueries(new CreateTargetQueries(parent));
        processor.setReorgQueries(new ReorgQueries(parent));

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