001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package javax.microedition.lcdui;
028:
029: import com.sun.midp.chameleon.*;
030: import com.sun.midp.chameleon.layers.*;
031: import com.sun.midp.i3test.TestCase;
032: import com.sun.midp.util.LcduiTestCanvas;
033: import com.sun.midp.util.LcduiTestMIDlet;
034:
035: /**
036: * This test case does extensive testing on command sorting in chameleon.
037: * It validates the rules specified in the spec for sorting commands
038: * in a particular order along with taking their priorities into
039: * consideration. For example, it tests the decision on what goes into the
040: * left soft button and what goes into the right soft button as the system
041: * menu list of commands.
042: */
043: public class TestCommandSorting extends TestCase {
044:
045: /** Name of the test */
046: static final String testName = "CommandSorting";
047:
048: /** The Display to use to view our test canvases */
049: protected Display display;
050:
051: /** Reference to MIDPWindow, to help us gain access into
052: * SoftButtonLayer's attributes used in this test.
053: */
054: protected MIDPWindow window;
055:
056: /**
057: * Simple constructor.
058: */
059: public TestCommandSorting() {
060: }
061:
062: /**
063: * Utility method that checks that the left soft button is indeed the
064: * intended one as passed in via parameter cmd, after making the canvas
065: * current.
066: *
067: * @param canvas the canvas being used to test the commands
068: * @param cmd the command under test
069: */
070: protected void checkLeftBtn(LcduiTestCanvas canvas, Command cmd) {
071: display.setCurrent(canvas);
072: if (!canvas.awaitPaint()) {
073: fail("test canvas not visible");
074: return;
075: }
076:
077: assertSame("Checking left button:", cmd, window.getSoftOne());
078: }
079:
080: /**
081: * This test is for a canvas with just the STOP command added. The STOP
082: * command should be the left soft button. (only negative commands are
083: * being tested)
084: */
085: protected void testOne() {
086: declare(testName + " 1: STOP command added to the screen.");
087:
088: LcduiTestCanvas canvas = new LcduiTestCanvas();
089:
090: Command stopCmd = new Command("Stop", Command.STOP, 0);
091: canvas.addCommand(stopCmd);
092:
093: checkLeftBtn(canvas, stopCmd);
094: }
095:
096: /**
097: * This test is for a canvas with the CANCEL & STOP commands added. The
098: * CANCEL command should be the left soft button. (only negative
099: * commands are being tested)
100: */
101: protected void testTwo() {
102: declare(testName
103: + " 2: CANCEL & STOP commands added to the screen.");
104:
105: LcduiTestCanvas canvas = new LcduiTestCanvas();
106:
107: Command cancelCmd = new Command("Cancel", Command.CANCEL, 0);
108: Command stopCmd = new Command("Stop", Command.STOP, 0);
109:
110: // we add the commands in reverse order so that we are
111: // sure the button layer is sorting it correctly
112: canvas.addCommand(stopCmd);
113: canvas.addCommand(cancelCmd);
114:
115: checkLeftBtn(canvas, cancelCmd);
116: }
117:
118: /**
119: * This test is for a canvas with the EXIT, CANCEL & STOP commands added.
120: * The EXIT command should be the left soft button. (only negative
121: * commands are being tested)
122: */
123: protected void testThree() {
124: declare(testName
125: + " 3: EXIT, CANCEL & STOP commands added to the screen.");
126:
127: LcduiTestCanvas canvas = new LcduiTestCanvas();
128:
129: Command exitCmd = new Command("Exit", Command.EXIT, 0);
130: Command cancelCmd = new Command("Cancel", Command.CANCEL, 0);
131: Command stopCmd = new Command("Stop", Command.STOP, 0);
132:
133: // we add the commands in reverse order so that we are
134: // sure the button layer is sorting it correctly
135: canvas.addCommand(stopCmd);
136: canvas.addCommand(cancelCmd);
137: canvas.addCommand(exitCmd);
138:
139: checkLeftBtn(canvas, exitCmd);
140: }
141:
142: /**
143: * This test is for a canvas with the BACK, EXIT, CANCEL & STOP
144: * commands added. The BACK command should be the left soft button.
145: * (only negative commands are being tested)
146: */
147: protected void testFour() {
148: declare(testName
149: + " 4: BACK, EXIT, CANCEL & STOP commands added to the screen.");
150:
151: LcduiTestCanvas canvas = new LcduiTestCanvas();
152:
153: Command backCmd = new Command("Back", Command.BACK, 0);
154: Command exitCmd = new Command("Exit", Command.EXIT, 0);
155: Command cancelCmd = new Command("Cancel", Command.CANCEL, 0);
156: Command stopCmd = new Command("Stop", Command.STOP, 0);
157:
158: // we add the commands in reverse order so that we are
159: // sure the button layer is sorting it correctly
160: canvas.addCommand(stopCmd);
161: canvas.addCommand(cancelCmd);
162: canvas.addCommand(exitCmd);
163: canvas.addCommand(backCmd);
164: checkLeftBtn(canvas, backCmd);
165: }
166:
167: /**
168: * This test is for a canvas with the BACK, STOP, CANCEL, EXIT, HELP,
169: * OK, SCREEN & ITEM commands added. The BACK command should be the
170: * left soft button. And all the other commands should be sorted
171: * correctly as defined in the spec on the right soft button as the
172: * system menu. (combination of positive & negative commands are
173: * being tested)
174: */
175: protected void testFive() {
176: declare(testName
177: + " 5: BACK, STOP, CANCEL, EXIT, HELP, OK, SCREEN & ITEM commands added to the screen.");
178:
179: LcduiTestCanvas canvas = new LcduiTestCanvas();
180:
181: // we add the commands in reverse order so that we are
182: // sure the button layer is sorting it correctly
183: for (int i = 0; i < 2; i++) {
184: canvas.addCommand(new Command("STOP" + i, Command.STOP, i));
185: canvas.addCommand(new Command("CANCEL" + i, Command.CANCEL,
186: i));
187: canvas.addCommand(new Command("EXIT" + i, Command.EXIT, i));
188: canvas.addCommand(new Command("HELP" + i, Command.HELP, i));
189: canvas.addCommand(new Command("OK" + i, Command.OK, i));
190: canvas.addCommand(new Command("SCREEN" + i, Command.SCREEN,
191: i));
192: canvas.addCommand(new Command("ITEM" + i, Command.ITEM, i));
193: }
194:
195: Command backCmd = new Command("Back", Command.BACK, 0);
196: canvas.addCommand(backCmd);
197:
198: checkLeftBtn(canvas, backCmd);
199: Command[] cmds = window.getSoftTwo();
200:
201: assertTrue("ITEM", cmds[0].getCommandType() == Command.ITEM);
202: assertTrue("ITEM", cmds[1].getCommandType() == Command.ITEM);
203: assertTrue("ITEM PRIORITY", cmds[0].getPriority() < cmds[1]
204: .getPriority());
205:
206: assertTrue("SCREEN", cmds[2].getCommandType() == Command.SCREEN);
207: assertTrue("SCREEN", cmds[3].getCommandType() == Command.SCREEN);
208: assertTrue("SCREEN PRIORITY", cmds[2].getPriority() < cmds[3]
209: .getPriority());
210:
211: assertTrue("OK", cmds[4].getCommandType() == Command.OK);
212: assertTrue("OK", cmds[5].getCommandType() == Command.OK);
213: assertTrue("OK PRIORITY", cmds[4].getPriority() < cmds[5]
214: .getPriority());
215:
216: assertTrue("EXIT, ", cmds[6].getCommandType() == Command.EXIT);
217: assertTrue("EXIT, ", cmds[7].getCommandType() == Command.EXIT);
218: assertTrue("EXIT PRIORITY, ", cmds[6].getPriority() < cmds[7]
219: .getPriority());
220:
221: assertTrue("CANCEL, ",
222: cmds[8].getCommandType() == Command.CANCEL);
223: assertTrue("CANCEL, ",
224: cmds[9].getCommandType() == Command.CANCEL);
225: assertTrue("CANCEL PRIORITY, ", cmds[8].getPriority() < cmds[9]
226: .getPriority());
227:
228: assertTrue("STOP, ", cmds[10].getCommandType() == Command.STOP);
229: assertTrue("STOP, ", cmds[11].getCommandType() == Command.STOP);
230: assertTrue("STOP PRIORITY, ", cmds[10].getPriority() < cmds[11]
231: .getPriority());
232:
233: assertTrue("HELP, ", cmds[12].getCommandType() == Command.HELP);
234: assertTrue("HELP, ", cmds[13].getCommandType() == Command.HELP);
235: assertTrue("HELP PRIORITY, ", cmds[12].getPriority() < cmds[13]
236: .getPriority());
237: }
238:
239: /**
240: * This test is for a canvas with the HELP, OK, SCREEN & ITEM
241: * commands added, without any negative commands and check to see
242: * if the left soft button is mapped to the highest positive
243: * command as expected. All the remaining positive
244: * commands should be sorted correctly as defined in the spec on
245: * the right soft button as the system menu. (only positive commands are
246: * being tested)
247: */
248: protected void testSix() {
249: declare(testName
250: + " 6: HELP, OK, SCREEN & ITEM commands added to the screen.");
251:
252: LcduiTestCanvas canvas = new LcduiTestCanvas();
253:
254: Command helpCmd = new Command("HELP", Command.HELP, 0);
255: Command okCmd = new Command("OK", Command.OK, 1);
256: Command screenCmd = new Command("SCREEN", Command.SCREEN, 2);
257: Command itemCmd = new Command("ITEM", Command.ITEM, 3);
258:
259: canvas.addCommand(helpCmd);
260: canvas.addCommand(okCmd);
261: canvas.addCommand(screenCmd);
262: canvas.addCommand(itemCmd);
263:
264: checkLeftBtn(canvas, itemCmd); // ITEM command has highest priority
265: }
266:
267: /**
268: * Overridden from TestCase parent. This method will kick off each
269: * individual test
270: */
271: public void runTests() throws Throwable {
272:
273: if (!LcduiTestMIDlet.invoke()) {
274: throw new RuntimeException("can't start LcduiTestMIDlet");
275: }
276:
277: try {
278: display = LcduiTestMIDlet.getDisplay();
279: window = display.getWindow();
280:
281: testOne();
282: testTwo();
283: testThree();
284: testFour();
285: testFive();
286: testSix();
287: } finally {
288: LcduiTestMIDlet.cleanup();
289: }
290: }
291: }
|