Search in sources :

Example 71 with com.codename1.io

use of com.codename1.io in project CodenameOne by codenameone.

the class TestRunner method init.

private void init(String[] argv) {
    try {
        if (argv[0].startsWith("-") || argv[0].startsWith("/")) {
            printUsage();
            return;
        }
        try {
            mainClass = argv[0];
            int pos = 1;
            while (pos < argv.length) {
                String s = argv[pos];
                if (s.equalsIgnoreCase("-stopOnFail")) {
                    pos++;
                    stopOnFail = true;
                    continue;
                }
                if (s.equalsIgnoreCase("-testCases")) {
                    pos++;
                    testCases = argv[pos].split(",");
                    pos++;
                    continue;
                }
                if (s.equalsIgnoreCase("-skins")) {
                    pos++;
                    skins = argv[pos].split(",");
                    pos++;
                    continue;
                }
                if (s.equalsIgnoreCase("-landscape")) {
                    forceLandscape = true;
                    pos++;
                    continue;
                }
                if (s.equalsIgnoreCase("-portrait")) {
                    forcePortrait = true;
                    pos++;
                    continue;
                }
                if (s.equalsIgnoreCase("-quietMode")) {
                    quietMode = true;
                    pos++;
                    continue;
                }
                if (s.equalsIgnoreCase("-junitXML")) {
                    TestReporting.setInstance(new JUnitXMLReporting());
                }
                if (s.equalsIgnoreCase("-cleanMode")) {
                    cleanMode = true;
                    pos++;
                    continue;
                }
                System.out.println("Unrecognized argument: " + s);
                printUsage();
                System.exit(1);
                return;
            }
        } catch (Exception err) {
            err.printStackTrace();
            printUsage();
            return;
        }
        String[] tests;
        if (testCases == null || testCases.length == 0) {
            InputStream is = getClass().getResourceAsStream("/tests.dat");
            if (is == null) {
                System.err.println("Test data not found in the file, make sure the ant task was executed in full");
                System.exit(2);
                return;
            }
            DataInputStream di = new DataInputStream(is);
            int version = di.readInt();
            if (version > VERSION) {
                System.err.println("Tests were built with a new version of Codename One and can't be executed with this runner");
                System.exit(4);
                return;
            }
            tests = new String[di.readInt()];
            for (int iter = 0; iter < tests.length; iter++) {
                tests[iter] = di.readUTF();
            }
            di.close();
        } else {
            tests = testCases;
        }
        if (forceLandscape) {
            System.out.println("Forcing landscape");
            Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
            pref.putBoolean("Portrait", false);
            pref.sync();
        } else {
            if (forcePortrait) {
                System.out.println("Forcing portrait");
                Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
                pref.putBoolean("Portrait", true);
                pref.sync();
            }
        }
        System.out.println("Preparing to execute " + tests.length + " tests");
        String classPathStr = System.getProperty("java.class.path");
        if (System.getProperty("cn1.class.path") != null) {
            classPathStr += File.pathSeparator + System.getProperty("cn1.class.path");
        }
        StringTokenizer t = new StringTokenizer(classPathStr, File.pathSeparator);
        File[] files = new File[t.countTokens()];
        for (int iter = 0; iter < files.length; iter++) {
            files[iter] = new File(t.nextToken());
        }
        int passedTests = 0;
        int failedTests = 0;
        if (cleanMode) {
            for (String currentTestClass : tests) {
                ClassLoader ldr = new ClassPathLoader(files);
                Class c = Class.forName("com.codename1.impl.javase.TestExecuter", true, ldr);
                Method m = c.getDeclaredMethod("runTest", String.class, String.class, Boolean.TYPE);
                Boolean passed = (Boolean) m.invoke(null, mainClass, currentTestClass, quietMode);
                if (passed.booleanValue()) {
                    passedTests++;
                } else {
                    failedTests++;
                    if (stopOnFail) {
                        System.exit(100);
                        return;
                    }
                }
            }
        } else {
            ClassLoader ldr = new ClassPathLoader(files);
            Class c = Class.forName("com.codename1.impl.javase.TestExecuter", true, ldr);
            for (String currentTestClass : tests) {
                Method m = c.getDeclaredMethod("runTest", String.class, String.class, Boolean.TYPE);
                Boolean passed = (Boolean) m.invoke(null, mainClass, currentTestClass, quietMode);
                if (passed.booleanValue()) {
                    System.out.println(currentTestClass + " passed!");
                    passedTests++;
                } else {
                    System.out.println(currentTestClass + " failed!");
                    failedTests++;
                    if (stopOnFail) {
                        System.exit(100);
                        return;
                    }
                }
            }
        }
        TestReporting.getInstance().testExecutionFinished();
        int exitCode = 0;
        if (failedTests > 0) {
            System.out.println("Test execution finished, some failed tests occured. Passed: " + passedTests + " tests. Failed: " + failedTests + " tests.");
            exitCode = 100;
        } else {
            System.out.println("All tests passed. Total " + passedTests + " tests passed");
        }
        System.exit(exitCode);
    } catch (Exception ex) {
        ex.printStackTrace();
        System.exit(3);
    }
}
Also used : DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) Method(java.lang.reflect.Method) DataInputStream(java.io.DataInputStream) InvocationTargetException(java.lang.reflect.InvocationTargetException) StringTokenizer(java.util.StringTokenizer) JUnitXMLReporting(com.codename1.testing.JUnitXMLReporting) Preferences(java.util.prefs.Preferences) File(java.io.File)

