summaryrefslogtreecommitdiffstats
path: root/tests/auto/quick/qmltests/data/tst_settings.qml
blob: f47674aa7fa5fbb78cbfbc53daf986c1f1760925 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

import QtQuick
import QtTest
import QtWebEngine

TestWebEngineView {
    id: webEngineView
    width: 400
    height: 300

    TestCase {
        id: testCase
        name: "WebEngineViewSettings"

        function test_javascriptEnabled() {
            webEngineView.settings.javascriptEnabled = true;

            webEngineView.url = Qt.resolvedUrl("javascript.html");
            verify(webEngineView.waitForLoadSucceeded());
            tryCompare(webEngineView, "title", "New Title");
        }

        function test_javascriptDisabled() {
            webEngineView.settings.javascriptEnabled = false;

            webEngineView.url = Qt.resolvedUrl("javascript.html");
            verify(webEngineView.waitForLoadSucceeded());
            tryCompare(webEngineView, "title", "Original Title");
        }

        function test_localStorageDisabled() {
            webEngineView.settings.javascriptEnabled = true;
            webEngineView.settings.localStorageEnabled = false;

            webEngineView.url = Qt.resolvedUrl("localStorage.html");
            verify(webEngineView.waitForLoadSucceeded());
            tryCompare(webEngineView, "title", "Original Title");
        }

        function test_localStorageEnabled() {
            webEngineView.settings.localStorageEnabled = true;
            webEngineView.settings.javascriptEnabled = true;

            webEngineView.url = Qt.resolvedUrl("localStorage.html");
            verify(webEngineView.waitForLoadSucceeded());
            webEngineView.reload();
            verify(webEngineView.waitForLoadSucceeded());
            tryCompare(webEngineView, "title", "New Title");
        }

        function test_settingsAffectCurrentViewOnly()  {
            var webEngineView2 = Qt.createQmlObject('TestWebEngineView {width: 400; height: 300;}', testCase);

            webEngineView.settings.javascriptEnabled = true;
            webEngineView2.settings.javascriptEnabled = true;

            var testUrl = Qt.resolvedUrl("javascript.html");

            webEngineView.url = testUrl;
            verify(webEngineView.waitForLoadSucceeded());
            webEngineView2.url = testUrl;
            verify(webEngineView2.waitForLoadSucceeded());

            tryCompare(webEngineView, "title", "New Title");
            tryCompare(webEngineView2, "title", "New Title");

            webEngineView.settings.javascriptEnabled = false;

            webEngineView.url = testUrl;
            verify(webEngineView.waitForLoadSucceeded());
            webEngineView2.url = testUrl;
            verify(webEngineView2.waitForLoadSucceeded());

            tryCompare(webEngineView, "title", "Original Title");
            tryCompare(webEngineView2, "title", "New Title");

            webEngineView2.destroy();
        }

        function test_disableReadingFromCanvas_data() {
            return [
                { tag: 'disabled', disableReadingFromCanvas: false, result: true },
                { tag: 'enabled', disableReadingFromCanvas: true, result: false },
            ]
        }

        function test_disableReadingFromCanvas(data) {
            webEngineView.settings.readingFromCanvasEnabled = !data.disableReadingFromCanvas;
            webEngineView.loadHtml("<html><body>" +
                                   "<canvas id='myCanvas' width='200' height='40' style='border:1px solid #000000;'></canvas>" +
                                   "</body></html>");
            verify(webEngineView.waitForLoadSucceeded());
            verify(webEngineView.settings.readingFromCanvasEnabled === !data.disableReadingFromCanvas )

            var jsCode = "(function(){" +
                       "   var canvas = document.getElementById(\"myCanvas\");" +
                       "   var ctx = canvas.getContext(\"2d\");" +
                       "   ctx.fillStyle = \"rgb(255,0,255)\";" +
                       "   ctx.fillRect(0, 0, 200, 40);" +
                       "   try {" +
                       "      src = canvas.toDataURL();" +
                       "   }" +
                       "   catch(err) {" +
                       "      src = \"\";" +
                       "   }" +
                       "   return src.length ? true : false;" +
                       "})();";

            var isDataRead = false;
            runJavaScript(jsCode, function(result) {
                isDataRead = result
            });
            tryVerify(function() { return isDataRead === data.result });
        }

        function test_forceDarkMode() {
            // based on: https://developer.chrome.com/blog/auto-dark-theme/#detecting-auto-dark-theme
            webEngineView.loadHtml("<html><body>" +
                                   "<div id=\"detection\", style=\"display: none; background-color: canvas; color-scheme: light\"</div>" +
                                   "</body></html>");
            const script = "(() => {"
                           + "  const detectionDiv = document.querySelector('#detection');"
                           + "  return getComputedStyle(detectionDiv).backgroundColor != 'rgb(255, 255, 255)';"
                           + "})()";
            verify(webEngineView.waitForLoadSucceeded());

            var isAutoDark = true;
            runJavaScript(script, result => isAutoDark = result);
            tryVerify(() => {return !isAutoDark});

            webEngineView.settings.forceDarkMode = true;
            verify(webEngineView.settings.forceDarkMode == true)

            isAutoDark = false;
            // the page is not updated immediately
            tryVerify(function() {
                runJavaScript(script, result => isAutoDark = result);
                return isAutoDark;
            });
        }
    }
}