aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/common/layout/TestDragElement.java
blob: b96de60c76c1a782974db2820fd78a4e7d39a538 (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
/*
 * Copyright (C) 2010 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.common.layout;

import static com.android.SdkConstants.ANDROID_URI;
import static com.android.SdkConstants.ATTR_ID;

import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.ide.common.api.IDragElement;
import com.android.ide.common.api.INode;
import com.android.ide.common.api.Rect;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** Test/mock implementation of {@link IDragElement} */
public class TestDragElement implements IDragElement {
    private Rect mRect;

    private final String mFqcn;

    private Map<String, TestAttribute> mAttributes = new HashMap<String, TestAttribute>();

    private List<TestDragElement> mChildren = new ArrayList<TestDragElement>();

    private TestDragElement mParent;

    public TestDragElement(String mFqcn, Rect mRect, List<TestDragElement> mChildren,
            TestDragElement mParent) {
        super();
        this.mRect = mRect;
        this.mFqcn = mFqcn;
        this.mChildren = mChildren;
        this.mParent = mParent;
    }

    public TestDragElement(String fqn) {
        this(fqn, null, null, null);
    }

    public TestDragElement setBounds(Rect bounds) {
        this.mRect = bounds;

        return this;
    }

    // Builder stuff
    public TestDragElement set(String uri, String name, String value) {
        if (mAttributes == null) {
            mAttributes = new HashMap<String, TestAttribute>();
        }

        mAttributes.put(uri + name, new TestAttribute(uri, name, value));

        return this;
    }

    public TestDragElement add(TestDragElement... children) {
        if (mChildren == null) {
            mChildren = new ArrayList<TestDragElement>();
        }

        for (TestDragElement child : children) {
            mChildren.add(child);
            child.mParent = this;
        }

        return this;
    }

    public TestDragElement id(String id) {
        return set(ANDROID_URI, ATTR_ID, id);
    }

    public static TestDragElement create(String fqn, Rect bounds) {
        return create(fqn).setBounds(bounds);
    }

    public static TestDragElement create(String fqn) {
        return new TestDragElement(fqn);
    }

    public static IDragElement[] create(TestDragElement... elements) {
        return elements;
    }

    // ==== IDragElement ====

    @Override
    public IDragAttribute getAttribute(@Nullable String uri, @NonNull String localName) {
        if (mAttributes == null) {
            return new TestAttribute(uri, localName, "");
        }

        return mAttributes.get(uri + localName);
    }

    @Override
    public @NonNull IDragAttribute[] getAttributes() {
        return mAttributes.values().toArray(new IDragAttribute[mAttributes.size()]);
    }

    @Override
    public @NonNull Rect getBounds() {
        return mRect;
    }

    @Override
    public @NonNull String getFqcn() {
        return mFqcn;
    }

    @Override
    public @NonNull IDragElement[] getInnerElements() {
        if (mChildren == null) {
            return new IDragElement[0];
        }

        return mChildren.toArray(new IDragElement[mChildren.size()]);
    }

    @Override
    public @NonNull Rect getParentBounds() {
        return mParent != null ? mParent.getBounds() : null;
    }

    @Override
    public String getParentFqcn() {
        return mParent != null ? mParent.getFqcn() : null;
    }

    @Override
    public String toString() {
        return "TestDragElement [fqn=" + mFqcn + ", attributes=" + mAttributes + ", bounds="
                + mRect + "]";
    }

    @Override
    public boolean isSame(INode node) {
        return node.getBounds().equals(getBounds());
    }
}