aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/AndroidXmlCharacterMatcherTest.java
blob: acb2b049b66acf4e1cff113e4d4674926f4a1de4 (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
/*
 * 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;

import com.android.ide.eclipse.adt.internal.editors.layout.refactoring.AdtProjectTest;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.IRegion;
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;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;

@SuppressWarnings("restriction")
public class AndroidXmlCharacterMatcherTest extends AdtProjectTest {
    public void testGotoMatchingFwd1() throws Exception {
        checkGotoMatching(
                "<app^lication android:icon",
                "^</application>");
    }

    public void testGotoMatchingFwd2() throws Exception {
        checkGotoMatching(
                "^<application android:icon",
                "^</application>");
    }

    public void testGotoMatchingFwd3() throws Exception {
        checkGotoMatching(
                "<application^ android:icon",
                "^</application>");
    }

    public void testGotoMatchingBwd1() throws Exception {
        checkGotoMatching(
                "^</application>",
                "^<application android:icon");
    }

    public void testGotoMatchingBwd2() throws Exception {
        checkGotoMatching(
                "<^/application>",
                "^<application android:icon");
    }

    public void testGotoMatchingBwd3() throws Exception {
        checkGotoMatching(
                "</^application>",
                "^<application android:icon");
    }

    public void testGotoMatchingBwd4() throws Exception {
        checkGotoMatching(
                "</app^lication>",
                "^<application android:icon");
    }

    public void testGotoMatchingBwd5() throws Exception {
        checkGotoMatching(
                "</^application>",
                "^<application android:icon");
    }

    public void testGotoMatchingBwd6() throws Exception {
        checkGotoMatching(
                "</^application>",
                "^<application android:icon");
    }

    public void testGotoMatchingFwd4() throws Exception {
        checkGotoMatching(
                "<intent-filter^>",
                "^</intent-filter>");
    }

    public void testGotoMatchingFwd5() throws Exception {
        checkGotoMatching(
                "<intent-filter>^",
                "^</intent-filter>");
    }

    public void testGotoMatchingFallback() throws Exception {
        // Character matching is done by the superclass; ensure that fallback to the
        // other XML matchers is working
        checkGotoMatching(
                "android:icon=^\"@drawable/icon\"",
                "android:icon=\"@drawable/icon^\"");
    }

    private void checkGotoMatching(IFile file, String caretBefore,
            String caretAfter) throws Exception {
        IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        assertNotNull(page);
        IEditorPart editor = IDE.openEditor(page, file);
        assertTrue(editor instanceof AndroidXmlEditor);
        AndroidXmlEditor layoutEditor = (AndroidXmlEditor) editor;
        ISourceViewer viewer = layoutEditor.getStructuredSourceViewer();

        int caretPosBefore = updateCaret(viewer, caretBefore);

        AndroidXmlCharacterMatcher matcher = new AndroidXmlCharacterMatcher();
        IStructuredDocument document = layoutEditor.getStructuredDocument();

        IRegion match = matcher.match(document, caretPosBefore);
        assertNotNull(match);

        String text = document.get();
        final String after = stripCaret(caretAfter);
        int index = text.indexOf(after);
        int caretPosAfter = match.getOffset() - index;
        String textWithCaret = after.substring(0, caretPosAfter)
                + "^" + after.substring(caretPosAfter);

        assertEquals(caretAfter, textWithCaret);
    }

    private static String stripCaret(String s) {
        int index = s.indexOf('^');
        assertTrue(index != -1);
        return s.substring(0, index) + s.substring(index + 1);
    }

    private void checkGotoMatching(String caretBefore,
            String caretAfter) throws Exception {
        checkGotoMatching(
                getTestDataFile(getProject(), "manifest.xml", "AndroidManifest.xml", true),
                caretBefore, caretAfter);
    }
}