summaryrefslogtreecommitdiff
path: root/Tools/TestWebKitAPI/Tests/WebKit2Gtk/EditorTest.cpp
blob: 87334427186c2e0b7477e1b0f2a9b27613ab7eab (plain)
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
/*
 * Copyright (C) 2015 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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 "WebProcessTest.h"
#include <webkit2/webkit-web-extension.h>

#define WEBKIT_DOM_USE_UNSTABLE_API
#include <webkitdom/WebKitDOMDOMSelection.h>
#include <webkitdom/WebKitDOMDOMWindowUnstable.h>

class WebKitWebEditorTest : public WebProcessTest {
public:
    static std::unique_ptr<WebProcessTest> create() { return std::unique_ptr<WebProcessTest>(new WebKitWebEditorTest()); }

private:
    static void selectionChangedCallback(bool* selectionChanged)
    {
        *selectionChanged = true;
    }

    void testSelectionSelectAll(WebKitWebPage* page)
    {
        WebKitDOMDocument* document = webkit_web_page_get_dom_document(page);
        g_assert(WEBKIT_DOM_IS_DOCUMENT(document));

        webkit_dom_document_exec_command(document, "SelectAll", false, "");
    }

    void testSelectionCollapse(WebKitWebPage* page)
    {
        WebKitDOMDocument* document = webkit_web_page_get_dom_document(page);
        g_assert(WEBKIT_DOM_IS_DOCUMENT(document));
        GRefPtr<WebKitDOMDOMWindow> domWindow = adoptGRef(webkit_dom_document_get_default_view(document));
        g_assert(WEBKIT_DOM_IS_DOM_WINDOW(domWindow.get()));
        GRefPtr<WebKitDOMDOMSelection> domSelection = adoptGRef(webkit_dom_dom_window_get_selection(domWindow.get()));
        g_assert(WEBKIT_DOM_IS_DOM_SELECTION(domSelection.get()));

        webkit_dom_dom_selection_collapse_to_start(domSelection.get(), nullptr);
    }

    void testSelectionModifyMove(WebKitWebPage* page)
    {
        WebKitDOMDocument* document = webkit_web_page_get_dom_document(page);
        g_assert(WEBKIT_DOM_IS_DOCUMENT(document));
        GRefPtr<WebKitDOMDOMWindow> domWindow = adoptGRef(webkit_dom_document_get_default_view(document));
        g_assert(WEBKIT_DOM_IS_DOM_WINDOW(domWindow.get()));
        GRefPtr<WebKitDOMDOMSelection> domSelection = adoptGRef(webkit_dom_dom_window_get_selection(domWindow.get()));
        g_assert(WEBKIT_DOM_IS_DOM_SELECTION(domSelection.get()));

        webkit_dom_dom_selection_modify(domSelection.get(), "move", "forward", "character");
    }

    void testSelectionModifyExtend(WebKitWebPage* page)
    {
        WebKitDOMDocument* document = webkit_web_page_get_dom_document(page);
        g_assert(WEBKIT_DOM_IS_DOCUMENT(document));
        GRefPtr<WebKitDOMDOMWindow> domWindow = adoptGRef(webkit_dom_document_get_default_view(document));
        g_assert(WEBKIT_DOM_IS_DOM_WINDOW(domWindow.get()));
        GRefPtr<WebKitDOMDOMSelection> domSelection = adoptGRef(webkit_dom_dom_window_get_selection(domWindow.get()));
        g_assert(WEBKIT_DOM_IS_DOM_SELECTION(domSelection.get()));

        webkit_dom_dom_selection_modify(domSelection.get(), "extend", "forward", "word");
    }

    void testSelectionUnselect(WebKitWebPage* page)
    {
        WebKitDOMDocument* document = webkit_web_page_get_dom_document(page);
        g_assert(WEBKIT_DOM_IS_DOCUMENT(document));

        webkit_dom_document_exec_command(document, "Unselect", false, "");
    }

    bool runTest(const char* testName, WebKitWebPage* page) override
    {
        if (!strcmp(testName, "selection-changed")) {
            bool selectionChanged = false;

            WebKitWebEditor* editor = webkit_web_page_get_editor(page);
            g_assert(WEBKIT_IS_WEB_EDITOR(editor));
            assertObjectIsDeletedWhenTestFinishes(G_OBJECT(editor));
            g_signal_connect_swapped(editor, "selection-changed", G_CALLBACK(selectionChangedCallback), &selectionChanged);

            testSelectionSelectAll(page);
            g_assert(selectionChanged);

            selectionChanged = false;
            testSelectionCollapse(page);
            g_assert(selectionChanged);

            selectionChanged = false;
            testSelectionModifyMove(page);
            g_assert(selectionChanged);

            selectionChanged = false;
            testSelectionModifyExtend(page);
            g_assert(selectionChanged);

            selectionChanged = false;
            testSelectionUnselect(page);
            g_assert(selectionChanged);

            return true;
        }

        g_assert_not_reached();
        return false;
    }
};

static void __attribute__((constructor)) registerTests()
{
    REGISTER_TEST(WebKitWebEditorTest, "WebKitWebEditor/selection-changed");
}