Author: [EMAIL PROTECTED] Date: Thu Oct 23 06:25:32 2008 New Revision: 3820
Modified: branches/1_6_events/user/test/com/google/gwt/user/client/ui/ImageTest.java Log: Updated ImageTest to test handlers. The listener tests still exist. Patch by: jlabanca Modified: branches/1_6_events/user/test/com/google/gwt/user/client/ui/ImageTest.java ============================================================================== --- branches/1_6_events/user/test/com/google/gwt/user/client/ui/ImageTest.java (original) +++ branches/1_6_events/user/test/com/google/gwt/user/client/ui/ImageTest.java Thu Oct 23 06:25:32 2008 @@ -16,6 +16,10 @@ package com.google.gwt.user.client.ui; import com.google.gwt.dom.client.Document; +import com.google.gwt.event.dom.client.ErrorEvent; +import com.google.gwt.event.dom.client.ErrorHandler; +import com.google.gwt.event.dom.client.LoadEvent; +import com.google.gwt.event.dom.client.LoadHandler; import com.google.gwt.junit.client.GWTTestCase; /** @@ -23,7 +27,46 @@ * are tested, along with the transitions between the two modes. */ public class ImageTest extends GWTTestCase { + private static class TestErrorHandler implements ErrorHandler { + private Image image; + public TestErrorHandler(Image image) { + this.image = image; + } + + public void onError(ErrorEvent event) { + fail("The image " + image.getUrl() + " failed to load."); + } + } + + @Deprecated + private abstract static class TestLoadListener implements LoadListener { + private Image image; + private boolean finished = false; + + public TestLoadListener(Image image) { + this.image = image; + } + + public void onError(Widget sender) { + fail("The image " + image.getUrl() + " failed to load."); + } + + /** + * Mark the test as finished. + */ + public void finish() { + finished = true; + } + + /** + * @return true if the test has finished + */ + public boolean isFinished() { + return finished; + } + } + /** * Helper method that allows us to 'peek' at the private <code>state</code> * field in the Image object, and call the <code>state.getStateName()</code> @@ -154,25 +197,40 @@ /** * Tests the creation of an image in clipped mode. */ + @SuppressWarnings("deprecation") public void testCreateClippedImage() { final Image image = new Image("counting-forwards.png", 16, 16, 16, 16); - image.addLoadListener(new LoadListener() { + final TestLoadListener listener = new TestLoadListener(image) { private int onLoadEventCount = 0; - public void onError(Widget sender) { - fail("The image " + ((Image) sender).getUrl() + " failed to load."); + public void onLoad(Widget sender) { + if (++onLoadEventCount == 1) { + assertEquals(16, image.getWidth()); + assertEquals(16, image.getHeight()); + finish(); + } } + }; + image.addLoadListener(listener); - public void onLoad(Widget sender) { + image.addLoadHandler(new LoadHandler() { + private int onLoadEventCount = 0; + + public void onLoad(LoadEvent event) { if (++onLoadEventCount == 1) { assertEquals(16, image.getWidth()); assertEquals(16, image.getHeight()); - finishTest(); + if (listener.isFinished()) { + finishTest(); + } else { + fail("Listener did not fire first"); + } } } }); + image.addErrorHandler(new TestErrorHandler(image)); RootPanel.get().add(image); assertEquals(16, image.getOriginLeft()); @@ -219,17 +277,14 @@ * [EMAIL PROTECTED] com.google.gwt.user.client.ui.Image#setUrlAndVisibleRect(String,int,int,int,int)} * on a clipped image. */ + @SuppressWarnings("deprecation") public void testSetUrlAndVisibleRectOnClippedImage() { final Image image = new Image("counting-backwards.png", 12, 12, 12, 12); - image.addLoadListener(new LoadListener() { + final TestLoadListener listener = new TestLoadListener(image) { private int onLoadEventCount = 0; - public void onError(Widget sender) { - fail("The image " + ((Image) sender).getUrl() + " failed to load."); - } - public void onLoad(Widget sender) { if (++onLoadEventCount == 2) { assertEquals(0, image.getOriginLeft()); @@ -237,81 +292,105 @@ assertEquals(16, image.getWidth()); assertEquals(16, image.getHeight()); assertEquals("clipped", getCurrentImageStateName(image)); - finishTest(); + finish(); } } - }); - - RootPanel.get().add(image); - assertEquals("clipped", getCurrentImageStateName(image)); - image.setUrlAndVisibleRect("counting-forwards.png", - 0, 16, 16, 16); - - delayTestFinish(5000); - } - - /** - * Tests the behavior of - * <code>setUrlAndVisibleRect(String, int, int, int, int)</code> method on - * an unclipped image, which causes a state transition to the clipped state. - */ - /* This test has been commented out because of issue #863 - public void testSetUrlAndVisibleRectOnUnclippedImage() { - final Image image = new Image("counting-backwards.png"); + }; + image.addLoadListener(listener); - image.addLoadListener(new LoadListener() { + image.addLoadHandler(new LoadHandler() { private int onLoadEventCount = 0; - public void onError(Widget sender) { - fail("The image " + ((Image) sender).getUrl() + " failed to load."); - } - - public void onLoad(Widget sender) { - if (getCurrentImageStateName(image).equals("unclipped")) { - image.setUrlAndVisibleRect("counting-forwards.png", - 0, 16, 16, 16); - } - + public void onLoad(LoadEvent event) { if (++onLoadEventCount == 2) { assertEquals(0, image.getOriginLeft()); assertEquals(16, image.getOriginTop()); assertEquals(16, image.getWidth()); assertEquals(16, image.getHeight()); assertEquals("clipped", getCurrentImageStateName(image)); - finishTest(); + if (listener.isFinished()) { + finishTest(); + } else { + fail("Listener did not fire first"); + } } } }); + image.addErrorHandler(new TestErrorHandler(image)); RootPanel.get().add(image); - assertEquals("unclipped", getCurrentImageStateName(image)); + assertEquals("clipped", getCurrentImageStateName(image)); + image.setUrlAndVisibleRect("counting-forwards.png", + 0, 16, 16, 16); delayTestFinish(5000); } - */ + + /** + * Tests the behavior of + * <code>setUrlAndVisibleRect(String, int, int, int, int)</code> method on an + * unclipped image, which causes a state transition to the clipped state. + */ + /* + * This test has been commented out because of issue #863 public void + * testSetUrlAndVisibleRectOnUnclippedImage() { final Image image = new + * Image("counting-backwards.png"); + * + * image.addLoadListener(new LoadListener() { private int onLoadEventCount = + * 0; + * + * public void onError(Widget sender) { fail("The image " + ((Image) + * sender).getUrl() + " failed to load."); } + * + * public void onLoad(Widget sender) { if + * (getCurrentImageStateName(image).equals("unclipped")) { + * image.setUrlAndVisibleRect("counting-forwards.png", 0, 16, 16, 16); } + * + * if (++onLoadEventCount == 2) { assertEquals(0, image.getOriginLeft()); + * assertEquals(16, image.getOriginTop()); assertEquals(16, image.getWidth()); + * assertEquals(16, image.getHeight()); assertEquals("clipped", + * getCurrentImageStateName(image)); finishTest(); } } }); + * + * RootPanel.get().add(image); assertEquals("unclipped", + * getCurrentImageStateName(image)); + * + * delayTestFinish(5000); } + */ /** * Tests the firing of onload events when calling * [EMAIL PROTECTED] com.google.gwt.user.client.ui.Image#setVisibleRect(int,int,int,int)} * on a clipped image. */ + @SuppressWarnings("deprecation") public void testSetVisibleRectAndLoadEventsOnClippedImage() { - final Image image = new Image("counting-backwards.png", - 16, 16, 16, 16); + final Image image = new Image("counting-backwards.png", 16, 16, 16, 16); - image.addLoadListener(new LoadListener() { + final TestLoadListener listener = new TestLoadListener(image) { private int onLoadEventCount = 0; - public void onError(Widget sender) { - fail("The image " + ((Image) sender).getUrl() + " failed to load."); + public void onLoad(Widget sender) { + if (++onLoadEventCount == 4) { + finish(); + } } + }; + image.addLoadListener(listener); - public void onLoad(Widget sender) { + image.addLoadHandler(new LoadHandler() { + private int onLoadEventCount = 0; + + public void onLoad(LoadEvent event) { if (++onLoadEventCount == 4) { - finishTest(); + if (listener.isFinished()) { + finishTest(); + } else { + fail("Listener did not fire first"); + } } } }); + image.addErrorHandler(new TestErrorHandler(image)); RootPanel.get().add(image); image.setVisibleRect(0, 0, 16, 16); --~--~---------~--~----~------------~-------~--~----~ http://groups.google.com/group/Google-Web-Toolkit-Contributors -~----------~----~----~----~------~----~------~--~---