| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Pyauto powered ui action runner. | |
| 7 | |
| 8 Developed primarily to verify validity of the model based action generator. | |
| 9 """ | |
| 10 | |
| 11 import time | |
| 12 import sys | |
| 13 | |
| 14 import pyauto_functional | |
| 15 import pyauto | |
| 16 import ui_model | |
| 17 | |
| 18 class Runner(pyauto.PyUITest): | |
| 19 | |
| 20 def setUp(self): | |
| 21 self.debug_mode = False | |
| 22 pyauto.PyUITest.setUp(self) | |
| 23 | |
| 24 def RunActionList(self): | |
| 25 """Runs actions from a file.""" | |
| 26 f = open('list') | |
| 27 actions = f.readlines() | |
| 28 self.browser = ui_model.BrowserState(advanced_actions=True) | |
| 29 count = 0 | |
| 30 for action in actions: | |
| 31 count += 1 | |
| 32 sys.stdout.write('%d > ' % count) | |
| 33 action = action.strip() | |
| 34 self.ApplyAction(action) | |
| 35 raw_input('Press key to continue.') | |
| 36 | |
| 37 | |
| 38 def DebugUIActions(self): | |
| 39 """Run testUIActions with debug mode on. | |
| 40 | |
| 41 Allows inspection of the browser after unexpected state is encountered. | |
| 42 """ | |
| 43 self.debug_mode = True | |
| 44 self.testUIActions() | |
| 45 | |
| 46 def testUIActions(self): | |
| 47 """Generates and runs actions forever.""" | |
| 48 self.browser = ui_model.BrowserState(advanced_actions=True) | |
| 49 count = 0 | |
| 50 start_time = time.time() | |
| 51 while True: | |
| 52 count += 1 | |
| 53 sys.stdout.write('%d:%.3f > ' % (count, time.time() - start_time)) | |
| 54 action = ui_model.GetRandomAction(self.browser) | |
| 55 self.ApplyAction(action) | |
| 56 | |
| 57 def ApplyAction(self, action): | |
| 58 sys.stdout.write('%s, ' % action) | |
| 59 if self._DoAction(action): | |
| 60 ui_model.UpdateState(self.browser, action) | |
| 61 self._CheckState() | |
| 62 | |
| 63 def Error(self, msg=''): | |
| 64 """Called when an unexpected state is encountered.""" | |
| 65 if msg: | |
| 66 print 'Error: %s' % msg | |
| 67 else: | |
| 68 print 'Error' | |
| 69 while self.debug_mode: | |
| 70 raw_input('Press key to continue.') | |
| 71 assertTrue(False, msg) | |
| 72 | |
| 73 def _CheckState(self): | |
| 74 """Check some basic properties of the browser against expected state.""" | |
| 75 active_window = self.browser.window_position | |
| 76 active_tab = self.GetActiveTabIndex(active_window) | |
| 77 expected_tab = self.browser.window.tab_position | |
| 78 print 'win: %d tab: %d navs: %d backs: %d' % ( | |
| 79 active_window, active_tab, self.browser.window.tab.navs, | |
| 80 self.browser.window.tab.backs) | |
| 81 if active_tab != expected_tab: | |
| 82 self.Error('active index out of sync: expected %d' % expected_tab) | |
| 83 tab_count = self.GetTabCount(active_window) | |
| 84 expected_count = self.browser.window.num_tabs | |
| 85 if tab_count != expected_count: | |
| 86 self.Error('tab count out of sync: count: %d expected: %d' % ( | |
| 87 tab_count, expected_count)) | |
| 88 window_count = self.GetBrowserWindowCount() | |
| 89 expected_count = self.browser.num_windows | |
| 90 if window_count != expected_count: | |
| 91 self.Error('window count out of sync: count: %d expected %d' % ( | |
| 92 window_count, expected_count)) | |
| 93 | |
| 94 def _GrabTab(self): | |
| 95 active_window = self.browser.window_position | |
| 96 window = self.GetBrowserWindow(active_window) | |
| 97 tab_count = self.GetTabCount(self.browser.window_position) | |
| 98 active_tab = self.browser.window.tab_position | |
| 99 if active_tab >= tab_count: | |
| 100 self.Error('active tab out of bounds: count: %d expected active: %d' % ( | |
| 101 (tab_count, active_tab))) | |
| 102 return window.GetTab(self.browser.window.tab_position) | |
| 103 | |
| 104 def _RunInActiveWindow(self, command): | |
| 105 active_window = self.browser.window_position | |
| 106 self.RunCommand(command, active_window) | |
| 107 | |
| 108 def _RunAsyncInActiveWindow(self, command): | |
| 109 active_window = self.browser.window_position | |
| 110 self.ApplyAccelerator(command, active_window) | |
| 111 | |
| 112 def _Zoom(self, command): | |
| 113 active_window = self.browser.window_position | |
| 114 title = self.GetActiveTabTitle(active_window) | |
| 115 model_active_tab = self.browser.window.tab_position | |
| 116 active_tab = self.GetActiveTabIndex(active_window) | |
| 117 num_tabs = self.GetTabCount(active_window) | |
| 118 self._RunAsyncInActiveWindow(command) | |
| 119 if title == 'New Tab': | |
| 120 self.Error('zoom called on new tab') | |
| 121 | |
| 122 def _WaitFor(self, test): | |
| 123 start = time.time() | |
| 124 test_result, detail = test() | |
| 125 while not test_result: | |
| 126 if time.time() - start > self.action_max_timeout_ms(): | |
| 127 self.Error('TIMEOUT: %s' % detail) | |
| 128 time.sleep(.1) | |
| 129 test_result, detail = test() | |
| 130 | |
| 131 def _DoAction(self, action): | |
| 132 """Execute action in the browser. | |
| 133 | |
| 134 Attempts to simulate synchronous execution for most actions. | |
| 135 | |
| 136 Args: | |
| 137 action: action string. | |
| 138 """ | |
| 139 a = action.split(';')[0] | |
| 140 if a == 'showbookmarks': | |
| 141 self._RunAsyncInActiveWindow(pyauto.IDC_SHOW_BOOKMARK_BAR) | |
| 142 if a == 'openwindow' or a == 'goofftherecord': | |
| 143 def NewWindowHasTab(): | |
| 144 result = self.GetTabCount(self.browser.num_windows) == 1 | |
| 145 return (result, 'NewWindowHasTab') | |
| 146 def TabLoaded(): | |
| 147 result = self.GetActiveTabTitle(self.browser.num_windows) == 'New Tab' | |
| 148 return (result, 'TabLoaded') | |
| 149 if a == 'openwindow': | |
| 150 self.OpenNewBrowserWindow(True) | |
| 151 elif a == 'goofftherecord': | |
| 152 self._RunAsyncInActiveWindow(pyauto.IDC_NEW_INCOGNITO_WINDOW) | |
| 153 self._WaitFor(NewWindowHasTab) | |
| 154 self._WaitFor(TabLoaded) | |
| 155 if a == 'newtab': | |
| 156 active_window = self.browser.window_position | |
| 157 target = pyauto.GURL('chrome://newtab') | |
| 158 self.AppendTab(target, active_window) | |
| 159 if a == 'downloads': | |
| 160 active_window = self.browser.window_position | |
| 161 def TabLoaded(): | |
| 162 result = self.GetActiveTabTitle(active_window) == 'Downloads' | |
| 163 return (result, 'TabLoaded') | |
| 164 self._RunAsyncInActiveWindow(pyauto.IDC_SHOW_DOWNLOADS) | |
| 165 self._WaitFor(TabLoaded) | |
| 166 if a == 'star': | |
| 167 self._RunAsyncInActiveWindow(pyauto.IDC_BOOKMARK_PAGE) | |
| 168 if a == 'zoomplus': | |
| 169 self._Zoom(pyauto.IDC_ZOOM_PLUS) | |
| 170 if a == 'zoomminus': | |
| 171 self._Zoom(pyauto.IDC_ZOOM_MINUS) | |
| 172 if a == 'pagedown': | |
| 173 return False | |
| 174 if a == 'back' or a == 'forward' or a == 'navigate': | |
| 175 tab = self._GrabTab() | |
| 176 active_window = self.browser.window_position | |
| 177 old_title = self.GetActiveTabTitle(active_window) | |
| 178 retries = 0 | |
| 179 nav_result = 0 | |
| 180 while nav_result != 1: | |
| 181 if retries == 1: | |
| 182 break | |
| 183 if retries == 1: | |
| 184 sys.stdout.write('retry ') | |
| 185 if retries > 0: | |
| 186 time.sleep(.1) | |
| 187 sys.stdout.write('%d, ' % retries) | |
| 188 if a == 'navigate': | |
| 189 target = pyauto.GURL(action.split(';')[1]) | |
| 190 nav_result = tab.NavigateToURL(target) | |
| 191 elif a == 'back': | |
| 192 self.browser.Back() | |
| 193 self.browser.Forward() | |
| 194 nav_result = tab.GoBack() | |
| 195 elif a == 'forward': | |
| 196 self.browser.Forward() | |
| 197 self.browser.Back() | |
| 198 nav_result = tab.GoForward() | |
| 199 retries += 1 | |
| 200 if a == 'closetab': | |
| 201 tab = self._GrabTab() | |
| 202 ui_model.UpdateState(self.browser, action) | |
| 203 active_window = self.browser.window_position | |
| 204 window_count = self.browser.num_windows | |
| 205 tab_count = self.browser.window.num_tabs | |
| 206 def WindowCount(): | |
| 207 actual = self.GetBrowserWindowCount() | |
| 208 result = actual == window_count | |
| 209 return (result, 'WindowCount (expected %d, actual %d)' % | |
| 210 (window_count, actual)) | |
| 211 def TabCount(): | |
| 212 actual = self.GetTabCount(active_window) | |
| 213 result = actual == tab_count | |
| 214 return (result, 'TabCount (expected %d, actual %d)' % | |
| 215 (tab_count, actual)) | |
| 216 tab.Close(True) | |
| 217 self._WaitFor(WindowCount) | |
| 218 self._WaitFor(TabCount) | |
| 219 return False | |
| 220 if a == 'closewindow': | |
| 221 window_count = self.browser.num_windows - 1 | |
| 222 def WindowCount(): | |
| 223 result = self.GetBrowserWindowCount() == window_count | |
| 224 return (result, 'WindowCount (expected %d)' % window_count) | |
| 225 self._RunInActiveWindow(pyauto.IDC_CLOSE_WINDOW) | |
| 226 self._WaitFor(WindowCount) | |
| 227 if a == 'dragtabout': | |
| 228 return False | |
| 229 if a == 'dragtableft': | |
| 230 self._RunAsyncInActiveWindow(pyauto.IDC_MOVE_TAB_PREVIOUS) | |
| 231 if a == 'dragtabright': | |
| 232 self._RunAsyncInActiveWindow(pyauto.IDC_MOVE_TAB_NEXT) | |
| 233 if a == 'lasttab': | |
| 234 self._RunAsyncInActiveWindow(pyauto.IDC_SELECT_PREVIOUS_TAB) | |
| 235 if a == 'nexttab': | |
| 236 self._RunAsyncInActiveWindow(pyauto.IDC_SELECT_NEXT_TAB) | |
| 237 if a == 'restoretab': | |
| 238 active_window = self.browser.window_position | |
| 239 self.ApplyAccelerator(pyauto.IDC_RESTORE_TAB, active_window) | |
| 240 self._GrabTab().WaitForTabToBeRestored(self.action_max_timeout_ms()) | |
| 241 ui_model.UpdateState(self.browser, action) | |
| 242 return False | |
| 243 return True | |
| 244 | |
| 245 | |
| 246 if __name__ == '__main__': | |
| 247 pyauto_functional.Main() | |
| OLD | NEW |