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.SdkConstants.ANDROID_URI;
19 import static com.android.SdkConstants.ATTR_ID;
20 
21 import com.android.annotations.NonNull;
22 import com.android.annotations.Nullable;
23 import com.android.ide.common.api.IDragElement;
24 import com.android.ide.common.api.INode;
25 import com.android.ide.common.api.Rect;
26 
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 
32 /** Test/mock implementation of {@link IDragElement} */
33 public class TestDragElement implements IDragElement {
34     private Rect mRect;
35 
36     private final String mFqcn;
37 
38     private Map<String, TestAttribute> mAttributes = new HashMap<String, TestAttribute>();
39 
40     private List<TestDragElement> mChildren = new ArrayList<TestDragElement>();
41 
42     private TestDragElement mParent;
43 
TestDragElement(String mFqcn, Rect mRect, List<TestDragElement> mChildren, TestDragElement mParent)44     public TestDragElement(String mFqcn, Rect mRect, List<TestDragElement> mChildren,
45             TestDragElement mParent) {
46         super();
47         this.mRect = mRect;
48         this.mFqcn = mFqcn;
49         this.mChildren = mChildren;
50         this.mParent = mParent;
51     }
52 
TestDragElement(String fqn)53     public TestDragElement(String fqn) {
54         this(fqn, null, null, null);
55     }
56 
setBounds(Rect bounds)57     public TestDragElement setBounds(Rect bounds) {
58         this.mRect = bounds;
59 
60         return this;
61     }
62 
63     // Builder stuff
set(String uri, String name, String value)64     public TestDragElement set(String uri, String name, String value) {
65         if (mAttributes == null) {
66             mAttributes = new HashMap<String, TestAttribute>();
67         }
68 
69         mAttributes.put(uri + name, new TestAttribute(uri, name, value));
70 
71         return this;
72     }
73 
add(TestDragElement... children)74     public TestDragElement add(TestDragElement... children) {
75         if (mChildren == null) {
76             mChildren = new ArrayList<TestDragElement>();
77         }
78 
79         for (TestDragElement child : children) {
80             mChildren.add(child);
81             child.mParent = this;
82         }
83 
84         return this;
85     }
86 
id(String id)87     public TestDragElement id(String id) {
88         return set(ANDROID_URI, ATTR_ID, id);
89     }
90 
create(String fqn, Rect bounds)91     public static TestDragElement create(String fqn, Rect bounds) {
92         return create(fqn).setBounds(bounds);
93     }
94 
create(String fqn)95     public static TestDragElement create(String fqn) {
96         return new TestDragElement(fqn);
97     }
98 
create(TestDragElement... elements)99     public static IDragElement[] create(TestDragElement... elements) {
100         return elements;
101     }
102 
103     // ==== IDragElement ====
104 
105     @Override
getAttribute(@ullable String uri, @NonNull String localName)106     public IDragAttribute getAttribute(@Nullable String uri, @NonNull String localName) {
107         if (mAttributes == null) {
108             return new TestAttribute(uri, localName, "");
109         }
110 
111         return mAttributes.get(uri + localName);
112     }
113 
114     @Override
getAttributes()115     public @NonNull IDragAttribute[] getAttributes() {
116         return mAttributes.values().toArray(new IDragAttribute[mAttributes.size()]);
117     }
118 
119     @Override
getBounds()120     public @NonNull Rect getBounds() {
121         return mRect;
122     }
123 
124     @Override
getFqcn()125     public @NonNull String getFqcn() {
126         return mFqcn;
127     }
128 
129     @Override
getInnerElements()130     public @NonNull IDragElement[] getInnerElements() {
131         if (mChildren == null) {
132             return new IDragElement[0];
133         }
134 
135         return mChildren.toArray(new IDragElement[mChildren.size()]);
136     }
137 
138     @Override
getParentBounds()139     public @NonNull Rect getParentBounds() {
140         return mParent != null ? mParent.getBounds() : null;
141     }
142 
143     @Override
getParentFqcn()144     public String getParentFqcn() {
145         return mParent != null ? mParent.getFqcn() : null;
146     }
147 
148     @Override
toString()149     public String toString() {
150         return "TestDragElement [fqn=" + mFqcn + ", attributes=" + mAttributes + ", bounds="
151                 + mRect + "]";
152     }
153 
154     @Override
isSame(INode node)155     public boolean isSame(INode node) {
156         return node.getBounds().equals(getBounds());
157     }
158 }
159