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
|
/*
* 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 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, false);
if (prefix == null) {
// Add in new prefix...
prefix = XmlUtils.lookupNamespacePrefix(element,
TOOLS_URI, TOOLS_PREFIX, true);
}
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;
}
}
|