aboutsummaryrefslogtreecommitdiff
path: root/spec/LuaRuntimeApiSpec.cpp
blob: c340a03f74b803c88660fb58236f9cb1ff887c77 (plain) (blame)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include <QtCore>
#include <uv.h>

#include "LuaRuntime.hpp"
#include "WindowActionRouter.hpp"
#include "events.hpp"
#include "testUtils.h"

class TestEvent1 : public BrowserEvent {
public:
  int num;
  TestEvent1(int num) : num(num) { kind = "TestEvent1"; }
  void lua_push(lua_State *state) const override {
    lua_newtable(state);
    SET_FIELD("test_data", integer, num);
  }
};

// NOLINTBEGIN
class LuaRuntimeApiSpec : public QObject {
  Q_OBJECT

private slots:
  void beforeTestCase() { LuaRuntime::instance().start_event_loop(); }

  void cleanupTestCase() { LuaRuntime::instance().stop_event_loop(); }

  void test_web_event_add_listener() {
    describe("web.event.add_listener");

    context("when events, patterns and callback are specified correctly");
    it("returns true and registers event") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed);

      lua.evaluate(R"(
        return web.event.add_listener({ 'Hello', 'World' }, {
          patterns = { 'p1', 'p2' },
          callback = function() print("Called") end,
        });
      )");
      evaluation_completed_spy.wait();

