1""" 2Unit tests for cfm_facade.py. 3 4To execute them run: 5utils/unittest_suite.py \ 6 autotest_lib.client.cros.multimedia.cfm_facade_unittest 7""" 8 9# pylint: disable=missing-docstring 10 11import unittest 12from unittest import mock 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 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 CfmFacadeLocalUnitTest(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.CFMFacadeLocal( 41 self.facade_resource, self.screen) 42 cfm_facade.CFMFacadeLocal._DEFAULT_TIMEOUT = 1 43 self.extension_path = 'chrome-extension://' + self.cfm_facade._EXT_ID 44 45 @mock.patch.object(cfm_facade, 'kiosk_utils') 46 def test_check_hangout_extension_context(self, mock_kiosk_utils): 47 stub_ctx = create_mock_context('foo.bar?screen=stub') 48 stub_ctx.EvaluateJavaScript.return_value = ( 49 '%s/%s' % (self.extension_path, HANGOUT_WINDOW_0)) 50 51 mock_kiosk_utils.wait_for_kiosk_ext.return_value = [stub_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, 'kiosk_utils') 57 def test_webview_context_property(self, mock_kiosk_utils): 58 stub_ctx = create_mock_context('foo.bar?screen=stub') 59 hotrod_ctx = create_mock_context('www.qbc?screen=%s' % self.screen) 60 mock_kiosk_utils.get_webview_contexts.return_value = [ 61 stub_ctx, hotrod_ctx 62 ] 63 self.assertEqual(self.cfm_facade._webview_context, hotrod_ctx) 64 mock_kiosk_utils.get_webview_contexts.assert_called_with(self.browser, 65 self.cfm_facade._EXT_ID) 66 67 @mock.patch.object(cfm_facade, 'kiosk_utils') 68 def test_get_webview_context_by_screen_two_screens(self, mock_kiosk_utils): 69 screen_param = 'foo' 70 stub_ctx = create_mock_context('foo.bar?screen=stub') 71 hotrod_ctx = create_mock_context('www.qbc?screen=%s' % screen_param) 72 mock_kiosk_utils.get_webview_contexts.return_value = [ 73 stub_ctx, hotrod_ctx 74 ] 75 found_ctx = self.cfm_facade._get_webview_context_by_screen(screen_param) 76 self.assertEqual(found_ctx, hotrod_ctx) 77 78 @mock.patch.object(cfm_facade, 'kiosk_utils') 79 def test_get_webview_context_by_screen_only_hotrod_screen(self, 80 mock_kiosk_utils): 81 screen_param = 'foo' 82 stub_ctx = create_mock_context('foo.bar?screen=stub') 83 hotrod_ctx = create_mock_context('www.qbc?screen=%s' % screen_param) 84 mock_kiosk_utils.get_webview_contexts.return_value = [hotrod_ctx] 85 found_ctx = self.cfm_facade._get_webview_context_by_screen(screen_param) 86 self.assertEqual(found_ctx, hotrod_ctx) 87 88 @mock.patch.object(cfm_facade, 'kiosk_utils') 89 def test_get_webview_context_by_screen_with_mimo_and_main_screen( 90 self, mock_kiosk_utils): 91 screen_param = 'foo' 92 mimo_ctx = create_mock_context('www.qbc?screen=control') 93 hotrod_ctx = create_mock_context('www.qbc?screen=%s' % screen_param) 94 mock_kiosk_utils.get_webview_contexts.return_value = [hotrod_ctx, 95 mimo_ctx] 96 found_ctx = self.cfm_facade._get_webview_context_by_screen(screen_param) 97 self.assertEqual(found_ctx, hotrod_ctx) 98 99 @mock.patch.object(cfm_facade, 'kiosk_utils') 100 def test_get_webview_context_during_oobe_with_two_screens(self, 101 mock_kiosk_utils): 102 screen_param = 'foo' 103 node_screen_ctx = create_mock_context( 104 'node.screen.com?screen=hotrod&nooobestatesync&oobedone') 105 main_screen_ctx = create_mock_context( 106 'mimo.screen.com?screen=%s' % screen_param) 107 mock_kiosk_utils.get_webview_contexts.return_value = [ 108 node_screen_ctx, main_screen_ctx] 109 found_ctx = self.cfm_facade._get_webview_context_by_screen(screen_param) 110 self.assertEqual(found_ctx, main_screen_ctx) 111 112 @mock.patch.object(cfm_facade, 'kiosk_utils') 113 def test_get_webview_context_no_screen_found(self, mock_kiosk_utils): 114 foo_ctx = create_mock_context('node.screen.com?screen=foo') 115 bar_ctx = create_mock_context('mimo.screen.com?screen=bar') 116 mock_kiosk_utils.get_webview_contexts.return_value = [foo_ctx, bar_ctx] 117 with self.assertRaises(error.TestFail): 118 self.cfm_facade._get_webview_context_by_screen('unknown_param') 119 120 @mock.patch.object(cfm_facade, 'kiosk_utils') 121 def test_reboot_device_with_chrome_api(self, mock_kiosk_utils): 122 stub_ctx = create_mock_context('foo.bar?screen=stub') 123 stub_ctx.EvaluateJavaScript.return_value = ( 124 '%s/%s' % (self.extension_path, BACKGROUD_PAGE)) 125 mock_kiosk_utils.wait_for_kiosk_ext.return_value = [stub_ctx] 126 self.cfm_facade.reboot_device_with_chrome_api() 127 stub_ctx.ExecuteJavaScript.assert_called_with( 128 'chrome.runtime.restart();') 129 130 @mock.patch.object(cfm_facade, 'kiosk_utils') 131 def test_large_integers_in_media_info_data_points(self, mock_kiosk_utils): 132 hotrod_ctx = create_mock_context('www.qbc?screen=%s' % self.screen) 133 mock_kiosk_utils.get_webview_contexts.return_value = [hotrod_ctx] 134 hotrod_ctx.EvaluateJavaScript.return_value = [{ 135 'a': 123, 136 'b': { 137 'c': 2**31 - 1, 138 'd': 2**31 139 } 140 }, [-123]] 141 data_points = self.cfm_facade.get_media_info_data_points() 142 self.assertIsInstance(data_points[0]['a'], int) 143 self.assertIsInstance(data_points[0]['b']['c'], int) 144 self.assertIsInstance(data_points[0]['b']['d'], float) 145 self.assertIsInstance(data_points[1][0], int) 146