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.impl;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019: import com.google.gwt.user.client.ui.Image;
020: import com.google.gwt.user.client.ui.ImageTest;
021: import com.google.gwt.user.client.ui.RootPanel;
022: import com.google.gwt.user.client.ui.LoadListener;
023: import com.google.gwt.user.client.ui.Widget;
024: import com.google.gwt.user.client.Timer;
025:
026: import java.util.ArrayList;
027:
028: /**
029: * Tests for the ClippedImagePrototype implementation. Tests are done to ensure
030: * that clipped images are generated which match the prototype's specification,
031: * and that applications of the prototype to existing images in both
032: * clipped/unclipped mode change the image so that it matches the prototype.
033: * Tests are also done to ensure that load events fire correctly after the
034: * application of the prototype to the image.
035: */
036: public class ClippedImagePrototypeTest extends GWTTestCase {
037:
038: public String getModuleName() {
039: return "com.google.gwt.user.UserTest";
040: }
041:
042: /**
043: * Tests that a clipped image can be transformed to match a given prototype.
044: * Also checks to make sure that a load event is fired on when
045: * {@link com.google.gwt.user.client.ui.impl.ClippedImagePrototype#applyTo(com.google.gwt.user.client.ui.Image)}
046: * is called.
047: */
048: public void testApplyToClippedImage() {
049:
050: final Image image = new Image("counting-backwards.png", 12, 13,
051: 8, 8);
052:
053: assertEquals(12, image.getOriginLeft());
054: assertEquals(13, image.getOriginTop());
055: assertEquals(8, image.getWidth());
056: assertEquals(8, image.getHeight());
057: assertEquals("clipped", ImageTest
058: .getCurrentImageStateName(image));
059:
060: final ArrayList onloadEventFireCounter = new ArrayList();
061:
062: image.addLoadListener(new LoadListener() {
063: public void onError(Widget sender) {
064: fail("The image " + ((Image) sender).getUrl()
065: + " failed to load.");
066: }
067:
068: public void onLoad(Widget sender) {
069: onloadEventFireCounter.add(new Object());
070:
071: if (image.getOriginLeft() == 12
072: && image.getOriginTop() == 13) {
073: ClippedImagePrototype clippedImagePrototype = new ClippedImagePrototype(
074: "counting-forwards.png", 16, 16, 16, 16);
075:
076: clippedImagePrototype.applyTo(image);
077:
078: assertEquals(16, image.getOriginLeft());
079: assertEquals(16, image.getOriginTop());
080: assertEquals(16, image.getWidth());
081: assertEquals(16, image.getHeight());
082: assertEquals("clipped", ImageTest
083: .getCurrentImageStateName(image));
084: }
085: }
086: });
087:
088: RootPanel.get().add(image);
089: delayTestFinish(2000);
090:
091: Timer t = new Timer() {
092: public void run() {
093: assertEquals(2, onloadEventFireCounter.size());
094: finishTest();
095: }
096: };
097:
098: t.schedule(1000);
099: }
100:
101: /**
102: * Tests that an unclipped image can be transformed to match a given
103: * prototype. Also checks to make sure that a load event is fired on when
104: * <code>applyTo(Image)</code> is called.
105: */
106: /*
107: * This test has been commented out because of issue #863
108: *
109: * public void testApplyToUnclippedImage() {
110: * final Image image = new Image("counting-backwards.png");
111: *
112: * assertEquals(0, image.getOriginLeft()); assertEquals(0,
113: * image.getOriginTop()); assertEquals("unclipped",
114: * ImageTest.getCurrentImageStateName(image));
115: *
116: * final ArrayList onloadEventFireCounter = new ArrayList();
117: *
118: * image.addLoadListener(new LoadListener() { public void onError(Widget
119: * sender) { fail("The image " + ((Image) sender).getUrl() + " failed to
120: * load."); }
121: *
122: * public void onLoad(Widget sender) { onloadEventFireCounter.add(new
123: * Object());
124: *
125: * if (ImageTest.getCurrentImageStateName(image).equals("unclipped")) {
126: *
127: * assertEquals(32, image.getWidth()); assertEquals(32, image.getHeight());
128: *
129: * ClippedImagePrototype clippedImagePrototype = new
130: * ClippedImagePrototype("counting-forwards.png", 16, 16, 16, 16);
131: *
132: * clippedImagePrototype.adjust(image);
133: *
134: * assertEquals(16, image.getOriginLeft()); assertEquals(16,
135: * image.getOriginTop()); assertEquals(16, image.getWidth()); assertEquals(16,
136: * image.getHeight()); assertEquals("counting-forwards.png", image.getUrl());
137: * assertEquals("clipped", ImageTest.getCurrentImageStateName(image)); } } });
138: *
139: * RootPanel.get().add(image); delayTestFinish(2000);
140: *
141: * Timer t = new Timer() { public void run() { assertEquals(2,
142: * onloadEventFireCounter.size()); finishTest(); } };
143: *
144: * t.schedule(1000); }
145: */
146:
147: /**
148: * Tests that new clipped images can be generated from a prototype by calling
149: * {@link ClippedImagePrototype#createImage()}.
150: */
151: public void testGenerateNewImage() {
152: ClippedImagePrototype clippedImagePrototype = new ClippedImagePrototype(
153: "counting-forwards.png", 16, 16, 16, 16);
154:
155: Image image = clippedImagePrototype.createImage();
156:
157: RootPanel.get().add(image);
158:
159: assertEquals(16, image.getOriginLeft());
160: assertEquals(16, image.getOriginTop());
161: assertEquals(16, image.getWidth());
162: assertEquals(16, image.getHeight());
163: assertEquals("counting-forwards.png", image.getUrl());
164: assertEquals("clipped", ImageTest
165: .getCurrentImageStateName(image));
166: }
167: }
|