001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.ui;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019: import com.google.gwt.user.client.DOM;
020:
021: /**
022: * Tests UIObject. Currently, focuses on style name behaviors.
023: */
024: public class UIObjectTest extends GWTTestCase {
025:
026: static class MyObject extends UIObject {
027: MyObject() {
028: setElement(DOM.createDiv());
029: }
030: }
031:
032: public String getModuleName() {
033: return "com.google.gwt.user.User";
034: }
035:
036: public void testAccidentalPrimary() {
037: MyObject o = new MyObject();
038: o.addStyleName("accidentalPrimary");
039: assertEquals("accidentalPrimary", o.getStylePrimaryName());
040: }
041:
042: public void testAddAndRemoveEmptyStyleName() {
043: MyObject o = new MyObject();
044:
045: o.setStylePrimaryName("primary");
046: try {
047: o.addStyleName("");
048: fail();
049: } catch (IllegalArgumentException e) {
050: // This *should* throw.
051: }
052:
053: try {
054: o.addStyleName(" ");
055: fail();
056: } catch (IllegalArgumentException e) {
057: // This *should* throw.
058: }
059:
060: try {
061: o.removeStyleName("");
062: fail();
063: } catch (IllegalArgumentException e) {
064: // This *should* throw.
065: }
066:
067: try {
068: o.removeStyleName(" ");
069: fail();
070: } catch (IllegalArgumentException e) {
071: // This *should* throw.
072: }
073:
074: assertEquals("primary", o.getStylePrimaryName());
075: }
076:
077: public void testNormal() {
078: // Test the basic set/get case.
079: MyObject o = new MyObject();
080: o.setStylePrimaryName("primaryStyle");
081:
082: // Note: getStyleName() explicitly returns the className attribute, so it
083: // doesn't guarantee that there aren't leading or trailing spaces.
084: assertEquals("primaryStyle", o.getStyleName());
085: doDependentAndSecondaryStyleTest(o);
086: assertEquals("primaryStyle", o.getStyleName());
087: }
088:
089: public void testSetEmptyPrimaryStyleName() {
090: MyObject o = new MyObject();
091: try {
092: o.setStylePrimaryName("");
093: fail();
094: } catch (IllegalArgumentException e) {
095: // This *should* throw.
096: }
097:
098: try {
099: o.setStylePrimaryName(" ");
100: fail();
101: } catch (IllegalArgumentException e) {
102: // This *should* throw.
103: }
104: }
105:
106: public void testSetStyleNameNormalization() {
107: MyObject o = new MyObject();
108:
109: o.setStylePrimaryName(" one ");
110: o.addStyleName(" two ");
111: o.addStyleName("\tthree\t");
112:
113: assertEquals("one two three", o.getStyleName());
114: }
115:
116: public void testSetStylePrimaryName() {
117: MyObject o = new MyObject();
118: o.setStylePrimaryName("gwt");
119: o.addStyleDependentName("dependent");
120: o.addStyleName("i-heart-gwt");
121: o.addStyleName("i-gwt-heart");
122:
123: assertTrue(containsClass(o, "gwt"));
124: assertTrue(containsClass(o, "gwt-dependent"));
125: assertTrue(containsClass(o, "i-heart-gwt"));
126: assertTrue(containsClass(o, "i-gwt-heart"));
127:
128: o.setStylePrimaryName("awt");
129:
130: assertPrimaryStyleNameEquals(o, "awt");
131: assertTrue(containsClass(o, "awt-dependent"));
132: assertFalse(containsClass(o, "gwt-dependent"));
133: assertTrue(containsClass(o, "i-heart-gwt"));
134: assertTrue(containsClass(o, "i-gwt-heart"));
135: assertFalse(containsClass(o, "i-heart-awt"));
136: assertFalse(containsClass(o, "i-awt-heart"));
137: }
138:
139: private void assertPrimaryStyleNameEquals(UIObject o,
140: String className) {
141: String attr = DOM.getElementProperty(o.getElement(),
142: "className");
143: assertTrue(attr.indexOf(className) == 0);
144: assertTrue(attr.length() == className.length()
145: || attr.charAt(className.length()) == ' ');
146: }
147:
148: private boolean containsClass(UIObject o, String className) {
149: String[] classes = DOM.getElementProperty(o.getElement(),
150: "className").split("\\s+");
151: for (int i = 0; i < classes.length; i++) {
152: if (className.equals(classes[i])) {
153: return true;
154: }
155: }
156: return false;
157: }
158:
159: // doStuff() should leave MyObject's style in the same state it started in.
160: private void doDependentAndSecondaryStyleTest(MyObject o) {
161: // Test that the primary style remains the first class, and that the
162: // dependent style shows up.
163: o.addStyleDependentName("dependent");
164: assertTrue(containsClass(o, o.getStylePrimaryName()
165: + "-dependent"));
166:
167: String oldPrimaryStyle = o.getStylePrimaryName();
168:
169: // Test that replacing the primary style name works (and doesn't munge up
170: // the secondary style).
171: o.addStyleName("secondaryStyle");
172: o.setStylePrimaryName("newPrimaryStyle");
173:
174: assertEquals("newPrimaryStyle", o.getStylePrimaryName());
175: assertPrimaryStyleNameEquals(o, "newPrimaryStyle");
176: assertTrue(containsClass(o, "newPrimaryStyle-dependent"));
177: assertTrue(containsClass(o, "secondaryStyle"));
178: assertFalse(containsClass(o, oldPrimaryStyle));
179: assertFalse(containsClass(o, oldPrimaryStyle + "-dependent"));
180:
181: // Clean up & return.
182: o.setStylePrimaryName(oldPrimaryStyle);
183: o.removeStyleDependentName("dependent");
184: o.removeStyleName("secondaryStyle");
185: assertFalse(containsClass(o, o.getStylePrimaryName()
186: + "-dependent"));
187: assertFalse(containsClass(o, "secondaryStyle"));
188: }
189: }
|