• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2Unit tests for cfm_facade_native.py.
3
4To execute them run:
5utils/unittest_suite.py \
6    autotest_lib.client.cros.multimedia.cfm_facade_native_unittest
7"""
8
9# pylint: disable=missing-docstring
10
11import mock
12import unittest
13
14from autotest_lib.client.common_lib import error
15# Mock cros and graphics modules as they import telemetry which is not available
16# in unit tests.
17cros_mock = mock.Mock()
18graphics_mock = mock.Mock()
19modules = {'autotest_lib.client.common_lib.cros': cros_mock,
20           'autotest_lib.client.cros.graphics': graphics_mock}
21with mock.patch.dict('sys.modules', modules):
22    from autotest_lib.client.cros.multimedia import cfm_facade_native
23
24BACKGROUD_PAGE = '_generated_background_page.html'
25HANGOUT_WINDOW_0 = 'hangoutswindow.html?windowid=0'
26
27
28def create_mock_context(url):
29    ctx = mock.Mock()
30    ctx.GetUrl.return_value = url
31    return ctx
32
33
34class CfmFacadeNativeUnitTest(unittest.TestCase):
35
36    def setUp(self):
37        self.facade_resource = mock.Mock()
38        self.browser = self.facade_resource._browser
39        self.screen = 'hotrod'
40        self.cfm_facade = cfm_facade_native.CFMFacadeNative(
41            self.facade_resource, self.screen)
42        cfm_facade_native.CFMFacadeNative._DEFAULT_TIMEOUT = 1
43        self.extension_path = 'chrome-extension://' + self.cfm_facade._EXT_ID
44
45    @mock.patch.object(cfm_facade_native, 'kiosk_utils')
46    def test_check_hangout_extension_context(self, mock_kiosk_utils):
47        dummy_ctx = create_mock_context('foo.bar?screen=dummy')
48        dummy_ctx.EvaluateJavaScript.return_value = (
49            '%s/%s' % (self.extension_path, HANGOUT_WINDOW_0))
50
51        mock_kiosk_utils.wait_for_kiosk_ext.return_value = [dummy_ctx]
52        self.cfm_facade.check_hangout_extension_context()
53        mock_kiosk_utils.wait_for_kiosk_ext.assert_called_with(self.browser,
54            self.cfm_facade._EXT_ID)
55
56    @mock.patch.object(cfm_facade_native, 'kiosk_utils')
57    def test_webview_context_property(self, mock_kiosk_utils):
58        dummy_ctx = create_mock_context('foo.bar?screen=dummy')
59        hotrod_ctx = create_mock_context('www.qbc?screen=%s' % self.screen)
60        mock_kiosk_utils.get_webview_contexts.return_value = [dummy_ctx,
61                                                              hotrod_ctx]
62        self.assertEqual(self.cfm_facade._webview_context, hotrod_ctx)
63        mock_kiosk_utils.get_webview_contexts.assert_called_with(self.browser,
64            self.cfm_facade._EXT_ID)
65
66    @mock.patch.object(cfm_facade_native, 'kiosk_utils')
67    def test_get_webview_context_by_screen_two_screens(self, mock_kiosk_utils):
68        screen_param = 'foo'
69        dummy_ctx = create_mock_context('foo.bar?screen=dummy')
70        hotrod_ctx = create_mock_context('www.qbc?screen=%s' % screen_param)
71        mock_kiosk_utils.get_webview_contexts.return_value = [dummy_ctx,
72                                                              hotrod_ctx]
73        found_ctx = self.cfm_facade._get_webview_context_by_screen(screen_param)
74        self.assertEqual(found_ctx, hotrod_ctx)
75
76    @mock.patch.object(cfm_facade_native, 'kiosk_utils')
77    def test_get_webview_context_by_screen_only_hotrod_screen(self,
78                                                              mock_kiosk_utils):
79        screen_param = 'foo'
80        dummy_ctx = create_mock_context('foo.bar?screen=dummy')
81        hotrod_ctx = create_mock_context('www.qbc?screen=%s' % screen_param)
82        mock_kiosk_utils.get_webview_contexts.return_value = [hotrod_ctx]
83        found_ctx = self.cfm_facade._get_webview_context_by_screen(screen_param)
84        self.assertEqual(found_ctx, hotrod_ctx)
85
86    @mock.patch.object(cfm_facade_native, 'kiosk_utils')
87    def test_get_webview_context_by_screen_with_mimo_and_main_screen(
88            self, mock_kiosk_utils):
89        screen_param = 'foo'
90        mimo_ctx = create_mock_context('www.qbc?screen=control')
91        hotrod_ctx = create_mock_context('www.qbc?screen=%s' % screen_param)
92        mock_kiosk_utils.get_webview_contexts.return_value = [hotrod_ctx,
93                                                              mimo_ctx]
94        found_ctx = self.cfm_facade._get_webview_context_by_screen(screen_param)
95        self.assertEqual(found_ctx, hotrod_ctx)
96
97    @mock.patch.object(cfm_facade_native, 'kiosk_utils')
98    def test_get_webview_context_during_oobe_with_two_screens(self,
99                                                              mock_kiosk_utils):
100        screen_param = 'foo'
101        node_screen_ctx = create_mock_context(
102            'node.screen.com?screen=hotrod&nooobestatesync&oobedone')
103        main_screen_ctx = create_mock_context(
104            'mimo.screen.com?screen=%s' % screen_param)
105        mock_kiosk_utils.get_webview_contexts.return_value = [
106            node_screen_ctx, main_screen_ctx]
107        found_ctx = self.cfm_facade._get_webview_context_by_screen(screen_param)
108        self.assertEqual(found_ctx, main_screen_ctx)
109
110    @mock.patch.object(cfm_facade_native, 'kiosk_utils')
111    def test_get_webview_context_no_screen_found(self, mock_kiosk_utils):
112        foo_ctx = create_mock_context('node.screen.com?screen=foo')
113        bar_ctx = create_mock_context('mimo.screen.com?screen=bar')
114        mock_kiosk_utils.get_webview_contexts.return_value = [foo_ctx, bar_ctx]
115        with self.assertRaises(error.TestFail):
116            self.cfm_facade._get_webview_context_by_screen('unknown_param')
117
118    @mock.patch.object(cfm_facade_native, 'kiosk_utils')
119    def test_reboot_device_with_chrome_api(self, mock_kiosk_utils):
120        dummy_ctx = create_mock_context('foo.bar?screen=dummy')
121        dummy_ctx.EvaluateJavaScript.return_value = (
122            '%s/%s' % (self.extension_path, BACKGROUD_PAGE))
123        mock_kiosk_utils.wait_for_kiosk_ext.return_value = [dummy_ctx]
124        self.cfm_facade.reboot_device_with_chrome_api()
125        dummy_ctx.ExecuteJavaScript.assert_called_with(
126            'chrome.runtime.restart();')
127
128    @mock.patch.object(cfm_facade_native, 'kiosk_utils')
129    def test_large_integers_in_media_info_data_points(self, mock_kiosk_utils):
130        hotrod_ctx = create_mock_context('www.qbc?screen=%s' % self.screen)
131        mock_kiosk_utils.get_webview_contexts.return_value = [hotrod_ctx]
132        hotrod_ctx.EvaluateJavaScript.return_value = [{
133                'a': 123,
134                'b': {
135                        'c': 2**31 - 1,
136                        'd': 2**31
137                }
138        }, [-123]]
139        data_points = self.cfm_facade.get_media_info_data_points()
140        self.assertIsInstance(data_points[0]['a'], int)
141        self.assertIsInstance(data_points[0]['b']['c'], int)
142        self.assertIsInstance(data_points[0]['b']['d'], float)
143        self.assertIsInstance(data_points[1][0], int)
144