Search in sources :

Example 1 with PatchMethod

use of com.googlecode.gwt.test.patchers.PatchMethod in project gwt-test-utils by gwt-test-utils.

the class GwtDotCreateProviderPatcher method bind.

@PatchMethod
static <T> ScopedBindingBuilder bind(LinkedBindingBuilder<T> builder) {
    if (!(builder instanceof AbstractBindingBuilder)) {
        throw new GwtTestGinException("Not managed " + LinkedBindingBuilder.class.getSimpleName() + " implementation : " + builder.getClass().getName());
    }
    Binding<T> binding = GwtReflectionUtils.<Binding<T>>getPrivateFieldValue(builder, "binding");
    Type type = binding.getKey().getTypeLiteral().getType();
    if (!(type instanceof Class)) {
        throw new GwtTestGinException("Not managed binded type : " + type);
    }
    Constructor<T> atInjectConstructor = getAtInjectConstructor((Class<T>) type);
    if (atInjectConstructor != null) {
        return builder.toConstructor(atInjectConstructor);
    }
    GwtDotCreateProvider<T> gwtDotCreateProvider = GwtReflectionUtils.instantiateClass(GwtDotCreateProvider.class);
    GwtReflectionUtils.setPrivateFieldValue(gwtDotCreateProvider, BINDED_CLASS_FIELD, type);
    return builder.toProvider(gwtDotCreateProvider);
}
Also used : Binding(com.google.inject.Binding) Type(java.lang.reflect.Type) GwtTestGinException(com.googlecode.gwt.test.gin.GwtTestGinException) GWT(com.google.gwt.core.client.GWT) CtClass(javassist.CtClass) PatchClass(com.googlecode.gwt.test.patchers.PatchClass) AbstractBindingBuilder(com.google.inject.internal.AbstractBindingBuilder) PatchMethod(com.googlecode.gwt.test.patchers.PatchMethod)

Example 2 with PatchMethod

use of com.googlecode.gwt.test.patchers.PatchMethod in project gwt-test-utils by gwt-test-utils.

the class HTMLTablePatcher method getEventTargetCell.

@PatchMethod
static Element getEventTargetCell(HTMLTable table, Event event) {
    Object bodyElem = GwtReflectionUtils.getPrivateFieldValue(table, "bodyElem");
    Element td = DOM.eventGetTarget(event);
    for (; td != null; td = DOM.getParent(td)) {
        // If it's a TD, it might be the one we're looking for.
        if (DOM.getElementProperty(td, "tagName").equalsIgnoreCase("td")) {
            // Make sure it's directly a part of this table before returning
            // it.
            Element tr = DOM.getParent(td);
            Object body = DOM.getParent(tr);
            if (body == bodyElem) {
                return td;
            }
        }
        // If we run into this table's body, we're out of options.
        if (td == bodyElem) {
            return null;
        }
    }
    return null;
}
Also used : TableRowElement(com.google.gwt.dom.client.TableRowElement) Element(com.google.gwt.dom.client.Element) PatchMethod(com.googlecode.gwt.test.patchers.PatchMethod)

Example 3 with PatchMethod

use of com.googlecode.gwt.test.patchers.PatchMethod in project gwt-test-utils by gwt-test-utils.

the class JSONParserPatcher method evaluate.

@PatchMethod
static JSONValue evaluate(String json, boolean strict) {
    JsonParser jp = null;
    try {
        JsonFactory f = new JsonFactory();
        if (!strict) {
            f.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
            f.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
            f.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
            f.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
            f.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
            f.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
            f.configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS, true);
        }
        jp = f.createJsonParser(json);
        // will return JsonToken.START_OBJECT (verify?)
        jp.nextToken();
        JSONObject jsonObject = extractJSONObject(json, jp);
        return jsonObject;
    } catch (Exception e) {
        if (e instanceof GwtTestException) {
            throw (GwtTestException) e;
        }
        throw new GwtTestJSONException("Error while parsing JSON string '" + json + "'", e);
    } finally {
        if (jp != null) {
            try {
                // ensure resources get cleaned up timely and properly
                jp.close();
            } catch (IOException e) {
            // should never happen
            }
        }
    }
}
Also used : GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException) GwtTestJSONException(com.googlecode.gwt.test.exceptions.GwtTestJSONException) JsonFactory(org.codehaus.jackson.JsonFactory) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException) IOException(java.io.IOException) GwtTestJSONException(com.googlecode.gwt.test.exceptions.GwtTestJSONException) JsonParser(org.codehaus.jackson.JsonParser) PatchMethod(com.googlecode.gwt.test.patchers.PatchMethod)

