Changeset 621


Ignore:
Timestamp:
01/24/10 17:43:39 (2 years ago)
Author:
octorian
Message:

Minor code cleanup and addition of test tree icons

Location:
trunk/J2MEUnit
Files:
4 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/J2MEUnit/src/j2meunit/rimui/TestConsoleScreen.java

    r567 r621  
    1 /* 
    2  * TestConsoleScreen.java 
     1/*- 
     2 * Copyright (c) 2010, Derek Konigsberg 
     3 * All rights reserved. 
    34 * 
    4  * Created on February 2, 2007, 10:11 PM 
     5 * Redistribution and use in source and binary forms, with or without 
     6 * modification, are permitted provided that the following conditions 
     7 * are met: 
    58 * 
    6  * To change this template, choose Tools | Template Manager 
    7  * and open the template in the editor. 
     9 * 1. Redistributions of source code must retain the above copyright 
     10 *    notice, this list of conditions and the following disclaimer.  
     11 * 2. Redistributions in binary form must reproduce the above copyright 
     12 *    notice, this list of conditions and the following disclaimer in the 
     13 *    documentation and/or other materials provided with the distribution.  
     14 * 3. Neither the name of the project nor the names of its 
     15 *    contributors may be used to endorse or promote products derived 
     16 *    from this software without specific prior written permission. 
     17 * 
     18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
     19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
     20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
     21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
     22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
     23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
     24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
     25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
     26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
     27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
     28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
     29 * OF THE POSSIBILITY OF SUCH DAMAGE. 
    830 */ 
    931 
     
    2042import java.util.Hashtable; 
    2143import java.util.Vector; 
     44 
     45import net.rim.device.api.system.Bitmap; 
    2246import net.rim.device.api.system.Characters; 
    2347import net.rim.device.api.ui.Field; 
     
    3559 
    3660/** 
    37  * 
     61 * J2MEUnit Test Console Screen for the BlackBerry UI. 
     62 *  
    3863 * @author Derek Konigsberg 
    3964 */ 
    40 public class TestConsoleScreen extends MainScreen implements TreeFieldCallback, TestListener { 
    41         private LabelField titleLabel; 
     65public class TestConsoleScreen extends MainScreen implements TestListener { 
     66    private LabelField titleLabel; 
    4267    private LabelField statusLabel; 
    4368    private LabelField failureLabel; 
     
    5277    private Test selectedTest; 
    5378    private long elapsedTime; 
    54      
     79 
     80    private Bitmap testInitialBitmap = Bitmap.getBitmapResource("test_initial.png"); 
     81    private Bitmap testPassedBitmap = Bitmap.getBitmapResource("test_passed.png"); 
     82    private Bitmap testFailedBitmap = Bitmap.getBitmapResource("test_failed.png"); 
     83 
    5584    private static class TestTreeItem { 
    5685        public TestTreeItem(Test test) { 
    5786            this.test = test; 
    58             if(test instanceof TestCase) 
     87            if(test instanceof TestCase) { 
    5988                name = ((TestCase)test).getName(); 
    60             else if(test instanceof TestSuite) 
     89            } 
     90            else if(test instanceof TestSuite) { 
    6191                name = ((TestSuite)test).toString(); 
    62             else 
     92            } 
     93            else { 
    6394                name = "Test"; 
     95            } 
    6496            this.hasRun = false; 
    6597            this.hasPassed = true; 
     
    72104        public int id; 
    73105    } 
    74      
     106 
    75107    /** 
    76108     * Creates a new instance of TestConsoleScreen 
     
    83115        populateTestTree(mainTestSuite, 0); 
    84116    } 
    85      
     117 
    86118    private void initializeFields() { 
    87         Font labelFont = Font.getDefault().derive(Font.PLAIN, 12); 
     119        Font labelFont = Font.getDefault().derive(Font.PLAIN, 12); 
    88120        Font treeFont = Font.getDefault().derive(Font.PLAIN, 14); 
    89                  
    90                 titleLabel = new LabelField("J2MEUnit " + j2meunit.util.Version.id()); 
    91                 titleLabel.setFont(labelFont); 
     121 
     122        titleLabel = new LabelField("J2MEUnit " + j2meunit.util.Version.id()); 
     123        titleLabel.setFont(labelFont); 
    92124        statusLabel = new LabelField("Status: Idle"); 
    93125        statusLabel.setFont(labelFont); 
     
    100132        progressGauge = new GaugeField(null, 0, 100, 0, GaugeField.NO_TEXT); 
    101133        progressGauge.setFont(labelFont); 
    102          
    103         testTreeField = new TreeField(this, Field.FOCUSABLE); 
     134 
     135        testTreeField = new TreeField(new TreeFieldCallback() { 
     136            public void drawTreeItem(TreeField treeField, Graphics graphics, int node, int y, int width, int indent) { 
     137                testTreeField_DrawTreeItem(treeField, graphics, node, y, width, indent); 
     138            } 
     139        }, Field.FOCUSABLE); 
    104140        testTreeField.setEmptyString("No tests", 0); 
    105141        testTreeField.setDefaultExpanded(true); 
     
    107143        testTreeField.setFont(treeFont); 
    108144        testTreeField.setRowHeight(treeFont.getHeight() + 2); 
    109          
     145 
    110146        add(titleLabel); 
    111147        add(statusLabel); 
     
    117153        add(testTreeField); 
    118154    } 
    119      
     155 
    120156    private void populateTestTree(TestSuite suite, int parentId) { 
    121157        // Get the tests 
     
    124160            tests.addElement(e.nextElement()); 
    125161        } 
    126          
     162 
    127163        // Iterate backwards so the tree is populated in the correct order 
    128164        int size = tests.size(); 
     
    138174        } 
    139175    } 
    140      
     176 
    141177    /** Standard Escape-key handler */ 
    142178    public boolean keyChar(char key, int status, int time) { 
    143179        boolean retval = false; 
    144180        switch (key) { 
    145             case Characters.ESCAPE: 
    146                 System.exit(1); 
    147                 break; 
    148             default: 
    149                 retval = super.keyChar(key, status, time); 
     181        case Characters.ESCAPE: 
     182            System.exit(1); 
     183            break; 
     184        default: 
     185            retval = super.keyChar(key, status, time); 
    150186        } 
    151187        return retval; 
    152188    } 
    153      
    154     public void drawTreeItem(TreeField treeField, Graphics graphics, int node, int y, int width, int indent) { 
     189 
     190    private void testTreeField_DrawTreeItem(TreeField treeField, Graphics graphics, int node, int y, int width, int indent) { 
    155191        Object cookie = testTreeField.getCookie(node); 
    156          
     192 
    157193        if(cookie instanceof TestTreeItem) { 
    158194            TestTreeItem item = (TestTreeItem)cookie; 
    159195            int height = testTreeField.getRowHeight(); 
     196 
     197            Bitmap iconBitmap = getItemIcon(item); 
    160198             
    161             int oldColor = graphics.getColor(); 
    162              
    163             if(item.hasRun) { 
    164                 if(item.hasPassed) 
    165                     graphics.setColor(0x0000FF00); // green 
    166                 else 
    167                     graphics.setColor(0x00FF0000); // red 
    168             } else 
    169                 graphics.setColor(0x00C0C0C0); // grey 
    170              
    171             graphics.fillArc(indent, y+1, height-2, height-2, 0, 360); 
    172             graphics.setColor(oldColor); 
    173              
    174             graphics.drawText(item.name, indent + height, y, Graphics.ELLIPSIS, width); 
    175         } 
    176     } 
    177      
    178     private MenuItem resultsItem = new MenuItem("View results", 100000, 10) { 
     199            graphics.drawBitmap(indent, y, 16, 16, iconBitmap, 0, 0); 
     200 
     201            graphics.drawText(item.name, indent + height + 2, y, Graphics.ELLIPSIS, width - height - 2); 
     202        } 
     203    } 
     204 
     205    private Bitmap getItemIcon(TestTreeItem item) { 
     206        Bitmap iconBitmap; 
     207        if(item.hasRun) { 
     208            if(item.hasPassed) { 
     209                iconBitmap = testPassedBitmap; 
     210            } 
     211            else { 
     212                iconBitmap = testFailedBitmap; 
     213            } 
     214        } 
     215        else { 
     216            iconBitmap = testInitialBitmap; 
     217        } 
     218        return iconBitmap; 
     219    } 
     220 
     221    private MenuItem resultsItem = new MenuItem("View results", 300100, 1010) { 
    179222        public void run() { 
    180223            showTestResults(); 
    181224        } 
    182225    }; 
    183     private MenuItem runAllItem = new MenuItem("Run all", 100010, 10) { 
     226    private MenuItem runAllItem = new MenuItem("Run all", 400110, 1020) { 
    184227        public void run() { 
    185228            runAllTests(); 
    186229        } 
    187230    }; 
    188     private MenuItem runSelectedItem = new MenuItem("Run selected", 100020, 10) { 
     231    private MenuItem runSelectedItem = new MenuItem("Run selected", 400120, 1030) { 
    189232        public void run() { 
    190233            runSelectedTests(); 
    191234        } 
    192235    }; 
    193     private MenuItem exitItem = new MenuItem("Exit", 200001, 10) { 
     236    private MenuItem exitItem = new MenuItem("Exit", 60000100, 9000) { 
    194237        public void run() { 
    195238            System.exit(0); 
     
    202245            TestTreeItem item = (TestTreeItem)testTreeField.getCookie(node); 
    203246            if(item.hasRun && item.test instanceof TestCase) { 
    204                 menu.addSeparator(); 
    205247                menu.add(resultsItem); 
    206248            } 
    207249        } 
    208         menu.addSeparator(); 
    209250        menu.add(runAllItem); 
    210251        menu.add(runSelectedItem); 
    211         menu.addSeparator(); 
    212252        menu.add(exitItem); 
    213253    } 
     
    218258        } 
    219259    } 
    220      
     260 
    221261    public synchronized void addFailure(Test test, AssertionFailedError assertionFailedError) { 
    222262        synchronized(UiApplication.getEventLock()) { 
     
    224264        } 
    225265    } 
    226      
     266 
    227267    public void endTest(Test test) { 
    228268        synchronized(UiApplication.getEventLock()) { 
     
    233273        updateTestTree(); 
    234274    } 
    235      
     275 
    236276    public void endTestStep(Test test) { 
    237277        synchronized(UiApplication.getEventLock()) { 
     
    242282        updateTestTree(); 
    243283    } 
    244      
     284 
    245285    public synchronized void startTest(Test test) { 
    246286        TestTreeItem item = (TestTreeItem)testTreeItems.get(test.toString()); 
     
    266306        item.hasRun = hasAllRun; 
    267307        item.hasPassed = hasAllPassed; 
    268          
     308 
    269309        if(testTreeField.getParent(item.id) >= 0) { 
    270310            updateTestBranch(item.test); 
    271311        } 
    272312    } 
    273      
     313 
    274314    private void updateTreeErrors() { 
    275315        // Update the test data structures 
     
    288328        } 
    289329    } 
    290      
     330 
    291331    private synchronized void updateTestTree() { 
    292332        // Repaint the tree 
     
    306346        if(!(item.test instanceof TestCase)) return; 
    307347        TestCase testCase = (TestCase)item.test; 
    308          
     348 
    309349        Enumeration e; 
    310350        TestFailure testFailure = null; 
    311351        TestFailure testError = null; 
    312          
     352 
    313353        for(e = testResults.failures(); e.hasMoreElements(); ) { 
    314354            TestFailure failure = (TestFailure)e.nextElement(); 
     
    318358            } 
    319359        } 
    320          
     360 
    321361        for(e = testResults.errors(); e.hasMoreElements(); ) { 
    322362            TestFailure error = (TestFailure)e.nextElement(); 
     
    326366            } 
    327367        } 
    328          
     368 
    329369        UiApplication.getUiApplication().pushModalScreen( 
    330370                new TestResultsScreen(testCase, testFailure, testError)); 
    331371    } 
    332      
     372 
    333373    /** 
    334374     * Builds a test suite from all test case classes in a string array. 
     
    342382            return testSuite; 
    343383        } 
    344          
     384 
    345385        for (int i = 0; i < testCaseClasses.length; i++) { 
    346386            try { 
     
    351391            } catch (Throwable t) { 
    352392                System.out.println("Access to TestCase " + testCaseClasses[i] + 
    353                     " failed: " + t.getMessage() + " - " + 
    354                     t.getClass().getName()); 
    355             } 
    356         } 
    357          
     393                        " failed: " + t.getMessage() + " - " + 
     394                        t.getClass().getName()); 
     395            } 
     396        } 
     397 
    358398        return testSuite; 
    359399    } 
    360      
     400 
    361401    /** 
    362402     * Run all tests in the given test suite. 
     
    367407        testResults = new TestResult(); 
    368408        testResults.addListener(this); 
    369          
     409 
    370410        long startTime = System.currentTimeMillis(); 
    371411 
     
    377417        // Run the suite 
    378418        suite.run(testResults); 
    379          
     419 
    380420        long endTime = System.currentTimeMillis(); 
    381421        elapsedTime = endTime - startTime; 
     
    386426        runTests(); 
    387427    } 
    388      
     428 
    389429    private void runSelectedTests() { 
    390430        int nodeId = testTreeField.getCurrentNode(); 
     
    399439        new Thread() { 
    400440            public void run() { 
    401                 Thread.yield(); 
     441                Thread.yield(); 
    402442                try { 
    403443                    doRun(selectedTest); 
     
    410450        }.start(); 
    411451    } 
    412      
     452 
    413453    private synchronized void updateResults() { 
    414454        UiApplication.getUiApplication().invokeAndWait(new Runnable() { 
  • trunk/J2MEUnit/src/j2meunit/rimui/TestResultsScreen.java

    r305 r621  
    1 /* 
    2  * TestResultsScreen.java 
     1/*- 
     2 * Copyright (c) 2010, Derek Konigsberg 
     3 * All rights reserved. 
    34 * 
    4  * Created on February 4, 2007, 2:27 PM 
     5 * Redistribution and use in source and binary forms, with or without 
     6 * modification, are permitted provided that the following conditions 
     7 * are met: 
    58 * 
    6  * To change this template, choose Tools | Template Manager 
    7  * and open the template in the editor. 
     9 * 1. Redistributions of source code must retain the above copyright 
     10 *    notice, this list of conditions and the following disclaimer.  
     11 * 2. Redistributions in binary form must reproduce the above copyright 
     12 *    notice, this list of conditions and the following disclaimer in the 
     13 *    documentation and/or other materials provided with the distribution.  
     14 * 3. Neither the name of the project nor the names of its 
     15 *    contributors may be used to endorse or promote products derived 
     16 *    from this software without specific prior written permission. 
     17 * 
     18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
     19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
     20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
     21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
     22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
     23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
     24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
     25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
     26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
     27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
     28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
     29 * OF THE POSSIBILITY OF SUCH DAMAGE. 
    830 */ 
    931 
  • trunk/J2MEUnit/src/j2meunit/rimui/TestRunner.java

    r305 r621  
    1 /* 
    2  * TestRunner.java 
     1/*- 
     2 * Copyright (c) 2010, Derek Konigsberg 
     3 * All rights reserved. 
    34 * 
    4  * Created on February 2, 2007, 10:03 PM 
     5 * Redistribution and use in source and binary forms, with or without 
     6 * modification, are permitted provided that the following conditions 
     7 * are met: 
    58 * 
    6  * To change this template, choose Tools | Template Manager 
    7  * and open the template in the editor. 
     9 * 1. Redistributions of source code must retain the above copyright 
     10 *    notice, this list of conditions and the following disclaimer.  
     11 * 2. Redistributions in binary form must reproduce the above copyright 
     12 *    notice, this list of conditions and the following disclaimer in the 
     13 *    documentation and/or other materials provided with the distribution.  
     14 * 3. Neither the name of the project nor the names of its 
     15 *    contributors may be used to endorse or promote products derived 
     16 *    from this software without specific prior written permission. 
     17 * 
     18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
     19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
     20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
     21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
     22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
     23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
     24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
     25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
     26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
     27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
     28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
     29 * OF THE POSSIBILITY OF SUCH DAMAGE. 
    830 */ 
    931 
Note: See TracChangeset for help on using the changeset viewer.