|
1 /* |
|
2 * Copyright (c) 2009,2010 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.softnotification; |
|
18 |
|
19 import java.io.ByteArrayOutputStream; |
|
20 import java.io.IOException; |
|
21 import java.io.InputStream; |
|
22 |
|
23 import junit.framework.Test; |
|
24 import junit.framework.TestSuite; |
|
25 import com.nokia.openlcdui.mt.SWTTestCase; |
|
26 |
|
27 import com.nokia.mid.ui.SoftNotification; |
|
28 import com.nokia.mid.ui.SoftNotificationException; |
|
29 |
|
30 public class SoftNotificationTest extends SWTTestCase { |
|
31 /** |
|
32 * Constructor. |
|
33 */ |
|
34 |
|
35 /** |
|
36 * @param testName |
|
37 * Test name. |
|
38 * @param testMethod |
|
39 * Test method. |
|
40 */ |
|
41 public SoftNotificationTest(String testName) { |
|
42 super(testName); |
|
43 } |
|
44 |
|
45 public static Test suite() { |
|
46 TestSuite suite = new TestSuite(); |
|
47 |
|
48 java.util.Vector methodNames; |
|
49 java.util.Enumeration e; |
|
50 |
|
51 // Add tests |
|
52 methodNames = SoftNotificationTest.methodNames(); |
|
53 e = methodNames.elements(); |
|
54 while (e.hasMoreElements()) { |
|
55 suite.addTest(new SoftNotificationTest((String) e.nextElement())); |
|
56 } |
|
57 |
|
58 return suite; |
|
59 } |
|
60 |
|
61 public static java.util.Vector methodNames() { |
|
62 java.util.Vector methodNames = new java.util.Vector(); |
|
63 methodNames.addElement("test_newInstance"); |
|
64 methodNames.addElement("test_getId"); |
|
65 methodNames.addElement("test_setImage"); |
|
66 methodNames.addElement("test_setText"); |
|
67 methodNames.addElement("test_post"); |
|
68 methodNames.addElement("test_remove"); |
|
69 return methodNames; |
|
70 } |
|
71 |
|
72 protected void runTest() throws Throwable { |
|
73 if (getName().equals("test_newInstance")) |
|
74 test_newInstance(); |
|
75 else if (getName().equals("test_setText")) |
|
76 test_getId(); |
|
77 else if (getName().equals("test_setImage")) |
|
78 test_setImage(); |
|
79 else if (getName().equals("test_setText")) |
|
80 test_setText(); |
|
81 else if (getName().equals("test_post")) |
|
82 test_post(); |
|
83 else if (getName().equals("test_remove")) |
|
84 test_remove(); |
|
85 else |
|
86 super.runTest(); |
|
87 } |
|
88 |
|
89 SoftNotification sn1; |
|
90 SoftNotification sn2; |
|
91 |
|
92 private byte[] getImage(String aImageName) throws IOException { |
|
93 byte imageData[] = null; |
|
94 if (aImageName != null) { |
|
95 InputStream is = this.getClass().getResourceAsStream( |
|
96 "/" + aImageName); |
|
97 ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
98 try { |
|
99 byte[] buf = new byte[1024]; |
|
100 for (int readNum; (readNum = is.read(buf)) != -1;) { |
|
101 out.write(buf, 0, readNum); |
|
102 } |
|
103 |
|
104 } catch (Throwable e) { |
|
105 e.printStackTrace(); |
|
106 } |
|
107 imageData = out.toByteArray(); |
|
108 is.close(); |
|
109 } |
|
110 return imageData; |
|
111 } |
|
112 |
|
113 public void test_newInstance() { |
|
114 try { |
|
115 sn1 = SoftNotification.newInstance(); |
|
116 } catch (Exception e) { |
|
117 fail("Exception - in newInstance - " + e.getMessage()); |
|
118 } |
|
119 |
|
120 try { |
|
121 sn2 = SoftNotification.newInstance(101); |
|
122 } catch (Exception e) { |
|
123 fail("Exception - in newInstance - " + e.getMessage()); |
|
124 } |
|
125 } |
|
126 |
|
127 public void test_getId() { |
|
128 sn2 = SoftNotification.newInstance(101); |
|
129 if (sn2.getId() != 101) { |
|
130 fail(" SoftNotification getId() is not correct"); |
|
131 } |
|
132 } |
|
133 |
|
134 public void test_setImage() { |
|
135 sn2 = SoftNotification.newInstance(101); |
|
136 |
|
137 try { |
|
138 sn2.setImage(null); |
|
139 sn2.setImage(getImage("200x200.png")); |
|
140 sn2.setImage(null); |
|
141 |
|
142 } catch (IOException e) { |
|
143 fail(" Exception in setImage() " + e.getMessage()); |
|
144 } catch (SoftNotificationException e) { |
|
145 fail(" Exception in setImage() " + e.getMessage()); |
|
146 } |
|
147 } |
|
148 |
|
149 public void test_setText() { |
|
150 sn2 = SoftNotification.newInstance(101); |
|
151 try { |
|
152 sn2.setText(null, null); |
|
153 sn2.setText("Primary Text", "Secondary Text"); |
|
154 sn2.setText(null, "Changed Secondary Text"); |
|
155 sn2.setText("Changed Primary Text", null); |
|
156 } catch (SoftNotificationException e) { |
|
157 fail(" Exception in setText() " + e.getMessage()); |
|
158 } |
|
159 } |
|
160 |
|
161 public void test_post() { |
|
162 sn2 = SoftNotification.newInstance(101); |
|
163 try { |
|
164 sn2.post(); |
|
165 } catch (SoftNotificationException e) { |
|
166 fail(" Exception in post() " + e.getMessage()); |
|
167 } |
|
168 try { |
|
169 sn2.remove(); |
|
170 sn2.post(); |
|
171 fail("Expected exception not thrown post"); |
|
172 } catch (SoftNotificationException e) { |
|
173 assertTrue("Expected Exception", true); |
|
174 } |
|
175 } |
|
176 |
|
177 public void test_remove() { |
|
178 sn2 = SoftNotification.newInstance(101); |
|
179 try { |
|
180 sn2.remove(); |
|
181 } catch (SoftNotificationException e) { |
|
182 fail(" Exception in remove() " + e.getMessage()); |
|
183 } |
|
184 try { |
|
185 sn2.remove(); |
|
186 fail("Expected exception not thrown remove"); |
|
187 } catch (SoftNotificationException e) { |
|
188 assertTrue("Expected Exception", true); |
|
189 } |
|
190 } |
|
191 } |