summaryrefslogtreecommitdiffstats
path: root/chrome/test/functional/history.py
blob: 3875dc034b98a1bda12ab1f3b5743ecc2813cf1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/python
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import time

import pyauto_functional  # Must be imported before pyauto
import pyauto
import test_utils


class HistoryTest(pyauto.PyUITest):
  """TestCase for History."""

  def testBasic(self):
    url = self.GetFileURLForDataPath('title2.html')
    title = 'Title Of Awesomeness'
    self.NavigateToURL(url)

    history = self.GetHistoryInfo().History()
    self.assertEqual(1, len(history))
    self.assertEqual(title, history[0]['title'])
    self.assertEqual(url, history[0]['url'])

  def Debug(self):
    """Test method for experimentation.

    This method will not run automatically.
    """
    while True:
      raw_input('Interact with the browser and hit <enter> to dump history.. ')
      print '*' * 20
      self.pprint(self.GetHistoryInfo().History())

  def testHistoryPersists(self):
    """Verify that history persists after session restart."""
    assert not self.GetHistoryInfo().History(), 'Expecting clean history.'
    url = self.GetFileURLForDataPath('title2.html')
    title = 'Title Of Awesomeness'
    self.NavigateToURL(url)
    history = self.GetHistoryInfo().History()
    self.assertEqual(1, len(history))
    self.assertEqual(title, history[0]['title'])
    self.assertEqual(url, history[0]['url'])
    self.RestartBrowser(clear_profile=False)
    # Verify that history persists.
    history = self.GetHistoryInfo().History()
    self.assertEqual(1, len(history))
    self.assertEqual(title, history[0]['title'])
    self.assertEqual(url, history[0]['url'])

  def testInvalidURLNoHistory(self):
    """Invalid URLs should not go in history."""
    assert not self.GetHistoryInfo().History(), 'Expecting clean history.'
    urls = [ self.GetFileURLForPath('some_non-existing_path'),
             self.GetFileURLForPath('another_non-existing_path'),
           ]
    for url in urls:
      if not url.startswith('file://'):
        logging.warn('Using %s. Might depend on how dns failures are handled'
                     'on the network' % url)
      self.NavigateToURL(url)
    self.assertEqual(0, len(self.GetHistoryInfo().History()))

  def testNewTabNoHistory(self):
    """New tab page - chrome://newtab/ should not show up in history."""
    assert not self.GetHistoryInfo().History(), 'Expecting clean history.'
    self.AppendTab(pyauto.GURL('chrome://newtab/'))
    self.assertEqual(0, len(self.GetHistoryInfo().History()))

  def testIncognitoNoHistory(self):
    """Incognito browsing should not show up in history."""
    assert not self.GetHistoryInfo().History(), 'Expecting clean history.'
    url = self.GetFileURLForDataPath('title2.html')
    self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW)
    self.NavigateToURL(url, 1, 0)
    self.assertEqual(0, len(self.GetHistoryInfo().History()))

  def testStarredBookmarkInHistory(self):
    """Verify "starred" URLs in history."""
    url = self.GetFileURLForDataPath('title2.html')
    title = 'Title Of Awesomeness'
    self.NavigateToURL(url)

    # Should not be starred in history yet.
    history = self.GetHistoryInfo().History()
    self.assertEqual(1, len(history))
    self.assertFalse(history[0]['starred'])

    # Bookmark the URL.
    bookmarks = self.GetBookmarkModel()
    bar_id = bookmarks.BookmarkBar()['id']
    self.AddBookmarkURL(bar_id, 0, title, url)

    # Should be starred now.
    history = self.GetHistoryInfo().History()
    self.assertEqual(1, len(history))
    self.assertTrue(history[0]['starred'])

    # Remove bookmark.
    bookmarks = self.GetBookmarkModel()
    node = bookmarks.FindByTitle(title)
    self.assertTrue(node)
    id = node[0]['id']
    self.RemoveBookmark(id)

    # Should not be starred anymore.
    history = self.GetHistoryInfo().History()
    self.assertEqual(1, len(history))
    self.assertFalse(history[0]['starred'])

  def testNavigateMultiTimes(self):
    """Multiple navigations to the same url should have a single history."""
    assert not self.GetHistoryInfo().History(), 'Expecting clean history.'
    url = self.GetFileURLForDataPath('title2.html')
    for i in range(5):
      self.NavigateToURL(url)
    self.assertEqual(1, len(self.GetHistoryInfo().History()))

  def testMultiTabsWindowsHistory(self):
    """Verify history with multiple windows and tabs."""
    assert not self.GetHistoryInfo().History(), 'Expecting clean history.'
    urls = []
    for name in ['title2.html', 'title1.html', 'title3.html', 'simple.html']:
       urls.append(self.GetFileURLForDataPath(name))
    num_urls = len(urls)
    assert num_urls == 4, 'Need 4 urls'

    self.NavigateToURL(urls[0], 0, 0)        # window 0, tab 0
    self.OpenNewBrowserWindow(True)
    self.AppendTab(pyauto.GURL(urls[1]), 0)  # window 0, tab 1
    self.AppendTab(pyauto.GURL(urls[2]), 1)  # window 1
    self.AppendTab(pyauto.GURL(urls[3]), 1)  # window 1

    history = self.GetHistoryInfo().History()
    self.assertEqual(num_urls, len(history))
    # The history should be ordered most recent first.
    for i in range(num_urls):
      self.assertEqual(urls[-1 - i], history[i]['url'])

  def testDownloadNoHistory(self):
    """Downloaded URLs should not show up in history."""
    zip_file = 'a_zip_file.zip'
    assert not self.GetHistoryInfo().History(), 'Expecting clean history.'
    test_utils.DownloadFileFromDownloadsDataDir(self, zip_file)
    test_utils.RemoveDownloadedTestFile(self, zip_file)
    # We shouldn't have any history
    history = self.GetHistoryInfo().History()
    self.assertEqual(0, len(history))

  def testRedirectHistory(self):
    """HTTP meta-refresh redirects should have separate history entries."""
    assert not self.GetHistoryInfo().History(), 'Expecting clean history.'
    file_url = self.GetFileURLForDataPath('History', 'redirector.html')
    landing_url = self.GetFileURLForDataPath('History', 'landing.html')
    tab = self.GetBrowserWindow(0).GetTab(0)
    tab.NavigateToURLBlockUntilNavigationsComplete(pyauto.GURL(file_url), 2)
    self.assertEqual(landing_url, self.GetActiveTabURL().spec())
    # We should have two history items
    history = self.GetHistoryInfo().History()
    self.assertEqual(2, len(history))
    self.assertEqual(landing_url, history[0]['url'])

  def testForge(self):
    """Brief test of forging history items.

    Note the history system can tweak values (e.g. lower-case a URL or
    append an '/' on it) so be careful with exact comparison.
    """
    assert not self.GetHistoryInfo().History(), 'Expecting clean history.'
    # Minimal interface
    self.AddHistoryItem({'url': 'http://ZOINKS'})
    history = self.GetHistoryInfo().History()
    self.assertEqual(1, len(history))
    self.assertTrue('zoinks' in history[0]['url'])  # yes it gets lower-cased.
    # Python's time might be slightly off (~10 ms) from Chrome's time (on win).
    # time.time() on win counts in 1ms steps whereas it's 1us on linux.
    # So give the new history item some time separation, so that we can rely
    # on the history ordering.
    def _GetTimeLaterThan(tm):
      y = time.time()
      if y - tm < 0.5:  # 0.5s should be an acceptable separation
        return 0.5 + y
    new_time = _GetTimeLaterThan(history[0]['time'])
    # Full interface (specify both title and url)
    self.AddHistoryItem({'title': 'Google',
                         'url': 'http://www.google.com',
                         'time': new_time})
    # Expect a second item
    history = self.GetHistoryInfo().History()
    self.assertEqual(2, len(history))
    # And make sure our forged item is there.
    self.assertEqual('Google', history[0]['title'])
    self.assertTrue('google.com' in history[0]['url'])
    self.assertTrue(abs(new_time - history[0]['time']) < 1.0)

  def testHttpsHistory(self):
    """Verify a site using https protocol shows up within history."""
    https_url = 'https://encrypted.google.com/'
    url_title = 'Google'
    self.NavigateToURL(https_url)
    history = self.GetHistoryInfo().History()
    self.assertEqual(len(history), 1)
    self.assertEqual(url_title, history[0]['title'])
    self.assertEqual(https_url, history[0]['url'])

  def testFtpHistory(self):
    """Verify a site using ftp protocol shows up within history."""
    ftp_server = self.StartFTPServer(os.path.join('chrome', 'test', 'data'))
    ftp_title = 'A Small Hello'
    ftp_url = self.GetFtpURLForDataPath(ftp_server, 'History', 'landing.html')
    self.NavigateToURL(ftp_url)
    history = self.GetHistoryInfo().History()
    self.assertEqual(len(history), 1)
    self.assertEqual(ftp_title, history[0]['title'])
    self.StopFTPServer(ftp_server)

  def _CheckHistory(self, title, url, length, index=0):
    """Verify that the current history matches expectations.

    Verify that history item has the given title and url
    and that length of history list is as expected.

    Args:
      title: Expected title of given web page.
      url: Expected address of given web page.
      length: Expected length of history list.
      index: Position of item we want to check in history list.
    """
    history = self.GetHistoryInfo().History()
    self.assertEqual(
        length, len(history),
        msg='History length: expected = %d, actual = %d.'
            % (length, len(history)))
    self.assertEqual(
        title, history[index]['title'],
        msg='Title: expected = %s, actual = %s.'
            % (title,  history[index]['title']))
    self.assertEqual(
        url, history[index]['url'], msg='URL: expected = %s, actual = %s.'
            % (url,  history[index]['url']))

  def _NavigateAndCheckHistory(self, title, page, length):
    """Navigate to a page, then verify the history.

    Args:
      title: Title of given web page.
      page: Filename of given web page.
      length: Length of history list.
    """
    url = self.GetFileURLForDataPath(page)
    self.NavigateToURL(url)
    self._CheckHistory(title, url, length)

  def testNavigateBringPageToTop(self):
    """Verify that navigation brings current page to top of history list."""
    self._NavigateAndCheckHistory('Title Of Awesomeness', 'title2.html', 1)
    self._NavigateAndCheckHistory('Title Of More Awesomeness', 'title3.html',
                                  2)

  def testReloadBringPageToTop(self):
    """Verify that reloading a page brings it to top of history list."""
    url1 = self.GetFileURLForDataPath('title2.html')
    title1 = 'Title Of Awesomeness'
    self._NavigateAndCheckHistory(title1, 'title2.html', 1)

    url2 = self.GetFileURLForDataPath('title3.html')
    title2 = 'Title Of More Awesomeness'
    self.AppendTab(pyauto.GURL(url2))
    self._CheckHistory(title2, url2, 2)

    self.ActivateTab(0)
    self.ReloadActiveTab()
    self._CheckHistory(title1, url1, 2)

  def testBackForwardBringPageToTop(self):
    """Verify that back/forward brings current page to top of history list."""
    url1 = self.GetFileURLForDataPath('title2.html')
    title1 = 'Title Of Awesomeness'
    self._NavigateAndCheckHistory(title1, 'title2.html', 1)

    url2 = self.GetFileURLForDataPath('title3.html')
    title2 = 'Title Of More Awesomeness'
    self._NavigateAndCheckHistory(title2, 'title3.html', 2)

    tab = self.GetBrowserWindow(0).GetTab(0)
    tab.GoBack()
    self._CheckHistory(title1, url1, 2)
    tab.GoForward()
    self._CheckHistory(title2, url2, 2)

  def testAppendTabAddPage(self):
    """Verify that opening a new tab adds that page to history."""
    self._NavigateAndCheckHistory('Title Of Awesomeness', 'title2.html', 1)

    url2 = self.GetFileURLForDataPath('title3.html')
    title2 = 'Title Of More Awesomeness'
    self.AppendTab(pyauto.GURL(url2))
    self._CheckHistory(title2, url2, 2)

  def testOpenWindowAddPage(self):
    """Verify that opening new window to a page adds the page to history."""
    self._NavigateAndCheckHistory('Title Of Awesomeness', 'title2.html', 1)

    url2 = self.GetFileURLForDataPath('title3.html')
    title2 = 'Title Of More Awesomeness'
    self.OpenNewBrowserWindow(True)
    self.NavigateToURL(url2, 1)
    self._CheckHistory(title2, url2, 2)

  def testSubmitFormAddsTargetPage(self):
    """Verify that submitting form adds target page to history list."""
    url1 = self.GetFileURLForDataPath('History', 'form.html')
    self.NavigateToURL(url1)
    self.assertTrue(self.SubmitForm('form'))
    url2 = self.GetFileURLForDataPath('History', 'target.html')
    self.assertEqual(
        'SUCCESS',
        self.GetDOMValue('document.getElementById("result").innerHTML'))
    self._CheckHistory('Target Page', url2, 2)

  def testOneHistoryTabPerWindow(self):
    """Verify history shortcut opens only one history tab per window.

    Also, make sure that existing history tab is activated.
    """
    # Invoke History.
    self.RunCommand(pyauto.IDC_SHOW_HISTORY)
    self.assertEqual('History', self.GetActiveTabTitle(),
                     msg='History page was not opened.')

    # Open new tab, invoke History again.
    self.RunCommand(pyauto.IDC_NEW_TAB)
    self.RunCommand(pyauto.IDC_SHOW_HISTORY)

    # Verify there is only one history tab, and that it is activated.
    tab0url = self.GetBrowserInfo()['windows'][0]['tabs'][0]['url']
    self.assertEqual(
        'chrome://history/', tab0url, msg='Tab 0: expected = %s, actual = %s.'
            % ('chrome://history/',  tab0url))

    tab1url = self.GetBrowserInfo()['windows'][0]['tabs'][1]['url']
    self.assertNotEqual(
        'chrome://history/', tab1url,
        msg='Tab 1: History page not expected.')

    self.assertEqual('History', self.GetActiveTabTitle(),
                     msg='History page is not activated.')


if __name__ == '__main__':
  pyauto_functional.Main()