aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/RefactoringAssistantTest.java
blob: b22717991ee1801eb03acde3f914025e2364942e (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
/*
 * Copyright (C) 2011 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.editors.layout.refactoring;

import static com.android.SdkConstants.FD_RES;
import static com.android.SdkConstants.FD_RES_LAYOUT;

import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

public class RefactoringAssistantTest extends AdtProjectTest {
    public void testAssistant1() throws Exception {
        // "Extract String"
        checkFixes("sample1a.xml", "<Button android:text=\"Fir^stButton\"");
    }

    public void testAssistant2() throws Exception {
        // Visual refactoring operations
        checkFixes("sample1a.xml", "<Bu^tton android:text");
    }

    public void testAssistant3() throws Exception {
        checkFixes("sample1a.xml", "<Button andr^oid:text=\"FirstButton\"");
    }

    public void testAssistant4() throws Exception {
        // Check for resource rename refactoring (and don't offer extract string)
        checkFixes("sample1a.xml", "android:id=\"@+id/Linea^rLayout2\"");
    }

    private void checkFixes(String name, String caretLocation)
            throws Exception {
        IProject project = getProject();
        IFile file = getTestDataFile(project, name, FD_RES + "/" + FD_RES_LAYOUT + "/" + name);

        // Determine the offset
        String fileContent = AdtPlugin.readFile(file);
        int caretDelta = caretLocation.indexOf("^");
        assertTrue(caretLocation, caretDelta != -1);
        String caretContext = caretLocation.substring(0, caretDelta)
                + caretLocation.substring(caretDelta + "^".length());
        int caretContextIndex = fileContent.indexOf(caretContext);
        assertTrue("Caret content " + caretContext + " not found in file",
                caretContextIndex != -1);
        final int offset = caretContextIndex + caretDelta;


        RefactoringAssistant refactoringAssistant = new RefactoringAssistant();

        // Open file
        IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        assertNotNull(page);
        IEditorPart editor = IDE.openEditor(page, file);
        assertTrue(editor instanceof AndroidXmlEditor);
        AndroidXmlEditor layoutEditor = (AndroidXmlEditor) editor;
        final ISourceViewer viewer = layoutEditor.getStructuredSourceViewer();

        IQuickAssistInvocationContext invocationContext = new IQuickAssistInvocationContext() {
            @Override
            public int getLength() {
                return 0;
            }

            @Override
            public int getOffset() {
                return offset;
            }

            @Override
            public ISourceViewer getSourceViewer() {
                return viewer;
            }
        };
        ICompletionProposal[] proposals = refactoringAssistant
                .computeQuickAssistProposals(invocationContext);

        if (proposals != null) {
            for (ICompletionProposal proposal : proposals) {
                assertNotNull(proposal.getAdditionalProposalInfo());
                assertNotNull(proposal.getImage());
            }
        }

        StringBuilder sb = new StringBuilder(1000);
        sb.append("Quick assistant in " + name + " for " + caretLocation + ":\n");
        if (proposals != null) {
            for (ICompletionProposal proposal : proposals) {
                sb.append(proposal.getDisplayString());
                String help = proposal.getAdditionalProposalInfo();
                if (help != null && help.trim().length() > 0) {
                    sb.append(" : ");
                    sb.append(help.replace('\n', ' '));
                }
                sb.append('\n');
            }
        } else {
            sb.append("None found.\n");
        }
        assertEqualsGolden(name, sb.toString(), "txt");

        // No "apply" test on these assists since they are interactive. Refactoring
        // is tested elsewhere.
    }
}