1 /*******************************************************************************
2  * Copyright (c) 2000, 2016 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *     Red Hat Inc. - Bug 462631
11  * Port to the D Programming Language:
12  *     alice <stigma@disroot.org>
13  *******************************************************************************/
14 module tests.Platform;
15 
16 import java.lang.all;
17 import java.util.HashSet;
18 
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.program.Program;
21 import org.eclipse.swt.widgets.Display;
22 
23 @("test_equalsLjava_lang_Object")
24 unittest
25 {
26     Display.getDefault();
27     String[] extensions = Program.getExtensions();
28     // No assertion here because the doc does not guarantee a non-null result
29     if (extensions !is null) {
30         for (int i = 0; i < extensions.length; i++) {
31             Program program = Program.findProgram(extensions[i]);
32             if (program !is null) {
33                 assert(program == program);
34             }
35         }
36     }
37 }
38 
39 @("test_executeLjava_lang_String")
40 unittest
41 {
42     import std.exception : assertThrown;
43 
44     // This test is incomplete because a true test of execute would open
45     // an application that cannot be programmatically closed.
46 
47     Display.getDefault();
48 
49     Program[] programs = Program.getPrograms();
50     if (programs !is null && programs.length > 0) {
51         // Cannot test empty string argument because it may launch something
52         // bool result = programs[0].execute("");
53         // assert(result is false);
54 
55         assertThrown!IllegalArgumentException(programs[0].execute(null));
56     }
57 }
58 
59 @("test_findProgramLjava_lang_String")
60 unittest
61 {
62     import std.exception : assertThrown;
63 
64     Display.getDefault();
65     String[] extensions = Program.getExtensions();
66     // No assertion here because the doc does not guarantee a non-null result.
67     if (extensions !is null) {
68         for (int i = 0; i < extensions.length; i++) {
69             Program.findProgram(extensions[i]);
70             // No assertion here because a null result is allowed
71         }
72     }
73 
74     // SWT Extension: allows null for Program.findProgram()
75     // assertThrown!IllegalArgumentException(Program.findProgram(null));
76 }
77 
78 @("test_getExtensions")
79 unittest
80 {
81     Display.getDefault();
82     String[] extensions = Program.getExtensions();
83     // No assertion here because the doc does not guarantee a non-null result
84     if (extensions !is null) {
85         for (int i = 0; i < extensions.length; i++) {
86             assert(extensions[i] !is null);
87         }
88     }
89 }
90 
91 @("test_getImageData")
92 unittest
93 {
94     Display.getDefault();
95     String[] extensions = Program.getExtensions();
96     // No assertion here because the doc does not guarantee a non-null result.
97     if (extensions !is null) {
98         for (int i = 0; i < extensions.length; i++) {
99             Program program = Program.findProgram(extensions[i]);
100             if (program !is null) {
101                 program.getImageData();
102                 // Nothing to do
103             }
104         }
105     }
106 }
107 
108 @("test_getName")
109 unittest
110 {
111     Display.getDefault();
112     String[] extensions = Program.getExtensions();
113     // No assertion here because the doc does not guarantee a non-null result
114     if (extensions !is null) {
115         for (int i = 0; i < extensions.length; i++) {
116             Program program = Program.findProgram(extensions[i]);
117             if (program !is null) {
118                 String name = program.getName();
119                 assert(name !is null, "Program has null name");
120             }
121         }
122     }
123 }
124 
125 @("test_getPrograms")
126 unittest
127 {
128     Display.getDefault();
129     Program[] programs = Program.getPrograms();
130 
131     // The result is not well-documented, but it should
132     // be non-null and contain no null and no duplicated entries.
133 
134     assert(programs !is null);
135 
136     HashSet lookup = new HashSet();
137     foreach (program; programs) {
138         // test non-null entry
139         assert(program !is null);
140 
141         // test if the list contains same objects multiple times
142         if (lookup.contains(program)) {
143             assert(false, "Duplicated list entry for " ~ program.toString());
144         }
145         else {
146             lookup.add(program);
147         }
148     }
149 }
150 
151 @("test_launchLjava_lang_String")
152 unittest
153 {
154     import std.exception : assertThrown;
155 
156     // This test is incomplete because a true test of launch would open
157     // an application that cannot be programmatically closed.
158 
159     // Cannot test empty string argument because it may launch something.
160 
161     // test null argument
162 
163     Display.getDefault();
164     assertThrown!IllegalArgumentException(Program.launch(null));
165 }
166 
167 @("test_toString")
168 unittest
169 {
170     Display.getDefault();
171     String[] extensions = Program.getExtensions();
172     // No assertion here because the doc does not guarantee a non-null result
173     if (extensions !is null) {
174         for (int i = 0; i < extensions.length; i++) {
175             Program program = Program.findProgram(extensions[i]);
176             if (program !is null) {
177                 String string_ = program.toString();
178                 assert(string_ !is null, "toString returned null");
179             }
180         }
181     }
182 }