Merge lp:~jderose/userwebkit/links into lp:userwebkit

Proposed by Jason Gerard DeRose
Status: Merged
Merged at revision: 22
Proposed branch: lp:~jderose/userwebkit/links
Merge into: lp:userwebkit
Diff against target: 223 lines (+139/-5)
4 files modified
debian/control (+3/-3)
test_userwebkit.py (+91/-0)
ui/index.html (+3/-0)
userwebkit.py (+42/-2)
To merge this branch: bzr merge lp:~jderose/userwebkit/links
Reviewer Review Type Date Requested Status
Novacut Dev Pending
Review via email: mp+86652@code.launchpad.net

Description of the change

Adds CouchView._on_nav_policy_decision() and its test.

Also adds new CouchView 'open' signal, used to open external links in the user's default browser.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'debian/control'
--- debian/control 2011-10-01 01:05:20 +0000
+++ debian/control 2011-12-22 05:11:23 +0000
@@ -3,7 +3,7 @@
3Priority: optional3Priority: optional
4Maintainer: Jason Gerard DeRose <jderose@novacut.com>4Maintainer: Jason Gerard DeRose <jderose@novacut.com>
5Build-Depends: debhelper (>= 8.9), python3 (>= 3.2),5Build-Depends: debhelper (>= 8.9), python3 (>= 3.2),
6 python3-microfiber (>= 11.10),6 python3-microfiber (>= 11.12),
7 python3-gobject,7 python3-gobject,
8 gir1.2-gtk-3.0,8 gir1.2-gtk-3.0,
9 gir1.2-webkit-3.0,9 gir1.2-webkit-3.0,
@@ -15,8 +15,8 @@
15Package: python3-userwebkit15Package: python3-userwebkit
16Architecture: all16Architecture: all
17Depends: ${misc:Depends}, python3 (>= 3.2),17Depends: ${misc:Depends}, python3 (>= 3.2),
18 python3-microfiber (>= 11.10),18 python3-microfiber (>= 11.12),
19 dc3 (>= 11.10),19 dc3 (>= 11.12),
20 python3-gobject,20 python3-gobject,
21 gir1.2-gtk-3.0,21 gir1.2-gtk-3.0,
22 gir1.2-webkit-3.0,22 gir1.2-webkit-3.0,
2323
=== modified file 'test_userwebkit.py'
--- test_userwebkit.py 2011-09-26 10:25:21 +0000
+++ test_userwebkit.py 2011-12-22 05:11:23 +0000
@@ -63,6 +63,31 @@
63 }63 }
6464
6565
66class DummyCallback:
67 def __init__(self):
68 self._calls = []
69
70 def __call__(self, *args):
71 self._calls.append(args)
72
73
74class DummyRequest:
75 def __init__(self, uri):
76 self.__uri = uri
77
78 def get_uri(self):
79 return self.__uri
80
81
82class DummyPolicy:
83 def __init__(self):
84 self._called = False
85
86 def ignore(self):
87 assert self._called is False
88 self._called = True
89
90
66class TestCouchView(TestCase):91class TestCouchView(TestCase):
67 def test_init(self):92 def test_init(self):
68 view = userwebkit.CouchView()93 view = userwebkit.CouchView()
@@ -115,3 +140,69 @@
115 self.assertIsNone(view._env)140 self.assertIsNone(view._env)
116 self.assertIsNone(view._on_request(None, None, None, None, None))141 self.assertIsNone(view._on_request(None, None, None, None, None))
117142
143 def test_on_nav_policy_decision(self):
144 callback = DummyCallback()
145 view = userwebkit.CouchView()
146 view.connect('open', callback)
147
148 # Make sure on_request() immediately returns when env is None:
149 self.assertIsNone(
150 view._on_nav_policy_decision(None, None, None, None, None)
151 )
152 self.assertEqual(callback._calls, [])
153
154 # When URI netloc and scheme matches env, should return False
155 env = random_env()
156 view.set_env(env)
157 request = DummyRequest(env['url'] + 'foo')
158 self.assertIs(
159 view._on_nav_policy_decision(None, None, request, None, None),
160 False
161 )
162 self.assertEqual(callback._calls, [])
163
164 # For external http, https URI, should fire 'open' signal, call
165 # policy.ignore(), and return True
166 request = DummyRequest('http://www.ubuntu.com/')
167 policy = DummyPolicy()
168 self.assertIs(
169 view._on_nav_policy_decision(None, None, request, None, policy),
170 True
171 )
172 self.assertIs(policy._called, True)
173 self.assertEqual(callback._calls,
174 [
175 (view, 'http://www.ubuntu.com/'),
176 ]
177 )
178
179 request = DummyRequest('https://launchpad.net/novacut')
180 policy = DummyPolicy()
181 self.assertIs(
182 view._on_nav_policy_decision(None, None, request, None, policy),
183 True
184 )
185 self.assertIs(policy._called, True)
186 self.assertEqual(callback._calls,
187 [
188 (view, 'http://www.ubuntu.com/'),
189 (view, 'https://launchpad.net/novacut'),
190 ]
191 )
192
193 # For other non-internal URI, should just call policy.ignore() and
194 # return True... should not fire 'open'
195 request = DummyRequest('ftp://example.com/')
196 policy = DummyPolicy()
197 self.assertIs(
198 view._on_nav_policy_decision(None, None, request, None, policy),
199 True
200 )
201 self.assertIs(policy._called, True)
202 self.assertEqual(callback._calls,
203 [
204 (view, 'http://www.ubuntu.com/'),
205 (view, 'https://launchpad.net/novacut'),
206 ]
207 )
208
118209
=== modified file 'ui/index.html'
--- ui/index.html 2011-11-24 20:42:28 +0000
+++ ui/index.html 2011-12-22 05:11:23 +0000
@@ -54,5 +54,8 @@
54<p id="hello"></p>54<p id="hello"></p>
55<p>Right click and <strong>Inspect Element</strong></p>55<p>Right click and <strong>Inspect Element</strong></p>
56<button id="button">Start</button>56<button id="button">Start</button>
57<p>
58<a href="http://novacut.com/">A test link</a>
59</p>
57</body>60</body>
58</html>61</html>
5962
=== modified file 'userwebkit.py'
--- userwebkit.py 2011-12-18 14:41:37 +0000
+++ userwebkit.py 2011-12-22 05:11:23 +0000
@@ -74,12 +74,18 @@
74 'title_data': (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE,74 'title_data': (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE,
75 [TYPE_PYOBJECT]75 [TYPE_PYOBJECT]
76 ),76 ),
77 'open': (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE,
78 [TYPE_PYOBJECT]
79 ),
77 }80 }
7881
79 def __init__(self, env=None):82 def __init__(self, env=None):
80 super().__init__()83 super().__init__()
81 self.connect('resource-request-starting', self._on_request)84 self.connect('resource-request-starting', self._on_request)
82 self.connect('notify::title', self._on_notify_title)85 self.connect('notify::title', self._on_notify_title)
86 self.connect('navigation-policy-decision-requested',
87 self._on_nav_policy_decision
88 )
83 self.set_env(env)89 self.set_env(env)
8490
85 def set_env(self, env):91 def set_env(self, env):
@@ -116,6 +122,35 @@
116 for (key, value) in h.items():122 for (key, value) in h.items():
117 request.props.message.props.request_headers.append(key, value)123 request.props.message.props.request_headers.append(key, value)
118124
125 def _on_nav_policy_decision(self, view, frame, request, nav, policy):
126 """
127 Handle user trying to Navigate away from current page.
128
129 Note that this will be called before `CouchView._on_resource_request()`.
130
131 The *policy* arg is a ``WebPolicyDecision`` instance. To handle the
132 decision, call one of:
133
134 * ``WebPolicyDecision.ignore()``
135 * ``WebPolicyDecision.use()``
136 * ``WebPolicyDecision.download()``
137
138 And then return ``True``.
139
140 Otherwise, return ``False`` or ``None`` to have the WebKit default
141 behavior apply.
142 """
143 if self._env is None:
144 return
145 uri = request.get_uri()
146 u = urlparse(uri)
147 if u.netloc == self._u.netloc and u.scheme in ('http', 'https'):
148 return False
149 if u.scheme in ('http', 'https'):
150 self.emit('open', uri)
151 policy.ignore()
152 return True
153
119 def _on_notify_title(self, view, notify):154 def _on_notify_title(self, view, notify):
120 title = view.get_property('title')155 title = view.get_property('title')
121 if title is None:156 if title is None:
@@ -233,6 +268,7 @@
233 Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC268 Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC
234 )269 )
235 self.view = CouchView()270 self.view = CouchView()
271 self.view.connect('open', self.on_open)
236 self.scroll.add(self.view)272 self.scroll.add(self.view)
237 self.view.get_settings().set_property('enable-developer-extras', True)273 self.view.get_settings().set_property('enable-developer-extras', True)
238 inspector = self.view.get_inspector()274 inspector = self.view.get_inspector()
@@ -293,8 +329,12 @@
293329
294 def on_reload(self, button):330 def on_reload(self, button):
295 self.view.reload_bypass_cache()331 self.view.reload_bypass_cache()
296 332
297 def on_futon(self, button):333 def on_futon(self, button):
298 self.view.load_uri(self.server._full_url('/_utils/'))334 self.view.load_uri(self.server._full_url('/_utils/'))
299 335
336 def on_open(self, view, uri):
337 import subprocess
338 subprocess.check_call(['/usr/bin/xdg-open', uri])
339
300340

Subscribers

People subscribed via source and target branches