1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.eclipse.org/org/documents/epl-v10.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.ide.common.layout; 17 18 import static com.android.ide.common.layout.LayoutConstants.ANDROID_URI; 19 import static com.android.ide.common.layout.LayoutConstants.ATTR_ID; 20 21 import com.android.ide.common.api.IAttributeInfo; 22 import com.android.ide.common.api.INode; 23 import com.android.ide.common.api.INodeHandler; 24 import com.android.ide.common.api.Margins; 25 import com.android.ide.common.api.Rect; 26 27 import java.util.ArrayList; 28 import java.util.Collections; 29 import java.util.HashMap; 30 import java.util.List; 31 import java.util.Map; 32 33 /** Test/mock implementation of {@link INode} */ 34 public class TestNode implements INode { 35 private TestNode mParent; 36 37 private final List<TestNode> mChildren = new ArrayList<TestNode>(); 38 39 private final String mFqcn; 40 41 private Rect mBounds = new Rect(); // Invalid bounds initially 42 43 private Map<String, IAttribute> mAttributes = new HashMap<String, IAttribute>(); 44 45 private Map<String, IAttributeInfo> mAttributeInfos = new HashMap<String, IAttributeInfo>(); 46 47 private List<String> mAttributeSources; 48 49 public TestNode(String fqcn) { 50 this.mFqcn = fqcn; 51 } 52 53 public TestNode bounds(Rect bounds) { 54 this.mBounds = bounds; 55 56 return this; 57 } 58 59 public TestNode id(String id) { 60 return set(ANDROID_URI, ATTR_ID, id); 61 } 62 63 public TestNode set(String uri, String name, String value) { 64 setAttribute(uri, name, value); 65 66 return this; 67 } 68 69 public TestNode add(TestNode child) { 70 mChildren.add(child); 71 child.mParent = this; 72 73 return this; 74 } 75 76 public TestNode add(TestNode... children) { 77 for (TestNode child : children) { 78 mChildren.add(child); 79 child.mParent = this; 80 } 81 82 return this; 83 } 84 85 public static TestNode create(String fcqn) { 86 return new TestNode(fcqn); 87 } 88 89 public void removeChild(int index) { 90 TestNode removed = mChildren.remove(index); 91 removed.mParent = null; 92 } 93 94 // ==== INODE ==== 95 96 public INode appendChild(String viewFqcn) { 97 return insertChildAt(viewFqcn, mChildren.size()); 98 } 99 100 public void editXml(String undoName, INodeHandler callback) { 101 callback.handle(this); 102 } 103 104 public void putAttributeInfo(String uri, String attrName, IAttributeInfo info) { 105 mAttributeInfos.put(uri + attrName, info); 106 } 107 108 public IAttributeInfo getAttributeInfo(String uri, String attrName) { 109 return mAttributeInfos.get(uri + attrName); 110 } 111 112 public Rect getBounds() { 113 return mBounds; 114 } 115 116 public INode[] getChildren() { 117 return mChildren.toArray(new INode[mChildren.size()]); 118 } 119 120 public IAttributeInfo[] getDeclaredAttributes() { 121 return mAttributeInfos.values().toArray(new IAttributeInfo[mAttributeInfos.size()]); 122 } 123 124 public String getFqcn() { 125 return mFqcn; 126 } 127 128 public IAttribute[] getLiveAttributes() { 129 return mAttributes.values().toArray(new IAttribute[mAttributes.size()]); 130 } 131 132 public INode getParent() { 133 return mParent; 134 } 135 136 public INode getRoot() { 137 TestNode curr = this; 138 while (curr.mParent != null) { 139 curr = curr.mParent; 140 } 141 142 return curr; 143 } 144 145 public String getStringAttr(String uri, String attrName) { 146 IAttribute attr = mAttributes.get(uri + attrName); 147 if (attr == null) { 148 return null; 149 } 150 151 return attr.getValue(); 152 } 153 154 public INode insertChildAt(String viewFqcn, int index) { 155 TestNode child = new TestNode(viewFqcn); 156 if (index == -1) { 157 mChildren.add(child); 158 } else { 159 mChildren.add(index, child); 160 } 161 child.mParent = this; 162 return child; 163 } 164 165 public void removeChild(INode node) { 166 int index = mChildren.indexOf(node); 167 if (index != -1) { 168 removeChild(index); 169 } 170 } 171 172 public boolean setAttribute(String uri, String localName, String value) { 173 mAttributes.put(uri + localName, new TestAttribute(uri, localName, value)); 174 return true; 175 } 176 177 @Override 178 public String toString() { 179 return "TestNode [fqn=" + mFqcn + ", infos=" + mAttributeInfos 180 + ", attributes=" + mAttributes + ", bounds=" + mBounds + "]"; 181 } 182 183 public int getBaseline() { 184 return -1; 185 } 186 187 public Margins getMargins() { 188 return null; 189 } 190 191 public List<String> getAttributeSources() { 192 return mAttributeSources != null ? mAttributeSources : Collections.<String>emptyList(); 193 } 194 195 public void setAttributeSources(List<String> attributeSources) { 196 mAttributeSources = attributeSources; 197 } 198 }