Skip to content

Instantly share code, notes, and snippets.

@jesperronn
Created November 10, 2011 13:02
Show Gist options
  • Select an option

  • Save jesperronn/1354806 to your computer and use it in GitHub Desktop.

Select an option

Save jesperronn/1354806 to your computer and use it in GitHub Desktop.
GWT-test-utils helper to patch GWT Canvas
package com.google.gwt.widgetideas.graphics.client;
import com.google.gwt.user.client.Element;
/**
* IE fix for GWT canvas. Since
*
* Created by IntelliJ IDEA.
* User: Jesper Rønn-Jensen
* Date: 10-11-11
* Time: 14:05
* To change this template use File | Settings | File Templates.
*/
public class GWTCanvasIeFix extends GWTCanvas {
public GWTCanvasIeFix(int width, int height) {
super(width, height);
}
@Override
protected void onAttach() {
super.onAttach();
fixIt();
}
public void fixIt() {
fix(this.getElement());
}
public static native void fix(Element el) /*-{
var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf("msie") != -1 && $doc.documentMode && $doc.documentMode >= 8) {
if (!$doc.namespaces["v"]) {
$doc.namespaces.add("v", "urn:schemas-microsoft-com:vml", "#default#VML");
}
var col = el.getElementsByTagName('shape');
for (var i = 0; i < col.length; i++) {
col[i].outerHTML = col[i].outerHTML;
col[i].style.width = '10px';
col[i].style.height = '10px';
}
}
}-*/;
}
package com.nordea.testing;
import com.google.gwt.user.client.Element;
import com.google.gwt.widgetideas.graphics.client.GWTCanvasIeFix;
import com.octo.gwt.test.patchers.PatchClass;
import com.octo.gwt.test.patchers.PatchMethod;
/**
* gwt-test-utils to patch the GWT CAnvas createElement() method
*
*
* Our real-life code has a modification in GWTCanvas class, which is here overridden
*
*
*
* Created by IntelliJ IDEA.
* User: Jesper Rønn-Jensen
* Date: 10-11-11
* Time: 13:13
*/
@PatchClass(GWTCanvasIeFix.class)
public class GWTCanvasIeFixPatcher {
@PatchMethod
public static void fix(Element el) {
//nothing
}
}
package com.company.testing;
import com.google.gwt.dom.client.Element;
import com.google.gwt.widgetideas.graphics.client.GradientFactory;
import com.google.gwt.widgetideas.graphics.client.impl.GWTCanvasImpl;
import com.octo.gwt.test.GwtCreateHandler;
import com.octo.gwt.test.internal.patchers.dom.JavaScriptObjects;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
/**
* GWT-test-utils patcher to stub the parts of GWTCanvas which we use for piecharts in
* a real-life project.
*
* This implementations creates Mockito mocks for
* GradientFactory
* GWTCanvasImpl
*
* User: Jesper Rønn-Jensen
* Date: 10-11-11
* Time: 13:52
*
*/
public class PiechartGWTCreateHandler implements GwtCreateHandler {
/**
* <p>
* Instantiates an object of the given class.
* </p>
* <p/>
* <p>
* This handler may be able to instantiate objects of certain types only. If
* the class passed as parameter is not handled, the method should return
* null.
* </p>
*
* @param clazz the class to instantiate
* @return an object of this class if this GwtCreateHandler is capable of
* handling it, null otherwise
* @throws Exception if an error occurred when trying to create the object
*/
@Override
public Object create(Class<?> clazz) throws Exception {
if (GradientFactory.class.equals(clazz)) {
return mock(GradientFactory.class);
} else if (GWTCanvasImpl.class.equals(clazz)) {
GWTCanvasImpl canvasMock = mock(GWTCanvasImpl.class);
Element xxx = JavaScriptObjects.newElement("canvas");
doReturn(xxx).when(canvasMock).createElement();
return canvasMock;
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment