1/*
2 * Copyright (C) 2012, 2017 Igalia S.L.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2,1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21
22#include "LoadTrackingTest.h"
23#include <wtf/glib/GRefPtr.h>
24
25static const char* testString = "<html><body>first testing second testing secondHalf</body></html>";
26
27class FindControllerTest: public WebViewTest {
28public:
29 MAKE_GLIB_TEST_FIXTURE(FindControllerTest);
30
31 FindControllerTest()
32 : m_findController(webkit_web_view_get_find_controller(m_webView))
33 , m_runFindUntilCompletion(false)
34 {
35 assertObjectIsDeletedWhenTestFinishes(G_OBJECT(m_findController.get()));
36 }
37
38 ~FindControllerTest()
39 {
40 if (m_findController)
41 g_signal_handlers_disconnect_matched(m_findController.get(), G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, this);
42 }
43
44 void find(const char* searchText, guint32 findOptions, guint maxMatchCount)
45 {
46 g_signal_connect(m_findController.get(), "found-text", G_CALLBACK(foundTextCallback), this);
47 g_signal_connect(m_findController.get(), "failed-to-find-text", G_CALLBACK(failedToFindTextCallback), this);
48 webkit_find_controller_search(m_findController.get(), searchText, findOptions, maxMatchCount);
49 }
50
51 void count(const char* searchText, guint32 findOptions, guint maxMatchCount)
52 {
53 g_signal_connect(m_findController.get(), "counted-matches", G_CALLBACK(countedMatchesCallback), this);
54 webkit_find_controller_count_matches(m_findController.get(), searchText, findOptions, maxMatchCount);
55 }
56
57 void waitUntilFindFinished()
58 {
59 m_runFindUntilCompletion = true;
60 g_main_loop_run(m_mainLoop);
61 }
62
63 GRefPtr<WebKitFindController> m_findController;
64 bool m_textFound;
65 unsigned m_matchCount;
66
67private:
68 bool m_runFindUntilCompletion;
69
70 static void foundTextCallback(WebKitFindController*, guint matchCount, FindControllerTest* test)
71 {
72 test->m_textFound = true;
73 test->m_matchCount = matchCount;
74 if (test->m_runFindUntilCompletion)
75 g_main_loop_quit(test->m_mainLoop);
76 }
77
78 static void failedToFindTextCallback(WebKitFindController*, FindControllerTest* test)
79 {
80 test->m_textFound = false;
81 if (test->m_runFindUntilCompletion)
82 g_main_loop_quit(test->m_mainLoop);
83 }
84
85 static void countedMatchesCallback(WebKitFindController*, guint matchCount, FindControllerTest* test)
86 {
87 test->m_matchCount = matchCount;
88 if (test->m_runFindUntilCompletion)
89 g_main_loop_quit(test->m_mainLoop);
90 }
91};
92
93static void testFindControllerTextFound(FindControllerTest* test, gconstpointer)
94{
95 test->loadHtml(testString, 0);
96 test->waitUntilLoadFinished();
97
98 test->find("testing", WEBKIT_FIND_OPTIONS_NONE, 1);
99 test->waitUntilFindFinished();
100
101 g_assert_true(test->m_textFound);
102}
103
104static void testFindControllerTextNotFound(FindControllerTest* test, gconstpointer)
105{
106 test->loadHtml(testString, 0);
107 test->waitUntilLoadFinished();
108
109 test->find("notFound", WEBKIT_FIND_OPTIONS_NONE, 1);
110 test->waitUntilFindFinished();
111
112 g_assert_false(test->m_textFound);
113}
114
115static void testFindControllerMatchCount(FindControllerTest* test, gconstpointer)
116{
117 test->loadHtml(testString, 0);
118 test->waitUntilLoadFinished();
119
120 test->find("testing", WEBKIT_FIND_OPTIONS_NONE, 2);
121 test->waitUntilFindFinished();
122
123 g_assert_cmpuint(test->m_matchCount, ==, 2);
124 g_assert_true(test->m_textFound);
125}
126
127static void testFindControllerMaxMatchCount(FindControllerTest* test, gconstpointer)
128{
129 test->loadHtml(testString, 0);
130 test->waitUntilLoadFinished();
131
132 test->find("testing", WEBKIT_FIND_OPTIONS_NONE, 1);
133 test->waitUntilFindFinished();
134
135 g_assert_cmpuint(test->m_matchCount, ==, G_MAXUINT);
136 g_assert_true(test->m_textFound);
137}
138
139static void testFindControllerNext(FindControllerTest* test, gconstpointer)
140{
141 test->loadHtml(testString, 0);
142 test->waitUntilLoadFinished();
143
144 test->find("testing", WEBKIT_FIND_OPTIONS_NONE, 2);
145 test->waitUntilFindFinished();
146
147 g_assert_true(test->m_textFound);
148 g_assert_cmpuint(test->m_matchCount, ==, 2);
149
150 webkit_find_controller_search_next(test->m_findController.get());
151 test->waitUntilFindFinished();
152
153 g_assert_true(test->m_textFound);
154 g_assert_cmpuint(test->m_matchCount, ==, 1);
155 g_assert_false(webkit_find_controller_get_options(test->m_findController.get()) & WEBKIT_FIND_OPTIONS_BACKWARDS);
156
157 webkit_find_controller_search_next(test->m_findController.get());
158 test->waitUntilFindFinished();
159
160 g_assert_false(test->m_textFound);
161 g_assert_cmpuint(test->m_matchCount, ==, 1);
162 g_assert_false(webkit_find_controller_get_options(test->m_findController.get()) & WEBKIT_FIND_OPTIONS_BACKWARDS);
163}
164
165static void testFindControllerPrevious(FindControllerTest* test, gconstpointer)
166{
167 test->loadHtml(testString, 0);
168 test->waitUntilLoadFinished();
169
170 test->find("testing", WEBKIT_FIND_OPTIONS_NONE, 2);
171 test->waitUntilFindFinished();
172
173 g_assert_cmpuint(test->m_matchCount, ==, 2);
174 g_assert_true(test->m_textFound);
175
176 webkit_find_controller_search_next(test->m_findController.get());
177 test->waitUntilFindFinished();
178
179 g_assert_true(test->m_textFound);
180 g_assert_cmpuint(test->m_matchCount, ==, 1);
181 g_assert_false(webkit_find_controller_get_options(test->m_findController.get()) & WEBKIT_FIND_OPTIONS_BACKWARDS);
182
183 webkit_find_controller_search_previous(test->m_findController.get());
184 test->waitUntilFindFinished();
185
186 g_assert_true(test->m_textFound);
187 g_assert_cmpuint(test->m_matchCount, ==, 1);
188 g_assert_true(webkit_find_controller_get_options(test->m_findController.get()) & WEBKIT_FIND_OPTIONS_BACKWARDS);
189}
190
191static void testFindControllerCountedMatches(FindControllerTest* test, gconstpointer)
192{
193 test->loadHtml(testString, 0);
194 test->waitUntilLoadFinished();
195
196 test->count("testing", WEBKIT_FIND_OPTIONS_NONE, 2);
197 test->waitUntilFindFinished();
198
199 g_assert_cmpuint(test->m_matchCount, ==, 2);
200
201 test->count("first", WEBKIT_FIND_OPTIONS_NONE, 2);
202 test->waitUntilFindFinished();
203
204 g_assert_cmpuint(test->m_matchCount, ==, 1);
205
206 test->count("notFound", WEBKIT_FIND_OPTIONS_NONE, 2);
207 test->waitUntilFindFinished();
208
209 g_assert_cmpuint(test->m_matchCount, ==, 0);
210}
211
212static void testFindControllerOptions(FindControllerTest* test, gconstpointer)
213{
214 test->loadHtml(testString, 0);
215 test->waitUntilLoadFinished();
216
217 test->find("Testing", WEBKIT_FIND_OPTIONS_NONE, 2);
218 test->waitUntilFindFinished();
219
220 g_assert_false(test->m_textFound);
221
222 test->find("Testing", WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE, 2);
223 test->waitUntilFindFinished();
224
225 g_assert_true(test->m_textFound);
226
227 test->find("esting", WEBKIT_FIND_OPTIONS_NONE, 2);
228 test->waitUntilFindFinished();
229
230 g_assert_true(test->m_textFound);
231
232 test->find("esting", WEBKIT_FIND_OPTIONS_AT_WORD_STARTS, 2);
233 test->waitUntilFindFinished();
234
235 g_assert_false(test->m_textFound);
236
237 test->find("Half", WEBKIT_FIND_OPTIONS_AT_WORD_STARTS, 2);
238 test->waitUntilFindFinished();
239
240 g_assert_false(test->m_textFound);
241
242 test->find("Half", WEBKIT_FIND_OPTIONS_AT_WORD_STARTS | WEBKIT_FIND_OPTIONS_TREAT_MEDIAL_CAPITAL_AS_WORD_START, 2);
243 test->waitUntilFindFinished();
244
245 g_assert_true(test->m_textFound);
246
247 test->find("testing", WEBKIT_FIND_OPTIONS_WRAP_AROUND, 3);
248 test->waitUntilFindFinished();
249 g_assert_true(test->m_textFound);
250
251 webkit_find_controller_search_next(test->m_findController.get());
252 test->waitUntilFindFinished();
253 g_assert_true(test->m_textFound);
254
255 webkit_find_controller_search_next(test->m_findController.get());
256 test->waitUntilFindFinished();
257 g_assert_true(test->m_textFound);
258}
259
260// TODO: Rewrite this test to avoid using snapshots so it can be re-enabled
261// for WPE or, alternatively, make snapshots work for WPE as well.
262#if PLATFORM(GTK)
263static void testFindControllerHide(FindControllerTest* test, gconstpointer)
264{
265 test->loadHtml(testString, 0);
266 test->waitUntilLoadFinished();
267
268 test->showInWindowAndWaitUntilMapped();
269
270 cairo_surface_t* originalSurface = cairo_surface_reference(
271 test->getSnapshotAndWaitUntilReady(WEBKIT_SNAPSHOT_REGION_FULL_DOCUMENT, WEBKIT_SNAPSHOT_OPTIONS_NONE));
272 g_assert_nonnull(originalSurface);
273
274 test->find("testing", WEBKIT_FIND_OPTIONS_NONE, 1);
275 test->waitUntilFindFinished();
276 g_assert_true(test->m_textFound);
277
278 cairo_surface_t* highlightSurface = cairo_surface_reference(
279 test->getSnapshotAndWaitUntilReady(WEBKIT_SNAPSHOT_REGION_FULL_DOCUMENT, WEBKIT_SNAPSHOT_OPTIONS_NONE));
280 g_assert_nonnull(highlightSurface);
281 g_assert_false(Test::cairoSurfacesEqual(originalSurface, highlightSurface));
282
283 WebKitFindController* findController = webkit_web_view_get_find_controller(test->m_webView);
284 webkit_find_controller_search_finish(findController);
285 webkit_web_view_execute_editing_command(test->m_webView, "Unselect");
286
287 cairo_surface_t* unhighlightSurface = cairo_surface_reference(
288 test->getSnapshotAndWaitUntilReady(WEBKIT_SNAPSHOT_REGION_FULL_DOCUMENT, WEBKIT_SNAPSHOT_OPTIONS_NONE));
289 g_assert_nonnull(unhighlightSurface);
290 g_assert_true(Test::cairoSurfacesEqual(originalSurface, unhighlightSurface));
291
292 cairo_surface_destroy(originalSurface);
293 cairo_surface_destroy(highlightSurface);
294 cairo_surface_destroy(unhighlightSurface);
295}
296#endif
297
298static void testFindControllerInstance(FindControllerTest* test, gconstpointer)
299{
300 WebKitFindController* findController1 = webkit_web_view_get_find_controller(test->m_webView);
301 WebKitFindController* findController2 = webkit_web_view_get_find_controller(test->m_webView);
302
303 g_assert_true(findController1 == findController2);
304}
305
306static void testFindControllerGetters(FindControllerTest* test, gconstpointer)
307{
308 const char* searchText = "testing";
309 guint maxMatchCount = 1;
310 guint32 findOptions = WEBKIT_FIND_OPTIONS_WRAP_AROUND | WEBKIT_FIND_OPTIONS_AT_WORD_STARTS;
311 WebKitFindController* findController = webkit_web_view_get_find_controller(test->m_webView);
312
313 webkit_find_controller_search(findController, searchText, findOptions, maxMatchCount);
314 g_assert_true(webkit_find_controller_get_web_view(findController) == test->m_webView);
315 g_assert_cmpstr(webkit_find_controller_get_search_text(findController), ==, searchText);
316 g_assert_cmpuint(webkit_find_controller_get_max_match_count(findController), ==, maxMatchCount);
317 g_assert_cmpuint(webkit_find_controller_get_options(findController), ==, findOptions);
318}
319
320void beforeAll()
321{
322 FindControllerTest::add("WebKitFindController", "getters", testFindControllerGetters);
323 FindControllerTest::add("WebKitFindController", "instance", testFindControllerInstance);
324 FindControllerTest::add("WebKitFindController", "text-found", testFindControllerTextFound);
325 FindControllerTest::add("WebKitFindController", "text-not-found", testFindControllerTextNotFound);
326 FindControllerTest::add("WebKitFindController", "match-count", testFindControllerMatchCount);
327 FindControllerTest::add("WebKitFindController", "max-match-count", testFindControllerMaxMatchCount);
328 FindControllerTest::add("WebKitFindController", "next", testFindControllerNext);
329 FindControllerTest::add("WebKitFindController", "previous", testFindControllerPrevious);
330 FindControllerTest::add("WebKitFindController", "counted-matches", testFindControllerCountedMatches);
331 FindControllerTest::add("WebKitFindController", "options", testFindControllerOptions);
332#if PLATFORM(GTK)
333 FindControllerTest::add("WebKitFindController", "hide", testFindControllerHide);
334#endif
335}
336
337void afterAll()
338{
339}
340