bun-app-window 0.1.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/CHANGELOG.md +202 -0
  2. package/README.md +35 -3
  3. package/README.zh-CN.md +35 -3
  4. package/README.zh-TW.md +35 -3
  5. package/dist/libshell.dll +0 -0
  6. package/package.json +8 -10
  7. package/src/shell/native-asset.d.ts +18 -0
  8. package/src/shell/resolve.ts +1 -1
  9. package/native/build.ps1 +0 -67
  10. package/native/fetch.ps1 +0 -66
  11. package/native/shell.cc +0 -2134
  12. package/vendor/webview/compatibility/CMakeLists.txt +0 -3
  13. package/vendor/webview/compatibility/mingw/CMakeLists.txt +0 -14
  14. package/vendor/webview/compatibility/mingw/include/EventToken.h +0 -25
  15. package/vendor/webview/core/CMakeLists.txt +0 -94
  16. package/vendor/webview/core/include/webview/api.h +0 -251
  17. package/vendor/webview/core/include/webview/backends.hh +0 -40
  18. package/vendor/webview/core/include/webview/c_api_impl.hh +0 -258
  19. package/vendor/webview/core/include/webview/detail/backends/cocoa_webkit.hh +0 -626
  20. package/vendor/webview/core/include/webview/detail/backends/gtk_webkitgtk.hh +0 -355
  21. package/vendor/webview/core/include/webview/detail/backends/win32_edge.hh +0 -909
  22. package/vendor/webview/core/include/webview/detail/basic_result.hh +0 -119
  23. package/vendor/webview/core/include/webview/detail/engine_base.hh +0 -382
  24. package/vendor/webview/core/include/webview/detail/exceptions.hh +0 -42
  25. package/vendor/webview/core/include/webview/detail/json.hh +0 -337
  26. package/vendor/webview/core/include/webview/detail/native_library.hh +0 -170
  27. package/vendor/webview/core/include/webview/detail/optional.hh +0 -118
  28. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSApplication.hh +0 -107
  29. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSBundle.hh +0 -56
  30. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSEvent.hh +0 -71
  31. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSInvocation.hh +0 -66
  32. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSMethodSignature.hh +0 -52
  33. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSNotification.hh +0 -51
  34. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSNumber.hh +0 -53
  35. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSObject.hh +0 -51
  36. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSOpenPanel.hh +0 -71
  37. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSPoint.hh +0 -53
  38. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSRect.hh +0 -53
  39. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSSavePanel.hh +0 -52
  40. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSSize.hh +0 -53
  41. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSString.hh +0 -87
  42. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSURL.hh +0 -63
  43. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSURLRequest.hh +0 -52
  44. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSValue.hh +0 -56
  45. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSView.hh +0 -91
  46. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/NSWindow.hh +0 -137
  47. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/cocoa.hh +0 -59
  48. package/vendor/webview/core/include/webview/detail/platform/darwin/cocoa/types.hh +0 -49
  49. package/vendor/webview/core/include/webview/detail/platform/darwin/objc/Class.hh +0 -55
  50. package/vendor/webview/core/include/webview/detail/platform/darwin/objc/autoreleasepool.hh +0 -70
  51. package/vendor/webview/core/include/webview/detail/platform/darwin/objc/invoke.hh +0 -75
  52. package/vendor/webview/core/include/webview/detail/platform/darwin/objc/memory.hh +0 -57
  53. package/vendor/webview/core/include/webview/detail/platform/darwin/objc/objc.hh +0 -47
  54. package/vendor/webview/core/include/webview/detail/platform/darwin/webkit/WKOpenPanelParameters.hh +0 -57
  55. package/vendor/webview/core/include/webview/detail/platform/darwin/webkit/WKScriptMessage.hh +0 -51
  56. package/vendor/webview/core/include/webview/detail/platform/darwin/webkit/WKUserContentController.hh +0 -61
  57. package/vendor/webview/core/include/webview/detail/platform/darwin/webkit/WKUserScript.hh +0 -71
  58. package/vendor/webview/core/include/webview/detail/platform/darwin/webkit/WKWebView.hh +0 -108
  59. package/vendor/webview/core/include/webview/detail/platform/darwin/webkit/WKWebViewConfiguration.hh +0 -60
  60. package/vendor/webview/core/include/webview/detail/platform/darwin/webkit/webkit.hh +0 -46
  61. package/vendor/webview/core/include/webview/detail/platform/linux/gtk/compat.hh +0 -137
  62. package/vendor/webview/core/include/webview/detail/platform/linux/webkitgtk/compat.hh +0 -142
  63. package/vendor/webview/core/include/webview/detail/platform/linux/webkitgtk/dmabuf.hh +0 -167
  64. package/vendor/webview/core/include/webview/detail/platform/windows/com_init_wrapper.hh +0 -126
  65. package/vendor/webview/core/include/webview/detail/platform/windows/dpi.hh +0 -161
  66. package/vendor/webview/core/include/webview/detail/platform/windows/dwmapi.hh +0 -67
  67. package/vendor/webview/core/include/webview/detail/platform/windows/iid.hh +0 -78
  68. package/vendor/webview/core/include/webview/detail/platform/windows/ntdll.hh +0 -58
  69. package/vendor/webview/core/include/webview/detail/platform/windows/reg_key.hh +0 -135
  70. package/vendor/webview/core/include/webview/detail/platform/windows/shcore.hh +0 -59
  71. package/vendor/webview/core/include/webview/detail/platform/windows/theme.hh +0 -76
  72. package/vendor/webview/core/include/webview/detail/platform/windows/user32.hh +0 -90
  73. package/vendor/webview/core/include/webview/detail/platform/windows/version.hh +0 -151
  74. package/vendor/webview/core/include/webview/detail/platform/windows/webview2/loader.hh +0 -391
  75. package/vendor/webview/core/include/webview/detail/user_script.hh +0 -76
  76. package/vendor/webview/core/include/webview/detail/utility/string.hh +0 -99
  77. package/vendor/webview/core/include/webview/errors.h +0 -67
  78. package/vendor/webview/core/include/webview/errors.hh +0 -85
  79. package/vendor/webview/core/include/webview/json_deprecated.hh +0 -61
  80. package/vendor/webview/core/include/webview/macros.h +0 -123
  81. package/vendor/webview/core/include/webview/types.h +0 -81
  82. package/vendor/webview/core/include/webview/types.hh +0 -48
  83. package/vendor/webview/core/include/webview/version.h +0 -67
  84. package/vendor/webview/core/include/webview/webview.h +0 -32
  85. package/vendor/webview/core/include/webview.h +0 -15
  86. package/vendor/webview/core/src/webview.cc +0 -1
  87. package/vendor/webview/core/tests/CMakeLists.txt +0 -18
  88. package/vendor/webview/core/tests/src/functional_tests.cc +0 -316
  89. package/vendor/webview/core/tests/src/unit_tests.cc +0 -188
  90. package/vendor/webview2/build/native/include/WebView2.h +0 -23568
  91. package/vendor/webview2/build/native/include/WebView2EnvironmentOptions.h +0 -144
