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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
|
/*
* Copyright (C) 2012 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2,1 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "WebViewTest.h"
#include <wtf/glib/GRefPtr.h>
class Clipboard {
public:
Clipboard(GMainLoop* loop)
: m_mainLoop(loop)
#if USE(GTK4)
, m_clipboard(gdk_display_get_clipboard(gdk_display_get_default()))
#else
, m_clipboard(gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD))
#endif
{
}
void clear()
{
#if USE(GTK4)
g_assert_true(gdk_clipboard_set_content(m_clipboard, nullptr));
#else
gtk_clipboard_clear(m_clipboard);
#endif
m_text = nullptr;
}
bool containsText() const
{
#if USE(GTK4)
return gdk_content_formats_contain_gtype(gdk_clipboard_get_formats(m_clipboard), G_TYPE_STRING);
#else
return gtk_clipboard_wait_is_text_available(m_clipboard);
#endif
}
bool waitForText()
{
// There's no way to know when the selection has been written to
// the clipboard, so use a timeout source to query the clipboard.
static const unsigned kClipboardWaitTimeout = 50;
static const unsigned kClipboardWaitMaxTries = 2;
m_triesCount = 0;
g_timeout_add(kClipboardWaitTimeout, [](gpointer userData) -> gboolean {
auto* clipboard = static_cast<Clipboard*>(userData);
clipboard->m_triesCount++;
if (clipboard->containsText() || clipboard->m_triesCount > kClipboardWaitMaxTries) {
g_main_loop_quit(clipboard->m_mainLoop);
return FALSE;
}
return TRUE;
}, this);
g_main_loop_run(m_mainLoop);
return containsText();
}
const char* readText()
{
m_text = nullptr;
if (waitForText()) {
#if USE(GTK4)
gdk_clipboard_read_text_async(m_clipboard, nullptr, [](GObject* gdkClipboard, GAsyncResult* result, gpointer userData) {
auto* clipboard = static_cast<Clipboard*>(userData);
clipboard->m_text.reset(gdk_clipboard_read_text_finish(GDK_CLIPBOARD(gdkClipboard), result, nullptr));
g_main_loop_quit(clipboard->m_mainLoop);
}, this);
g_main_loop_run(m_mainLoop);
#else
m_text.reset(gtk_clipboard_wait_for_text(m_clipboard));
#endif
}
return m_text.get();
}
private:
GMainLoop* m_mainLoop { nullptr };
#if USE(GTK4)
GdkClipboard* m_clipboard { nullptr };
#else
GtkClipboard* m_clipboard { nullptr };
#endif
GUniquePtr<char> m_text;
unsigned m_triesCount { 0 };
};
class EditorTest: public WebViewTest {
public:
MAKE_GLIB_TEST_FIXTURE(EditorTest);
EditorTest()
: m_clipboard(m_mainLoop)
{
showInWindow();
m_clipboard.clear();
loadURI("about:blank");
waitUntilLoadFinished();
}
~EditorTest()
{
m_clipboard.clear();
}
static gboolean webViewDrawCallback(GMainLoop* mainLoop)
{
g_main_loop_quit(mainLoop);
return FALSE;
}
void flushEditorState()
{
#if USE(GTK4)
auto* surface = gtk_native_get_surface(gtk_widget_get_native(GTK_WIDGET(m_webView)));
auto* clock = gdk_surface_get_frame_clock(surface);
auto signalID = g_signal_connect_swapped(clock, "paint", G_CALLBACK(webViewDrawCallback), m_mainLoop);
#else
auto signalID = g_signal_connect_swapped(m_webView, "draw", G_CALLBACK(webViewDrawCallback), m_mainLoop);
#endif
gtk_widget_queue_draw(GTK_WIDGET(m_webView));
g_main_loop_run(m_mainLoop);
#if USE(GTK4)
g_signal_handler_disconnect(clock, signalID);
#else
g_signal_handler_disconnect(m_webView, signalID);
#endif
}
static void canExecuteEditingCommandReadyCallback(GObject*, GAsyncResult* result, EditorTest* test)
{
GUniqueOutPtr<GError> error;
test->m_canExecuteEditingCommand = webkit_web_view_can_execute_editing_command_finish(test->m_webView, result, &error.outPtr());
g_assert_no_error(error.get());
g_main_loop_quit(test->m_mainLoop);
}
bool canExecuteEditingCommand(const char* command)
{
m_canExecuteEditingCommand = false;
webkit_web_view_can_execute_editing_command(m_webView, command, 0, reinterpret_cast<GAsyncReadyCallback>(canExecuteEditingCommandReadyCallback), this);
g_main_loop_run(m_mainLoop);
if (!strcmp(command, WEBKIT_EDITING_COMMAND_CUT))
g_assert_cmpint(m_canExecuteEditingCommand, ==, webkit_editor_state_is_cut_available(editorState()));
else if (!strcmp(command, WEBKIT_EDITING_COMMAND_COPY))
g_assert_cmpint(m_canExecuteEditingCommand, ==, webkit_editor_state_is_copy_available(editorState()));
else if (!strcmp(command, WEBKIT_EDITING_COMMAND_PASTE) || !strcmp(command, WEBKIT_EDITING_COMMAND_PASTE_AS_PLAIN_TEXT))
g_assert_cmpint(m_canExecuteEditingCommand, ==, webkit_editor_state_is_paste_available(editorState()));
// FIXME: Figure out how to add tests for undo and redo. It will probably require using user
// scripts to message the UI process when the content has been altered.
else if (!strcmp(command, WEBKIT_EDITING_COMMAND_UNDO))
g_assert_cmpint(m_canExecuteEditingCommand, ==, webkit_editor_state_is_undo_available(editorState()));
else if (!strcmp(command, WEBKIT_EDITING_COMMAND_REDO))
g_assert_cmpint(m_canExecuteEditingCommand, ==, webkit_editor_state_is_redo_available(editorState()));
return m_canExecuteEditingCommand;
}
const char* copyClipboard()
{
webkit_web_view_execute_editing_command(m_webView, WEBKIT_EDITING_COMMAND_COPY);
return m_clipboard.readText();
}
const char* cutSelection()
{
g_assert_true(canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_CUT));
g_assert_true(canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_PASTE));
g_assert_true(canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_PASTE_AS_PLAIN_TEXT));
webkit_web_view_execute_editing_command(m_webView, WEBKIT_EDITING_COMMAND_CUT);
return m_clipboard.readText();
}
WebKitEditorState* editorState()
{
if (m_editorState)
return m_editorState;
m_editorState = webkit_web_view_get_editor_state(m_webView);
assertObjectIsDeletedWhenTestFinishes(G_OBJECT(m_editorState));
return m_editorState;
}
static void quitMainLoopInCallback(EditorTest* test)
{
g_main_loop_quit(test->m_mainLoop);
}
unsigned typingAttributes()
{
return webkit_editor_state_get_typing_attributes(editorState());
}
unsigned waitUntilTypingAttributesChanged()
{
unsigned long handlerID = g_signal_connect_swapped(editorState(), "notify::typing-attributes", G_CALLBACK(quitMainLoopInCallback), this);
g_main_loop_run(m_mainLoop);
g_signal_handler_disconnect(m_editorState, handlerID);
return typingAttributes();
}
Clipboard m_clipboard;
bool m_canExecuteEditingCommand { false };
WebKitEditorState* m_editorState { nullptr };
};
static const char* selectedSpanHTMLFormat =
"<html><body contentEditable=\"%s\">"
"<span id=\"mainspan\">All work and no play <span id=\"subspan\">make Jack a dull</span> boy.</span>"
"<script>document.getSelection().removeAllRanges();\n"
"document.getSelection().selectAllChildren(document.getElementById('subspan'));\n"
"</script></body></html>";
static void testWebViewEditorCutCopyPasteNonEditable(EditorTest* test, gconstpointer)
{
// Nothing loaded yet.
g_assert_false(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_CUT));
g_assert_false(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_COPY));
g_assert_false(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_PASTE));
g_assert_false(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_PASTE_AS_PLAIN_TEXT));
GUniquePtr<char> selectedSpanHTML(g_strdup_printf(selectedSpanHTMLFormat, "false"));
test->loadHtml(selectedSpanHTML.get(), nullptr);
test->waitUntilLoadFinished();
test->flushEditorState();
g_assert_true(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_COPY));
// It's not possible to cut and paste when content is not editable
// even if there's a selection.
g_assert_false(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_CUT));
g_assert_false(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_PASTE));
g_assert_false(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_PASTE_AS_PLAIN_TEXT));
g_assert_cmpstr(test->copyClipboard(), ==, "make Jack a dull");
}
static void testWebViewEditorCutCopyPasteEditable(EditorTest* test, gconstpointer)
{
// Nothing loaded yet.
g_assert_false(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_CUT));
g_assert_false(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_COPY));
g_assert_false(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_PASTE));
g_assert_false(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_PASTE_AS_PLAIN_TEXT));
g_assert_false(test->isEditable());
test->setEditable(true);
g_assert_true(test->isEditable());
GUniquePtr<char> selectedSpanHTML(g_strdup_printf(selectedSpanHTMLFormat, "false"));
test->loadHtml(selectedSpanHTML.get(), nullptr);
test->waitUntilLoadFinished();
test->flushEditorState();
// There's a selection.
g_assert_true(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_CUT));
g_assert_true(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_COPY));
g_assert_true(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_PASTE));
g_assert_true(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_PASTE_AS_PLAIN_TEXT));
g_assert_cmpstr(test->copyClipboard(), ==, "make Jack a dull");
}
static void testWebViewEditorSelectAllNonEditable(EditorTest* test, gconstpointer)
{
g_assert_true(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_SELECT_ALL));
GUniquePtr<char> selectedSpanHTML(g_strdup_printf(selectedSpanHTMLFormat, "false"));
test->loadHtml(selectedSpanHTML.get(), nullptr);
test->waitUntilLoadFinished();
test->flushEditorState();
g_assert_true(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_SELECT_ALL));
// Initially only the subspan is selected.
g_assert_cmpstr(test->copyClipboard(), ==, "make Jack a dull");
webkit_web_view_execute_editing_command(test->m_webView, WEBKIT_EDITING_COMMAND_SELECT_ALL);
// The mainspan should be selected after calling SELECT_ALL.
g_assert_cmpstr(test->copyClipboard(), ==, "All work and no play make Jack a dull boy.");
}
static void testWebViewEditorSelectAllEditable(EditorTest* test, gconstpointer)
{
g_assert_true(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_SELECT_ALL));
g_assert_false(test->isEditable());
test->setEditable(true);
g_assert_true(test->isEditable());
GUniquePtr<char> selectedSpanHTML(g_strdup_printf(selectedSpanHTMLFormat, "false"));
test->loadHtml(selectedSpanHTML.get(), nullptr);
test->waitUntilLoadFinished();
test->flushEditorState();
g_assert_true(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_SELECT_ALL));
// Initially only the subspan is selected.
g_assert_cmpstr(test->copyClipboard(), ==, "make Jack a dull");
webkit_web_view_execute_editing_command(test->m_webView, WEBKIT_EDITING_COMMAND_SELECT_ALL);
// The mainspan should be selected after calling SELECT_ALL.
g_assert_cmpstr(test->copyClipboard(), ==, "All work and no play make Jack a dull boy.");
}
static void loadContentsAndTryToCutSelection(EditorTest* test, bool contentEditable)
{
// View is not editable by default.
g_assert_false(test->isEditable());
GUniquePtr<char> selectedSpanHTML(g_strdup_printf(selectedSpanHTMLFormat, contentEditable ? "true" : "false"));
test->loadHtml(selectedSpanHTML.get(), nullptr);
test->waitUntilLoadFinished();
test->flushEditorState();
g_assert_false(test->isEditable());
test->setEditable(true);
g_assert_true(test->isEditable());
test->flushEditorState();
// Cut the selection to the clipboard to see if the view is indeed editable.
g_assert_cmpstr(test->cutSelection(), ==, "make Jack a dull");
// Reset the editable for next test.
test->setEditable(false);
g_assert_false(test->isEditable());
}
static void testWebViewEditorNonEditable(EditorTest* test)
{
GUniquePtr<char> selectedSpanHTML(g_strdup_printf(selectedSpanHTMLFormat, "false"));
test->loadHtml(selectedSpanHTML.get(), nullptr);
test->waitUntilLoadFinished();
test->flushEditorState();
g_assert_false(test->isEditable());
test->setEditable(true);
g_assert_true(test->isEditable());
test->setEditable(false);
g_assert_false(test->isEditable());
// Check if view is indeed non-editable.
g_assert_false(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_CUT));
g_assert_false(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_PASTE));
g_assert_false(test->canExecuteEditingCommand(WEBKIT_EDITING_COMMAND_PASTE_AS_PLAIN_TEXT));
}
static void testWebViewEditorEditable(EditorTest* test, gconstpointer)
{
testWebViewEditorNonEditable(test);
// Reset the editable for next test.
test->setEditable(false);
g_assert_false(test->isEditable());
loadContentsAndTryToCutSelection(test, true);
// Reset the editable for next test.
test->setEditable(false);
g_assert_false(test->isEditable());
loadContentsAndTryToCutSelection(test, false);
}
static void testWebViewEditorEditorStateTypingAttributes(EditorTest* test, gconstpointer)
{
static const char* typingAttributesHTML =
"<html><body>"
"normal <b>bold </b><i>italic </i><u>underline </u><strike>strike </strike>"
"<b><i>boldanditalic </i></b>"
"</body></html>";
test->loadHtml(typingAttributesHTML, nullptr);
test->waitUntilLoadFinished();
test->flushEditorState();
test->setEditable(true);
unsigned typingAttributes = test->typingAttributes();
g_assert_cmpuint(typingAttributes, ==, WEBKIT_EDITOR_TYPING_ATTRIBUTE_NONE);
webkit_web_view_execute_editing_command(test->m_webView, "MoveWordForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
typingAttributes = test->waitUntilTypingAttributesChanged();
g_assert_true(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_BOLD);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_ITALIC);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_UNDERLINE);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_STRIKETHROUGH);
webkit_web_view_execute_editing_command(test->m_webView, "MoveWordForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
typingAttributes = test->waitUntilTypingAttributesChanged();
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_BOLD);
g_assert_true(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_ITALIC);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_UNDERLINE);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_STRIKETHROUGH);
webkit_web_view_execute_editing_command(test->m_webView, "MoveWordForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
typingAttributes = test->waitUntilTypingAttributesChanged();
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_BOLD);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_ITALIC);
g_assert_true(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_UNDERLINE);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_STRIKETHROUGH);
webkit_web_view_execute_editing_command(test->m_webView, "MoveWordForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
typingAttributes = test->waitUntilTypingAttributesChanged();
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_BOLD);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_ITALIC);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_UNDERLINE);
g_assert_true(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_STRIKETHROUGH);
webkit_web_view_execute_editing_command(test->m_webView, "MoveWordForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
typingAttributes = test->waitUntilTypingAttributesChanged();
g_assert_true(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_BOLD);
g_assert_true(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_ITALIC);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_UNDERLINE);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_STRIKETHROUGH);
// Selections.
webkit_web_view_execute_editing_command(test->m_webView, "MoveToBeginningOfDocument");
webkit_web_view_execute_editing_command(test->m_webView, "MoveWordForwardAndModifySelection");
typingAttributes = test->waitUntilTypingAttributesChanged();
g_assert_cmpuint(typingAttributes, ==, WEBKIT_EDITOR_TYPING_ATTRIBUTE_NONE);
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveWordForwardAndModifySelection");
typingAttributes = test->waitUntilTypingAttributesChanged();
g_assert_true(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_BOLD);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_ITALIC);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_UNDERLINE);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_STRIKETHROUGH);
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveWordForwardAndModifySelection");
typingAttributes = test->waitUntilTypingAttributesChanged();
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_BOLD);
g_assert_true(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_ITALIC);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_UNDERLINE);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_STRIKETHROUGH);
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveWordForwardAndModifySelection");
typingAttributes = test->waitUntilTypingAttributesChanged();
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_BOLD);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_ITALIC);
g_assert_true(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_UNDERLINE);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_STRIKETHROUGH);
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveWordForwardAndModifySelection");
typingAttributes = test->waitUntilTypingAttributesChanged();
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_BOLD);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_ITALIC);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_UNDERLINE);
g_assert_true(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_STRIKETHROUGH);
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveForward");
webkit_web_view_execute_editing_command(test->m_webView, "MoveWordForwardAndModifySelection");
typingAttributes = test->waitUntilTypingAttributesChanged();
g_assert_true(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_BOLD);
g_assert_true(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_ITALIC);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_UNDERLINE);
g_assert_false(typingAttributes & WEBKIT_EDITOR_TYPING_ATTRIBUTE_STRIKETHROUGH);
webkit_web_view_execute_editing_command(test->m_webView, "SelectAll");
typingAttributes = test->waitUntilTypingAttributesChanged();
g_assert_cmpuint(typingAttributes, ==, WEBKIT_EDITOR_TYPING_ATTRIBUTE_NONE);
}
static void testWebViewEditorInsertImage(EditorTest* test, gconstpointer)
{
test->loadHtml("<html><body></body></html>", "file:///");
test->waitUntilLoadFinished();
test->flushEditorState();
test->setEditable(true);
GUniquePtr<char> imagePath(g_build_filename(Test::getResourcesDir().data(), "blank.ico", nullptr));
GUniquePtr<char> imageURI(g_filename_to_uri(imagePath.get(), nullptr, nullptr));
webkit_web_view_execute_editing_command_with_argument(test->m_webView, WEBKIT_EDITING_COMMAND_INSERT_IMAGE, imageURI.get());
GUniqueOutPtr<GError> error;
WebKitJavascriptResult* javascriptResult = test->runJavaScriptAndWaitUntilFinished("document.getElementsByTagName('IMG')[0].src", &error.outPtr());
g_assert_nonnull(javascriptResult);
g_assert_no_error(error.get());
GUniquePtr<char> resultString(WebViewTest::javascriptResultToCString(javascriptResult));
g_assert_cmpstr(resultString.get(), ==, imageURI.get());
}
static void testWebViewEditorCreateLink(EditorTest* test, gconstpointer)
{
test->loadHtml("<html><body onload=\"document.getSelection().selectAllChildren(document.body);\">webkitgtk.org</body></html>", nullptr);
test->waitUntilLoadFinished();
test->flushEditorState();
test->setEditable(true);
static const char* webkitGTKURL = "http://www.webkitgtk.org/";
webkit_web_view_execute_editing_command_with_argument(test->m_webView, WEBKIT_EDITING_COMMAND_CREATE_LINK, webkitGTKURL);
GUniqueOutPtr<GError> error;
WebKitJavascriptResult* javascriptResult = test->runJavaScriptAndWaitUntilFinished("document.getElementsByTagName('A')[0].href;", &error.outPtr());
g_assert_nonnull(javascriptResult);
g_assert_no_error(error.get());
GUniquePtr<char> resultString(WebViewTest::javascriptResultToCString(javascriptResult));
g_assert_cmpstr(resultString.get(), ==, webkitGTKURL);
javascriptResult = test->runJavaScriptAndWaitUntilFinished("document.getElementsByTagName('A')[0].innerText;", &error.outPtr());
g_assert_nonnull(javascriptResult);
g_assert_no_error(error.get());
resultString.reset(WebViewTest::javascriptResultToCString(javascriptResult));
g_assert_cmpstr(resultString.get(), ==, "webkitgtk.org");
// When there isn't text selected, the URL is used as link text.
webkit_web_view_execute_editing_command(test->m_webView, "MoveToEndOfLine");
webkit_web_view_execute_editing_command_with_argument(test->m_webView, WEBKIT_EDITING_COMMAND_CREATE_LINK, webkitGTKURL);
javascriptResult = test->runJavaScriptAndWaitUntilFinished("document.getElementsByTagName('A')[1].href;", &error.outPtr());
g_assert_nonnull(javascriptResult);
g_assert_no_error(error.get());
resultString.reset(WebViewTest::javascriptResultToCString(javascriptResult));
g_assert_cmpstr(resultString.get(), ==, webkitGTKURL);
javascriptResult = test->runJavaScriptAndWaitUntilFinished("document.getElementsByTagName('A')[1].innerText;", &error.outPtr());
g_assert_nonnull(javascriptResult);
g_assert_no_error(error.get());
resultString.reset(WebViewTest::javascriptResultToCString(javascriptResult));
g_assert_cmpstr(resultString.get(), ==, webkitGTKURL);
}
void beforeAll()
{
EditorTest::add("WebKitWebView", "editable/editable", testWebViewEditorEditable);
EditorTest::add("WebKitWebView", "cut-copy-paste/non-editable", testWebViewEditorCutCopyPasteNonEditable);
EditorTest::add("WebKitWebView", "cut-copy-paste/editable", testWebViewEditorCutCopyPasteEditable);
EditorTest::add("WebKitWebView", "select-all/non-editable", testWebViewEditorSelectAllNonEditable);
EditorTest::add("WebKitWebView", "select-all/editable", testWebViewEditorSelectAllEditable);
EditorTest::add("WebKitWebView", "editor-state/typing-attributes", testWebViewEditorEditorStateTypingAttributes);
EditorTest::add("WebKitWebView", "insert/image", testWebViewEditorInsertImage);
EditorTest::add("WebKitWebView", "insert/link", testWebViewEditorCreateLink);
}
void afterAll()
{
}
|