1# Copyright 2019 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4import time 5 6from autotest_lib.client.common_lib import error 7from autotest_lib.client.cros.enterprise import enterprise_policy_base 8from autotest_lib.client.cros.input_playback import keyboard 9 10 11class policy_DefaultSearchProvider( 12 enterprise_policy_base.EnterprisePolicyTest): 13 """ 14 Verify effects of the DefaultSearchProviderSearchURL and 15 DefaultSearchProviderKeyword policy. When the 16 DefaultSearchProviderSearchURL policy is set, the specified search url 17 will be used when a value is entered in the omnibox. When the 18 DefaultSearchProviderKeyword is set, the value will trigger the shortcut 19 used in the omnibox to trigger the search for this provider. 20 21 """ 22 version = 1 23 24 def _search_check(self, case): 25 """ 26 Open a new tab, use the omnibox as a search box, and check the URL. 27 28 @param case: Value of the test being run. 29 30 """ 31 self.ui.start_ui_root(self.cr) 32 self.keyboard = keyboard.Keyboard() 33 self.ui.doDefault_on_obj(name='Address and search bar') 34 # The keys to be pressed for the test 35 if case == 'Keyword': 36 buttons = ['d', 'a', 'd', 'tab', 's', 's', 'enter'] 37 expected = '{}{}'.format(self.BASE_URL, 'ss') 38 else: 39 buttons = ['f', 's', 'w', 'enter'] 40 expected = '{}{}'.format(self.BASE_URL, 'fsw') 41 42 # Enter the buttons 43 for button in buttons: 44 self.keyboard.press_key(button) 45 46 tabFound = False 47 startTime = time.time() 48 while time.time() - startTime < 1: 49 tabs = set([tab.GetUrl() for tab in self.cr.browser.tabs]) 50 if expected in tabs: 51 tabFound = True 52 break 53 54 if not tabFound: 55 raise error.TestFail( 56 'Search not formated correctly. expected {} got {}' 57 .format(expected, tabs)) 58 59 def run_once(self, case): 60 """ 61 Setup and run the test configured for the specified test case. 62 63 @param case: Name of the test case to run. 64 65 """ 66 self.BASE_URL = 'https://fakeurl/search?q=' 67 POLICIES = {'DefaultSearchProviderEnabled': True, 68 'DefaultSearchProviderSearchURL': 69 '%s{searchTerms}' % (self.BASE_URL)} 70 if case == 'Keyword': 71 POLICIES['DefaultSearchProviderKeyword'] = 'dad' 72 self.setup_case(user_policies=POLICIES) 73 self._search_check(case) 74