1 /*
2  * Copyright (C) 2011 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.GravityHelper.GRAVITY_BOTTOM;
19 import static com.android.ide.common.layout.GravityHelper.GRAVITY_CENTER_HORIZ;
20 import static com.android.ide.common.layout.GravityHelper.GRAVITY_CENTER_VERT;
21 import static com.android.ide.common.layout.GravityHelper.GRAVITY_END;
22 import static com.android.ide.common.layout.GravityHelper.GRAVITY_FILL_HORIZ;
23 import static com.android.ide.common.layout.GravityHelper.GRAVITY_FILL_VERT;
24 import static com.android.ide.common.layout.GravityHelper.GRAVITY_LEFT;
25 import static com.android.ide.common.layout.GravityHelper.GRAVITY_RIGHT;
26 import static com.android.ide.common.layout.GravityHelper.GRAVITY_START;
27 import static com.android.ide.common.layout.GravityHelper.GRAVITY_TOP;
28 import static com.android.ide.common.layout.GravityHelper.getGravity;
29 
30 import com.android.utils.XmlUtils;
31 
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
34 
35 import junit.framework.TestCase;
36 
37 @SuppressWarnings("javadoc")
38 public class GravityHelperTest extends TestCase {
testGravity()39     public void testGravity() throws Exception {
40         assertEquals(GRAVITY_BOTTOM, GravityHelper.getGravity("bottom", 0));
41         assertEquals(GRAVITY_BOTTOM | GRAVITY_LEFT, GravityHelper.getGravity("bottom|left", 0));
42         assertEquals(GRAVITY_BOTTOM | GRAVITY_LEFT | GRAVITY_START,
43                 GravityHelper.getGravity("bottom|left|start", 0));
44         assertEquals(GRAVITY_BOTTOM | GRAVITY_LEFT | GRAVITY_START,
45                 GravityHelper.getGravity("bottom|start|left", 0));
46         assertEquals(GRAVITY_BOTTOM | GRAVITY_RIGHT | GRAVITY_END,
47                 GravityHelper.getGravity("bottom|right|end", 0));
48         assertEquals(GRAVITY_BOTTOM | GRAVITY_RIGHT | GRAVITY_END,
49                 GravityHelper.getGravity("bottom|end|right", 0));
50         assertEquals(GRAVITY_CENTER_HORIZ | GRAVITY_CENTER_VERT,
51                 GravityHelper.getGravity("center", 0));
52     }
53 
testGravityString()54     public void testGravityString() throws Exception {
55         assertEquals("left", getGravity(GRAVITY_LEFT));
56         assertEquals("start", getGravity(GRAVITY_START));
57         assertEquals("right", getGravity(GRAVITY_RIGHT));
58         assertEquals("end", getGravity(GRAVITY_END));
59         assertEquals("top", getGravity(GRAVITY_TOP));
60         assertEquals("bottom", getGravity(GRAVITY_BOTTOM));
61         assertEquals("center_horizontal", getGravity(GRAVITY_CENTER_HORIZ));
62         assertEquals("center_vertical", getGravity(GRAVITY_CENTER_VERT));
63         assertEquals("fill_horizontal", getGravity(GRAVITY_FILL_HORIZ));
64         assertEquals("fill_vertical", getGravity(GRAVITY_FILL_VERT));
65 
66         assertEquals("center", getGravity(GRAVITY_CENTER_HORIZ|GRAVITY_CENTER_VERT));
67 
68         assertEquals("left|bottom", getGravity(GRAVITY_LEFT|GRAVITY_BOTTOM));
69         assertEquals("left|start|bottom", getGravity(GRAVITY_LEFT|GRAVITY_START|GRAVITY_BOTTOM));
70         assertEquals("center_horizontal|top", getGravity(GRAVITY_CENTER_HORIZ|GRAVITY_TOP));
71     }
72 
testConstrained()73     public void testConstrained() throws Exception {
74         assertTrue(GravityHelper.isConstrainedHorizontally(GRAVITY_LEFT));
75         assertTrue(GravityHelper.isConstrainedHorizontally(GRAVITY_START));
76         assertTrue(GravityHelper.isConstrainedHorizontally(GRAVITY_RIGHT));
77         assertTrue(GravityHelper.isConstrainedHorizontally(GRAVITY_END));
78         assertTrue(GravityHelper.isConstrainedHorizontally(GRAVITY_CENTER_HORIZ));
79         assertTrue(GravityHelper.isConstrainedHorizontally(GRAVITY_FILL_HORIZ));
80 
81         assertFalse(GravityHelper.isConstrainedVertically(GRAVITY_LEFT));
82         assertFalse(GravityHelper.isConstrainedVertically(GRAVITY_START));
83         assertFalse(GravityHelper.isConstrainedVertically(GRAVITY_RIGHT));
84         assertFalse(GravityHelper.isConstrainedVertically(GRAVITY_END));
85         assertFalse(GravityHelper.isConstrainedVertically(GRAVITY_CENTER_HORIZ));
86         assertFalse(GravityHelper.isConstrainedVertically(GRAVITY_FILL_HORIZ));
87 
88         assertTrue(GravityHelper.isConstrainedVertically(GRAVITY_TOP));
89         assertTrue(GravityHelper.isConstrainedVertically(GRAVITY_BOTTOM));
90         assertTrue(GravityHelper.isConstrainedVertically(GRAVITY_CENTER_VERT));
91         assertTrue(GravityHelper.isConstrainedVertically(GRAVITY_FILL_VERT));
92 
93         assertFalse(GravityHelper.isConstrainedHorizontally(GRAVITY_TOP));
94         assertFalse(GravityHelper.isConstrainedHorizontally(GRAVITY_BOTTOM));
95         assertFalse(GravityHelper.isConstrainedHorizontally(GRAVITY_CENTER_VERT));
96         assertFalse(GravityHelper.isConstrainedHorizontally(GRAVITY_FILL_VERT));
97     }
98 
testAligned()99     public void testAligned() throws Exception {
100         assertTrue(GravityHelper.isLeftAligned(GRAVITY_LEFT|GRAVITY_TOP));
101         assertTrue(GravityHelper.isLeftAligned(GRAVITY_START|GRAVITY_TOP));
102         assertTrue(GravityHelper.isLeftAligned(GRAVITY_LEFT));
103         assertTrue(GravityHelper.isLeftAligned(GRAVITY_START));
104         assertFalse(GravityHelper.isLeftAligned(GRAVITY_RIGHT));
105         assertFalse(GravityHelper.isLeftAligned(GRAVITY_END));
106 
107         assertTrue(GravityHelper.isTopAligned(GRAVITY_LEFT|GRAVITY_TOP));
108         assertTrue(GravityHelper.isTopAligned(GRAVITY_START|GRAVITY_TOP));
109         assertTrue(GravityHelper.isTopAligned(GRAVITY_TOP));
110         assertFalse(GravityHelper.isTopAligned(GRAVITY_BOTTOM));
111     }
112 
testElement()113     public void testElement() throws Exception {
114         Document document = XmlUtils.parseDocumentSilently(""
115                 + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
116                 + "<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
117                 + "    android:layout_width=\"match_parent\"\n"
118                 + "    android:layout_height=\"wrap_content\"\n"
119                 + "    android:layout_gravity=\"start|center_vertical|left\""
120                 + "    android:orientation=\"vertical\" />\n", true);
121         assertNotNull(document);
122         Element element = document.getDocumentElement();
123         assertNotNull(element);
124         int gravity = GravityHelper.getGravity(element);
125         assertEquals("left|start|center_vertical", GravityHelper.getGravity(gravity));
126     }
127 }