Search in sources :

Example 61 with Name

use of com.codename1.rad.models.Property.Name in project CodenameOne by codenameone.

the class TestUtils method findLabelText.

/**
 * Finds a component with the given name, works even with UI's that weren't created with the GUI builder
 * @param text the text of the label/button
 * @return the component with the given label text within the tree
 */
private static Label findLabelText(Container root, String text) {
    if (verbose) {
        log("findLabelText(" + root + ", " + text + ")");
    }
    int count = root.getComponentCount();
    for (int iter = 0; iter < count; iter++) {
        Component c = root.getComponentAt(iter);
        if (c instanceof Label) {
            String n = ((Label) c).getText();
            if (n != null && n.equals(text)) {
                return (Label) c;
            }
            // will work for cases of upcase due to Android theme upcasing of buttons
            n = (String) c.getClientProperty("cn1$origText");
            if (n != null && n.equals(text)) {
                return (Label) c;
            }
            continue;
        }
        if (c instanceof Container) {
            Label l = findLabelText((Container) c, text);
            if (l != null) {
                return l;
            }
        }
    }
    return null;
}
Also used : Container(com.codename1.ui.Container) Label(com.codename1.ui.Label) Component(com.codename1.ui.Component)

Example 62 with Name

use of com.codename1.rad.models.Property.Name in project CodenameOne by codenameone.

the class TestUtils method assertTextArea.

/**
 * Asserts that we have a label with the given text baring the given name
 * @param path the path to the text area
 * @param text the text of the label
 */
public static void assertTextArea(int[] path, String text) {
    if (verbose) {
        log("assertTextArea(" + toString(path) + ", " + text + ")");
    }
    TextArea l = (TextArea) getComponentByPath(path);
    assertBool(l != null, "Null area " + text);
    assertBool(l.getText().equals(text), "assertTextArea: " + l.getText() + " != " + text);
}
Also used : TextArea(com.codename1.ui.TextArea)

Example 63 with Name

use of com.codename1.rad.models.Property.Name in project CodenameOne by codenameone.

the class TestUtils method screenshotTest.

/**
 * The screenshot test takes a screenshot of the screen and compares it to
 * a prior screenshot, if both are 100% identical the test passes. If not
 * the test fails.<br>
 * If this is the first time the test is run then the screenshot is taken
 * and saved under the given name in the devices storage. The test passes
 * for this case but a warning is printed to the console. The name will have
 * .png appended to it so it will be identified.<br>
 * This test will only work on devices that support the ImageIO API with PNG
 * file format.
 *
 * @param screenshotName the name to use for the storage, must be unique!
 * @return true if the screenshots are identical or no prior screenshot exists
 * or if the test can't be run on this device. False if a screenshot exists and
 * it isn't 100% identical.
 */
public static boolean screenshotTest(String screenshotName) {
    if (verbose) {
        log("screenshotTest(" + screenshotName + ")");
    }
    try {
        ImageIO io = ImageIO.getImageIO();
        if (io == null || !io.isFormatSupported(ImageIO.FORMAT_PNG)) {
            log("screenshot test skipped due to no image IO support for PNG format");
            return true;
        }
        Image mute = Image.createImage(Display.getInstance().getDisplayWidth(), Display.getInstance().getDisplayHeight());
        Display.getInstance().getCurrent().paintComponent(mute.getGraphics(), true);
        screenshotName = screenshotName + ".png";
        if (Storage.getInstance().exists(screenshotName)) {
            int[] rgba = mute.getRGBCached();
            Image orig = Image.createImage(Storage.getInstance().createInputStream(screenshotName));
            int[] origRgba = orig.getRGBCached();
            orig = null;
            for (int iter = 0; iter < rgba.length; iter++) {
                if (rgba[iter] != origRgba[iter]) {
                    log("screenshots do not match at offset " + iter + " saving additional image under " + screenshotName + ".fail");
                    io.save(mute, Storage.getInstance().createOutputStream(screenshotName + ".fail"), ImageIO.FORMAT_PNG, 1);
                    return false;
                }
            }
        } else {
            io.save(mute, Storage.getInstance().createOutputStream(screenshotName), ImageIO.FORMAT_PNG, 1);
        }
        return true;
    } catch (IOException err) {
        log(err);
        return false;
    }
}
Also used : IOException(java.io.IOException) Image(com.codename1.ui.Image) ImageIO(com.codename1.ui.util.ImageIO)

Example 64 with Name

use of com.codename1.rad.models.Property.Name in project CodenameOne by codenameone.

the class TestUtils method setText.

/**
 * Sets the text for the given component
 * @param name the name of the component
 * @param text the text to set
 */
public static void setText(String name, String text) {
    if (verbose) {
        log("setText(" + name + ", " + text + ")");
    }
    Component c = findByName(name);
    if (c instanceof Label) {
        ((Label) c).setText(text);
        return;
    }
    ((TextArea) c).setText(text);
    Display.getInstance().onEditingComplete(c, text);
}
Also used : TextArea(com.codename1.ui.TextArea) Label(com.codename1.ui.Label) Component(com.codename1.ui.Component)

Example 65 with Name

use of com.codename1.rad.models.Property.Name in project CodenameOne by codenameone.

the class TestUtils method assertTextArea.

/**
 * Asserts that we have a label with the given text baring the given name
 * @param text the text of the label
 */
public static void assertTextArea(String text) {
    if (verbose) {
        log("assertTextArea(" + text + ")");
    }
    TextArea l = findTextAreaText(text);
    assertBool(l != null, "Null text " + text);
}
Also used : TextArea(com.codename1.ui.TextArea)

Aggregations

IOException (java.io.IOException)36 Component (com.codename1.ui.Component)17 Hashtable (java.util.Hashtable)17 ArrayList (java.util.ArrayList)16 AnimationObject (com.codename1.ui.animations.AnimationObject)15 File (java.io.File)14 Form (com.codename1.ui.Form)13 ByteArrayInputStream (java.io.ByteArrayInputStream)13 FileInputStream (java.io.FileInputStream)13 InputStream (java.io.InputStream)12 Label (com.codename1.ui.Label)11 Button (com.codename1.ui.Button)10 Container (com.codename1.ui.Container)10 EncodedImage (com.codename1.ui.EncodedImage)10 TextArea (com.codename1.ui.TextArea)10 BorderLayout (com.codename1.ui.layouts.BorderLayout)10 Point (java.awt.Point)10 FileOutputStream (java.io.FileOutputStream)10 Paint (android.graphics.Paint)8 BufferedInputStream (com.codename1.io.BufferedInputStream)7