Example 72 with com.codename1.io

use of com.codename1.io in project CodenameOne by codenameone.

the class ComponentSelector method setIcon.

/**
 * Sets the icon of all elements in this set to a material icon.  This will use
 * the foreground color of each label as the icon's foreground color.
 * @param materialIcon The icon charcode.
 * @param size The size of the icon (in mm)
 * @return Self for chaining
 * @see FontImage#createMaterial(char, com.codename1.ui.plaf.Style, float)
 */
public ComponentSelector setIcon(char materialIcon, float size) {
    for (Component c : this) {
        if (c instanceof Label) {
            Label l = (Label) c;
            Style style = new Style();
            Style cStyle = c.getUnselectedStyle();
            style.setBgTransparency(0);
            style.setFgColor(cStyle.getFgColor());
            l.setIcon(FontImage.createMaterial(materialIcon, style, size));
            if (c instanceof Button) {
                Button b = (Button) c;
                style = new Style();
                cStyle = c.getPressedStyle();
                style.setBgTransparency(0);
                style.setFgColor(cStyle.getFgColor());
                b.setPressedIcon(FontImage.createMaterial(materialIcon, style, size));
            }
        }
    }
    return this;
}
Also used : SpanButton(com.codename1.components.SpanButton) SpanLabel(com.codename1.components.SpanLabel) Style(com.codename1.ui.plaf.Style)

Example 73 with com.codename1.io

use of com.codename1.io in project CodenameOne by codenameone.

the class ComponentSelector method setPadding.

/**
 * Sets padding to all components in found set.
 * @param top Top padding in pixels.
 * @param right Right padding in pixels
 * @param bottom Bottom padding in pixels.
 * @param left Left padding in pixels.
 * @return
 * @see #getStyle(com.codename1.ui.Component)
 */
public ComponentSelector setPadding(int top, int right, int bottom, int left) {
    Style s = currentStyle();
    s.setPaddingUnit(Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS);
    s.setPadding(top, bottom, left, right);
    return this;
}
Also used : Style(com.codename1.ui.plaf.Style)

Example 74 with com.codename1.io

use of com.codename1.io in project CodenameOne by codenameone.

the class ComponentSelector method fadeOut.

