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: import com.google.gwt.user.client.Element;
021:
022: /**
023: * Tests the semantics and ordering of onAttach/onDetach/onLoad/onUnload.
024: */
025: public class WidgetOnLoadTest extends GWTTestCase {
026:
027: public String getModuleName() {
028: return "com.google.gwt.user.User";
029: }
030:
031: static int orderIndex;
032:
033: static boolean isElementAttached(Element elem) {
034: return DOM.isOrHasChild(RootPanel.getBodyElement(), elem);
035: }
036:
037: static class TestPanel extends FlowPanel {
038: int onAttachOrder;
039: int onLoadOrder;
040: int onDetachOrder;
041: int onUnloadOrder;
042: boolean domAttachedOnLoad;
043: boolean domAttachedOnUnload;
044:
045: protected void onAttach() {
046: onAttachOrder = ++orderIndex;
047: super .onAttach();
048: }
049:
050: protected void onLoad() {
051: onLoadOrder = ++orderIndex;
052: domAttachedOnLoad = isElementAttached(getElement());
053: super .onLoad();
054: }
055:
056: protected void onDetach() {
057: onDetachOrder = ++orderIndex;
058: super .onDetach();
059: }
060:
061: protected void onUnload() {
062: onUnloadOrder = ++orderIndex;
063: domAttachedOnUnload = isElementAttached(getElement());
064: super .onUnload();
065: }
066: }
067:
068: static class TestWidget extends Label {
069: int onAttachOrder;
070: int onLoadOrder;
071: int onDetachOrder;
072: int onUnloadOrder;
073: boolean domAttachedOnLoad;
074: boolean domAttachedOnUnload;
075:
076: protected void onAttach() {
077: onAttachOrder = ++orderIndex;
078: super .onAttach();
079: }
080:
081: protected void onLoad() {
082: domAttachedOnLoad = isElementAttached(getElement());
083: onLoadOrder = ++orderIndex;
084: super .onLoad();
085: }
086:
087: protected void onDetach() {
088: onDetachOrder = ++orderIndex;
089: super .onDetach();
090: }
091:
092: protected void onUnload() {
093: onUnloadOrder = ++orderIndex;
094: domAttachedOnUnload = isElementAttached(getElement());
095: super .onUnload();
096: }
097: }
098:
099: public void testOnLoadAndUnloadOrder() {
100: TestPanel tp = new TestPanel();
101: TestWidget tw = new TestWidget();
102:
103: tp.add(tw);
104: RootPanel.get().add(tp);
105: RootPanel.get().remove(tp);
106:
107: // Trivial tests. Ensure that each panel/widget's onAttach/onDetach are
108: // called before their associated onLoad/onUnload.
109: assertTrue(tp.onAttachOrder < tp.onLoadOrder);
110: assertTrue(tp.onDetachOrder < tp.onUnloadOrder);
111: assertTrue(tw.onAttachOrder < tw.onLoadOrder);
112: assertTrue(tw.onDetachOrder < tw.onUnloadOrder);
113:
114: // Also trivial. Ensure that the panel's onAttach/onDetach is called before
115: // its child's onAttach/onDetach.
116: assertTrue(tp.onAttachOrder < tw.onAttachOrder);
117:
118: // Ensure that the panel's onLoad is only called after its widgets are
119: // attached/loaded.
120: assertTrue(tp.onLoadOrder > tw.onLoadOrder);
121:
122: // Ensure that the panel's onUnload is called before its widgets are
123: // detached/unloaded.
124: assertTrue(tp.onUnloadOrder < tw.onUnloadOrder);
125:
126: // Make sure each widget/panel's elements are actually still attached to the
127: // DOM during onLoad/onUnload.
128: assertTrue(tp.domAttachedOnLoad);
129: assertTrue(tp.domAttachedOnUnload);
130: assertTrue(tw.domAttachedOnLoad);
131: assertTrue(tw.domAttachedOnUnload);
132: }
133: }
|