javauis/lcdui_qt/tsrc/src/com/nokia/openlcdui/mt/alert/AlertTest.java
changeset 21 2a9601315dfc
child 23 98ccebc37403
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 package com.nokia.openlcdui.mt.alert;
       
    18 
       
    19 import junit.framework.*;
       
    20 import javax.microedition.lcdui.*;
       
    21 
       
    22 import java.io.IOException;
       
    23 
       
    24 import com.nokia.openlcdui.mt.SWTTestCase;
       
    25 
       
    26 /**
       
    27  * TEST CASE SPECIFICATION <br>
       
    28  * <br>
       
    29  * Short description of the module test:
       
    30  * <li>Collection of tests to test Alert's API functionality. <br>
       
    31  * <br>
       
    32  */
       
    33 public class AlertTest extends SWTTestCase {
       
    34 
       
    35     private static String shortString = "Lorem ipsum";
       
    36 
       
    37     private static String longString = "Lorem ipsum dolor sit amet, "
       
    38             + "consectetuer adipiscing elit. Cras turpis ligula, "
       
    39             + "condimentum nec, rhoncus quis, molestie in, arcu. "
       
    40             + "Curabitur id lacus. Quisque dictum nulla id odio. "
       
    41             + "Nullam nec urna. Cras ac lacus nec lacus iaculis aliquet."
       
    42             + " Integer ut eros. Proin laoreet justo et augue. "
       
    43             + "Praesent dui. Proin vel leo a eros auctor convallis. "
       
    44             + "Aenean urna nunc, sagittis vel, pellentesque a, "
       
    45             + "luctus a, metus. Phasellus posuere lacus nec augue."
       
    46             + "Nullam nec urna. Cras ac lacus nec lacus iaculis aliquet."
       
    47             + " Integer ut eros. Proin laoreet justo et augue. "
       
    48             + "Praesent dui. Proin vel leo a eros auctor convallis. "
       
    49             + "Aenean urna nunc, sagittis vel, pellentesque a, "
       
    50             + "luctus a, metus. Phasellus posuere lacus nec augue."
       
    51             + "Nullam nec urna. Cras ac lacus nec lacus iaculis aliquet."
       
    52             + " Integer ut eros. Proin laoreet justo et augue. "
       
    53             + "Praesent dui. Proin vel leo a eros auctor convallis. "
       
    54             + "Aenean urna nunc, sagittis vel, pellentesque a, "
       
    55             + "luctus a, metus. Phasellus posuere lacus nec augue.";
       
    56 
       
    57 
       
    58     private static final int INVALID_ALERT_TIMEOUT = -3000;
       
    59     private static final int VALID_ALERT_TIMEOUT = 2000;
       
    60 
       
    61 
       
    62     /**
       
    63      * Constructor.
       
    64      */
       
    65     public AlertTest() {
       
    66     }
       
    67 
       
    68     /**
       
    69      * Constructor.
       
    70      *
       
    71      * @param sTestName Test name.
       
    72      * @param rTestMethod Test method.
       
    73      */
       
    74     public AlertTest(String sTestName) {
       
    75         super(sTestName);
       
    76     }
       
    77 
       
    78     /**
       
    79      * To create the test suite. You need to add a new aSuite.addTest entry for
       
    80      * any new test methods.
       
    81      *
       
    82      * @return New testsuite.
       
    83      */
       
    84     public static Test suite() {
       
    85         TestSuite suite = new TestSuite();
       
    86 
       
    87         java.util.Vector methodNames;
       
    88 	    java.util.Enumeration e;
       
    89 
       
    90 	    // Add widget tests
       
    91 	    methodNames = AlertTest.methodNames();
       
    92 	    e = methodNames.elements();
       
    93 	    while (e.hasMoreElements()) {
       
    94 	        suite.addTest(new AlertTest((String)e.nextElement()));
       
    95 	    }
       
    96         
       
    97         return suite;
       
    98     }
       
    99 
       
   100     public static java.util.Vector methodNames() {
       
   101         java.util.Vector methodNames = new java.util.Vector();
       
   102         methodNames.addElement("testConstructors");
       
   103         methodNames.addElement("testSetGetTimeout");
       
   104         methodNames.addElement("testSetGetType");
       
   105         methodNames.addElement("testSetGetImage");
       
   106         methodNames.addElement("testSetGetTexts");
       
   107         methodNames.addElement("testSetGetIndicators");
       
   108         methodNames.addElement("testCommands");
       
   109         return methodNames;
       
   110     }
       
   111     
       
   112     protected void runTest() throws Throwable {
       
   113         if (getName().equals("testConstructors")) testConstructors();
       
   114         else if (getName().equals("testSetGetTimeout")) testSetGetTimeout();
       
   115         else if (getName().equals("testSetGetType")) testSetGetType();
       
   116         else if (getName().equals("testSetGetImage")) testSetGetImage();
       
   117         else if (getName().equals("testSetGetTexts")) testSetGetTexts();
       
   118         else if (getName().equals("testSetGetIndicators")) testSetGetIndicators();
       
   119         else if (getName().equals("testCommands")) testCommands();
       
   120         else super.runTest();
       
   121     }    
       
   122     
       
   123     /**
       
   124      * Tests the constructors.
       
   125      */
       
   126     public void testConstructors() {
       
   127         try {
       
   128             new Alert(null);
       
   129         }
       
   130         catch (Exception e) {
       
   131             fail("Exception - Constructor with null title - " + e.getMessage());
       
   132         }
       
   133 
       
   134         try {
       
   135             new Alert(shortString);
       
   136         }
       
   137         catch (Exception e) {
       
   138             fail("Exception - Constructor with short text - " + e.getMessage());
       
   139         }
       
   140 
       
   141         try {
       
   142             new Alert(shortString, longString, null, null);
       
   143         }
       
   144         catch (Exception e) {
       
   145             fail("Exception - Constructor with long text - " + e.getMessage());
       
   146         }
       
   147     }
       
   148 
       
   149     /**
       
   150      * Tests setter and getter t.
       
   151      */
       
   152     public void testSetGetTimeout() {
       
   153         Alert alert = new Alert(shortString);
       
   154         alert.setTimeout(VALID_ALERT_TIMEOUT);
       
   155         assertEquals("Timeouts don't match.", VALID_ALERT_TIMEOUT, alert
       
   156                 .getTimeout());
       
   157         alert.setTimeout(Alert.FOREVER);
       
   158         assertEquals("Timeouts don't match.", Alert.FOREVER, alert.getTimeout());
       
   159         try {
       
   160             alert.setTimeout(INVALID_ALERT_TIMEOUT);
       
   161             fail("IllegalArgumentException should be thrown");
       
   162         }
       
   163         catch (IllegalArgumentException iae) {
       
   164             // OK
       
   165         }
       
   166     }
       
   167 
       
   168     /**
       
   169      * Tests set and get type.
       
   170      */
       
   171     public void testSetGetType() {
       
   172         Alert alert = new Alert(shortString);
       
   173 
       
   174         alert.setType(AlertType.ALARM);
       
   175         assertEquals("Types don't match.", AlertType.ALARM, alert.getType());
       
   176 
       
   177         alert.setType(AlertType.CONFIRMATION);
       
   178         assertEquals("Types don't match.", AlertType.CONFIRMATION, alert
       
   179                 .getType());
       
   180 
       
   181         alert.setType(AlertType.ERROR);
       
   182         assertEquals("Types don't match.", AlertType.ERROR, alert.getType());
       
   183 
       
   184         alert.setType(AlertType.INFO);
       
   185         assertEquals("Types don't match.", AlertType.INFO, alert.getType());
       
   186 
       
   187         alert.setType(AlertType.WARNING);
       
   188         assertEquals("Types don't match.", AlertType.WARNING, alert.getType());
       
   189     }
       
   190 
       
   191     /**
       
   192      * Tests set and get image.
       
   193      */
       
   194     public void testSetGetImage() {
       
   195         try {
       
   196             Alert alert = new Alert(shortString);
       
   197             alert.setImage(null);
       
   198             assertEquals("Images don't match.", null, alert.getImage());
       
   199 
       
   200             Image param = Image.createImage("100x100.png");
       
   201             alert.setImage(param);
       
   202             assertEquals("Images don't match.", param, alert.getImage());
       
   203         }
       
   204         catch (IOException e) {
       
   205             fail("Cannot load image 100x100.png");
       
   206         }
       
   207     }
       
   208 
       
   209     /**
       
   210      * Tests set and get texts.
       
   211      */
       
   212     public void testSetGetTexts() {
       
   213         Alert alert = new Alert(shortString);
       
   214         alert.setString(null);
       
   215         assertEquals("String don't match.", null, alert.getString());
       
   216 
       
   217         alert.setString(shortString);
       
   218         assertEquals("Strings dont match.", shortString, alert.getString());
       
   219         assertTrue("Alert with short text is modal",
       
   220                 alert.getTimeout() != Alert.FOREVER);
       
   221 
       
   222         alert.setString(longString);
       
   223         assertEquals("Strings dont match", longString, alert.getString());
       
   224         assertTrue("Alert with long text is not modal",
       
   225                 alert.getTimeout() == Alert.FOREVER);
       
   226     }
       
   227 
       
   228     /**
       
   229      * Tests set and get indicator methods and Gauge-Alert interaction related
       
   230      * Item methods.
       
   231      */
       
   232     public void testSetGetIndicators() {
       
   233         Alert alert = new Alert(shortString);
       
   234         Gauge param1 = new Gauge(null, false, 10, 0);
       
   235         alert.setIndicator(param1);
       
   236         assertEquals("Indicators don't match.", param1, alert.getIndicator());
       
   237 
       
   238         try {
       
   239             Gauge param2 = new Gauge("Label", true, 10, 0);
       
   240             alert.setIndicator(param2);
       
   241             fail("1. IllegalArgumentException expected");
       
   242         }
       
   243         catch (IllegalArgumentException iae) {
       
   244         }
       
   245 
       
   246         try {
       
   247             param1.addCommand(new Command("Comm1", Command.ITEM, 0));
       
   248             fail("2. IllegalStateException expected");
       
   249         }
       
   250         catch (IllegalStateException ise) {
       
   251         }
       
   252 
       
   253         try {
       
   254             param1.setDefaultCommand(new Command("Comm1", Command.ITEM, 0));
       
   255             fail("3. IllegalStateException expected");
       
   256         }
       
   257         catch (IllegalStateException ise) {
       
   258         }
       
   259 
       
   260         try {
       
   261             ItemCommandListener icl = new ItemCommandListener() {
       
   262                 public void commandAction(Command command, Item item) {
       
   263                 }
       
   264             };
       
   265             param1.setItemCommandListener(icl);
       
   266             fail("4. IllegalStateException expected");
       
   267         }
       
   268         catch (IllegalStateException ise) {
       
   269         }
       
   270 
       
   271         try {
       
   272             param1.setLabel("Label");
       
   273             fail("5. IllegalStateException expected");
       
   274         }
       
   275         catch (IllegalStateException ise) {
       
   276         }
       
   277 
       
   278         try {
       
   279             param1.setLayout(Item.LAYOUT_BOTTOM);
       
   280             fail("6. IllegalStateException expected");
       
   281         }
       
   282         catch (IllegalStateException ise) {
       
   283         }
       
   284 
       
   285         try {
       
   286             param1.setPreferredSize(10, 10);
       
   287             fail("7. IllegalStateException expected");
       
   288         }
       
   289         catch (IllegalStateException ise) {
       
   290         }
       
   291 
       
   292         alert.setIndicator(null);
       
   293     }
       
   294 
       
   295     /**
       
   296      * Tests commands on Alert.
       
   297      */
       
   298     public void testCommands() {
       
   299         try {
       
   300             Alert alert = new Alert(shortString);
       
   301             CommandListener param = new CommandListener() {
       
   302                 public void commandAction(Command c, Displayable d) {
       
   303                 }
       
   304             };
       
   305             alert.setCommandListener(param);
       
   306             alert.setCommandListener(null);
       
   307         }
       
   308         catch (Exception e) {
       
   309             fail("Exception - Setting and removing CommandListener - "
       
   310                     + e.getMessage());
       
   311         }
       
   312 
       
   313         try {
       
   314             Alert alert = new Alert(shortString);
       
   315             // adding null command
       
   316             alert.addCommand(null);
       
   317             fail("NullPointerException expected - NULL command added to Alert");
       
   318         }
       
   319         catch (NullPointerException npe) {
       
   320             // Ok
       
   321         }
       
   322         catch (Exception e) {
       
   323             fail("Wrong exception is thrown instead of NullPointerException - "
       
   324                     + "NULL command added to Alert" + e.getMessage());
       
   325         }
       
   326 
       
   327         try {
       
   328             Alert alert = new Alert(shortString);
       
   329 
       
   330             // test default command
       
   331             alert.addCommand(Alert.DISMISS_COMMAND);
       
   332             assertTrue("Default Alert shouldn't be modal",
       
   333                     alert.getTimeout() != Alert.FOREVER);
       
   334             alert.removeCommand(Alert.DISMISS_COMMAND);
       
   335 
       
   336             Command c1 = new Command("Comm1", Command.ITEM, 0);
       
   337             Command c2 = new Command("Comm2", Command.ITEM, 0);
       
   338             Command c3 = new Command("Comm3", Command.ITEM, 0);
       
   339             Command c4 = new Command("Comm4", Command.ITEM, 0);
       
   340             Command c5 = new Command("Comm5", Command.ITEM, 0);
       
   341 
       
   342             // add commands
       
   343             alert.addCommand(c1);
       
   344             assertTrue("Default Alert with 1 command shouldn't be modal", alert
       
   345                     .getTimeout() != Alert.FOREVER);
       
   346             alert.addCommand(c2);
       
   347             assertTrue("Default Alert with 2 commands should be modal", alert
       
   348                     .getTimeout() == Alert.FOREVER);
       
   349             alert.addCommand(c3);
       
   350             alert.addCommand(c4);
       
   351             alert.addCommand(c5);
       
   352             assertTrue("Default Alert with 5 commands should be modal", alert
       
   353                     .getTimeout() == Alert.FOREVER);
       
   354 
       
   355             // remove commands
       
   356             alert.removeCommand(c5);
       
   357             alert.removeCommand(c4);
       
   358             alert.removeCommand(c3);
       
   359             assertTrue("Default Alert with 2 commands should be modal", alert
       
   360                     .getTimeout() == Alert.FOREVER);
       
   361             alert.removeCommand(c2);
       
   362             assertTrue("Default Alert with 1 command shouldn't be modal", alert
       
   363                     .getTimeout() != Alert.FOREVER);
       
   364             alert.removeCommand(c1);
       
   365             assertTrue("Default Alert shouldn't be modal",
       
   366                     alert.getTimeout() != Alert.FOREVER);
       
   367         }
       
   368         catch (Exception e) {
       
   369             fail("Exception - Adding 5 commands - " + e.getMessage());
       
   370         }
       
   371     }
       
   372 
       
   373 }