forked from webview/webview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebview_test.cc
176 lines (166 loc) · 4.69 KB
/
webview_test.cc
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
// +build ignore
#include <cassert>
#include <cstdio>
#include <functional>
#include <iostream>
#include <string>
#include <thread>
#include <utility>
#include <vector>
#define WEBVIEW_IMPLEMENTATION
#include "webview.h"
extern "C" void webview_dispatch_proxy(struct webview *w, void *arg) {
(*static_cast<std::function<void(struct webview *)> *>(arg))(w);
}
class runner {
public:
runner(struct webview *w) : w(w) { webview_init(this->w); }
~runner() { webview_exit(this->w); }
runner &then(std::function<void(struct webview *w)> fn) {
auto arg = new std::pair<std::function<void(struct webview *)>, void *>(
fn, nullptr);
this->queue.push_back([=](struct webview *w) {
webview_dispatch(
w,
[](struct webview *w, void *arg) {
auto dispatch_arg = reinterpret_cast<
std::pair<std::function<void(struct webview *)>, void *> *>(
arg);
dispatch_arg->first(w);
delete dispatch_arg;
},
reinterpret_cast<void *>(arg));
});
return *this;
}
runner &sleep(const int millis) {
this->queue.push_back([=](struct webview *w) {
(void)w;
std::this_thread::sleep_for(std::chrono::milliseconds(millis));
});
return *this;
}
void wait() {
this->then([](struct webview *w) { webview_terminate(w); });
auto q = this->queue;
auto w = this->w;
std::thread bg_thread([w, q]() {
for (auto f : q) {
f(w);
}
});
while (webview_loop(w, 1) == 0) {
}
bg_thread.join();
}
private:
struct webview *w;
std::vector<std::function<void(struct webview *)>> queue;
};
static void test_minimal() {
struct webview w = {};
std::cout << "TEST: minimal" << std::endl;
w.title = "Minimal test";
w.width = 480;
w.height = 320;
webview_init(&w);
webview_dispatch(&w,
[](struct webview *w, void *arg) {
(void)arg;
webview_terminate(w);
},
nullptr);
while (webview_loop(&w, 1) == 0) {
}
webview_exit(&w);
}
static void test_window_size() {
struct webview w = {};
std::vector<std::string> results;
std::cout << "TEST: window size" << std::endl;
w.width = 480;
w.height = 320;
w.resizable = 1;
w.userdata = static_cast<void *>(&results);
w.external_invoke_cb = [](struct webview *w, const char *arg) {
auto *v = static_cast<std::vector<std::string> *>(w->userdata);
v->push_back(std::string(arg));
};
runner(&w)
.then([](struct webview *w) {
webview_eval(w, "window.external.invoke(''+window.screen.width+' ' + "
"window.screen.height)");
webview_eval(w, "window.external.invoke(''+window.innerWidth+' ' + "
"window.innerHeight)");
})
.sleep(200)
.then([](struct webview *w) { webview_set_fullscreen(w, 1); })
.sleep(500)
.then([](struct webview *w) {
webview_eval(w, "window.external.invoke(''+window.innerWidth+' ' + "
"window.innerHeight)");
})
.sleep(200)
.then([](struct webview *w) { webview_set_fullscreen(w, 0); })
.sleep(500)
.then([](struct webview *w) {
webview_eval(w, "window.external.invoke(''+window.innerWidth+' ' + "
"window.innerHeight)");
})
.wait();
assert(results.size() == 4);
assert(results[1] == "480 320");
assert(results[0] == results[2]);
assert(results[1] == results[3]);
}
static void test_inject_js() {
struct webview w = {};
std::vector<std::string> results;
std::cout << "TEST: inject JS" << std::endl;
w.width = 480;
w.height = 320;
w.userdata = static_cast<void *>(&results);
w.external_invoke_cb = [](struct webview *w, const char *arg) {
auto *v = static_cast<std::vector<std::string> *>(w->userdata);
v->push_back(std::string(arg));
};
runner(&w)
.then([](struct webview *w) {
webview_eval(w,
R"(document.body.innerHTML = '<div id="foo">Foo</div>';)");
webview_eval(
w,
"window.external.invoke(document.getElementById('foo').innerText)");
})
.wait();
assert(results.size() == 1);
assert(results[0] == "Foo");
}
static void test_inject_css() {
struct webview w = {};
std::vector<std::string> results;
std::cout << "TEST: inject CSS" << std::endl;
w.width = 480;
w.height = 320;
w.userdata = static_cast<void *>(&results);
w.external_invoke_cb = [](struct webview *w, const char *arg) {
auto *v = static_cast<std::vector<std::string> *>(w->userdata);
v->push_back(std::string(arg));
};
runner(&w)
.then([](struct webview *w) {
webview_inject_css(w, "#app { margin-left: 4px; }");
webview_eval(w, "window.external.invoke(getComputedStyle(document."
"getElementById('app')).marginLeft)");
})
.wait();
assert(results.size() == 1);
assert(results[0] == "4px");
}
int main() {
test_minimal();
test_window_size();
test_inject_js();
test_inject_css();
return 0;
}