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:
017: package com.google.gwt.user.client.ui;
018:
019: import com.google.gwt.junit.client.GWTTestCase;
020: import com.google.gwt.user.client.DOM;
021: import com.google.gwt.user.client.ui.CustomButton.Face;
022:
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.Map;
026: import java.util.Map.Entry;
027:
028: /**
029: * Test for <code>PushButton</code> as most of this widget's functionality is
030: * UI based, the primary test will be in the new UI testing framework once it is
031: * released.
032: *
033: */
034: public class CustomButtonTest extends GWTTestCase {
035:
036: public String getModuleName() {
037: return "com.google.gwt.user.User";
038: }
039:
040: public void testCSS() {
041: ToggleButton b = new ToggleButton("up", "down");
042: b.setStyleName("random");
043: b.setDown(true);
044: assertEquals(b.getStylePrimaryName(), "random");
045:
046: Map faces = new HashMap();
047: faces.put("downDisabled", b.getDownDisabledFace());
048: faces.put("upDisabled", b.getUpDisabledFace());
049: faces.put("down", b.getDownFace());
050: faces.put("up", b.getUpFace());
051: faces.put("upHovering", b.getUpHoveringFace());
052: faces.put("downHovering", b.getDownHoveringFace());
053: Iterator entries = faces.entrySet().iterator();
054: // Set all faces as text.
055: while (entries.hasNext()) {
056: Map.Entry entry = (Entry) entries.next();
057: Face f = (Face) entry.getValue();
058: b.setCurrentFace(f);
059: assertEquals("random", b.getStylePrimaryName());
060: assertTrue(b.getStyleName()
061: .indexOf("random-" + f.getName()) != -1);
062: }
063:
064: entries = faces.entrySet().iterator();
065: b.addStyleName("fobar");
066: // Set all faces as text.
067: while (entries.hasNext()) {
068: Map.Entry entry = (Entry) entries.next();
069: Face f = (Face) entry.getValue();
070: b.setCurrentFace(f);
071: String computedStyleName = DOM.getElementProperty(b
072: .getElement(), "className");
073: assertTrue(computedStyleName.indexOf("random") == 0);
074: assertTrue(computedStyleName.indexOf("random-"
075: + f.getName()) >= 0);
076: assertTrue(computedStyleName.indexOf("fobar") >= 0);
077: }
078: }
079:
080: public void testSettingFaces() {
081: PushButton b = new PushButton();
082: Map faces = new HashMap();
083: faces.put("downDisabled", b.getDownDisabledFace());
084: faces.put("upDisabled", b.getUpDisabledFace());
085: faces.put("down", b.getDownFace());
086: faces.put("up", b.getUpFace());
087: faces.put("upHovering", b.getUpHoveringFace());
088: faces.put("downHovering", b.getDownHoveringFace());
089: Iterator entries = faces.entrySet().iterator();
090:
091: // Set all faces as text.
092: while (entries.hasNext()) {
093: Map.Entry entry = (Entry) entries.next();
094: Face f = (Face) entry.getValue();
095: String faceName = (String) entry.getKey();
096: f.setText(faceName);
097: }
098: entries = faces.entrySet().iterator();
099: while (entries.hasNext()) {
100: Map.Entry entry = (Entry) entries.next();
101: Face f = (Face) entry.getValue();
102: String faceName = (String) entry.getKey();
103: assertEquals(f.getText(), faceName);
104: }
105: // Set all faces as HTML
106: entries = faces.entrySet().iterator();
107: while (entries.hasNext()) {
108: Map.Entry entry = (Entry) entries.next();
109: Face f = (Face) entry.getValue();
110: String faceName = (String) entry.getKey();
111: f.setHTML("<b>" + faceName + "</b>");
112: }
113:
114: entries = faces.entrySet().iterator();
115: while (entries.hasNext()) {
116: Map.Entry entry = (Entry) entries.next();
117: Face f = (Face) entry.getValue();
118: String faceName = (String) entry.getKey();
119: assertEquals(f.getText(), faceName);
120: assertEquals(f.getHTML().toLowerCase(), "<b>"
121: + faceName.toLowerCase() + "</b>");
122: }
123: }
124:
125: public void testTransitions() {
126: ToggleButton b = new ToggleButton("transitions");
127:
128: b.setDown(true);
129: assertTrue(b.isDown());
130: assertFalse(b.isHovering());
131: assertTrue(b.isEnabled());
132:
133: b.setHovering(true);
134: assertTrue(b.isDown());
135: assertTrue(b.isHovering());
136: assertTrue(b.isEnabled());
137:
138: b.setEnabled(false);
139: assertTrue(b.isDown());
140: assertFalse(b.isHovering());
141: assertFalse(b.isEnabled());
142:
143: b.setDown(false);
144: assertFalse(b.isHovering());
145: assertFalse(b.isEnabled());
146: assertFalse(b.isDown());
147:
148: b.setEnabled(false);
149: assertFalse(b.isHovering());
150: assertFalse(b.isEnabled());
151: assertFalse(b.isDown());
152:
153: b.setEnabled(false);
154: b.setHovering(true);
155: assertTrue(b.isHovering());
156: assertFalse(b.isDown());
157: assertFalse(b.isEnabled());
158: }
159: }
|