Example 4 with PatchMethod

use of com.googlecode.gwt.test.patchers.PatchMethod in project gwt-test-utils by gwt-test-utils.

the class FramePatcher method setUrl.

@PatchMethod
static void setUrl(Frame frame, String url) {
    IFrameElement e = frame.getElement().cast();
    e.setSrc(url);
}
Also used : IFrameElement(com.google.gwt.dom.client.IFrameElement) PatchMethod(com.googlecode.gwt.test.patchers.PatchMethod)

Example 5 with PatchMethod

use of com.googlecode.gwt.test.patchers.PatchMethod in project gwt-test-utils by gwt-test-utils.

the class AutomaticPatcher method getPatchMethods.

private Set<CtMethod> getPatchMethods(Set<CtClass> patchClasses) {
    Set<CtMethod> result = new HashSet<CtMethod>();
    // add all @PatchMethod found in a temporary map
    Map<String, List<CtMethod>> temp = new HashMap<String, List<CtMethod>>();
    for (CtClass patchClass : patchClasses) {
        for (CtMethod ctMethod : patchClass.getDeclaredMethods()) {
            if (ctMethod.hasAnnotation(PatchMethod.class)) {
                if (!Modifier.isStatic(ctMethod.getModifiers())) {
                    throw new GwtTestPatchException("@" + PatchMethod.class.getName() + " has to be static : '" + ctMethod.getLongName() + "'");
                }
                String nameAndSignature = ctMethod.getName() + Descriptor.toString(ctMethod.getSignature());
                List<CtMethod> correspondingMethods = temp.get(nameAndSignature);
                if (correspondingMethods == null) {
                    correspondingMethods = new ArrayList<CtMethod>();
                    temp.put(nameAndSignature, correspondingMethods);
                }
                correspondingMethods.add(ctMethod);
            }
        }
    }
    // override=true
    for (Map.Entry<String, List<CtMethod>> entry : temp.entrySet()) {
        CtMethod methodToUse = getMethodToUse(entry.getValue(), PatchMethod.class);
        methodToUse.setModifiers(Modifier.PUBLIC + Modifier.STATIC);
        result.add(methodToUse);
    }
    return result;
}
Also used : GwtTestPatchException(com.googlecode.gwt.test.exceptions.GwtTestPatchException) CtClass(javassist.CtClass) PatchMethod(com.googlecode.gwt.test.patchers.PatchMethod) CtMethod(javassist.CtMethod)

Aggregations

PatchMethod (com.googlecode.gwt.test.patchers.PatchMethod)26 Element (com.google.gwt.dom.client.Element)8 Event (com.google.gwt.user.client.Event)3 PotentialElement (com.google.gwt.user.client.ui.PotentialElement)3 TableRowElement (com.google.gwt.dom.client.TableRowElement)2 Timer (com.google.gwt.user.client.Timer)2 GwtTestException (com.googlecode.gwt.test.exceptions.GwtTestException)2 GwtTestPatchException (com.googlecode.gwt.test.exceptions.GwtTestPatchException)2 TimerTask (java.util.TimerTask)2 CtClass (javassist.CtClass)2 CtMethod (javassist.CtMethod)2 GWT (com.google.gwt.core.client.GWT)1 BodyElement (com.google.gwt.dom.client.BodyElement)1 IFrameElement (com.google.gwt.dom.client.IFrameElement)1 Node (com.google.gwt.dom.client.Node)1 AutoDirectionHandler (com.google.gwt.i18n.client.AutoDirectionHandler)1 SafeHtml (com.google.gwt.safehtml.shared.SafeHtml)1 EventListener (com.google.gwt.user.client.EventListener)1 FormPanel (com.google.gwt.user.client.ui.FormPanel)1 SubmitCompleteEvent (com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent)1