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 core functionality of {@link DisclosurePanel}.
023: */
024: public class DisclosurePanelTest extends GWTTestCase {
025: private static final int OPEN = 0;
026:
027: private static final int CLOSE = 1;
028:
029: public String getModuleName() {
030: return "com.google.gwt.user.User";
031: }
032:
033: /**
034: * Test to ensure css style changes that control core functionality do change
035: * appropriately.
036: */
037: public void testCoreFunctionality() {
038: DisclosurePanel panel = createTestPanel();
039: assertTrue(DOM.getStyleAttribute(
040: panel.getContent().getElement(), "display")
041: .equalsIgnoreCase("none"));
042:
043: panel.setOpen(true);
044: assertTrue(DOM.getStyleAttribute(
045: panel.getContent().getElement(), "display").trim()
046: .equals(""));
047: }
048:
049: public void testAttachDetachOrder() {
050: HasWidgetsTester.testAll(new DisclosurePanel());
051: }
052:
053: /**
054: * Test to ensure that event handler dispatch function appropriately.
055: */
056: public void testEventHandlers() {
057:
058: final boolean[] aDidFire = new boolean[2];
059: final boolean[] bDidFire = new boolean[2];
060: final DisclosurePanel panel = createTestPanel();
061:
062: DisclosureHandler handleA = new DisclosureHandler() {
063: public void onClose(DisclosureEvent event) {
064: aDidFire[CLOSE] = true;
065: }
066:
067: public void onOpen(DisclosureEvent event) {
068: aDidFire[OPEN] = true;
069: }
070: };
071:
072: DisclosureHandler handleB = new DisclosureHandler() {
073: public void onClose(DisclosureEvent event) {
074: assertEquals(event.getSource(), panel);
075: bDidFire[CLOSE] = true;
076: }
077:
078: public void onOpen(DisclosureEvent event) {
079: assertEquals(event.getSource(), panel);
080: bDidFire[OPEN] = true;
081: }
082: };
083:
084: panel.addEventHandler(handleA);
085: panel.addEventHandler(handleB);
086:
087: panel.setOpen(true);
088: // We expect onOpen to fire and onClose to not fire.
089: assertTrue(aDidFire[OPEN] && bDidFire[OPEN] && !aDidFire[CLOSE]
090: && !bDidFire[CLOSE]);
091:
092: aDidFire[OPEN] = bDidFire[OPEN] = false;
093:
094: panel.setOpen(false);
095: // We expect onOpen to fire and onClose to not fire.
096: assertTrue(aDidFire[CLOSE] && bDidFire[CLOSE]
097: && !aDidFire[OPEN] && !bDidFire[OPEN]);
098:
099: aDidFire[OPEN] = bDidFire[CLOSE] = false;
100:
101: panel.removeEventHandler(handleB);
102:
103: panel.setOpen(true);
104: panel.setOpen(false);
105: // We expect a to have fired both events, and b to have fired none.
106: assertTrue(aDidFire[OPEN] && aDidFire[CLOSE] && !bDidFire[OPEN]
107: && !bDidFire[CLOSE]);
108: }
109:
110: private DisclosurePanel createTestPanel() {
111: DisclosurePanel panel = new DisclosurePanel("Test Subject",
112: false);
113: panel.setContent(new SimplePanel());
114: return panel;
115: }
116: }
|