~gary-lasker/software-center/handle-trans-cancel-lp1027209-for-5.2

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
import unittest

from gi.repository import (
    GObject,
    Soup,
    WebKit,
    )

from mock import patch

from testutils import setup_test_env
setup_test_env()

from softwarecenter.enums import WEBKIT_USER_AGENT_SUFFIX
from softwarecenter.ui.gtk3.views.webkit import SoftwareCenterWebView


class TestWebkit(unittest.TestCase):

    def test_have_cookie_jar(self):
        # ensure we have a cookie jar available
        session = WebKit.get_default_session()
        cookie_jars = [feature 
                for feature in  session.get_features(Soup.SessionFeature)
                if isinstance(feature, Soup.CookieJar)]
        self.assertEqual(len(cookie_jars), 1)
    
    def test_user_agent_string(self):
        webview = SoftwareCenterWebView()
        settings = webview.get_settings()
        self.assertTrue(
            WEBKIT_USER_AGENT_SUFFIX in settings.get_property("user-agent"))

    @patch("softwarecenter.ui.gtk3.views.webkit.get_oem_channel_descriptor")
    def test_user_agent_oem_channel_descriptor(self, mock_oem_channel):
        canary = "she-loves-you-yeah-yeah-yeah"
        mock_oem_channel.return_value = canary
        webview = SoftwareCenterWebView()
        settings = webview.get_settings()
        self.assertTrue(
            canary in settings.get_property("user-agent"))
        
    def test_auto_fill_in_email(self):
        def _load_status_changed(view, status):
            if view.get_property("load-status") == WebKit.LoadStatus.FINISHED:
                loop.quit()
        loop =  GObject.MainLoop(GObject.main_context_default())       
        webview = SoftwareCenterWebView()
        email = "foo@bar"
        webview.set_auto_insert_email(email)
        with patch.object(webview, "execute_script") as mock_execute_js:
            webview.connect("notify::load-status", _load_status_changed)
            webview.load_uri("https://login.ubuntu.com")
            loop.run()
            mock_execute_js.assert_called()
            mock_execute_js.assert_called_with(
                SoftwareCenterWebView.AUTO_FILL_EMAIL_JS % email)


if __name__ == "__main__":
    unittest.main()