      QCOMPARE(evaluation_completed_spy.count(), 1);
      QVariant result = evaluation_completed_spy.takeFirst().at(0);
      QCOMPARE(result, true);
    }

    context("when a single event is passed");
    it("returns true and registers event") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed);

      lua.evaluate(R"(
        return web.event.add_listener('Hello', {
          patterns = { 'p1', 'p2' },
          callback = function() print("Called") end,
        });
      )");
      evaluation_completed_spy.wait();

      QCOMPARE(evaluation_completed_spy.count(), 1);
      QVariant result = evaluation_completed_spy.takeFirst().at(0);
      QCOMPARE(result, true);
    }

    context("when patterns is not passed");
    it("returns true and registers event") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed);

      lua.evaluate(R"(
        return web.event.add_listener({ 'Hello', 'World' }, {
          callback = function() print("Called") end,
        });
      )");
      evaluation_completed_spy.wait();

      QCOMPARE(evaluation_completed_spy.count(), 1);
      QVariant result = evaluation_completed_spy.takeFirst().at(0);
      QCOMPARE(result, true);
    }

    context("when events list is not passed");
    it("returns false and doesnt register event") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed);

      lua.evaluate(R"(
        return web.event.add_listener(nil, {
          patterns = { 'p1', 'p2' },
          callback = function() print("Called") end,
        });
      )");
      evaluation_completed_spy.wait();

      QCOMPARE(evaluation_completed_spy.count(), 1);
      QVariant result = evaluation_completed_spy.first().first();
      QCOMPARE(result, false);
    }

    context("when callback is not passed");
    it("returns false and doesnt register event") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed);

      lua.evaluate(R"(
        return web.event.add_listener({'Ev'}, {
          patterns = { 'p1', 'p2' },
        });
      )");
      evaluation_completed_spy.wait();

      QCOMPARE(evaluation_completed_spy.count(), 1);
      QVariant result = evaluation_completed_spy.first().first();
      QCOMPARE(result, false);
    }
  }

  void test_web_event_dispatching() {
    describe("web.event.add_listener (event dispatch)");

    context("when dispatching a registered event (without pattern)");
    it("calls the registered event handler") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed);
      lua.evaluate(R"(
        _G.event1_called = false;
        _G.event1_called_with = nil;
        web.event.add_listener('TestEvent1', {
          callback = function(opts)
            _G.event1_called = true
            _G.event1_called_with = opts
          end,
        });
        _G.event2_called = false;
        web.event.add_listener('TestEvent2', {
          callback = function(opts) _G.event2_called = true end,
        });
      )");
      evaluation_completed_spy.wait();

      TestEvent1 event(42);
      WindowActionRouter::instance().dispatch_event(&event);

      QVERIFY(wait_for_lua_to_be_true("return _G.event1_called"));
      QVERIFY(wait_for_lua_to_be_true("return _G.event1_called_with.test_data == 42"));
      QVERIFY(wait_for_lua_to_be_true("return not _G.event2_called"));
    }
  }

  void lua_api_view_set_url() {
    describe("web.search.set_url");

    context("when called with a url and view id");
    it("emits url_opened for the given view id") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy url_opened(&lua, &LuaRuntime::url_opened);

      lua.evaluate(R"(
        web.view.set_url("https://updated-url.com", 42)
      )");

      QVERIFY(url_opened.wait());
      QCOMPARE(url_opened.first()[0], "https://updated-url.com");
      QCOMPARE(url_opened.first()[1], OpenType::OpenUrl);
      QCOMPARE(url_opened.first()[2], 42);
    }

    context("when called with a url");
    it("emits url_opened with the url and view id = 0") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy url_opened(&lua, &LuaRuntime::url_opened);

      lua.evaluate(R"(
        web.view.set_url("https://updated-url.com")
      )");

      QVERIFY(url_opened.wait());
      QCOMPARE(url_opened.first()[0], "https://updated-url.com");
      QCOMPARE(url_opened.first()[1], OpenType::OpenUrl);
      QCOMPARE(url_opened.first()[2], 0);
    }

    context("when called without a url");
    it("emits url_opened with empty url and view id = 0") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy url_opened(&lua, &LuaRuntime::url_opened);

      lua.evaluate(R"(
        web.view.set_url()
      )");

      QVERIFY(url_opened.wait());
      QCOMPARE(url_opened.first()[0], "");
      QCOMPARE(url_opened.first()[1], OpenType::OpenUrl);
      QCOMPARE(url_opened.first()[2], 0);
    }
  }

  void lua_api_search_set_text() {
    describe("web.search.set_text");

    context("when called with just the search text");
    it("emits search_requested with search text and view id 0") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy search_requested(&lua, &LuaRuntime::search_requested);

      lua.evaluate("web.search.set_text('search text')");

      QVERIFY(search_requested.wait());
      QCOMPARE(search_requested.first()[0], "search text");
      QCOMPARE(search_requested.first()[1], 0);
    }

    context("when called with search text and view id");
    it("emits search_requested with search text and given view id") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy search_requested(&lua, &LuaRuntime::search_requested);

      lua.evaluate("web.search.set_text('search text', 2)");

      QVERIFY(search_requested.wait());
      QCOMPARE(search_requested.first()[0], "search text");
      QCOMPARE(search_requested.first()[1], 2);
    }
  }

  void lua_api_search_next() {
    describe("web.search.next");

    context("when called without view id");
    it("emits search_next_requested with view id 0") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy search_next_requested(&lua, &LuaRuntime::search_next_requested);

      lua.evaluate("web.search.next()");

      QVERIFY(search_next_requested.wait());
      QCOMPARE(search_next_requested.first().first(), 0);
    }

    context("when called with view id");
    it("emits search_next_requested with given view id") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy search_next_requested(&lua, &LuaRuntime::search_next_requested);

      lua.evaluate("web.search.next(2)");

      QVERIFY(search_next_requested.wait());
      QCOMPARE(search_next_requested.first().first(), 2);
    }
  }

  void lua_api_search_previous() {
    describe("web.search.previous");

    context("when called without view id");
    it("emits search_previous_requested with view id 0") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy search_previous_requested(&lua, &LuaRuntime::search_previous_requested);

      lua.evaluate("web.search.previous()");

      QVERIFY(search_previous_requested.wait());
      QCOMPARE(search_previous_requested.first().first(), 0);
    }

    context("when called with view id");
    it("emits search_previous_requested with given view id") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy search_previous_requested(&lua, &LuaRuntime::search_previous_requested);

      lua.evaluate("web.search.previous(2)");

      QVERIFY(search_previous_requested.wait());
      QCOMPARE(search_previous_requested.first().first(), 2);
    }
  }

  void lua_api_search_get_text() {
    describe("web.search.get_text");

    it("returns the current search text") {
      auto &lua = LuaRuntime::instance();
      auto &router = WindowActionRouter::instance();
      router.set_current_search_text("some search text");
      QSignalSpy evaluation_completed(&lua, &LuaRuntime::evaluation_completed);

      lua.evaluate("return web.search.get_text()");

      QVERIFY(evaluation_completed.wait());
      QCOMPARE(evaluation_completed.first().first(), "some search text");
    }
  }

  void lua_api_view_open_devtools() {
    describe("web.view.open_devtools");

    context("when called with view id");
    it("emits devtools_requested with view id") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy devtools_requested(&lua, &LuaRuntime::devtools_requested);

      lua.evaluate("web.view.open_devtools(42)");

      QVERIFY(devtools_requested.wait());
      QCOMPARE(devtools_requested.first().first(), 42);
    }
  }
};

QTEST_REGISTER(LuaRuntimeApiSpec)
#include "LuaRuntimeApiSpec.moc"
// NOLINTEND