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.Timer;
020:
021: import java.util.ArrayList;
022:
023: /**
024: * Tests for the Image widget. Images in both clipped mode and unclipped mode
025: * are tested, along with the transitions between the two modes.
026: */
027: public class ImageTest extends GWTTestCase {
028:
029: /**
030: * Helper method that allows us to 'peek' at the private <code>state</code>
031: * field in the Image object, and call the <code>state.getStateName()</code>
032: * method.
033: *
034: * @param image The image instance
035: * @return "unclipped" if image is in the unclipped state, or
036: * "clipped" if the image is in the clipped state
037: */
038: public static native String getCurrentImageStateName(Image image) /*-{
039: var imgState = image.@com.google.gwt.user.client.ui.Image::state;
040: return imgState.@com.google.gwt.user.client.ui.Image.State::getStateName() ();
041: }-*/;
042:
043: public String getModuleName() {
044: return "com.google.gwt.user.UserTest";
045: }
046:
047: /**
048: * Tests the transition from the unclipped state to the clipped state
049: */
050: /* This test is commented out because of issue #863
051: public void testChangeImageToClipped() {
052: final ArrayList onloadEventFireCounter = new ArrayList();
053: final Image image = new Image("counting-forwards.png");
054: assertEquals("unclipped", getCurrentImageStateName(image));
055:
056: image.addLoadListener(new LoadListener() {
057: public void onError(Widget sender) {
058: fail("The image " + ((Image) sender).getUrl() + " failed to load.");
059: }
060:
061: public void onLoad(Widget sender) {
062: onloadEventFireCounter.add(new Object());
063:
064: if (getCurrentImageStateName(image).equals("unclipped")) {
065: image.setVisibleRect(12, 13, 8, 8);
066: }
067: }
068: });
069:
070: RootPanel.get().add(image);
071: delayTestFinish(2000);
072:
073: Timer t = new Timer() {
074: public void run() {
075: assertEquals(12, image.getOriginLeft());
076: assertEquals(13, image.getOriginTop());
077: assertEquals(8, image.getWidth());
078: assertEquals(8, image.getHeight());
079: assertEquals("clipped", getCurrentImageStateName(image));
080: assertEquals(2, onloadEventFireCounter.size());
081: finishTest();
082: }
083: };
084:
085: t.schedule(1000);
086: }
087: */
088:
089: /**
090: * Tests the transition from the clipped state to the unclipped state.
091: */
092: public void testChangeClippedImageToUnclipped() {
093: final ArrayList onloadEventFireCounter = new ArrayList();
094: final Image image = new Image("counting-forwards.png", 12, 13,
095: 8, 8);
096: assertEquals("clipped", getCurrentImageStateName(image));
097:
098: image.addLoadListener(new LoadListener() {
099: public void onError(Widget sender) {
100: fail("The image " + ((Image) sender).getUrl()
101: + " failed to load.");
102: }
103:
104: public void onLoad(Widget sender) {
105: onloadEventFireCounter.add(new Object());
106: }
107: });
108:
109: RootPanel.get().add(image);
110: delayTestFinish(2000);
111:
112: image.setUrl("counting-forwards.png");
113:
114: Timer t = new Timer() {
115: public void run() {
116: assertEquals(0, image.getOriginLeft());
117: assertEquals(0, image.getOriginTop());
118: assertEquals(32, image.getWidth());
119: assertEquals(32, image.getHeight());
120: assertEquals("unclipped",
121: getCurrentImageStateName(image));
122: assertEquals(2, onloadEventFireCounter.size());
123: finishTest();
124: }
125: };
126:
127: t.schedule(1000);
128: }
129:
130: /**
131: * Tests the creation of an image in unclipped mode
132: */
133: /* This test is commented out because of issue #864 and issue #863
134: public void testCreateImage() {
135: final ArrayList onloadEventFireCounter = new ArrayList();
136: final Image image = new Image("counting-forwards.png");
137:
138: image.addLoadListener(new LoadListener() {
139: public void onError(Widget sender) {
140: fail("The image " + ((Image) sender).getUrl() + " failed to load.");
141: }
142:
143: public void onLoad(Widget sender) {
144: onloadEventFireCounter.add(new Object());
145: assertEquals(32, image.getWidth());
146: assertEquals(32, image.getHeight());
147: }
148: });
149:
150: RootPanel.get().add(image);
151: delayTestFinish(2000);
152:
153: assertEquals(0, image.getOriginLeft());
154: assertEquals(0, image.getOriginTop());
155: assertEquals("unclipped", getCurrentImageStateName(image));
156:
157: Timer t = new Timer() {
158: public void run() {
159: assertEquals(1, onloadEventFireCounter.size());
160: finishTest();
161: }
162: };
163:
164: t.schedule(1000);
165: }
166: */
167:
168: /**
169: * Tests the creation of an image in clipped mode.
170: */
171: public void testCreateClippedImage() {
172: final ArrayList onloadEventFireCounter = new ArrayList();
173: final Image image = new Image("counting-forwards.png", 16, 16,
174: 16, 16);
175:
176: image.addLoadListener(new LoadListener() {
177: public void onError(Widget sender) {
178: fail("The image " + ((Image) sender).getUrl()
179: + " failed to load.");
180: }
181:
182: public void onLoad(Widget sender) {
183: onloadEventFireCounter.add(new Object());
184: assertEquals(16, image.getWidth());
185: assertEquals(16, image.getHeight());
186: }
187: });
188:
189: RootPanel.get().add(image);
190: delayTestFinish(2000);
191:
192: assertEquals(16, image.getOriginLeft());
193: assertEquals(16, image.getOriginTop());
194: assertEquals("clipped", getCurrentImageStateName(image));
195:
196: Timer t = new Timer() {
197: public void run() {
198: assertEquals(1, onloadEventFireCounter.size());
199: finishTest();
200: }
201: };
202:
203: t.schedule(1000);
204: }
205:
206: /**
207: * Tests the firing of onload events when
208: * {@link com.google.gwt.user.client.ui.Image#setUrl(String)}
209: * is called on an unclipped image.
210: */
211: /* This test has been commented out because of issue #863
212: public void testSetUrlAndLoadEventsOnUnclippedImage() {
213:
214: final ArrayList onloadEventFireCounter = new ArrayList();
215: final Image image = new Image("counting-backwards.png");
216:
217: image.addLoadListener(new LoadListener() {
218: public void onError(Widget sender) {
219: fail("The image " + ((Image) sender).getUrl() + " failed to load.");
220: }
221:
222: public void onLoad(Widget sender) {
223:
224: onloadEventFireCounter.add(new Object());
225:
226: if (onloadEventFireCounter.size() < 4) {
227: image.setUrl("counting-forwards.png");
228: }
229: }
230: });
231:
232: RootPanel.get().add(image);
233: delayTestFinish(2000);
234:
235: Timer t = new Timer() {
236: public void run() {
237: assertEquals(4, onloadEventFireCounter.size());
238: finishTest();
239: }
240: };
241:
242: t.schedule(1000);
243: }
244: */
245:
246: /**
247: * Tests the behavior of
248: * {@link com.google.gwt.user.client.ui.Image#setUrlAndVisibleRect(String,int,int,int,int)}
249: * on a clipped image.
250: */
251: public void testSetUrlAndVisibleRectOnClippedImage() {
252: final ArrayList onloadEventFireCounter = new ArrayList();
253: final Image image = new Image("counting-backwards.png", 12, 12,
254: 12, 12);
255:
256: image.addLoadListener(new LoadListener() {
257: public void onError(Widget sender) {
258: fail("The image " + ((Image) sender).getUrl()
259: + " failed to load.");
260: }
261:
262: public void onLoad(Widget sender) {
263: onloadEventFireCounter.add(new Object());
264: }
265: });
266:
267: RootPanel.get().add(image);
268: delayTestFinish(2000);
269:
270: assertEquals("clipped", getCurrentImageStateName(image));
271:
272: image.setUrlAndVisibleRect("counting-forwards.png", 0, 16, 16,
273: 16);
274:
275: Timer t = new Timer() {
276: public void run() {
277: assertEquals(0, image.getOriginLeft());
278: assertEquals(16, image.getOriginTop());
279: assertEquals(16, image.getWidth());
280: assertEquals(16, image.getHeight());
281: assertEquals("clipped", getCurrentImageStateName(image));
282: assertEquals(2, onloadEventFireCounter.size());
283: finishTest();
284: }
285: };
286:
287: t.schedule(1000);
288: }
289:
290: /**
291: * Tests the behavior of
292: * <code>setUrlAndVisibleRect(String, int, int, int, int)</code> method on
293: * an unclipped image, which causes a state transition to the clipped state.
294: */
295: /* This test has been commented out because of issue #863
296: public void testSetUrlAndVisibleRectOnUnclippedImage() {
297: final ArrayList onloadEventFireCounter = new ArrayList();
298: final Image image =
299: new Image("counting-backwards.png");
300:
301: image.addLoadListener(new LoadListener() {
302: public void onError(Widget sender) {
303: fail("The image " + ((Image) sender).getUrl() + " failed to load.");
304: }
305:
306: public void onLoad(Widget sender) {
307: onloadEventFireCounter.add(new Object());
308: if (getCurrentImageStateName(image).equals("unclipped")) {
309: image.setUrlAndVisibleRect("counting-forwards.png",
310: 0, 16, 16, 16);
311: }
312: }
313: });
314:
315: RootPanel.get().add(image);
316: delayTestFinish(2000);
317:
318: assertEquals("unclipped", getCurrentImageStateName(image));
319:
320: Timer t = new Timer() {
321: public void run() {
322: assertEquals(0, image.getOriginLeft());
323: assertEquals(16, image.getOriginTop());
324: assertEquals(16, image.getWidth());
325: assertEquals(16, image.getHeight());
326: assertEquals("clipped", getCurrentImageStateName(image));
327: assertEquals(2, onloadEventFireCounter.size());
328: finishTest();
329: }
330: };
331:
332: t.schedule(1000);
333: }
334: */
335:
336: /**
337: * Tests the firing of onload events when calling
338: * {@link com.google.gwt.user.client.ui.Image#setVisibleRect(int,int,int,int)}
339: * on a clipped image.
340: */
341: public void testSetVisibleRectAndLoadEventsOnClippedImage() {
342: final ArrayList onloadEventFireCounter = new ArrayList();
343: final Image image = new Image("counting-backwards.png", 16, 16,
344: 16, 16);
345:
346: image.addLoadListener(new LoadListener() {
347: public void onError(Widget sender) {
348: fail("The image " + ((Image) sender).getUrl()
349: + " failed to load.");
350: }
351:
352: public void onLoad(Widget sender) {
353: onloadEventFireCounter.add(new Object());
354: }
355: });
356:
357: RootPanel.get().add(image);
358: delayTestFinish(2000);
359:
360: image.setVisibleRect(0, 0, 16, 16);
361: image.setVisibleRect(0, 0, 16, 16);
362: image.setVisibleRect(16, 0, 16, 16);
363: image.setVisibleRect(16, 8, 8, 8);
364:
365: Timer t = new Timer() {
366: public void run() {
367: assertEquals(4, onloadEventFireCounter.size());
368: finishTest();
369: }
370: };
371:
372: t.schedule(1000);
373: }
374: }
|