Repository navigation
Expand file tree
/
Copy pathJavaScriptBuildHintsTest.java
More file actions
258 lines (228 loc) · 12.2 KB
/
Copy pathJavaScriptBuildHintsTest.java
File metadata and controls
258 lines (228 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.builders;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/// Where the JavaScript port's build hints go: the launcher, the translator, and the choice of
/// native themes the bundle carries.
class JavaScriptBuildHintsTest {
private static BuildRequest request(String... keyValues) {
BuildRequest r = new BuildRequest();
for (int i = 0; i < keyValues.length; i += 2) {
r.putArgument(keyValues[i], keyValues[i + 1]);
}
return r;
}
private static Set<String> ship(BuildRequest r) {
return JavaScriptBuildHints.themesToShip(r, Collections.<String>emptySet());
}
private static Set<String> set(String... names) {
return new HashSet<String>(Arrays.asList(names));
}
@Test
void aBundleMissingAReachableThemeIsNotPruned() {
// With nativeTheme=native a Mac is sent to the Aqua theme. A bundle without it must keep
// the legacy pair the runtime falls back to when Aqua will not open -- which pruning to
// "what the app can reach" would delete -- so nothing is pruned at all.
Set<String> keep = ship(request("nativeTheme", "native"));
assertTrue(keep.contains("MacOSAquaTheme"), "the hint reaches the macOS theme");
Set<String> present = new LinkedHashSet<String>(JavaScriptBuildHints.ALL_THEMES);
present.remove("MacOSAquaTheme");
assertTrue(JavaScriptBuildHints.themesToDelete(keep, present).isEmpty(), "a partial bundle is shipped whole");
}
@Test
void aCompleteBundleLosesExactlyTheUnreachableThemes() {
Set<String> keep = ship(request("nativeTheme", "native"));
Set<String> present = new LinkedHashSet<String>(JavaScriptBuildHints.ALL_THEMES);
Set<String> expected = new LinkedHashSet<String>(present);
expected.removeAll(keep);
assertTrue(!expected.isEmpty(), "some theme has to be unreachable for this to mean anything");
assertEquals(expected, new LinkedHashSet<String>(JavaScriptBuildHints.themesToDelete(keep, present)), "deleted");
}
@Test
void launcherPublishesOnlyTheHintsThatAreSet() {
String src = JavaScriptBuildHints.launcherProperties(request(
"nativeTheme", "native", "javascript.titleBar", "html", "unrelated.hint", "x"));
assertTrue(src.contains("Display.getInstance().setProperty(\"nativeTheme\", \"native\");"), src);
assertTrue(src.contains("Display.getInstance().setProperty(\"javascript.titleBar\", \"html\");"), src);
assertFalse(src.contains("unrelated.hint"), src);
assertFalse(src.contains("javascript.textSelection"), src);
}
@Test
void launcherValuesAreJavaStringLiterals() {
String src = JavaScriptBuildHints.launcherProperties(request("javascript.native.theme", "a\"b\\c\n"));
assertTrue(src.contains("\"a\\\"b\\\\c\\n\""), src);
}
@Test
void translatorGetsTheTitleAndOnlyTheOptOutsThatWereSet() {
BuildRequest r = request();
r.setDisplayName("My App");
assertEquals(Collections.singletonList("-Dcodename1.javascript.title=My App"),
JavaScriptBuildHints.translatorOptions(r));
r = request("javascript.allowBrowserTranslation", "TRUE", "javascript.darkreaderLock", "false");
List<String> opts = JavaScriptBuildHints.translatorOptions(r);
assertTrue(opts.contains("-Dcodename1.javascript.allowBrowserTranslation=true"), opts.toString());
assertTrue(opts.contains("-Dcodename1.javascript.darkreaderLock=false"), opts.toString());
}
@Test
void splashHintsReachTheTranslatorAndTheIconIsTheDefaultLogo() {
BuildRequest r = request("javascript.splash.appearance", " dark ",
"javascript.splash.darkBackground", "#1e1e1e");
List<String> opts = JavaScriptBuildHints.translatorOptions(r);
assertTrue(opts.contains("-Dcodename1.javascript.splash.appearance=dark"), opts.toString());
assertTrue(opts.contains("-Dcodename1.javascript.splash.darkBackground=#1e1e1e"), opts.toString());
// No icon: the PWA step writes a placeholder square, which is not worth showing.
assertFalse(opts.toString().contains("splash.logo"), opts.toString());
r.setIcon(new byte[] {1});
opts = JavaScriptBuildHints.translatorOptions(r);
assertTrue(opts.contains("-Dcodename1.javascript.splash.logo=" + JavaScriptBuildHints.SPLASH_DEFAULT_LOGO),
opts.toString());
r = request("javascript.splash.logo", "none");
r.setIcon(new byte[] {1});
opts = JavaScriptBuildHints.translatorOptions(r);
assertTrue(opts.contains("-Dcodename1.javascript.splash.logo=none"), opts.toString());
assertFalse(opts.contains("-Dcodename1.javascript.splash.logo=" + JavaScriptBuildHints.SPLASH_DEFAULT_LOGO),
opts.toString());
}
@Test
void noHintsShipsOnlyTheLegacyPair() {
assertEquals(set("iOS7Theme", "android_holo_light"), ship(request()));
}
@Test
void nativeShipsTheModernMobileThemesAndEveryDesktopTheme() {
assertEquals(set("iOSModernTheme", "AndroidMaterialTheme",
"WindowsFluentTheme", "MacOSAquaTheme", "GnomeAdwaitaTheme"),
ship(request("nativeTheme", "native")));
}
@Test
void modernStaysOffTheDesktopThemes() {
assertEquals(set("iOSModernTheme", "AndroidMaterialTheme"), ship(request("nativeTheme", "modern")));
}
@Test
void aPinnedDesktopThemeShipsOnlyThatDesktopTheme() {
assertEquals(set("iOSModernTheme", "AndroidMaterialTheme", "MacOSAquaTheme"),
ship(request("nativeTheme", "native", "javascript.desktopTheme", "aqua")));
assertEquals(set("iOSModernTheme", "AndroidMaterialTheme"),
ship(request("nativeTheme", "native", "javascript.desktopTheme", "none")));
}
@Test
void anExplicitResourceShipsOnlyThatTheme() {
assertEquals(set("WindowsFluentTheme"),
ship(request("javascript.native.theme", "/WindowsFluentTheme.res", "nativeTheme", "native")));
// Not one of ours: the port falls back to the legacy pair if it cannot open it.
assertEquals(set("iOS7Theme", "android_holo_light"),
ship(request("javascript.native.theme", "/mytheme.res")));
}
@Test
void platformModesAreHonoured() {
assertEquals(set("iPhoneTheme", "androidTheme"),
ship(request("ios.themeMode", "legacy", "and.themeMode", "legacy")));
assertEquals(set("iOS7Theme", "android_holo_light"), ship(request("nativeTheme", "legacy")));
}
@Test
void whatTheApplicationNamesIsKept() {
Set<String> refs = new LinkedHashSet<String>();
JavaScriptBuildHints.scanBytes("xx/GnomeAdwaitaTheme.resyy".getBytes(StandardCharsets.ISO_8859_1), refs);
JavaScriptBuildHints.scanBytes("cn1.modernThemeResource".getBytes(StandardCharsets.ISO_8859_1), refs);
Set<String> shipped = JavaScriptBuildHints.themesToShip(request(), refs);
assertTrue(shipped.containsAll(set("GnomeAdwaitaTheme", "iOSModernTheme", "AndroidMaterialTheme")),
shipped.toString());
}
@Test
void anApplicationThatSetsAThemeHintItselfKeepsEveryTheme() {
// Display.setProperty("and.themeMode", "modern") in init() picks at run time what the
// build hints did not, so pruning by the hints would delete the theme it asks for.
Set<String> refs = new LinkedHashSet<String>();
JavaScriptBuildHints.scanBytes(utf8Constant("and.themeMode"), refs);
assertTrue(refs.contains(JavaScriptBuildHints.RUNTIME_THEME_CHOICE), refs.toString());
assertEquals(new HashSet<String>(JavaScriptBuildHints.ALL_THEMES),
JavaScriptBuildHints.themesToShip(request(), refs));
}
@Test
void pruningCanBeTurnedOff() {
// For an app that computes its theme path, e.g. Resources.open("/" + name + ".res").
assertEquals(new HashSet<String>(JavaScriptBuildHints.ALL_THEMES),
ship(request("javascript.pruneThemes", "false")));
assertEquals(set("iOS7Theme", "android_holo_light"), ship(request("javascript.pruneThemes", "true")));
}
@Test
void anExtensionlessLayeredThemeReferenceIsKept() {
// Resources.openLayered("/AndroidMaterialTheme") appends ".res" itself.
Set<String> refs = new LinkedHashSet<String>();
JavaScriptBuildHints.scanBytes(utf8Constant("/AndroidMaterialTheme"), refs);
JavaScriptBuildHints.scanBytes(utf8Constant("iOSModernTheme"), refs);
assertTrue(refs.contains("AndroidMaterialTheme"), refs.toString());
assertTrue(refs.contains("iOSModernTheme"), refs.toString());
Set<String> longer = new LinkedHashSet<String>();
JavaScriptBuildHints.scanBytes(utf8Constant("iOSModernThemeHelper"), longer);
assertFalse(longer.contains("iOSModernTheme"), longer.toString());
}
@Test
void aLongerNameContainingAHintIsNotAThemeChoice() {
Set<String> refs = new LinkedHashSet<String>();
JavaScriptBuildHints.scanBytes(utf8Constant("nativeThemeResourceName"), refs);
assertFalse(refs.contains(JavaScriptBuildHints.RUNTIME_THEME_CHOICE), refs.toString());
}
@Test
void frameworkClassesAreNotScanned() {
assertTrue(JavaScriptBuildHints.isFrameworkPath("com/codename1/annotations/buildhints/Android.class"));
assertTrue(JavaScriptBuildHints.isFrameworkPath("com/codename1/impl/html5/HTML5Implementation.class"));
assertFalse(JavaScriptBuildHints.isFrameworkPath("com/example/app/Main.class"));
// Applications and cn1libs use com.codename1.* packages too; their choices must be seen.
assertFalse(JavaScriptBuildHints.isFrameworkPath("com/codename1/demos/theme/Main.class"));
}
/// The bytes of a CONSTANT_Utf8 entry, as a class file stores a string constant.
private static byte[] utf8Constant(String value) {
byte[] v = value.getBytes(StandardCharsets.UTF_8);
byte[] out = new byte[v.length + 5];
out[0] = 7;
out[1] = 1;
out[2] = (byte) (v.length >> 8);
out[3] = (byte) v.length;
System.arraycopy(v, 0, out, 4, v.length);
out[out.length - 1] = 12;
return out;
}
@Test
void everyShippedNameIsAKnownTheme() {
String[][] combos = {
{}, {"nativeTheme", "native"}, {"nativeTheme", "modern"}, {"nativeTheme", "legacy"},
{"nativeTheme", "native", "javascript.desktopTheme", "fluent"},
{"ios.themeMode", "flat", "and.themeMode", "holo"},
};
for (String[] combo : combos) {
for (String name : ship(request(combo))) {
assertTrue(JavaScriptBuildHints.ALL_THEMES.contains(name), name);
}
}
}
}