aboutsummaryrefslogtreecommitdiffstats
path: root/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/LayoutMetadataTest.java
blob: a75fc96a9782806547f6ab446cff4b870fd9bb10 (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
/*
 * 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.gle2;

import static com.android.SdkConstants.ATTR_ID;
import static com.android.SdkConstants.ID_PREFIX;
import static com.android.SdkConstants.NEW_ID_PREFIX;
import static com.android.SdkConstants.TOOLS_PREFIX;
import static com.android.SdkConstants.TOOLS_URI;

import com.android.ide.common.layout.BaseLayoutRule;
import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.AdtUtils;
import com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor;
import com.android.ide.eclipse.adt.internal.editors.layout.refactoring.AdtProjectTest;
import com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode;
import com.android.utils.Pair;
import com.android.utils.XmlUtils;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 *
 */
@SuppressWarnings({"restriction", "javadoc", "deprecation"}) // XML DOM model
public class LayoutMetadataTest extends AdtProjectTest {

    public void testOldMetadata1() throws Exception {
        Pair<IDocument, UiElementNode> pair = getNode("metadata.xml", "listView1");
        IDocument document = pair.getFirst();
        UiElementNode uiNode = pair.getSecond();
        Node node = uiNode.getXmlNode();

        assertNull(LayoutMetadata.getProperty(document, node, "foo"));
        String before =
            "<ListView android:layout_width=\"match_parent\" android:id=\"@+id/listView1\"\n" +
            "        android:layout_height=\"wrap_content\">\n" +
            "    </ListView>";
        assertEquals(before, getText(document, node));

        // Set the property
        LayoutMetadata.setProperty(document, node,
                "listitem", "@android:layout/simple_list_item_checked");
        String after =
            "<ListView android:layout_width=\"match_parent\" android:id=\"@+id/listView1\"\n" +
            "        android:layout_height=\"wrap_content\">\n" +
            "        <!-- Preview: listitem=@android:layout/simple_list_item_checked -->\n" +
            "    </ListView>";
        assertEquals(after, getText(document, node));

        // Set a second property
        LayoutMetadata.setProperty(document, node,
                "listheader", "@android:layout/browser_link_context_header");
        after =
            "<ListView android:layout_width=\"match_parent\" android:id=\"@+id/listView1\"\n" +
            "        android:layout_height=\"wrap_content\">\n" +
            "        <!-- Preview: \n" +
            "            listheader=@android:layout/browser_link_context_header\n" +
            "            listitem=@android:layout/simple_list_item_checked\n" +
            "         -->\n" +
            "    </ListView>";
        assertEquals(after, getText(document, node));

        // Set list item to a different layout
        LayoutMetadata.setProperty(document, node,
                "listitem", "@android:layout/simple_list_item_single_choice");
        after =
            "<ListView android:layout_width=\"match_parent\" android:id=\"@+id/listView1\"\n" +
            "        android:layout_height=\"wrap_content\">\n" +
            "        <!-- Preview: \n" +
            "            listheader=@android:layout/browser_link_context_header\n" +
            "            listitem=@android:layout/simple_list_item_single_choice\n" +
            "         -->\n" +
            "    </ListView>";
        assertEquals(after, getText(document, node));

        // Set header to a different layout
        LayoutMetadata.setProperty(document, node,
                "listheader", "@layout/foo");
        after =
            "<ListView android:layout_width=\"match_parent\" android:id=\"@+id/listView1\"\n" +
            "        android:layout_height=\"wrap_content\">\n" +
            "        <!-- Preview: \n" +
            "            listheader=@layout/foo\n" +
            "            listitem=@android:layout/simple_list_item_single_choice\n" +
            "         -->\n" +
            "    </ListView>";
        assertEquals(after, getText(document, node));

        // Clear out list item
        LayoutMetadata.setProperty(document, node,
                "listitem", null);
        after =
            "<ListView android:layout_width=\"match_parent\" android:id=\"@+id/listView1\"\n" +
            "        android:layout_height=\"wrap_content\">\n" +
            "        <!-- Preview: listheader=@layout/foo -->\n" +
            "    </ListView>";
        assertEquals(after, getText(document, node));

        // Clear out list header
        LayoutMetadata.setProperty(document, node,
                "listheader", null);
        after =
            "<ListView android:layout_width=\"match_parent\" android:id=\"@+id/listView1\"\n" +
            "        android:layout_height=\"wrap_content\"></ListView>";
        assertEquals(after, getText(document, node));

        // Check node expansion on the button which doesn't have an end tag:
        before = "<Button android:text=\"Button\" android:id=\"@+id/button1\"/>";
    }

    public void testMetadata2() throws Exception {
        Pair<IDocument, UiElementNode> pair = getNode("metadata.xml", "button1");
        IDocument document = pair.getFirst();
        UiElementNode uiNode = pair.getSecond();
        Node node = uiNode.getXmlNode();

        assertNull(LayoutMetadata.getProperty(document, node, "foo"));
        String before =
            "<Button android:text=\"Button\" android:id=\"@+id/button1\"/>";
        assertEquals(before, getText(document, node));

        // Set the property
        LayoutMetadata.setProperty(document, node,
                "listitem", "@android:layout/simple_list_item_checked");
        String after =
            "<Button android:text=\"Button\" android:id=\"@+id/button1\">\n" +
            "        <!-- Preview: listitem=@android:layout/simple_list_item_checked -->\n" +
            "    </Button>";
        assertEquals(after, getText(document, node));
    }

    public void testMetadata1() throws Exception {
        Pair<IDocument, UiElementNode> pair = getNode("metadata.xml", "listView1");
        UiElementNode uiNode = pair.getSecond();
        Node node = uiNode.getXmlNode();

        assertNull(LayoutMetadata.getProperty(node, "foo"));

        Element element = (Element) node;
        String prefix = XmlUtils.lookupNamespacePrefix(element, TOOLS_URI, null);
        if (prefix == null) {
            // Add in new prefix...
            prefix = XmlUtils.lookupNamespacePrefix(element,
                    TOOLS_URI, TOOLS_PREFIX);
        }
        element.setAttribute(prefix + ':' + "foo", "bar");
    }

    // ==== Test utilities ====

    private static String getText(IDocument document, Node node) throws Exception {
        IndexedRegion region = (IndexedRegion) node;
        // This often returns the wrong value:
        //int length = region.getLength();
        int length = region.getEndOffset() - region.getStartOffset();
        return document.get(region.getStartOffset(), length);
    }

    private Pair<IDocument, UiElementNode> getNode(String filename, String targetId)
            throws Exception, PartInitException {
        IFile file = getLayoutFile(getProject(), filename);
        AdtPlugin.openFile(file, null);
        IEditorPart newEditor = AdtUtils.getActiveEditor();
        assertTrue(newEditor instanceof AndroidXmlEditor);
        AndroidXmlEditor xmlEditor = (AndroidXmlEditor) newEditor;
        IStructuredDocument document = xmlEditor.getStructuredDocument();
        UiElementNode root = xmlEditor.getUiRootNode();
        assertNotNull(root);
        UiElementNode node = findById(root, targetId);
        assertNotNull(node);
        Pair<IDocument, UiElementNode> pair = Pair.<IDocument, UiElementNode>of(document, node);
        return pair;
    }

    private static UiElementNode findById(UiElementNode node, String targetId) {
        assertFalse(targetId.startsWith(NEW_ID_PREFIX));
        assertFalse(targetId.startsWith(ID_PREFIX));

        String id = node.getAttributeValue(ATTR_ID);
        if (id != null && targetId.equals(BaseLayoutRule.stripIdPrefix(id))) {
            return node;
        }

        for (UiElementNode child : node.getUiChildren()) {
            UiElementNode result = findById(child, targetId);
            if (result != null) {
                return result;
            }
        }

        return null;
    }
}