OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import os | 6 import os |
7 | 7 |
8 import pyauto_functional # Must be imported before pyauto | 8 import pyauto_functional # Must be imported before pyauto |
9 import pyauto | 9 import pyauto |
| 10 import test_utils |
10 | 11 |
11 | 12 |
12 class NTPTest(pyauto.PyUITest): | 13 class NTPTest(pyauto.PyUITest): |
13 """Test of the NTP.""" | 14 """Test of the NTP.""" |
14 | 15 |
15 def Debug(self): | 16 def Debug(self): |
16 """Test method for experimentation. | 17 """Test method for experimentation. |
17 | 18 |
18 This method is not run automatically. | 19 This method is not run automatically. |
19 """ | 20 """ |
20 while True: | 21 while True: |
21 raw_input('Interact with the browser and hit <enter> to dump NTP info...') | 22 raw_input('Interact with the browser and hit <enter> to dump NTP info...') |
22 print '*' * 20 | 23 print '*' * 20 |
23 import pprint | 24 import pprint |
24 pp = pprint.PrettyPrinter(indent=2) | 25 pp = pprint.PrettyPrinter(indent=2) |
25 pp.pprint(self._GetNTPInfo()) | 26 pp.pprint(self._GetNTPInfo()) |
26 | 27 |
27 def __init__(self, methodName='runTest'): | 28 def __init__(self, methodName='runTest'): |
28 super(NTPTest, self).__init__(methodName) | 29 super(NTPTest, self).__init__(methodName) |
29 | 30 |
30 # Create some dummy file urls we can use in the tests. | 31 # Create some dummy file urls we can use in the tests. |
31 filenames = ['title1.html', 'title2.html'] | 32 filenames = ['title1.html', 'title2.html'] |
32 titles = [u'', u'Title Of Awesomeness'] | 33 titles = [u'', u'Title Of Awesomeness'] |
33 urls = map(lambda name: self.GetFileURLForDataPath(name), filenames) | 34 urls = map(lambda name: self.GetFileURLForDataPath(name), filenames) |
34 self.PAGES = map(lambda url, title: {'url': url, 'title': title}, | 35 self.PAGES = map(lambda url, title: {'url': url, 'title': title}, |
35 urls, titles) | 36 urls, titles) |
36 | 37 |
| 38 def _NTPContainsThumbnail(self, check_thumbnail): |
| 39 """Returns whether the NTP's Most Visited section contains the given |
| 40 thumbnail.""" |
| 41 for thumbnail in self.GetNTPThumbnails(): |
| 42 if check_thumbnail['url'] == thumbnail['url']: |
| 43 return True |
| 44 return False |
| 45 |
37 def testFreshProfile(self): | 46 def testFreshProfile(self): |
38 """Tests that the NTP with a fresh profile is correct""" | 47 """Tests that the NTP with a fresh profile is correct""" |
39 thumbnails = self.GetNTPThumbnails() | 48 thumbnails = self.GetNTPThumbnails() |
40 default_sites = self.GetNTPDefaultSites() | 49 default_sites = self.GetNTPDefaultSites() |
41 self.assertEqual(len(default_sites), len(thumbnails)) | 50 self.assertEqual(len(default_sites), len(thumbnails)) |
42 for thumbnail, default_site in zip(thumbnails, default_sites): | 51 for thumbnail, default_site in zip(thumbnails, default_sites): |
43 self.assertEqual(thumbnail['url'], default_site) | 52 self.assertEqual(thumbnail['url'], default_site) |
44 self.assertEqual(0, len(self.GetNTPRecentlyClosed())) | 53 self.assertEqual(0, len(self.GetNTPRecentlyClosed())) |
45 | 54 |
46 def testRemoveDefaultThumbnails(self): | 55 def testRemoveDefaultThumbnails(self): |
47 """Tests that the default thumbnails can be removed""" | 56 """Tests that the default thumbnails can be removed""" |
48 self.RemoveNTPDefaultThumbnails() | 57 self.RemoveNTPDefaultThumbnails() |
49 self.assertFalse(self.GetNTPThumbnails()) | 58 self.assertFalse(self.GetNTPThumbnails()) |
50 self.RestoreAllNTPThumbnails() | 59 self.RestoreAllNTPThumbnails() |
51 self.assertEqual(len(self.GetNTPDefaultSites()), | 60 self.assertEqual(len(self.GetNTPDefaultSites()), |
52 len(self.GetNTPThumbnails())) | 61 len(self.GetNTPThumbnails())) |
53 self.RemoveNTPDefaultThumbnails() | 62 self.RemoveNTPDefaultThumbnails() |
54 self.assertFalse(self.GetNTPThumbnails()) | 63 self.assertFalse(self.GetNTPThumbnails()) |
55 | 64 |
56 def testOneMostVisitedSite(self): | 65 def testOneMostVisitedSite(self): |
57 """Tests that a site is added to the most visited sites""" | 66 """Tests that a site is added to the most visited sites""" |
58 self.RemoveNTPDefaultThumbnails() | 67 self.RemoveNTPDefaultThumbnails() |
59 self.NavigateToURL(self.PAGES[1]['url']) | 68 self.NavigateToURL(self.PAGES[1]['url']) |
60 self.assertEqual(self.PAGES[1]['url'], self.GetNTPThumbnails()[0]['url']) | 69 thumbnail = self.GetNTPThumbnails()[0] |
61 self.assertEqual(self.PAGES[1]['title'], | 70 self.assertEqual(self.PAGES[1]['url'], thumbnail['url']) |
62 self.GetNTPThumbnails()[0]['title']) | 71 self.assertEqual(self.PAGES[1]['title'], thumbnail['title']) |
63 | 72 self.assertFalse(thumbnail['is_pinned']) |
64 def testOneRecentlyClosedTab(self): | |
65 """Tests that closing a tab populates the recently closed tabs list""" | |
66 self.RemoveNTPDefaultThumbnails() | |
67 self.AppendTab(pyauto.GURL(self.PAGES[1]['url'])) | |
68 self.GetBrowserWindow(0).GetTab(1).Close(True) | |
69 self.assertEqual(self.PAGES[1]['url'], | |
70 self.GetNTPRecentlyClosed()[0]['url']) | |
71 self.assertEqual(self.PAGES[1]['title'], | |
72 self.GetNTPRecentlyClosed()[0]['title']) | |
73 | 73 |
74 def testMoveThumbnailBasic(self): | 74 def testMoveThumbnailBasic(self): |
75 """Tests moving a thumbnail to a different index""" | 75 """Tests moving a thumbnail to a different index""" |
76 self.RemoveNTPDefaultThumbnails() | 76 self.RemoveNTPDefaultThumbnails() |
77 self.NavigateToURL(self.PAGES[0]['url']) | 77 self.NavigateToURL(self.PAGES[0]['url']) |
78 self.NavigateToURL(self.PAGES[1]['url']) | 78 self.NavigateToURL(self.PAGES[1]['url']) |
79 thumbnails = self.GetNTPThumbnails() | 79 thumbnails = self.GetNTPThumbnails() |
80 self.MoveNTPThumbnail(thumbnails[0], 1) | 80 self.MoveNTPThumbnail(thumbnails[0], 1) |
81 self.assertTrue(self.IsNTPThumbnailPinned(thumbnails[0])) | 81 self.assertTrue(self.IsNTPThumbnailPinned(thumbnails[0])) |
82 self.assertFalse(self.IsNTPThumbnailPinned(thumbnails[1])) | 82 self.assertFalse(self.IsNTPThumbnailPinned(thumbnails[1])) |
83 self.assertEqual(self.PAGES[0]['url'], self.GetNTPThumbnails()[1]['url']) | 83 self.assertEqual(self.PAGES[0]['url'], self.GetNTPThumbnails()[1]['url']) |
84 self.assertEqual(1, self.GetNTPThumbnailIndex(thumbnails[0])) | 84 self.assertEqual(1, self.GetNTPThumbnailIndex(thumbnails[0])) |
85 | 85 |
86 def testPinningThumbnailBasic(self): | 86 def testPinningThumbnailBasic(self): |
87 """Tests that we can pin/unpin a thumbnail""" | 87 """Tests that we can pin/unpin a thumbnail""" |
88 self.RemoveNTPDefaultThumbnails() | 88 self.RemoveNTPDefaultThumbnails() |
89 self.NavigateToURL(self.PAGES[0]['url']) | 89 self.NavigateToURL(self.PAGES[0]['url']) |
90 thumbnail1 = self.GetNTPThumbnails()[0] | 90 thumbnail1 = self.GetNTPThumbnails()[0] |
91 self.assertFalse(self.IsNTPThumbnailPinned(thumbnail1)) | 91 self.assertFalse(self.IsNTPThumbnailPinned(thumbnail1)) |
92 self.PinNTPThumbnail(thumbnail1) | 92 self.PinNTPThumbnail(thumbnail1) |
93 self.assertTrue(self.IsNTPThumbnailPinned(thumbnail1)) | 93 self.assertTrue(self.IsNTPThumbnailPinned(thumbnail1)) |
94 self.UnpinNTPThumbnail(thumbnail1) | 94 self.UnpinNTPThumbnail(thumbnail1) |
95 self.assertFalse(self.IsNTPThumbnailPinned(thumbnail1)) | 95 self.assertFalse(self.IsNTPThumbnailPinned(thumbnail1)) |
96 | 96 |
| 97 def testRemoveThumbnail(self): |
| 98 """Tests removing a thumbnail works""" |
| 99 self.RemoveNTPDefaultThumbnails() |
| 100 for page in self.PAGES: |
| 101 self.AppendTab(pyauto.GURL(page['url'])) |
| 102 |
| 103 thumbnails = self.GetNTPThumbnails() |
| 104 for thumbnail in thumbnails: |
| 105 self.assertEquals(thumbnail, self.GetNTPThumbnails()[0]) |
| 106 self.RemoveNTPThumbnail(thumbnail) |
| 107 self.assertFalse(self._NTPContainsThumbnail(thumbnail)) |
| 108 self.assertFalse(self.GetNTPThumbnails()) |
| 109 |
| 110 def testIncognitoNotAppearInMostVisited(self): |
| 111 """Tests that visiting a page in incognito mode does cause it to appear in |
| 112 the Most Visited section""" |
| 113 self.RemoveNTPDefaultThumbnails() |
| 114 self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW) |
| 115 self.NavigateToURL(self.PAGES[0]['url'], 1, 0) |
| 116 self.assertFalse(self.GetNTPThumbnails()) |
| 117 |
| 118 def testRestoreOncePinnedThumbnail(self): |
| 119 """Tests that after restoring a once pinned thumbnail, the thumbnail is |
| 120 not pinned""" |
| 121 self.RemoveNTPDefaultThumbnails() |
| 122 self.NavigateToURL(self.PAGES[0]['url']) |
| 123 thumbnail1 = self.GetNTPThumbnails()[0] |
| 124 self.PinNTPThumbnail(thumbnail1) |
| 125 self.RemoveNTPThumbnail(thumbnail1) |
| 126 self.RestoreAllNTPThumbnails() |
| 127 self.RemoveNTPDefaultThumbnails() |
| 128 self.assertFalse(self.IsNTPThumbnailPinned(thumbnail1)) |
| 129 |
| 130 def testThumbnailPersistence(self): |
| 131 """Tests that thumbnails persist across Chrome restarts""" |
| 132 self.RemoveNTPDefaultThumbnails() |
| 133 for page in self.PAGES: |
| 134 self.AppendTab(pyauto.GURL(page['url'])) |
| 135 thumbnails = self.GetNTPThumbnails() |
| 136 self.MoveNTPThumbnail(thumbnails[0], 1) |
| 137 thumbnails = self.GetNTPThumbnails() |
| 138 |
| 139 self.RestartBrowser(clear_profile=False) |
| 140 self.assertEqual(thumbnails, self.GetNTPThumbnails()) |
| 141 |
| 142 def testRestoreAllRemovedThumbnails(self): |
| 143 """Tests restoring all removed thumbnails""" |
| 144 for page in self.PAGES: |
| 145 self.AppendTab(pyauto.GURL(page['url'])) |
| 146 |
| 147 thumbnails = self.GetNTPThumbnails() |
| 148 for thumbnail in thumbnails: |
| 149 self.RemoveNTPThumbnail(thumbnail) |
| 150 |
| 151 self.RestoreAllNTPThumbnails() |
| 152 self.assertEquals(thumbnails, self.GetNTPThumbnails()) |
| 153 |
| 154 def testThumbnailRanking(self): |
| 155 """Tests that the thumbnails are ordered according to visit count""" |
| 156 self.RemoveNTPDefaultThumbnails() |
| 157 for page in self.PAGES: |
| 158 self.AppendTab(pyauto.GURL(page['url'])) |
| 159 thumbnails = self.GetNTPThumbnails() |
| 160 self.assertEqual(self.PAGES[0]['url'], self.GetNTPThumbnails()[0]['url']) |
| 161 self.AppendTab(pyauto.GURL(self.PAGES[1]['url'])) |
| 162 self.assertEqual(self.PAGES[1]['url'], self.GetNTPThumbnails()[0]['url']) |
| 163 self.AppendTab(pyauto.GURL(self.PAGES[0]['url'])) |
| 164 self.AppendTab(pyauto.GURL(self.PAGES[0]['url'])) |
| 165 self.assertEqual(self.PAGES[0]['url'], self.GetNTPThumbnails()[0]['url']) |
| 166 |
| 167 def testPinnedThumbnailNeverMoves(self): |
| 168 """Tests that once a thumnail is pinned it never moves""" |
| 169 self.RemoveNTPDefaultThumbnails() |
| 170 for page in self.PAGES: |
| 171 self.AppendTab(pyauto.GURL(page['url'])) |
| 172 self.PinNTPThumbnail(self.GetNTPThumbnails()[0]) |
| 173 thumbnails = self.GetNTPThumbnails() |
| 174 self.AppendTab(pyauto.GURL(self.PAGES[1]['url'])) |
| 175 self.assertEqual(thumbnails, self.GetNTPThumbnails()) |
| 176 |
| 177 def testThumbnailTitleChangeAfterPageTitleChange(self): |
| 178 """Tests that once a page title changes, the thumbnail title changes too""" |
| 179 self.RemoveNTPDefaultThumbnails() |
| 180 self.NavigateToURL(self.PAGES[0]['url']) |
| 181 self.assertEqual(self.PAGES[0]['title'], |
| 182 self.GetNTPThumbnails()[0]['title']) |
| 183 self.ExecuteJavascript('window.domAutomationController.send(' + |
| 184 'document.title = "new title")') |
| 185 self.assertEqual('new title', self.GetNTPThumbnails()[0]['title']) |
| 186 |
| 187 def testCloseOneTab(self): |
| 188 """Tests that closing a tab populates the recently closed list""" |
| 189 self.RemoveNTPDefaultThumbnails() |
| 190 self.AppendTab(pyauto.GURL(self.PAGES[1]['url'])) |
| 191 self.GetBrowserWindow(0).GetTab(1).Close(True) |
| 192 self.assertEqual(self.PAGES[1]['url'], |
| 193 self.GetNTPRecentlyClosed()[0]['url']) |
| 194 self.assertEqual(self.PAGES[1]['title'], |
| 195 self.GetNTPRecentlyClosed()[0]['title']) |
| 196 |
| 197 def testCloseOneWindow(self): |
| 198 """Tests that closing a window populates the recently closed list""" |
| 199 self.RemoveNTPDefaultThumbnails() |
| 200 self.OpenNewBrowserWindow(True) |
| 201 self.NavigateToURL(self.PAGES[0]['url'], 1, 0) |
| 202 self.AppendTab(pyauto.GURL(self.PAGES[1]['url']), 1) |
| 203 self.CloseBrowserWindow(1) |
| 204 expected = [{ u'type': u'window', |
| 205 u'tabs': [ |
| 206 { u'type': u'tab', |
| 207 u'url': self.PAGES[0]['url'], |
| 208 u'direction': u'ltr' }, |
| 209 { u'type': u'tab', |
| 210 u'url': self.PAGES[1]['url']}] |
| 211 }] |
| 212 self.assertEquals(expected, test_utils.StripUnmatchedKeys( |
| 213 self.GetNTPRecentlyClosed(), expected)) |
| 214 |
| 215 def testCloseMultipleTabs(self): |
| 216 """Tests closing multiple tabs populates the Recently Closed section in |
| 217 order""" |
| 218 self.RemoveNTPDefaultThumbnails() |
| 219 self.AppendTab(pyauto.GURL(self.PAGES[0]['url'])) |
| 220 self.AppendTab(pyauto.GURL(self.PAGES[1]['url'])) |
| 221 self.GetBrowserWindow(0).GetTab(2).Close(True) |
| 222 self.GetBrowserWindow(0).GetTab(1).Close(True) |
| 223 expected = [{ u'type': u'tab', |
| 224 u'url': self.PAGES[0]['url'] |
| 225 }, |
| 226 { u'type': u'tab', |
| 227 u'url': self.PAGES[1]['url'] |
| 228 }] |
| 229 self.assertEquals(expected, test_utils.StripUnmatchedKeys( |
| 230 self.GetNTPRecentlyClosed(), expected)) |
| 231 |
| 232 def testCloseWindowWithOneTab(self): |
| 233 """Tests that closing a window with only one tab only shows up as a tab in |
| 234 the Recently Closed section""" |
| 235 self.RemoveNTPDefaultThumbnails() |
| 236 self.OpenNewBrowserWindow(True) |
| 237 self.NavigateToURL(self.PAGES[0]['url'], 1, 0) |
| 238 self.CloseBrowserWindow(1) |
| 239 expected = [{ u'type': u'tab', |
| 240 u'url': self.PAGES[0]['url'] |
| 241 }] |
| 242 self.assertEquals(expected, test_utils.StripUnmatchedKeys( |
| 243 self.GetNTPRecentlyClosed(), expected)) |
| 244 |
| 245 def testCloseMultipleWindows(self): |
| 246 """Tests closing multiple windows populates the Recently Closed list""" |
| 247 self.RemoveNTPDefaultThumbnails() |
| 248 self.OpenNewBrowserWindow(True) |
| 249 self.NavigateToURL(self.PAGES[0]['url'], 1, 0) |
| 250 self.AppendTab(pyauto.GURL(self.PAGES[1]['url']), 1) |
| 251 self.OpenNewBrowserWindow(True) |
| 252 self.NavigateToURL(self.PAGES[1]['url'], 2, 0) |
| 253 self.AppendTab(pyauto.GURL(self.PAGES[0]['url']), 2) |
| 254 self.CloseBrowserWindow(2) |
| 255 self.CloseBrowserWindow(1) |
| 256 expected = [{ u'type': u'window', |
| 257 u'tabs': [ |
| 258 { u'type': u'tab', |
| 259 u'url': self.PAGES[0]['url'], |
| 260 u'direction': u'ltr' }, |
| 261 { u'type': u'tab', |
| 262 u'url': self.PAGES[1]['url']}] |
| 263 }, |
| 264 { u'type': u'window', |
| 265 u'tabs': [ |
| 266 { u'type': u'tab', |
| 267 u'url': self.PAGES[1]['url'], |
| 268 u'direction': u'ltr' }, |
| 269 { u'type': u'tab', |
| 270 u'url': self.PAGES[0]['url']}] |
| 271 }] |
| 272 self.assertEquals(expected, test_utils.StripUnmatchedKeys( |
| 273 self.GetNTPRecentlyClosed(), expected)) |
| 274 |
| 275 def testRecentlyClosedShowsUniqueItems(self): |
| 276 """Tests that the Recently Closed section does not show duplicate items""" |
| 277 self.RemoveNTPDefaultThumbnails() |
| 278 self.AppendTab(pyauto.GURL(self.PAGES[0]['url'])) |
| 279 self.AppendTab(pyauto.GURL(self.PAGES[0]['url'])) |
| 280 self.GetBrowserWindow(0).GetTab(1).Close(True) |
| 281 self.GetBrowserWindow(0).GetTab(1).Close(True) |
| 282 self.assertEquals(1, len(self.GetNTPRecentlyClosed())) |
| 283 |
| 284 def testRecentlyClosedIncognito(self): |
| 285 """Tests that we don't record closure of Incognito tabs or windows""" |
| 286 self.RemoveNTPDefaultThumbnails() |
| 287 self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW) |
| 288 self.NavigateToURL(self.PAGES[0]['url'], 1, 0) |
| 289 self.AppendTab(pyauto.GURL(self.PAGES[0]['url']), 1) |
| 290 self.AppendTab(pyauto.GURL(self.PAGES[1]['url']), 1) |
| 291 self.GetBrowserWindow(1).GetTab(0).Close(True) |
| 292 self.assertFalse(self.GetNTPRecentlyClosed()) |
| 293 self.CloseBrowserWindow(1) |
| 294 self.assertFalse(self.GetNTPRecentlyClosed()) |
| 295 |
97 | 296 |
98 if __name__ == '__main__': | 297 if __name__ == '__main__': |
99 pyauto_functional.Main() | 298 pyauto_functional.Main() |
OLD | NEW |