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 @Override 97 public INode appendChild(String viewFqcn) { 98 return insertChildAt(viewFqcn, mChildren.size()); 99 } 100 101 @Override 102 public void editXml(String undoName, INodeHandler callback) { 103 callback.handle(this); 104 } 105 106 public void putAttributeInfo(String uri, String attrName, IAttributeInfo info) { 107 mAttributeInfos.put(uri + attrName, info); 108 } 109 110 @Override 111 public IAttributeInfo getAttributeInfo(String uri, String attrName) { 112 return mAttributeInfos.get(uri + attrName); 113 } 114 115 @Override 116 public Rect getBounds() { 117 return mBounds; 118 } 119 120 @Override 121 public INode[] getChildren() { 122 return mChildren.toArray(new INode[mChildren.size()]); 123 } 124 125 @Override 126 public IAttributeInfo[] getDeclaredAttributes() { 127 return mAttributeInfos.values().toArray(new IAttributeInfo[mAttributeInfos.size()]); 128 } 129 130 @Override 131 public String getFqcn() { 132 return mFqcn; 133 } 134 135 @Override 136 public IAttribute[] getLiveAttributes() { 137 return mAttributes.values().toArray(new IAttribute[mAttributes.size()]); 138 } 139 140 @Override 141 public INode getParent() { 142 return mParent; 143 } 144 145 @Override 146 public INode getRoot() { 147 TestNode curr = this; 148 while (curr.mParent != null) { 149 curr = curr.mParent; 150 } 151 152 return curr; 153 } 154 155 @Override 156 public String getStringAttr(String uri, String attrName) { 157 IAttribute attr = mAttributes.get(uri + attrName); 158 if (attr == null) { 159 return null; 160 } 161 162 return attr.getValue(); 163 } 164 165 @Override 166 public INode insertChildAt(String viewFqcn, int index) { 167 TestNode child = new TestNode(viewFqcn); 168 if (index == -1) { 169 mChildren.add(child); 170 } else { 171 mChildren.add(index, child); 172 } 173 child.mParent = this; 174 return child; 175 } 176 177 @Override 178 public void removeChild(INode node) { 179 int index = mChildren.indexOf(node); 180 if (index != -1) { 181 removeChild(index); 182 } 183 } 184 185 @Override 186 public boolean setAttribute(String uri, String localName, String value) { 187 mAttributes.put(uri + localName, new TestAttribute(uri, localName, value)); 188 return true; 189 } 190 191 @Override 192 public String toString() { 193 return "TestNode [fqn=" + mFqcn + ", infos=" + mAttributeInfos 194 + ", attributes=" + mAttributes + ", bounds=" + mBounds + "]"; 195 } 196 197 @Override 198 public int getBaseline() { 199 return -1; 200 } 201 202 @Override 203 public Margins getMargins() { 204 return null; 205 } 206 207 @Override 208 public List<String> getAttributeSources() { 209 return mAttributeSources != null ? mAttributeSources : Collections.<String>emptyList(); 210 } 211 212 public void setAttributeSources(List<String> attributeSources) { 213 mAttributeSources = attributeSources; 214 } 215 }