@@ -1,316 +0,0 @@
1
- #include "webview/test_driver.hh"
2
-
3
- #define WEBVIEW_VERSION_MAJOR 1
4
- #define WEBVIEW_VERSION_MINOR 2
5
- #define WEBVIEW_VERSION_PATCH 3
6
- #define WEBVIEW_VERSION_PRE_RELEASE "-test"
7
- #define WEBVIEW_VERSION_BUILD_METADATA "+gaabbccd"
8
-
9
- #include "webview/webview.h"
10
-
11
- #include <cassert>
12
- #include <cstdint>
13
-
14
- // This test should only run on Windows to enable us to perform a controlled
15
- // "warm-up" of MS WebView2 in order to avoid the initial test from
16
- // occationally timing out in CI.
17
- #ifdef WEBVIEW_PLATFORM_WINDOWS
18
- #include <iostream>
19
-
20
- TEST_CASE("# Warm-up") {
21
- // Signal to the test runner that this may be a slow test.
22
- std::cerr << "[[slow]]" << std::endl; // NOLINT(performance-avoid-endl)
23
- webview::webview w(false, nullptr);
24
- w.dispatch([&]() { w.terminate(); });
25
- w.run();
26
- }
27
- #endif
28
-
29
- TEST_CASE("Start app loop and terminate it") {
30
- webview::webview w(false, nullptr);
31
- w.dispatch([&]() { w.terminate(); });
32
- w.run();
33
- }
34
-
35
- void cb_assert_arg(webview_t w, void *arg) {
36
- REQUIRE(w != nullptr);
37
- REQUIRE(memcmp(arg, "arg", 3) == 0);
38
- }
39
-
40
- void cb_terminate(webview_t w, void *arg) {
41
- REQUIRE(arg == nullptr);
42
- webview_terminate(w);
43
- }
44
-
45
- TEST_CASE("Use C API to create a window, run app and terminate it") {
46
- webview_t w;
47
- w = webview_create(false, nullptr);
48
- webview_set_size(w, 480, 320, WEBVIEW_HINT_NONE);
49
- webview_set_title(w, "Test");
50
- webview_set_html(w, "set_html ok");
51
- webview_navigate(w, "data:text/plain,navigate%20ok");
52
- webview_dispatch(w, cb_assert_arg, (void *)"arg");
53
- webview_dispatch(w, cb_terminate, nullptr);
54
- webview_run(w);
55
- webview_destroy(w);
56
- }
57
-
58
- TEST_CASE("Use C API to test binding and unbinding") {
59
- struct context_t {
60
- webview_t w;
61
- unsigned int number;
62
- } context{};
63
- auto test = +[](const char *seq, const char *req, void *arg) {
64
- auto increment = +[](const char *seq, const char * /*req*/, void *arg) {
65
- auto *context = static_cast<context_t *>(arg);
66
- ++context->number;
67
- webview_return(context->w, seq, 0, "");
68
- };
69
- auto context = static_cast<context_t *>(arg);
70
- std::string req_(req);
71
- // Bind and increment number.
72
- if (req_ == "[0]") {
73
- REQUIRE(context->number == 0);
74
- webview_bind(context->w, "increment", increment, context);
75
- webview_eval(context->w,
76
- "try{window.increment().then(r => window.test(1))"
77
- ".catch(() => window.test(1,1))}"
78
- "catch{window.test(1,1)}");
79
- webview_return(context->w, seq, 0, "");
80
- return;
81
- }
82
- // Unbind and make sure that we cannot increment even if we try.
83
- if (req_ == "[1]") {
84
- REQUIRE(context->number == 1);
85
- webview_unbind(context->w, "increment");
86
- webview_eval(context->w,
87
- "try{window.increment().then(r => window.test(2))"
88
- ".catch(() => window.test(2,1))}"
89
- "catch{window.test(2,1)}");
90
- webview_return(context->w, seq, 0, "");
91
- return;
92
- }
93
- // Number should not have changed but we can bind again and change the number.
94
- if (req_ == "[2,1]") {
95
- REQUIRE(context->number == 1);
96
- webview_bind(context->w, "increment", increment, context);
97
- webview_eval(context->w,
98
- "try{window.increment().then(r => window.test(3))"
99
- ".catch(() => window.test(3,1))}"
100
- "catch{window.test(3,1)}");
101
- webview_return(context->w, seq, 0, "");
102
- return;
103
- }
104
- // Finish test.
105
- if (req_ == "[3]") {
106
- REQUIRE(context->number == 2);
107
- webview_terminate(context->w);
108
- return;
109
- }
110
- REQUIRE(!"Should not reach here");
111
- };
112
- auto html = "<script>\n"
113
- " window.test(0);\n"
114
- "</script>";
115
- auto w = webview_create(1, nullptr);
116
- context.w = w;
117
- // Attempting to remove non-existing binding is OK
118
- webview_unbind(w, "test");
119
- webview_bind(w, "test", test, &context);
120
- // Attempting to bind multiple times only binds once
121
- webview_bind(w, "test", test, &context);
122
- webview_set_html(w, html);
123
- webview_run(w);
124
- }
125
-
126
- TEST_CASE("Test synchronous binding and unbinding") {
127
- auto make_call_js = [](unsigned int result) {
128
- std::string js;
129
- js += "try{window.increment().then(r => window.test(";
130
- js += std::to_string(result);
131
- js += "))";
132
- js += ".catch(() => window.test(";
133
- js += std::to_string(result);
134
- js += ",1))}catch{window.test(";
135
- js += std::to_string(result);
136
- js += ",1)}";
137
- return js;
138
- };
139
- unsigned int number = 0;
140
- webview::webview w(false, nullptr);
141
- auto test = [&](const std::string &req) -> std::string {
142
- auto increment = [&](const std::string & /*req*/) -> std::string {
143
- ++number;
144
- return "";
145
- };
146
- // Bind and increment number.
147
- if (req == "[0]") {
148
- REQUIRE(number == 0);
149
- w.bind("increment", increment);
150
- w.eval(make_call_js(1));
151
- return "";
152
- }
153
- // Unbind and make sure that we cannot increment even if we try.
154
- if (req == "[1]") {
155
- REQUIRE(number == 1);
156
- w.unbind("increment");
157
- w.eval(make_call_js(2));
158
- return "";
159
- }
160
- // We should have gotten an error on the JS side.
161
- // Number should not have changed but we can bind again and change the number.
162
- if (req == "[2,1]") {
163
- REQUIRE(number == 1);
164
- w.bind("increment", increment);
165
- w.eval(make_call_js(3));
166
- return "";
167
- }
168
- // Finish test.
169
- if (req == "[3]") {
170
- REQUIRE(number == 2);
171
- w.terminate();
172
- return "";
173
- }
174
- REQUIRE(!"Should not reach here");
175
- return "";
176
- };
177
- auto html = "<script>\n"
178
- " window.test(0);\n"
179
- "</script>";
180
- // Attempting to remove non-existing binding is OK
181
- w.unbind("test");
182
- w.bind("test", test);
183
- // Attempting to bind multiple times only binds once
184
- w.bind("test", test);
185
- w.set_html(html);
186
- w.run();
187
- }
188
-
189
- TEST_CASE("The string returned from a binding call must be JSON") {
190
- constexpr auto html =
191
- R"html(<script>
192
- try {
193
- window.loadData()
194
- .then(() => window.endTest(0))
195
- .catch(() => window.endTest(1));
196
- } catch {
197
- window.endTest(2);
198
- }
199
- </script>)html";
200
-
201
- webview::webview w(true, nullptr);
202
-
203
- w.bind("loadData", [](const std::string & /*req*/) -> std::string {
204
- return "\"hello\"";
205
- });
206
-
207
- w.bind("endTest", [&](const std::string &req) -> std::string {
208
- REQUIRE(req != "[2]");
209
- REQUIRE(req != "[1]");
210
- REQUIRE(req == "[0]");
211
- w.terminate();
212
- return "";
213
- });
214
-
215
- w.set_html(html);
216
- w.run();
217
- }
218
-
219
- TEST_CASE("The string returned of a binding call must not be JS") {
220
- constexpr const auto html =
221
- R"html(<script>
222
- try {
223
- window.loadData()
224
- .then(() => window.endTest(0))
225
- .catch(() => window.endTest(1));
226
- } catch {
227
- window.endTest(2);
228
- }
229
- </script>)html";
230
-
231
- webview::webview w(true, nullptr);
232
-
233
- w.bind("loadData", [](const std::string & /*req*/) -> std::string {
234
- // Try to load malicious JS code
235
- return "(()=>{document.body.innerHTML='gotcha';return 'hello';})()";
236
- });
237
-
238
- w.bind("endTest", [&](const std::string &req) -> std::string {
239
- REQUIRE(req != "[0]");
240
- REQUIRE(req != "[2]");
241
- REQUIRE(req == "[1]");
242
- w.terminate();
243
- return "";
244
- });
245
-
246
- w.set_html(html);
247
- w.run();
248
- }
249
-
250
- TEST_CASE("webview_version()") {
251
- auto vi = webview_version();
252
- REQUIRE(vi);
253
- REQUIRE(vi->version.major == 1);
254
- REQUIRE(vi->version.minor == 2);
255
- REQUIRE(vi->version.patch == 3);
256
- REQUIRE(std::string(vi->version_number) == "1.2.3");
257
- REQUIRE(std::string(vi->pre_release) == "-test");
258
- REQUIRE(std::string(vi->build_metadata) == "+gaabbccd");
259
- // The function should return the same pointer when called again.
260
- REQUIRE(webview_version() == vi);
261
- }
262
-
263
- struct test_webview : webview::browser_engine {
264
- using cb_t = std::function<void(test_webview *, int, const std::string &)>;
265
- test_webview(cb_t cb) : webview::browser_engine(true, nullptr), m_cb(cb) {}
266
- void on_message(const std::string &msg) override { m_cb(this, i++, msg); }
267
- int i = 0;
268
- cb_t m_cb;
269
- };
270
-
271
- TEST_CASE("Ensure that JS code can call native code and vice versa") {
272
- test_webview browser([](test_webview *w, int i, const std::string &msg) {
273
- switch (i) {
274
- case 0:
275
- REQUIRE(msg == "loaded");
276
- w->eval("window.__webview__.post('exiting ' + window.x)");
277
- break;
278
- case 1:
279
- REQUIRE(msg == "exiting 42");
280
- w->terminate();
281
- break;
282
- default:
283
- REQUIRE(0);
284
- }
285
- });
286
- browser.init(R"(
287
- window.x = 42;
288
- window.onload = () => {
289
- window.__webview__.post('loaded');
290
- };
291
- )");
292
- browser.navigate("data:text/html,%3Chtml%3Ehello%3C%2Fhtml%3E");
293
- browser.run();
294
- }
295
-
296
- #define ASSERT_WEBVIEW_FAILED(expr) REQUIRE(WEBVIEW_FAILED(expr))
297
-
298
- TEST_CASE("Bad C API usage without crash") {
299
- webview_t w{};
300
- REQUIRE(webview_get_window(w) == nullptr);
301
- REQUIRE(webview_get_native_handle(w, WEBVIEW_NATIVE_HANDLE_KIND_UI_WINDOW) ==
302
- nullptr);
303
- ASSERT_WEBVIEW_FAILED(webview_set_size(w, 0, 0, WEBVIEW_HINT_NONE));
304
- ASSERT_WEBVIEW_FAILED(webview_navigate(w, nullptr));
305
- ASSERT_WEBVIEW_FAILED(webview_set_title(w, nullptr));
306
- ASSERT_WEBVIEW_FAILED(webview_set_html(w, nullptr));
307
- ASSERT_WEBVIEW_FAILED(webview_init(w, nullptr));
308
- ASSERT_WEBVIEW_FAILED(webview_eval(w, nullptr));
309
- ASSERT_WEBVIEW_FAILED(webview_bind(w, nullptr, nullptr, nullptr));
310
- ASSERT_WEBVIEW_FAILED(webview_unbind(w, nullptr));
311
- ASSERT_WEBVIEW_FAILED(webview_return(w, nullptr, 0, nullptr));
312
- ASSERT_WEBVIEW_FAILED(webview_dispatch(w, nullptr, nullptr));
313
- ASSERT_WEBVIEW_FAILED(webview_terminate(w));
314
- ASSERT_WEBVIEW_FAILED(webview_run(w));
315
- ASSERT_WEBVIEW_FAILED(webview_destroy(w));
316
- }
@@ -1,188 +0,0 @@
1
- #include "webview/test_driver.hh"
2
- #include "webview/webview.h"
3
-
4
- TEST_CASE("Ensure that JSON parsing works") {
5
- auto J = webview::detail::json_parse;
6
- // Valid input with expected output
7
- REQUIRE(J(R"({"foo":"bar"})", "foo", -1) == "bar");
8
- REQUIRE(J(R"({"foo":""})", "foo", -1).empty());
9
- REQUIRE(J(R"({"foo":{}")", "foo", -1) == "{}");
10
- REQUIRE(J(R"({"foo": {"bar": 1}})", "foo", -1) == R"({"bar": 1})");
11
- REQUIRE(J(R"(["foo", "bar", "baz"])", "", 0) == "foo");
12
- REQUIRE(J(R"(["foo", "bar", "baz"])", "", 2) == "baz");
13
- // Valid UTF-8 with expected output
14
- REQUIRE(J(R"({"フー":"バー"})", "フー", -1) == "バー");
15
- REQUIRE(J(R"(["フー", "バー", "バズ"])", "", 2) == "バズ");
16
- // Invalid input with valid output - should probably fail
17
- REQUIRE(J(R"({"foo":"bar")", "foo", -1) == "bar");
18
- // Valid input with other invalid parameters - should fail
19
- REQUIRE(J(R"([])", "", 0).empty());
20
- REQUIRE(J(R"({})", "foo", -1).empty());
21
- REQUIRE(J(R"(["foo", "bar", "baz"])", "", -1).empty());
22
- REQUIRE(J(R"(["foo"])", "", 1234).empty());
23
- REQUIRE(J(R"(["foo"])", "", -1234).empty());
24
- // Invalid input - should fail
25
- REQUIRE(J("", "", 0).empty());
26
- REQUIRE(J("", "foo", -1).empty());
27
- REQUIRE(J(R"({"foo":")", "foo", -1).empty());
28
- REQUIRE(J(R"({"foo":{)", "foo", -1).empty());
29
- REQUIRE(J(R"({"foo":{")", "foo", -1).empty());
30
- REQUIRE(J(R"(}")", "foo", -1).empty());
31
- REQUIRE(J(R"({}}")", "foo", -1).empty());
32
- REQUIRE(J(R"("foo)", "foo", -1).empty());
33
- REQUIRE(J(R"(foo)", "foo", -1).empty());
34
- REQUIRE(J(R"({{[[""foo""]]}})", "", 1234).empty());
35
- REQUIRE(J("bad", "", 0).empty());
36
- REQUIRE(J("bad", "foo", -1).empty());
37
- }
38
-
39
- TEST_CASE("Ensure that JSON escaping works") {
40
- using webview::detail::json_escape;
41
-
42
- // Simple case without need for escaping. Quotes added by default.
43
- REQUIRE(json_escape("hello") == "\"hello\"");
44
- // Simple case without need for escaping. Quotes explicitly not added.
45
- REQUIRE(json_escape("hello", false) == "hello");
46
- // Empty input should return empty output.
47
- REQUIRE(json_escape("", false).empty());
48
- // '"' and '\' should be escaped.
49
- REQUIRE(json_escape("\"", false) == "\\\"");
50
- REQUIRE(json_escape("\\", false) == "\\\\");
51
- // Commonly-used characters that should be escaped.
52
- REQUIRE(json_escape("\b\f\n\r\t", false) == "\\b\\f\\n\\r\\t");
53
- // ASCII control characters should be escaped.
54
- REQUIRE(json_escape(std::string{"\0\x1f", 2}, false) == "\\u0000\\u001f");
55
- // ASCII printable characters (even DEL) shouldn't be escaped.
56
- REQUIRE(json_escape("\x20\x7e\x7f", false) == "\x20\x7e\x7f");
57
- // Valid UTF-8.
58
- REQUIRE(json_escape("\u2328", false) == "\u2328");
59
- REQUIRE(json_escape("フーバー", false) == "フーバー");
60
- // Replacement character for invalid characters.
61
- REQUIRE(json_escape("�", false) == "�");
62
- // Invalid characters should be replaced with '�' but we just leave them as-is.
63
- REQUIRE(json_escape("\x80\x9f\xa0\xff", false) == "\x80\x9f\xa0\xff");
64
- // JS code should not be executed (eval).
65
- auto expected_gotcha = R"js(alert(\"gotcha\"))js";
66
- REQUIRE(json_escape(R"(alert("gotcha"))", false) == expected_gotcha);
67
- }
68
-
69
- TEST_CASE("optional class") {
70
- using namespace webview::detail;
71
-
72
- REQUIRE(!optional<int>{}.has_value());
73
- REQUIRE(optional<int>{1}.has_value());
74
- REQUIRE(optional<int>{1}.get() == 1);
75
-
76
- REQUIRE_THROW(bad_access, [] { optional<int>{}.get(); });
77
-
78
- REQUIRE(!optional<int>{optional<int>{}}.has_value());
79
- REQUIRE(optional<int>{optional<int>{1}}.has_value());
80
- REQUIRE(optional<int>{optional<int>{1}}.get() == 1);
81
- }
82
-
83
- TEST_CASE("result class") {
84
- using namespace webview::detail;
85
- using namespace webview;
86
-
87
- REQUIRE(result<int>{}.has_value());
88
- REQUIRE(result<int>{}.value() == 0);
89
- REQUIRE(result<int>{1}.has_value());
90
- REQUIRE(result<int>{1}.value() == 1);
91
- REQUIRE(!result<int>{}.has_error());
92
- REQUIRE(!result<int>{1}.has_error());
93
- REQUIRE(result<int>{}.ok());
94
- REQUIRE(result<int>{1}.ok());
95
- REQUIRE(!result<int>{error_info{}}.ok());
96
- REQUIRE(!result<int>{error_info{}}.has_value());
97
- REQUIRE(result<int>{error_info{}}.has_error());
98
-
99
- auto result_with_error = result<int>{
100
- error_info{WEBVIEW_ERROR_INVALID_ARGUMENT, "invalid argument"}};
101
- REQUIRE(result_with_error.error().code() == WEBVIEW_ERROR_INVALID_ARGUMENT);
102
- REQUIRE(result_with_error.error().message() == "invalid argument");
103
-
104
- REQUIRE_THROW(bad_access, [] { result<int>{}.error(); });
105
- }
106
-
107
- TEST_CASE("noresult class") {
108
- using namespace webview::detail;
109
- using namespace webview;
110
-
111
- REQUIRE(!noresult{}.has_error());
112
- REQUIRE(noresult{}.ok());
113
- REQUIRE(!noresult{error_info{}}.ok());
114
- REQUIRE(noresult{error_info{}}.has_error());
115
-
116
- auto result_with_error =
117
- noresult{error_info{WEBVIEW_ERROR_INVALID_ARGUMENT, "invalid argument"}};
118
- REQUIRE(result_with_error.error().code() == WEBVIEW_ERROR_INVALID_ARGUMENT);
119
- REQUIRE(result_with_error.error().message() == "invalid argument");
120
-
121
- REQUIRE_THROW(bad_access, [] { noresult{}.error(); });
122
- }
123
-
124
- #if _WIN32
125
- TEST_CASE("Ensure that version number parsing works on Windows") {
126
- using namespace webview::detail;
127
- auto v = parse_version("");
128
- REQUIRE(v.size() == 4);
129
- REQUIRE(v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0);
130
- v = parse_version("1");
131
- REQUIRE(v[0] == 1 && v[1] == 0 && v[2] == 0 && v[3] == 0);
132
- v = parse_version("0.2");
133
- REQUIRE(v[0] == 0 && v[1] == 2 && v[2] == 0 && v[3] == 0);
134
- v = parse_version("0.0.3");
135
- REQUIRE(v[0] == 0 && v[1] == 0 && v[2] == 3 && v[3] == 0);
136
- v = parse_version("0.0.0.4");
137
- REQUIRE(v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 4);
138
- v = parse_version("1.2.3.4.5");
139
- REQUIRE(v.size() == 4);
140
- REQUIRE(v[0] == 1 && v[1] == 2 && v[2] == 3 && v[3] == 4);
141
- v = parse_version("1.2.3.4.5.6");
142
- REQUIRE(v[0] == 1 && v[1] == 2 && v[2] == 3 && v[3] == 4);
143
- v = parse_version("11.22.33.44");
144
- REQUIRE(v[0] == 11 && v[1] == 22 && v[2] == 33 && v[3] == 44);
145
- v = parse_version("0.0.0.0");
146
- REQUIRE(v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0);
147
- v = parse_version("-1.-2.-3.-4");
148
- REQUIRE(v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0);
149
- v = parse_version("a.b.c.d");
150
- REQUIRE(v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0);
151
- v = parse_version("...");
152
- REQUIRE(v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0);
153
- }
154
-
155
- TEST_CASE("Ensure that narrow/wide string conversion works on Windows") {
156
- using namespace webview::detail;
157
- REQUIRE(widen_string("").empty());
158
- REQUIRE(narrow_string(L"").empty());
159
- REQUIRE(widen_string("foo") == L"foo");
160
- REQUIRE(narrow_string(L"foo") == "foo");
161
- REQUIRE(widen_string("フー") == L"フー");
162
- REQUIRE(narrow_string(L"フー") == "フー");
163
- REQUIRE(widen_string("æøå") == L"æøå");
164
- REQUIRE(narrow_string(L"æøå") == "æøå");
165
- // Unicode number for the smiley face below: U+1F600
166
- REQUIRE(widen_string("😀") == L"😀");
167
- REQUIRE(narrow_string(L"😀") == "😀");
168
- // Ensure that elements of wide string are correct
169
- {
170
- auto s = widen_string("😀");
171
- REQUIRE(s.size() == 2);
172
- REQUIRE(static_cast<std::uint16_t>(s[0]) == 0xD83D);
173
- REQUIRE(static_cast<std::uint16_t>(s[1]) == 0xDE00);
174
- }
175
- // Ensure that elements of narrow string are correct
176
- {
177
- auto s = narrow_string(L"😀");
178
- REQUIRE(s.size() == 4);
179
- REQUIRE(static_cast<std::uint8_t>(s[0]) == 0xf0);
180
- REQUIRE(static_cast<std::uint8_t>(s[1]) == 0x9f);
181
- REQUIRE(static_cast<std::uint8_t>(s[2]) == 0x98);
182
- REQUIRE(static_cast<std::uint8_t>(s[3]) == 0x80);
183
- }
184
- // Null-characters must also be converted
185
- REQUIRE(widen_string(std::string(2, '\0')) == std::wstring(2, L'\0'));
186
- REQUIRE(narrow_string(std::wstring(2, L'\0')) == std::string(2, '\0'));
187
- }
188
- #endif