/**
 * Fades out components in this set.
 * @param duration Duration of animation.
 * @param callback Callback to run when animation completes.
 * @return Self for chaining.
 */
public ComponentSelector fadeOut(int duration, final SuccessCallback<ComponentSelector> callback) {
    final String placeholderProperty = "com.codename1.ui.ComponentSelector#fadeOutPlaceholder";
    AnimationManager mgr = null;
    ArrayList<ComponentAnimation> animations = new ArrayList<ComponentAnimation>();
    final ArrayList<Component> animatingComponents = new ArrayList<Component>();
    for (Component c : this) {
        Container parent = c.getParent();
        if (parent != null) {
            AnimationManager cmgr = c.getAnimationManager();
            if (cmgr != null) {
                mgr = cmgr;
                Container placeholder = new Container();
                // placeholder.setShowEvenIfBlank(true);
                c.putClientProperty(placeholderProperty, placeholder);
                Component.setSameHeight(placeholder, c);
                Component.setSameWidth(placeholder, c);
                $(placeholder).setMargin(c.getStyle().getMarginTop(), c.getStyle().getMarginRight(false), c.getStyle().getMarginBottom(), c.getStyle().getMarginLeft(false)).setPadding(c.getStyle().getPaddingTop(), c.getStyle().getPaddingRight(false), c.getStyle().getPaddingBottom(), c.getStyle().getPaddingLeft(false));
                ComponentAnimation a = parent.createReplaceTransition(c, placeholder, CommonTransitions.createFade(duration));
                animations.add(a);
                animatingComponents.add(c);
            }
        // centerBackground.add(BorderLayout.CENTER, boxy);
        }
    }
    if (mgr != null) {
        mgr.addAnimation(ComponentAnimation.compoundAnimation(animations.toArray(new ComponentAnimation[animations.size()])), new Runnable() {

            public void run() {
                for (final Component c : animatingComponents) {
                    // c.setHidden(true);
                    c.setVisible(false);
                    final Container placeholder = (Container) c.getClientProperty(placeholderProperty);
                    c.putClientProperty(placeholderProperty, null);
                    if (placeholder != null) {
                        Container parent = placeholder.getParent();
                        /*
                            if (parent == null) {
                                System.out.println("Deferring replace back");
                                $(new Runnable() {
                                    public void run() {
                                        Container parent = placeholder.getParent();
                                        if (parent != null) {
                                            System.out.println("Found parent after deferral");
                                            parent.replace(placeholder, c, CommonTransitions.createEmpty());
                                        }
                                    }
                                });
                            } else {
                            */
                        if (parent != null) {
                            parent.replace(placeholder, c, CommonTransitions.createEmpty());
                        }
                    // }
                    }
                }
                if (callback != null) {
                    callback.onSucess(ComponentSelector.this);
                }
            }
        });
    }
    return this;
}
Also used : ComponentAnimation(com.codename1.ui.animations.ComponentAnimation) ArrayList(java.util.ArrayList)

Example 75 with com.codename1.io

use of com.codename1.io 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)

Aggregations

IOException (java.io.IOException)39 EncodedImage (com.codename1.ui.EncodedImage)29 ArrayList (java.util.ArrayList)27 Point (java.awt.Point)25 File (java.io.File)24 BufferedImage (java.awt.image.BufferedImage)23 AnimationObject (com.codename1.ui.animations.AnimationObject)22 Form (com.codename1.ui.Form)19 Hashtable (java.util.Hashtable)19 Component (com.codename1.ui.Component)18 Image (com.codename1.ui.Image)18 EditableResources (com.codename1.ui.util.EditableResources)16 FileInputStream (java.io.FileInputStream)16 Timeline (com.codename1.ui.animations.Timeline)15 BorderLayout (com.codename1.ui.layouts.BorderLayout)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)11 FileOutputStream (java.io.FileOutputStream)10 EditorFont (com.codename1.ui.EditorFont)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9