javauis/lcdui_qt/tsrc/uitestsrc/t_uirobot/stringitem/HyperLinkTest.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 t_uirobot.stringitem;
       
    18 
       
    19 import j2meunit.framework.*;
       
    20 
       
    21 import javax.microedition.lcdui.*;
       
    22 
       
    23 import t_uirobot.ItemUITestBase;
       
    24 
       
    25 import com.nokia.mj.impl.uitestutils.Key;
       
    26 
       
    27 /**
       
    28  * TEST CASE SPECIFICATION <br>
       
    29  * <br>
       
    30  * Short description of the module test:
       
    31  * <li> UI-robot based test to test hyperlink. <br>
       
    32  * <br>
       
    33  * Created: 2008-10-20
       
    34  */
       
    35 public class HyperLinkTest extends ItemUITestBase {
       
    36 
       
    37     /**
       
    38      * Constructor.
       
    39      */
       
    40     public HyperLinkTest() {
       
    41     }
       
    42 
       
    43     /**
       
    44      * Constructor.
       
    45      *
       
    46      * @param sTestName Test name.
       
    47      * @param rTestMethod Test method.
       
    48      */
       
    49     public HyperLinkTest(String sTestName, TestMethod rTestMethod) {
       
    50         super(sTestName, rTestMethod);
       
    51     }
       
    52 
       
    53     /**
       
    54      * Creates the test suite. You need to add a new aSuite.addTest entry for
       
    55      * any new test methods, otherwise they won't be run.
       
    56      *
       
    57      * @return New testsuite.
       
    58      */
       
    59     public Test suite() {
       
    60         TestSuite aSuite = new TestSuite();
       
    61 
       
    62         aSuite.addTest(new HyperLinkTest("testCommands",
       
    63                 new TestMethod() {
       
    64                     public void run(TestCase tc) {
       
    65                         ((HyperLinkTest) tc).testCommands();
       
    66                     }
       
    67                 }));
       
    68 
       
    69         aSuite.addTest(new HyperLinkTest("testAppearanceModeChangePlain",
       
    70                 new TestMethod() {
       
    71                     public void run(TestCase tc) {
       
    72                         ((HyperLinkTest) tc).
       
    73                         testAppearanceModeChange(StringItem.PLAIN);
       
    74                     }
       
    75                 }));
       
    76 
       
    77         aSuite.addTest(new HyperLinkTest("testAppearanceModeChangeButton",
       
    78                 new TestMethod() {
       
    79                     public void run(TestCase tc) {
       
    80                         ((HyperLinkTest) tc).
       
    81                         testAppearanceModeChange(StringItem.BUTTON);
       
    82                     }
       
    83                 }));
       
    84 
       
    85         aSuite.addTest(new HyperLinkTest("testAppearanceModeChangeHyperlink",
       
    86                 new TestMethod() {
       
    87                     public void run(TestCase tc) {
       
    88                         ((HyperLinkTest) tc).
       
    89                         testAppearanceModeChange(StringItem.HYPERLINK);
       
    90                     }
       
    91                 }));
       
    92 
       
    93         aSuite.addTest(new HyperLinkTest("testFocusInAppearanceModeChangePlain",
       
    94                 new TestMethod() {
       
    95                     public void run(TestCase tc) {
       
    96                         ((HyperLinkTest) tc).
       
    97                         testFocusInAppearanceModeChange(StringItem.PLAIN);
       
    98                     }
       
    99                 }));
       
   100 
       
   101         aSuite.addTest(new HyperLinkTest(
       
   102                 "testFocusInAppearanceModeChangeButton",
       
   103                 new TestMethod() {
       
   104                     public void run(TestCase tc) {
       
   105                         ((HyperLinkTest) tc).
       
   106                         testFocusInAppearanceModeChange(StringItem.BUTTON);
       
   107                     }
       
   108                 }));
       
   109 
       
   110         aSuite.addTest(new HyperLinkTest(
       
   111                 "testFocusInAppearanceModeChangeHyperlink",
       
   112                 new TestMethod() {
       
   113                     public void run(TestCase tc) {
       
   114                         ((HyperLinkTest) tc).
       
   115                         testFocusInAppearanceModeChange(StringItem.HYPERLINK);
       
   116                     }
       
   117                 }));
       
   118 
       
   119         aSuite.addTest(new HyperLinkTest("testLongHyperlinkWithLSK",
       
   120                 new TestMethod() {
       
   121                     public void run(TestCase tc) {
       
   122                         ((HyperLinkTest) tc).testLongHyperlink(Key.CBA1);
       
   123                     }
       
   124                 }));
       
   125 
       
   126         aSuite.addTest(new HyperLinkTest("testLongHyperlinkWithMSK",
       
   127                 new TestMethod() {
       
   128                     public void run(TestCase tc) {
       
   129                         ((HyperLinkTest) tc).testLongHyperlink(Key.Select);
       
   130                     }
       
   131                 }));
       
   132 
       
   133         return aSuite;
       
   134     }
       
   135 
       
   136     /**
       
   137 	 * Tests the basic functionality of command added to Hyperlink.
       
   138 	 */
       
   139 	public void testCommands() {
       
   140 		StringItem hyperlink = new StringItem("label", "url",
       
   141 				StringItem.HYPERLINK);
       
   142 
       
   143 		testCommand(hyperlink);
       
   144 
       
   145 		// Because focus transfer in case of hyperlink is different than
       
   146 		// in normal cases it is good to test that commands works when
       
   147 		// moving focus from previous item to hyperlink as well as when
       
   148 		// moving focus from next item:
       
   149 		form.deleteAll();
       
   150 		StringItem button1 = new StringItem(null, "button1", StringItem.BUTTON);
       
   151 		StringItem button2 = new StringItem(null, "button2", StringItem.BUTTON);
       
   152 		Command dummyCommand = new Command("dummy", "", Command.ITEM, 0);
       
   153 		button1.addCommand(dummyCommand);
       
   154 		button2.addCommand(dummyCommand);
       
   155 		button1.setItemCommandListener(this);
       
   156 		button2.setItemCommandListener(this);
       
   157 
       
   158 		// Some typical url-characters are replaced with 'x'-chars to avoid
       
   159 		// some problems in eSWT/AVKON implementation. Those problems should
       
   160 		// go away when using QT-based eSWT.
       
   161 
       
   162 		Command ok = new Command("Ok", "", Command.ITEM, 0);
       
   163 
       
   164 		StringItem hyperlink3 = new StringItem(null,
       
   165 				"xxxx://this.link.should.be.wrapped.to.many"
       
   166 						+ ".lines.info/index.php?wrapped=truexi=hope",
       
   167 				StringItem.HYPERLINK);
       
   168 		hyperlink3.addCommand(ok);
       
   169 		hyperlink3.setItemCommandListener(this);
       
   170 
       
   171 		form.append(button1);
       
   172 		form.append(hyperlink3);
       
   173 		form.append(button2);
       
   174 
       
   175 		block(CHANGE_DISPLAYABLE_DELAY);
       
   176 
       
   177 		key(Key.RightArrow);
       
   178 		key(Key.CBA1);
       
   179 		assertItemCmdListener("hyperlink focus transfer test, case 1", ok,
       
   180 				hyperlink3);
       
   181 
       
   182 		key(Key.RightArrow);
       
   183 		key(Key.LeftArrow);
       
   184 		key(Key.CBA1);
       
   185 		assertItemCmdListener("hyperlink focus transfer test, case 2", ok,
       
   186 				hyperlink3);
       
   187 	}
       
   188 
       
   189 	/**
       
   190 	 * Tests that commands are working as expected when commands
       
   191 	 * are added or removed dynamically.
       
   192 	 *
       
   193 	 * @param mode Appearancemode of StringItem.
       
   194 	 */
       
   195 	public void testAppearanceModeChange(int mode) {
       
   196 		switch (mode) {
       
   197 		case StringItem.PLAIN:
       
   198 			print("Running test with appearance mode PLAIN.");
       
   199 			break;
       
   200 		case StringItem.BUTTON:
       
   201 			print("Running test with appearance mode BUTTON.");
       
   202 			break;
       
   203 		case StringItem.HYPERLINK:
       
   204 			print("Running test with appearance mode HYPERLINK.");
       
   205 			break;
       
   206 		default:
       
   207 			fail("Invalid test (incorrect appearance mode).");
       
   208 			break;
       
   209 		}
       
   210 
       
   211 		StringItem stringItem = new StringItem("label", "url", mode);
       
   212 		Command ok = new Command("Ok", "", Command.ITEM, 0);
       
   213 
       
   214 		form.append(stringItem);
       
   215 		stringItem.addCommand(ok);
       
   216 		stringItem.setItemCommandListener(this);
       
   217 
       
   218 		setCurrent(form);
       
   219 
       
   220 		// Remove command and make sure the command is not working anymore:
       
   221 		stringItem.removeCommand(ok);
       
   222 		block(CHANGE_DISPLAYABLE_DELAY);
       
   223 
       
   224 		key(Key.CBA1);
       
   225 		assertItemCmdListener("cmd activated after removing", null, null);
       
   226 
       
   227 		// Add command again and verify it works:
       
   228 		stringItem.addCommand(ok);
       
   229 		block(CHANGE_DISPLAYABLE_DELAY);
       
   230 
       
   231 		key(Key.CBA1);
       
   232 		assertItemCmdListener("", ok, stringItem);
       
   233 	}
       
   234 
       
   235 	/**
       
   236 	 * Tests that focus transfer works when commands are added/removed
       
   237 	 * dynamically.
       
   238 	 *
       
   239 	 * @param mode Appearancemode of StringItem.
       
   240 	 */
       
   241 	public void testFocusInAppearanceModeChange(int mode) {
       
   242 		switch (mode) {
       
   243 		case StringItem.PLAIN:
       
   244 			print("Running test with appearance mode PLAIN.");
       
   245 			break;
       
   246 		case StringItem.BUTTON:
       
   247 			print("Running test with appearance mode BUTTON.");
       
   248 			break;
       
   249 		case StringItem.HYPERLINK:
       
   250 			print("Running test with appearance mode HYPERLINK.");
       
   251 			break;
       
   252 		default:
       
   253 			fail("Invalid test (incorrect appearance mode).");
       
   254 			break;
       
   255 		}
       
   256 
       
   257 		StringItem stringItem = new StringItem(null, "url", mode);
       
   258 
       
   259 		StringItem button1 = new StringItem(null, "button1", Item.BUTTON);
       
   260 		StringItem button2 = new StringItem(null, "button2", Item.BUTTON);
       
   261 
       
   262 		Command ok = new Command("giamcc1", "", Command.ITEM, 0);
       
   263 		Command buttonCommand1 = new Command("giamcc12", "", Command.ITEM, 0);
       
   264 		Command buttonCommand2 = new Command("giamcc13", "", Command.ITEM, 0);
       
   265 
       
   266 		form.append(button1);
       
   267 		form.append(stringItem);
       
   268 		form.append(button2);
       
   269 
       
   270 		// Set listener but do not add commands yet:
       
   271 		stringItem.setItemCommandListener(this);
       
   272 		button1.addCommand(buttonCommand1);
       
   273 		button2.addCommand(buttonCommand2);
       
   274 		button1.setItemCommandListener(this);
       
   275 		button2.setItemCommandListener(this);
       
   276 
       
   277 		setCurrent(form);
       
   278 
       
   279 		// At this point there are two buttons and plain stringItem
       
   280 		// in middle of them. Focus is in first button.
       
   281 		// Move to second button, add command to StringItem, move
       
   282 		// to StringItem and verify command works:
       
   283 		key(Key.RightArrow);
       
   284 
       
   285 		stringItem.addCommand(ok);
       
   286 		block(CHANGE_DISPLAYABLE_DELAY);
       
   287 
       
   288 		key(Key.LeftArrow);
       
   289 
       
   290 		assertItemCmdListener("case1", ok, stringItem);
       
   291 
       
   292 		// Now remove command from StringItem. Test assumes that in this
       
   293 		// case focus is moved to next item which is the second button.
       
   294 		// Verify that happens and also make sure it is possible to move
       
   295 		// to first button also:
       
   296 		stringItem.removeCommand(ok);
       
   297 		block(CHANGE_DISPLAYABLE_DELAY);
       
   298 
       
   299 		key(Key.CBA1);
       
   300 		assertItemCmdListener("case2", buttonCommand2, button2);
       
   301 
       
   302 		key(Key.LeftArrow);
       
   303 
       
   304 		key(Key.CBA1);
       
   305 		assertItemCmdListener("case3", buttonCommand1, button1);
       
   306 
       
   307 		// Now add command back to StringItem again and make sure it
       
   308 		// works:
       
   309 		stringItem.addCommand(ok);
       
   310 		block(CHANGE_DISPLAYABLE_DELAY);
       
   311 
       
   312 		key(Key.RightArrow);
       
   313 
       
   314 		key(Key.CBA1);
       
   315 		assertItemCmdListener("case4", ok, stringItem);
       
   316 
       
   317 		// And verify also the functionality of second button after
       
   318 		// these changes:
       
   319 		key(Key.RightArrow);
       
   320 
       
   321 		key(Key.CBA1);
       
   322 		assertItemCmdListener("case5", buttonCommand2, button2);
       
   323 	}
       
   324 
       
   325 	/**
       
   326 	 * Tests that Hyperlink works as expected when its size exceeds
       
   327 	 * screen height.
       
   328 	 *
       
   329 	 * @param key Keycode which is used to verify the hyperlink command
       
   330 	 *      works.
       
   331 	 */
       
   332 	public void testLongHyperlink(int key) {
       
   333 		StringItem stringItem = new StringItem(
       
   334 				"label",
       
   335 				"The quick brown fox jumps over the lazy dog and realizes"
       
   336 						+ " that this hyperlink text is repeated several times to make"
       
   337 						+ " sure the size of the hyperlink exceeds screen height. "
       
   338 						+ "The quick brown fox jumps over the lazy dog and realizes"
       
   339 						+ " that this hyperlink text is repeated several times to make"
       
   340 						+ " sure the size of the hyperlink exceeds screen height. "
       
   341 						+ "The quick brown fox jumps over the lazy dog and realizes"
       
   342 						+ " that this hyperlink text is repeated several times to make"
       
   343 						+ " sure the size of the hyperlink exceeds screen height. ",
       
   344 				StringItem.HYPERLINK);
       
   345 
       
   346 		Command ok = new Command("lhlc1", "", Command.ITEM, 0);
       
   347 
       
   348 		StringItem button1 = new StringItem(null, "button1", Item.BUTTON);
       
   349 		Command buttonCommand1 = new Command("lhlc2", "", Command.ITEM, 0);
       
   350 
       
   351 		form.append(stringItem);
       
   352 		stringItem.addCommand(ok);
       
   353 		stringItem.setItemCommandListener(this);
       
   354 
       
   355 		form.append(button1);
       
   356 		button1.addCommand(buttonCommand1);
       
   357 		button1.setItemCommandListener(this);
       
   358 
       
   359 		setCurrent(form);
       
   360 
       
   361 		//TODO: When running all tests and the key is LSK and in following loop
       
   362 		//the i is 0, latestCommand is buttonCommand1 and latestItem is null.
       
   363 		//If button is not added to the form then the latestCommand also
       
   364 		//will be null.
       
   365 		//Problem has something to do with timing, because
       
   366 		//adding debugprints to this test will sometimes make it pass.
       
   367 		//For example following delay will make the test pass:
       
   368 		block(CHANGE_DISPLAYABLE_DELAY);
       
   369 
       
   370 		// Scroll down and after each keydown-press make sure the hyperlink
       
   371 		// is still focused:
       
   372 		final int rounds = 10;
       
   373 		final int roundsInHyperlink = 3;
       
   374 		for (int i = 0; i < rounds; i++) {
       
   375 			key(key);
       
   376 
       
   377 			if (i < roundsInHyperlink) {
       
   378 				// Code assumes that it takes at least three
       
   379 				// keypresses to reach button which is located below hyperlink:
       
   380 				assertItemCmdListener("clicking hyperlink. i=" + i, ok,
       
   381 						stringItem);
       
   382 			} else if (i >= (rounds - 1)) {
       
   383 				// After 10 keypresses button should be focused:
       
   384 				assertItemCmdListener("clicking button. i=" + i,
       
   385 						buttonCommand1, button1);
       
   386 			}
       
   387 
       
   388 			key(Key.RightArrow);
       
   389 			block(CHANGE_DISPLAYABLE_DELAY);
       
   390 		}
       
   391 	}
       
   392 
       
   393 }