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 # Must import this first | 6 # Must import this first |
7 import pyauto_functional | 7 import pyauto_functional |
8 | 8 |
| 9 import os |
9 import unittest | 10 import unittest |
10 | 11 |
11 import pyauto | 12 import pyauto |
12 | 13 |
13 | 14 |
14 class BookmarksTest(pyauto.PyUITest): | 15 class BookmarksTest(pyauto.PyUITest): |
15 """Test of bookmarks.""" | 16 """Test of bookmarks.""" |
16 | 17 |
17 def testBasics(self): | 18 def testBasics(self): |
18 """Basic tests with an empty bookmark model.""" | 19 """Basic tests with an empty bookmark model.""" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 # Move it to that group | 107 # Move it to that group |
107 self.ReparentBookmark(bookmarks.FindByTitle('marked')[0]['id'], | 108 self.ReparentBookmark(bookmarks.FindByTitle('marked')[0]['id'], |
108 bookmarks.FindByTitle('Group2')[0]['id'], 0) | 109 bookmarks.FindByTitle('Group2')[0]['id'], 0) |
109 # Confirm. | 110 # Confirm. |
110 bookmarks = self.GetBookmarkModel() | 111 bookmarks = self.GetBookmarkModel() |
111 nodes = bookmarks.FindByTitle('Group0') | 112 nodes = bookmarks.FindByTitle('Group0') |
112 self.assertEqual([], nodes[0]['children']) | 113 self.assertEqual([], nodes[0]['children']) |
113 nodes = bookmarks.FindByTitle('Group2') | 114 nodes = bookmarks.FindByTitle('Group2') |
114 self.assertEqual(1, len(nodes[0]['children'])) | 115 self.assertEqual(1, len(nodes[0]['children'])) |
115 | 116 |
| 117 def _GetTestURLs(self, filename): |
| 118 """Read the given data file and return a dictionary of items.""" |
| 119 data_file = os.path.join(self.DataDir(), "bookmarks", filename) |
| 120 contents = open(data_file).read() |
| 121 try: |
| 122 dictionary = eval(contents, {'__builtins__': None}, None) |
| 123 except: |
| 124 print >>sys.stderr, "%s is an invalid data file." % data_file |
| 125 raise |
| 126 return dictionary |
| 127 |
| 128 def testUnicodeStrings(self): |
| 129 """Test bookmarks with unicode strings.""" |
| 130 bookmarks = self.GetBookmarkModel() |
| 131 bar_id = bookmarks.BookmarkBar()['id'] |
| 132 orig_nodes_count = bookmarks.NodeCount() |
| 133 |
| 134 sites = self._GetTestURLs("urls_and_titles") |
| 135 # Add bookmarks |
| 136 for index, (url, name) in enumerate(sites.iteritems()): |
| 137 self.AddBookmarkURL(bar_id, index, name, url) |
| 138 |
| 139 # Fetch the added bookmarks and verify them. |
| 140 bookmarks = self.GetBookmarkModel() |
| 141 self.assertEqual(orig_nodes_count + len(sites), bookmarks.NodeCount()) |
| 142 |
| 143 for node, k in zip(bookmarks.BookmarkBar()['children'], sites.keys()): |
| 144 self.assertEqual(node['type'], 'url') |
| 145 self.assertEqual(node['name'], sites[k]) |
| 146 self.assertEqual(node['url'], k) |
| 147 |
| 148 def testSizes(self): |
| 149 """Verify bookmarks with short and long names.""" |
| 150 bookmarks = self.GetBookmarkModel() |
| 151 bar_id = bookmarks.BookmarkBar()['id'] |
| 152 orig_nodes_count = bookmarks.NodeCount() |
| 153 |
| 154 sites = self._GetTestURLs("long-and-short-names") |
| 155 # Add bookmarks |
| 156 for index, (url, name) in enumerate(sites.iteritems()): |
| 157 self.AddBookmarkURL(bar_id, index, name, url) |
| 158 |
| 159 # Fetch the added urls and verify them. |
| 160 bookmarks = self.GetBookmarkModel() |
| 161 self.assertEqual(orig_nodes_count + len(sites), bookmarks.NodeCount()) |
| 162 |
| 163 for node, k in zip(bookmarks.BookmarkBar()['children'], sites.keys()): |
| 164 self.assertEqual(node['type'], 'url') |
| 165 self.assertEqual(node['name'], sites[k]) |
| 166 self.assertEqual(node['url'], k) |
| 167 |
| 168 def testAddingBookmarksToBarAndOther(self): |
| 169 """Add bookmarks to the Bookmark Bar and "Other Bookmarks" group.""" |
| 170 url_data = self._GetTestURLs("urls_and_titles") |
| 171 list_of_urls = url_data.keys() |
| 172 list_of_titles = url_data.values() |
| 173 |
| 174 # We need at least 3 URLs for this test to run |
| 175 self.assertTrue(len(list_of_urls) > 2) |
| 176 |
| 177 bookmarks = self.GetBookmarkModel() |
| 178 nodes = bookmarks.NodeCount() |
| 179 |
| 180 # Add bookmarks to the bar and other |
| 181 bar_id = bookmarks.BookmarkBar()['id'] |
| 182 self.AddBookmarkURL(bar_id, 0, list_of_titles[0], list_of_urls[0]) |
| 183 |
| 184 other_id = bookmarks.Other()['id'] |
| 185 self.AddBookmarkURL(other_id, 0, list_of_titles[1], list_of_urls[1]) |
| 186 |
| 187 # Now check that we added them |
| 188 bookmarks = self.GetBookmarkModel() |
| 189 self.assertEqual(nodes + 2, bookmarks.NodeCount()) |
| 190 |
| 191 bar_child = bookmarks.BookmarkBar()['children'][0] |
| 192 self.assertEqual(bar_child['type'], 'url') |
| 193 self.assertEqual(bar_child['name'], list_of_titles[0]) |
| 194 self.assertEqual(bar_child['url'], list_of_urls[0]) |
| 195 |
| 196 other_child = bookmarks.Other()['children'][0] |
| 197 self.assertEqual(other_child['type'], 'url') |
| 198 self.assertEqual(other_child['name'], list_of_titles[1]) |
| 199 self.assertEqual(other_child['url'], list_of_urls[1]) |
| 200 |
| 201 def testAddingFoldersToBarAndOther(self): |
| 202 """Add folders to the Bookmark Bar and "Other Bookmarks" group.""" |
| 203 url_data = self._GetTestURLs("urls_and_titles") |
| 204 list_of_titles = url_data.values() |
| 205 |
| 206 bookmarks = self.GetBookmarkModel() |
| 207 nodes = bookmarks.NodeCount() |
| 208 |
| 209 # Add folders to the bar and other |
| 210 bar_id = bookmarks.BookmarkBar()['id'] |
| 211 self.AddBookmarkGroup(bar_id, 0, list_of_titles[0]) |
| 212 |
| 213 other_id = bookmarks.Other()['id'] |
| 214 self.AddBookmarkGroup(other_id, 0, list_of_titles[1]) |
| 215 |
| 216 # Now check that we added them |
| 217 bookmarks = self.GetBookmarkModel() |
| 218 self.assertEqual(nodes + 2, bookmarks.NodeCount()) |
| 219 |
| 220 bar_child = bookmarks.BookmarkBar()['children'][0] |
| 221 self.assertEqual(bar_child['type'], 'folder') |
| 222 self.assertEqual(bar_child['name'], list_of_titles[0]) |
| 223 |
| 224 other_child = bookmarks.Other()['children'][0] |
| 225 self.assertEqual(other_child['type'], 'folder') |
| 226 self.assertEqual(other_child['name'], list_of_titles[1]) |
| 227 |
| 228 def testAddingFoldersWithChildrenToBarAndOther(self): |
| 229 """Add folders with children to the bar and "Other Bookmarks" group.""" |
| 230 url_data = self._GetTestURLs("urls_and_titles") |
| 231 list_of_urls = url_data.keys() |
| 232 list_of_titles = url_data.values() |
| 233 |
| 234 bookmarks = self.GetBookmarkModel() |
| 235 nodes = bookmarks.NodeCount() |
| 236 |
| 237 # Add a folder to the bar |
| 238 bar_id = bookmarks.BookmarkBar()['id'] |
| 239 self.AddBookmarkGroup(bar_id, 0, list_of_titles[0]) |
| 240 |
| 241 # Get a handle on the folder, and add a bookmark in it |
| 242 bookmarks = self.GetBookmarkModel() |
| 243 bar_folder_id = bookmarks.BookmarkBar()['children'][0]['id'] |
| 244 self.AddBookmarkURL(bar_folder_id, 0, list_of_titles[1], list_of_urls[1]) |
| 245 |
| 246 # Add a folder to other |
| 247 other_id = bookmarks.Other()['id'] |
| 248 self.AddBookmarkGroup(other_id, 0, list_of_titles[2]) |
| 249 |
| 250 # Get a handle on the folder, and add a bookmark in it |
| 251 bookmarks = self.GetBookmarkModel() |
| 252 other_folder_id = bookmarks.Other()['children'][0]['id'] |
| 253 self.AddBookmarkURL(other_folder_id, 0, list_of_titles[3], list_of_urls[3]) |
| 254 |
| 255 # Verify we have added a folder in the bar and other, and each folder has |
| 256 # a URL in it |
| 257 bookmarks = self.GetBookmarkModel() |
| 258 self.assertEqual(nodes + 4, bookmarks.NodeCount()) |
| 259 |
| 260 bar_child = bookmarks.BookmarkBar()['children'][0] |
| 261 self.assertEqual(bar_child['type'], 'folder') |
| 262 self.assertEqual(bar_child['name'], list_of_titles[0]) |
| 263 |
| 264 bar_child_0 = bar_child['children'][0] |
| 265 self.assertEqual(bar_child_0['type'], 'url') |
| 266 self.assertEqual(bar_child_0['name'], list_of_titles[1]) |
| 267 self.assertEqual(bar_child_0['url'], list_of_urls[1]) |
| 268 |
| 269 other_child = bookmarks.Other()['children'][0] |
| 270 self.assertEqual(other_child['type'], 'folder') |
| 271 self.assertEqual(other_child['name'], list_of_titles[2]) |
| 272 |
| 273 other_child_0 = other_child['children'][0] |
| 274 self.assertEqual(other_child_0['type'], 'url') |
| 275 self.assertEqual(other_child_0['name'], list_of_titles[3]) |
| 276 self.assertEqual(other_child_0['url'], list_of_urls[3]) |
| 277 |
116 | 278 |
117 if __name__ == '__main__': | 279 if __name__ == '__main__': |
118 pyauto_functional.Main() | 280 pyauto_functional.Main() |
OLD | NEW |