| 1 | /* |
| 2 | * Copyright (C) 2015 Red Hat Inc. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 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 "WebProcessTest.h" |
| 23 | |
| 24 | class WebKitWebEditorTest : public WebProcessTest { |
| 25 | public: |
| 26 | static std::unique_ptr<WebProcessTest> create() { return std::unique_ptr<WebProcessTest>(new WebKitWebEditorTest()); } |
| 27 | |
| 28 | private: |
| 29 | static void selectionChangedCallback(bool* selectionChanged) |
| 30 | { |
| 31 | *selectionChanged = true; |
| 32 | } |
| 33 | |
| 34 | bool testSelectionChanged(WebKitWebPage* page) |
| 35 | { |
| 36 | bool selectionChanged = false; |
| 37 | |
| 38 | WebKitWebEditor* editor = webkit_web_page_get_editor(page); |
| 39 | g_assert_true(WEBKIT_IS_WEB_EDITOR(editor)); |
| 40 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(editor)); |
| 41 | g_signal_connect_swapped(editor, "selection-changed" , G_CALLBACK(selectionChangedCallback), &selectionChanged); |
| 42 | |
| 43 | GRefPtr<JSCContext> jsContext = adoptGRef(webkit_frame_get_js_context(webkit_web_page_get_main_frame(page))); |
| 44 | g_assert_true(JSC_IS_CONTEXT(jsContext.get())); |
| 45 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(jsContext.get())); |
| 46 | |
| 47 | static const char* steps[] = { |
| 48 | "document.execCommand('SelectAll')" , |
| 49 | "window.getSelection().collapseToStart()" , |
| 50 | "window.getSelection().modify('move', 'forward', 'character')" , |
| 51 | "window.getSelection().modify('extend', 'forward', 'word')" , |
| 52 | "document.execCommand('Unselect')" , |
| 53 | nullptr |
| 54 | }; |
| 55 | |
| 56 | GRefPtr<JSCValue> result; |
| 57 | unsigned i = 0; |
| 58 | while (const char* command = steps[i++]) { |
| 59 | g_assert_false(selectionChanged); |
| 60 | result = adoptGRef(jsc_context_evaluate(jsContext.get(), command, -1)); |
| 61 | g_assert_true(JSC_IS_VALUE(result.get())); |
| 62 | g_assert_true(selectionChanged); |
| 63 | selectionChanged = false; |
| 64 | } |
| 65 | |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | bool runTest(const char* testName, WebKitWebPage* page) override |
| 70 | { |
| 71 | if (!strcmp(testName, "selection-changed" )) |
| 72 | return testSelectionChanged(page); |
| 73 | |
| 74 | g_assert_not_reached(); |
| 75 | return false; |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | static void __attribute__((constructor)) registerTests() |
| 80 | { |
| 81 | REGISTER_TEST(WebKitWebEditorTest, "WebKitWebEditor/selection-changed" ); |
| 82 | } |
| 83 | |