| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2012 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 import os | |
| 7 import time | |
| 8 | |
| 9 import pyauto_functional # Must be imported before pyauto | |
| 10 import pyauto | |
| 11 import test_utils | |
| 12 | |
| 13 | |
| 14 class HistoryTest(pyauto.PyUITest): | |
| 15 """TestCase for History.""" | |
| 16 | |
| 17 def testBasic(self): | |
| 18 url = self.GetFileURLForDataPath('title2.html') | |
| 19 title = 'Title Of Awesomeness' | |
| 20 self.NavigateToURL(url) | |
| 21 | |
| 22 history = self.GetHistoryInfo().History() | |
| 23 self.assertEqual(1, len(history)) | |
| 24 self.assertEqual(title, history[0]['title']) | |
| 25 self.assertEqual(url, history[0]['url']) | |
| 26 | |
| 27 def Debug(self): | |
| 28 """Test method for experimentation. | |
| 29 | |
| 30 This method will not run automatically. | |
| 31 """ | |
| 32 while True: | |
| 33 raw_input('Interact with the browser and hit <enter> to dump history.. ') | |
| 34 print '*' * 20 | |
| 35 self.pprint(self.GetHistoryInfo().History()) | |
| 36 | |
| 37 def testHistoryPersists(self): | |
| 38 """Verify that history persists after session restart.""" | |
| 39 assert not self.GetHistoryInfo().History(), 'Expecting clean history.' | |
| 40 url = self.GetFileURLForDataPath('title2.html') | |
| 41 title = 'Title Of Awesomeness' | |
| 42 self.NavigateToURL(url) | |
| 43 history = self.GetHistoryInfo().History() | |
| 44 self.assertEqual(1, len(history)) | |
| 45 self.assertEqual(title, history[0]['title']) | |
| 46 self.assertEqual(url, history[0]['url']) | |
| 47 self.RestartBrowser(clear_profile=False) | |
| 48 # Verify that history persists. | |
| 49 history = self.GetHistoryInfo().History() | |
| 50 self.assertEqual(1, len(history)) | |
| 51 self.assertEqual(title, history[0]['title']) | |
| 52 self.assertEqual(url, history[0]['url']) | |
| 53 | |
| 54 def testInvalidURLNoHistory(self): | |
| 55 """Invalid URLs should not go in history.""" | |
| 56 assert not self.GetHistoryInfo().History(), 'Expecting clean history.' | |
| 57 urls = [ self.GetFileURLForPath('some_non-existing_path'), | |
| 58 self.GetFileURLForPath('another_non-existing_path'), | |
| 59 ] | |
| 60 for url in urls: | |
| 61 if not url.startswith('file://'): | |
| 62 logging.warn('Using %s. Might depend on how dns failures are handled' | |
| 63 'on the network' % url) | |
| 64 self.NavigateToURL(url) | |
| 65 self.assertEqual(0, len(self.GetHistoryInfo().History())) | |
| 66 | |
| 67 def testNewTabNoHistory(self): | |
| 68 """New tab page - chrome://newtab/ should not show up in history.""" | |
| 69 assert not self.GetHistoryInfo().History(), 'Expecting clean history.' | |
| 70 self.AppendTab(pyauto.GURL('chrome://newtab/')) | |
| 71 self.assertEqual(0, len(self.GetHistoryInfo().History())) | |
| 72 | |
| 73 def testIncognitoNoHistory(self): | |
| 74 """Incognito browsing should not show up in history.""" | |
| 75 assert not self.GetHistoryInfo().History(), 'Expecting clean history.' | |
| 76 url = self.GetFileURLForDataPath('title2.html') | |
| 77 self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW) | |
| 78 self.NavigateToURL(url, 1, 0) | |
| 79 self.assertEqual(0, len(self.GetHistoryInfo().History())) | |
| 80 | |
| 81 def testStarredBookmarkInHistory(self): | |
| 82 """Verify "starred" URLs in history.""" | |
| 83 url = self.GetFileURLForDataPath('title2.html') | |
| 84 title = 'Title Of Awesomeness' | |
| 85 self.NavigateToURL(url) | |
| 86 | |
| 87 # Should not be starred in history yet. | |
| 88 history = self.GetHistoryInfo().History() | |
| 89 self.assertEqual(1, len(history)) | |
| 90 self.assertFalse(history[0]['starred']) | |
| 91 | |
| 92 # Bookmark the URL. | |
| 93 bookmarks = self.GetBookmarkModel() | |
| 94 bar_id = bookmarks.BookmarkBar()['id'] | |
| 95 self.AddBookmarkURL(bar_id, 0, title, url) | |
| 96 | |
| 97 # Should be starred now. | |
| 98 history = self.GetHistoryInfo().History() | |
| 99 self.assertEqual(1, len(history)) | |
| 100 self.assertTrue(history[0]['starred']) | |
| 101 | |
| 102 # Remove bookmark. | |
| 103 bookmarks = self.GetBookmarkModel() | |
| 104 node = bookmarks.FindByTitle(title) | |
| 105 self.assertTrue(node) | |
| 106 id = node[0]['id'] | |
| 107 self.RemoveBookmark(id) | |
| 108 | |
| 109 # Should not be starred anymore. | |
| 110 history = self.GetHistoryInfo().History() | |
| 111 self.assertEqual(1, len(history)) | |
| 112 self.assertFalse(history[0]['starred']) | |
| 113 | |
| 114 def testNavigateMultiTimes(self): | |
| 115 """Multiple navigations to the same url should have a single history.""" | |
| 116 assert not self.GetHistoryInfo().History(), 'Expecting clean history.' | |
| 117 url = self.GetFileURLForDataPath('title2.html') | |
| 118 for i in range(5): | |
| 119 self.NavigateToURL(url) | |
| 120 self.assertEqual(1, len(self.GetHistoryInfo().History())) | |
| 121 | |
| 122 def testMultiTabsWindowsHistory(self): | |
| 123 """Verify history with multiple windows and tabs.""" | |
| 124 assert not self.GetHistoryInfo().History(), 'Expecting clean history.' | |
| 125 urls = [] | |
| 126 for name in ['title2.html', 'title1.html', 'title3.html', 'simple.html']: | |
| 127 urls.append(self.GetFileURLForDataPath(name)) | |
| 128 num_urls = len(urls) | |
| 129 assert num_urls == 4, 'Need 4 urls' | |
| 130 | |
| 131 self.NavigateToURL(urls[0], 0, 0) # window 0, tab 0 | |
| 132 self.OpenNewBrowserWindow(True) | |
| 133 self.AppendTab(pyauto.GURL(urls[1]), 0) # window 0, tab 1 | |
| 134 self.AppendTab(pyauto.GURL(urls[2]), 1) # window 1 | |
| 135 self.AppendTab(pyauto.GURL(urls[3]), 1) # window 1 | |
| 136 | |
| 137 history = self.GetHistoryInfo().History() | |
| 138 self.assertEqual(num_urls, len(history)) | |
| 139 # The history should be ordered most recent first. | |
| 140 for i in range(num_urls): | |
| 141 self.assertEqual(urls[-1 - i], history[i]['url']) | |
| 142 | |
| 143 def testDownloadNoHistory(self): | |
| 144 """Downloaded URLs should not show up in history.""" | |
| 145 zip_file = 'a_zip_file.zip' | |
| 146 assert not self.GetHistoryInfo().History(), 'Expecting clean history.' | |
| 147 test_utils.DownloadFileFromDownloadsDataDir(self, zip_file) | |
| 148 test_utils.RemoveDownloadedTestFile(self, zip_file) | |
| 149 # We shouldn't have any history | |
| 150 history = self.GetHistoryInfo().History() | |
| 151 self.assertEqual(0, len(history)) | |
| 152 | |
| 153 def testRedirectHistory(self): | |
| 154 """HTTP meta-refresh redirects should have separate history entries.""" | |
| 155 assert not self.GetHistoryInfo().History(), 'Expecting clean history.' | |
| 156 file_url = self.GetFileURLForDataPath('History', 'redirector.html') | |
| 157 landing_url = self.GetFileURLForDataPath('History', 'landing.html') | |
| 158 self.NavigateToURL(file_url, tab_index=0, windex=0, navigation_count=2) | |
| 159 self.assertEqual(landing_url, self.GetActiveTabURL().spec()) | |
| 160 # We should have two history items | |
| 161 history = self.GetHistoryInfo().History() | |
| 162 self.assertEqual(2, len(history)) | |
| 163 self.assertEqual(landing_url, history[0]['url']) | |
| 164 | |
| 165 def testForge(self): | |
| 166 """Brief test of forging history items. | |
| 167 | |
| 168 Note the history system can tweak values (e.g. lower-case a URL or | |
| 169 append an '/' on it) so be careful with exact comparison. | |
| 170 """ | |
| 171 assert not self.GetHistoryInfo().History(), 'Expecting clean history.' | |
| 172 # Minimal interface | |
| 173 self.AddHistoryItem({'url': 'http://ZOINKS'}) | |
| 174 history = self.GetHistoryInfo().History() | |
| 175 self.assertEqual(1, len(history)) | |
| 176 self.assertTrue('zoinks' in history[0]['url']) # yes it gets lower-cased. | |
| 177 # Python's time might be slightly off (~10 ms) from Chrome's time (on win). | |
| 178 # time.time() on win counts in 1ms steps whereas it's 1us on linux. | |
| 179 # So give the new history item some time separation, so that we can rely | |
| 180 # on the history ordering. | |
| 181 def _GetTimeLaterThan(tm): | |
| 182 y = time.time() | |
| 183 if y - tm < 0.5: # 0.5s should be an acceptable separation | |
| 184 return 0.5 + y | |
| 185 new_time = _GetTimeLaterThan(history[0]['time']) | |
| 186 # Full interface (specify both title and url) | |
| 187 self.AddHistoryItem({'title': 'Google', | |
| 188 'url': 'http://www.google.com', | |
| 189 'time': new_time}) | |
| 190 # Expect a second item | |
| 191 history = self.GetHistoryInfo().History() | |
| 192 self.assertEqual(2, len(history)) | |
| 193 # And make sure our forged item is there. | |
| 194 self.assertEqual('Google', history[0]['title']) | |
| 195 self.assertTrue('google.com' in history[0]['url']) | |
| 196 self.assertTrue(abs(new_time - history[0]['time']) < 1.0) | |
| 197 | |
| 198 def testHttpsHistory(self): | |
| 199 """Verify a site using https protocol shows up within history.""" | |
| 200 https_url = 'https://encrypted.google.com/' | |
| 201 url_title = 'Google' | |
| 202 self.NavigateToURL(https_url) | |
| 203 history = self.GetHistoryInfo().History() | |
| 204 self.assertEqual(len(history), 1) | |
| 205 self.assertEqual(url_title, history[0]['title']) | |
| 206 self.assertEqual(https_url, history[0]['url']) | |
| 207 | |
| 208 def testFtpHistory(self): | |
| 209 """Verify a site using ftp protocol shows up within history.""" | |
| 210 ftp_server = self.StartFTPServer(os.path.join('chrome', 'test', 'data')) | |
| 211 ftp_title = 'A Small Hello' | |
| 212 ftp_url = self.GetFtpURLForDataPath(ftp_server, 'History', 'landing.html') | |
| 213 self.NavigateToURL(ftp_url) | |
| 214 history = self.GetHistoryInfo().History() | |
| 215 self.assertEqual(len(history), 1) | |
| 216 self.assertEqual(ftp_title, history[0]['title']) | |
| 217 self.StopFTPServer(ftp_server) | |
| 218 | |
| 219 def _CheckHistory(self, title, url, length, index=0): | |
| 220 """Verify that the current history matches expectations. | |
| 221 | |
| 222 Verify that history item has the given title and url | |
| 223 and that length of history list is as expected. | |
| 224 | |
| 225 Args: | |
| 226 title: Expected title of given web page. | |
| 227 url: Expected address of given web page. | |
| 228 length: Expected length of history list. | |
| 229 index: Position of item we want to check in history list. | |
| 230 """ | |
| 231 history = self.GetHistoryInfo().History() | |
| 232 self.assertEqual( | |
| 233 length, len(history), | |
| 234 msg='History length: expected = %d, actual = %d.' | |
| 235 % (length, len(history))) | |
| 236 self.assertEqual( | |
| 237 title, history[index]['title'], | |
| 238 msg='Title: expected = %s, actual = %s.' | |
| 239 % (title, history[index]['title'])) | |
| 240 self.assertEqual( | |
| 241 url, history[index]['url'], msg='URL: expected = %s, actual = %s.' | |
| 242 % (url, history[index]['url'])) | |
| 243 | |
| 244 def _NavigateAndCheckHistory(self, title, page, length): | |
| 245 """Navigate to a page, then verify the history. | |
| 246 | |
| 247 Args: | |
| 248 title: Title of given web page. | |
| 249 page: Filename of given web page. | |
| 250 length: Length of history list. | |
| 251 """ | |
| 252 url = self.GetFileURLForDataPath(page) | |
| 253 self.NavigateToURL(url) | |
| 254 self._CheckHistory(title, url, length) | |
| 255 | |
| 256 def testNavigateBringPageToTop(self): | |
| 257 """Verify that navigation brings current page to top of history list.""" | |
| 258 self._NavigateAndCheckHistory('Title Of Awesomeness', 'title2.html', 1) | |
| 259 self._NavigateAndCheckHistory('Title Of More Awesomeness', 'title3.html', | |
| 260 2) | |
| 261 | |
| 262 def testReloadBringPageToTop(self): | |
| 263 """Verify that reloading a page brings it to top of history list.""" | |
| 264 url1 = self.GetFileURLForDataPath('title2.html') | |
| 265 title1 = 'Title Of Awesomeness' | |
| 266 self._NavigateAndCheckHistory(title1, 'title2.html', 1) | |
| 267 | |
| 268 url2 = self.GetFileURLForDataPath('title3.html') | |
| 269 title2 = 'Title Of More Awesomeness' | |
| 270 self.AppendTab(pyauto.GURL(url2)) | |
| 271 self._CheckHistory(title2, url2, 2) | |
| 272 | |
| 273 self.ReloadTab() | |
| 274 self._CheckHistory(title1, url1, 2) | |
| 275 | |
| 276 def testBackForwardBringPageToTop(self): | |
| 277 """Verify that back/forward brings current page to top of history list.""" | |
| 278 url1 = self.GetFileURLForDataPath('title2.html') | |
| 279 title1 = 'Title Of Awesomeness' | |
| 280 self._NavigateAndCheckHistory(title1, 'title2.html', 1) | |
| 281 | |
| 282 url2 = self.GetFileURLForDataPath('title3.html') | |
| 283 title2 = 'Title Of More Awesomeness' | |
| 284 self._NavigateAndCheckHistory(title2, 'title3.html', 2) | |
| 285 | |
| 286 self.TabGoBack() | |
| 287 self._CheckHistory(title1, url1, 2) | |
| 288 self.TabGoForward() | |
| 289 self._CheckHistory(title2, url2, 2) | |
| 290 | |
| 291 def testAppendTabAddPage(self): | |
| 292 """Verify that opening a new tab adds that page to history.""" | |
| 293 self._NavigateAndCheckHistory('Title Of Awesomeness', 'title2.html', 1) | |
| 294 | |
| 295 url2 = self.GetFileURLForDataPath('title3.html') | |
| 296 title2 = 'Title Of More Awesomeness' | |
| 297 self.AppendTab(pyauto.GURL(url2)) | |
| 298 self._CheckHistory(title2, url2, 2) | |
| 299 | |
| 300 def testOpenWindowAddPage(self): | |
| 301 """Verify that opening new window to a page adds the page to history.""" | |
| 302 self._NavigateAndCheckHistory('Title Of Awesomeness', 'title2.html', 1) | |
| 303 | |
| 304 url2 = self.GetFileURLForDataPath('title3.html') | |
| 305 title2 = 'Title Of More Awesomeness' | |
| 306 self.OpenNewBrowserWindow(True) | |
| 307 self.NavigateToURL(url2, 1) | |
| 308 self._CheckHistory(title2, url2, 2) | |
| 309 | |
| 310 def testSubmitFormAddsTargetPage(self): | |
| 311 """Verify that submitting form adds target page to history list.""" | |
| 312 url1 = self.GetFileURLForDataPath('History', 'form.html') | |
| 313 self.NavigateToURL(url1) | |
| 314 self.assertTrue(self.SubmitForm('form')) | |
| 315 url2 = self.GetFileURLForDataPath('History', 'target.html') | |
| 316 self.assertEqual( | |
| 317 'SUCCESS', | |
| 318 self.GetDOMValue('document.getElementById("result").innerHTML')) | |
| 319 self._CheckHistory('Target Page', url2, 2) | |
| 320 | |
| 321 def testOneHistoryTabPerWindow(self): | |
| 322 """Verify history shortcut opens only one history tab per window. | |
| 323 | |
| 324 Also, make sure that existing history tab is activated. | |
| 325 """ | |
| 326 command_line = self.GetBrowserInfo()['properties']['command_line_string'] | |
| 327 history_url = 'chrome://chrome/history/' | |
| 328 | |
| 329 # Invoke History. | |
| 330 self.RunCommand(pyauto.IDC_SHOW_HISTORY) | |
| 331 # Even when the above command completes, the currently-active tab title | |
| 332 # is 'Loading...' for a brief time while the history page loads. | |
| 333 self.assertTrue( | |
| 334 self.WaitUntil(lambda: 'History' == self.GetActiveTabTitle()), | |
| 335 msg='History page was not opened.') | |
| 336 | |
| 337 # Open new tab, invoke History again. | |
| 338 self.RunCommand(pyauto.IDC_NEW_TAB) | |
| 339 self.RunCommand(pyauto.IDC_SHOW_HISTORY) | |
| 340 | |
| 341 # Verify there is only one history tab, and that it is activated. | |
| 342 tab0url = self.GetBrowserInfo()['windows'][0]['tabs'][0]['url'] | |
| 343 self.assertEqual( | |
| 344 history_url, tab0url, msg='Tab 0: expected = %s, actual = %s.' | |
| 345 % (history_url, tab0url)) | |
| 346 | |
| 347 tab1url = self.GetBrowserInfo()['windows'][0]['tabs'][1]['url'] | |
| 348 self.assertNotEqual( | |
| 349 history_url, tab1url, | |
| 350 msg='Tab 1: History page not expected.') | |
| 351 | |
| 352 self.assertTrue( | |
| 353 self.WaitUntil(lambda: 'History' == self.GetActiveTabTitle()), | |
| 354 msg='History page is not activated.') | |
| 355 | |
| 356 | |
| 357 if __name__ == '__main__': | |
| 358 pyauto_functional.Main() | |
| OLD | NEW |