~osomon/oxide/i18n

« back to all changes in this revision

Viewing changes to qt/tests/qmltests/api/tst_NavigationRequest.qml

  • Committer: Olivier Tilloy
  • Date: 2014-04-08 10:03:11 UTC
  • mfrom: (312.2.173 oxide)
  • Revision ID: olivier.tilloy@canonical.com-20140408100311-b3zb7q1jfrevbrf1
Merge the latest changes from trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import QtQuick 2.0
 
2
import QtTest 1.0
 
3
import com.canonical.Oxide 1.0
 
4
import com.canonical.Oxide.Testing 1.0
 
5
 
 
6
// FIXME: Test navigations in a subwindow (ie, one opened with window.open() and with
 
7
// window.opener pointing to its parent - we should only get onNavigationRequest
 
8
// notifications for cross-domain navigations as opposed to all navigations)
 
9
 
 
10
TestWebView {
 
11
  id: webView
 
12
  width: 200
 
13
  height: 200
 
14
 
 
15
  property string lastRequestUrl: ""
 
16
  property int lastRequestDisposition: NavigationRequest.DispositionCurrentTab
 
17
  property bool lastRequestUserGesture: false
 
18
 
 
19
  property bool shouldReject: false
 
20
 
 
21
  onNavigationRequested: {
 
22
    if (shouldReject) {
 
23
      request.action = NavigationRequest.ActionReject;
 
24
      return;
 
25
    }
 
26
 
 
27
    lastRequestUrl = request.url;
 
28
    lastRequestDisposition = request.disposition;
 
29
    lastRequestUserGesture = request.userGesture;
 
30
  }
 
31
 
 
32
  // XXX(chrisccoulson): If we don't do this, then all future calls to
 
33
  // onNewViewRequested fail (we get a null request object)
 
34
  onNewViewRequested: {
 
35
    var r = request;
 
36
  }
 
37
 
 
38
  SignalSpy {
 
39
    id: spy
 
40
    target: webView
 
41
    signalName: "navigationRequested"
 
42
  }
 
43
 
 
44
  SignalSpy {
 
45
    id: newViewSpy
 
46
    target: webView
 
47
    signalName: "newViewRequested"
 
48
  }
 
49
 
 
50
  SignalSpy {
 
51
    id: frameSpy
 
52
    signalName: "urlChanged"
 
53
  }
 
54
 
 
55
  TestCase {
 
56
    id: test
 
57
    name: "NavigationRequest"
 
58
    when: windowShown
 
59
 
 
60
    function init() {
 
61
      webView.url = "about:blank";
 
62
      verify(webView.waitForLoadSucceeded(),
 
63
             "Timed out waiting for successful load");
 
64
      spy.clear();
 
65
      newViewSpy.clear();
 
66
      frameSpy.clear();
 
67
      webView.shouldReject = false;
 
68
      webView.context.popupBlockerEnabled = true;
 
69
    }
 
70
 
 
71
    function test_NavigationRequest1_from_user_gestures_data() {
 
72
      return [
 
73
        { link: "#link1", url: "http://localhost:8080/empty.html", modifiers: Qt.NoModifier, disposition: NavigationRequest.DispositionCurrentTab, current: true },
 
74
        { link: "#link1", url: "http://localhost:8080/empty.html", modifiers: Qt.ShiftModifier, disposition: NavigationRequest.DispositionNewWindow, current: true },
 
75
        { link: "#link1", url: "http://localhost:8080/empty.html", modifiers: Qt.ControlModifier, disposition: NavigationRequest.DispositionNewBackgroundTab, current: true },
 
76
        { link: "#link1", url: "http://localhost:8080/empty.html", modifiers: Qt.ShiftModifier | Qt.ControlModifier, disposition: NavigationRequest.DispositionNewForegroundTab, current: true },
 
77
        { link: "#button1", url: "http://localhost:8080/empty.html", modifiers: Qt.NoModifier, disposition: NavigationRequest.DispositionNewForegroundTab, current: false },
 
78
        { link: "#button1", url: "http://localhost:8080/empty.html", modifiers: Qt.ShiftModifier, disposition: NavigationRequest.DispositionNewWindow, current: false },
 
79
        { link: "#button1", url: "http://localhost:8080/empty.html", modifiers: Qt.ControlModifier, disposition: NavigationRequest.DispositionNewBackgroundTab, current: false },
 
80
        { link: "#button1", url: "http://localhost:8080/empty.html", modifiers: Qt.ShiftModifier | Qt.ControlModifier, disposition: NavigationRequest.DispositionNewForegroundTab, current: false },
 
81
        { link: "#link2", url: "http://localhost:8080/empty.html", modifiers: Qt.NoModifier, disposition: NavigationRequest.DispositionNewForegroundTab, current: false },
 
82
        { link: "#link2", url: "http://localhost:8080/empty.html", modifiers: Qt.ShiftModifier, disposition: NavigationRequest.DispositionNewWindow, current: false },
 
83
        { link: "#link2", url: "http://localhost:8080/empty.html", modifiers: Qt.ControlModifier, disposition: NavigationRequest.DispositionNewBackgroundTab, current: false },
 
84
        { link: "#link2", url: "http://localhost:8080/empty.html", modifiers: Qt.ShiftModifier | Qt.ControlModifier, disposition: NavigationRequest.DispositionNewForegroundTab, current: false },
 
85
        // XXX(chrisccoulson): These 2 disabled due to https://launchpad.net/bugs/1302743
 
86
        // { link: "#button2", url: "http://localhost:8080/empty.html", modifiers: Qt.NoModifier, disposition: NavigationRequest.DispositionNewPopup, current: false },
 
87
        // { link: "#button2", url: "http://localhost:8080/empty.html", modifiers: Qt.ShiftModifier, disposition: NavigationRequest.DispositionNewPopup, current: false },
 
88
        { link: "#button2", url: "http://localhost:8080/empty.html", modifiers: Qt.ControlModifier, disposition: NavigationRequest.DispositionNewBackgroundTab, current: false },
 
89
        { link: "#button2", url: "http://localhost:8080/empty.html", modifiers: Qt.ShiftModifier | Qt.ControlModifier, disposition: NavigationRequest.DispositionNewForegroundTab, current: false },
 
90
      ];
 
91
    }
 
92
 
 
93
    // Test that we get an onNavigationRequested signal for all renderer-initiated
 
94
    // top-level navigations (also verifies that we don't get one for browser-
 
95
    // initiated navigations)
 
96
    function test_NavigationRequest1_from_user_gestures(data) {
 
97
      webView.url = "http://localhost:8080/tst_NavigationRequest.html";
 
98
      verify(webView.waitForLoadSucceeded(),
 
99
             "Timed out waiting for successful load");
 
100
 
 
101
      compare(spy.count, 0,
 
102
              "Shouldn't get an onNavigationRequested signal for browser-initiated navigation");
 
103
 
 
104
      var r = webView.getTestApi().getBoundingClientRectForSelector(data.link);
 
105
      mouseClick(webView, r.x + r.width / 2, r.y + r.height / 2, Qt.LeftButton, data.modifiers);
 
106
 
 
107
      if (data.disposition == NavigationRequest.DispositionCurrentTab) {
 
108
        verify(webView.waitForLoadSucceeded());
 
109
      } else {
 
110
        newViewSpy.wait();
 
111
      }
 
112
 
 
113
      compare(spy.count, 1, "Should have had an onNavigationRequested signal");
 
114
      compare(webView.lastRequestUrl, data.url);
 
115
      compare(webView.lastRequestDisposition, data.disposition);
 
116
      compare(webView.lastRequestUserGesture, true);
 
117
    }
 
118
 
 
119
    function test_NavigationRequest2_no_user_gesture_data() {
 
120
      return test_NavigationRequest1_from_user_gestures_data();
 
121
    }
 
122
 
 
123
    // Verify that the userGesture property indicates the appropriate value
 
124
    // for renderer-initiated top-level navigations that don't come from an
 
125
    // input event
 
126
    function test_NavigationRequest2_no_user_gesture(data) {
 
127
      webView.context.popupBlockerEnabled = false;
 
128
 
 
129
      webView.url = "http://localhost:8080/tst_NavigationRequest.html";
 
130
      verify(webView.waitForLoadSucceeded(),
 
131
             "Timed out waiting for successful load");
 
132
 
 
133
      compare(spy.count, 0,
 
134
              "Shouldn't get an onNavigationRequested signal for browser-initiated navigation");
 
135
 
 
136
      webView.getTestApi().evaluateCode(
 
137
"var e = document.createEvent(\"HTMLEvents\");
 
138
e.initEvent(\"click\", true, false);
 
139
document.querySelector(\"" + data.link + "\").dispatchEvent(e);", true);
 
140
 
 
141
      if (data.current) {
 
142
        verify(webView.waitForLoadSucceeded());
 
143
      } else {
 
144
        newViewSpy.wait();
 
145
      }
 
146
 
 
147
      compare(spy.count, 1, "Should have had an onNavigationRequested signal")
 
148
      compare(webView.lastRequestUrl, "http://localhost:8080/empty.html");
 
149
      compare(webView.lastRequestDisposition, data.current ? NavigationRequest.DispositionCurrentTab : NavigationRequest.DispositionNewPopup );
 
150
      compare(webView.lastRequestUserGesture, false);
 
151
    }
 
152
 
 
153
    function test_NavigationRequest3_reject_data() {
 
154
      return test_NavigationRequest1_from_user_gestures_data();
 
155
    }
 
156
 
 
157
    // Verify that rejecting an onNavigationRequested request for all
 
158
    // renderer-initiated top-level navigations blocks the navigation and that
 
159
    // we don't get any onNewViewRequested signals.
 
160
    //
 
161
    // XXX(chrisccoulson): This is a bit hacky, because we use a 100ms delay
 
162
    // before verifying no loads started
 
163
    function test_NavigationRequest3_reject(data) {
 
164
      webView.shouldReject = true;
 
165
 
 
166
      webView.url = "http://localhost:8080/tst_NavigationRequest.html";
 
167
      verify(webView.waitForLoadSucceeded(),
 
168
             "Timed out waiting for successful load");
 
169
 
 
170
      webView.clearLoadEventCounters();
 
171
 
 
172
      var r = webView.getTestApi().getBoundingClientRectForSelector(data.link);
 
173
      mouseClick(webView, r.x + r.width / 2, r.y + r.height / 2, Qt.LeftButton, data.modifiers);
 
174
      webView.waitFor(function() { return false; }, 100);
 
175
 
 
176
      compare(spy.count, 1);
 
177
      compare(newViewSpy.count, 0, "Shouldn't have called onNewViewRequested for rejected navigation");
 
178
      compare(webView.loadsStartedCount, 0, "Shouldn't have started a load for rejected navigation");
 
179
    }
 
180
 
 
181
    function test_NavigationRequest4_subframe_data() {
 
182
      return test_NavigationRequest1_from_user_gestures_data();
 
183
    }
 
184
 
 
185
    // Verify that we don't get an onNavigationRequested signal for
 
186
    // renderer-initiated subframe navigations unless the disposition is not
 
187
    // DispositionCurrentTab.
 
188
    // We get them for other dispositions via
 
189
    // content::RenderFrameImpl::loadURLExternally(), which is called from
 
190
    // WebCore::DocumentLoader::shouldContinueForNavigationPolicy()
 
191
    function test_NavigationRequest4_subframe(data) {
 
192
      webView.url = "http://localhost:8080/tst_NavigationRequest2.html";
 
193
      verify(webView.waitForLoadSucceeded(),
 
194
             "Timed out waiting for successful load");
 
195
 
 
196
      var frame = webView.rootFrame.childFrames[0];
 
197
 
 
198
      frameSpy.target = frame;
 
199
      var r = webView.getTestApiForFrame(frame).getBoundingClientRectForSelector(data.link);
 
200
      mouseClick(webView, r.x + r.width / 2, r.y + r.height / 2, Qt.LeftButton, data.modifiers);
 
201
 
 
202
      if (data.disposition == NavigationRequest.DispositionCurrentTab) {
 
203
        frameSpy.wait();
 
204
        compare(spy.count, 0, "Shouldn't get onNavigationRequested from CurrentTab subframe navigations");
 
205
      } else {
 
206
        newViewSpy.wait();
 
207
        compare(spy.count, 1, "Should get onNavigationRequested from non-CurrentTab subframe navigations");
 
208
      }
 
209
    }
 
210
  }
 
211
}