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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
#!/usr/bin/env 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.
# Must import this first
import pyauto_functional
import logging
import os
import sys
import unittest
import pyauto
class BookmarksTest(pyauto.PyUITest):
"""Test of bookmarks."""
def testBasics(self):
"""Basic tests with an empty bookmark model."""
bookmarks = self.GetBookmarkModel()
# Make sure we have the two root nodes and that they are empty
for node in (bookmarks.BookmarkBar(), bookmarks.Other()):
self.assertEqual(node['type'], 'folder')
self.assertFalse(node['children'])
def testAddOneNode(self):
"""Add a bookmark to the bar; confirm it."""
bookmarks = self.GetBookmarkModel()
bar_id = bookmarks.BookmarkBar()['id']
name = 'Google'
url = 'http://www.google.com'
c = bookmarks.NodeCount()
self.AddBookmarkURL(bar_id, 0, name, url)
bookmarks = self.GetBookmarkModel()
node = bookmarks.BookmarkBar()['children'][0]
self.assertEqual(c+1, bookmarks.NodeCount())
self.assertEqual(node['type'], 'url')
self.assertEqual(node['name'], name)
# URLs may not be exact; e.g. http://www.com --> http://www.com/
self.assertTrue(url in node['url'])
# Make sure we can search and find the same thing
nodes = bookmarks.FindByTitle(name)
self.assertEqual(1, len(nodes))
self.assertTrue(nodes[0]['id'] == node['id'])
def testAddGroup(self):
"""Add a group to the bar; confirm it."""
bookmarks = self.GetBookmarkModel()
c = bookmarks.NodeCount()
parent_id = bookmarks.Other()['id']
name = 'SuperGroup'
self.AddBookmarkGroup(parent_id, 0, name)
# Confirm group.
bookmarks = self.GetBookmarkModel()
node = bookmarks.Other()['children'][0]
self.assertEqual(c+1, bookmarks.NodeCount())
self.assertEqual(node['type'], 'folder')
self.assertEqual(node['name'], name)
# Make sure we can search and find the same thing
findnode = bookmarks.FindByID(node['id'])
self.assertEqual(node, findnode)
def testAddChangeRemove(self):
"""Add some bookmarks. Change their title and URL. Remove one."""
# Add some.
bookmarks = self.GetBookmarkModel()
bar_id = bookmarks.BookmarkBar()['id']
self.AddBookmarkURL(bar_id, 0, 'Title1',
'http://www.google.com')
self.AddBookmarkURL(bar_id, 1, 'Title1',
'http://www.google.com/reader')
# Change a title and URL.
bookmarks = self.GetBookmarkModel()
nodes = bookmarks.FindByTitle('Title1')
self.assertEqual(2, len(nodes))
self.SetBookmarkTitle(nodes[0]['id'], 'Title2')
self.SetBookmarkURL(nodes[1]['id'], 'http://www.youtube.com')
# Confirm, then remove.
bookmarks = self.GetBookmarkModel()
nodes = bookmarks.FindByTitle('Title1')
self.assertEqual(1, len(nodes))
self.assertTrue('http://www.youtube.com' in nodes[0]['url'])
nodes = bookmarks.FindByTitle('Title2')
self.assertEqual(1, len(nodes))
self.assertTrue('google.com' in nodes[0]['url'])
self.RemoveBookmark(nodes[0]['id'])
# Confirm removal.
bookmarks = self.GetBookmarkModel()
nodes = bookmarks.FindByTitle('Title2')
self.assertEqual(0, len(nodes))
def testReparent(self):
bookmarks = self.GetBookmarkModel()
bar_id = bookmarks.BookmarkBar()['id']
# Add some groups
for i in range(3):
self.AddBookmarkGroup(bar_id, i, 'Group' + str(i))
# Add a bookmark in one group
bookmarks = self.GetBookmarkModel()
nodes = bookmarks.FindByTitle('Group0')
self.AddBookmarkURL(nodes[0]['id'], 0,
'marked', 'http://www.youtube.com')
# Make sure it's not in a different group
bookmarks = self.GetBookmarkModel()
nodes = bookmarks.FindByTitle('Group2')
self.assertFalse(nodes[0]['children'])
# Move it to that group
self.ReparentBookmark(bookmarks.FindByTitle('marked')[0]['id'],
bookmarks.FindByTitle('Group2')[0]['id'], 0)
# Confirm.
bookmarks = self.GetBookmarkModel()
nodes = bookmarks.FindByTitle('Group0')
self.assertEqual([], nodes[0]['children'])
nodes = bookmarks.FindByTitle('Group2')
self.assertEqual(1, len(nodes[0]['children']))
def _GetTestURLs(self, filename):
"""Read the given data file and return a dictionary of items."""
data_file = os.path.join(self.DataDir(), "bookmarks", filename)
contents = open(data_file).read()
try:
dictionary = eval(contents, {'__builtins__': None}, None)
except:
print >>sys.stderr, "%s is an invalid data file." % data_file
raise
return dictionary
def testUnicodeStrings(self):
"""Test bookmarks with unicode strings."""
bookmarks = self.GetBookmarkModel()
bar_id = bookmarks.BookmarkBar()['id']
orig_nodes_count = bookmarks.NodeCount()
sites = self._GetTestURLs("urls_and_titles")
# Add bookmarks
for index, (url, name) in enumerate(sites.iteritems()):
self.AddBookmarkURL(bar_id, index, name, url)
# Fetch the added bookmarks and verify them.
bookmarks = self.GetBookmarkModel()
self.assertEqual(orig_nodes_count + len(sites), bookmarks.NodeCount())
for node, k in zip(bookmarks.BookmarkBar()['children'], sites.keys()):
self.assertEqual(node['type'], 'url')
self.assertEqual(node['name'], sites[k])
self.assertEqual(node['url'], k)
def testSizes(self):
"""Verify bookmarks with short and long names."""
bookmarks = self.GetBookmarkModel()
bar_id = bookmarks.BookmarkBar()['id']
orig_nodes_count = bookmarks.NodeCount()
sites = self._GetTestURLs("long-and-short-names")
# Add bookmarks
for index, (url, name) in enumerate(sites.iteritems()):
self.AddBookmarkURL(bar_id, index, name, url)
# Fetch the added urls and verify them.
bookmarks = self.GetBookmarkModel()
self.assertEqual(orig_nodes_count + len(sites), bookmarks.NodeCount())
for node, k in zip(bookmarks.BookmarkBar()['children'], sites.keys()):
self.assertEqual(node['type'], 'url')
self.assertEqual(node['name'], sites[k])
self.assertEqual(node['url'], k)
def testAddingBookmarksToBarAndOther(self):
"""Add bookmarks to the Bookmark Bar and "Other Bookmarks" group."""
url_data = self._GetTestURLs("urls_and_titles")
list_of_urls = url_data.keys()
list_of_titles = url_data.values()
# We need at least 3 URLs for this test to run
self.assertTrue(len(list_of_urls) > 2)
bookmarks = self.GetBookmarkModel()
nodes = bookmarks.NodeCount()
# Add bookmarks to the bar and other
bar_id = bookmarks.BookmarkBar()['id']
self.AddBookmarkURL(bar_id, 0, list_of_titles[0], list_of_urls[0])
other_id = bookmarks.Other()['id']
self.AddBookmarkURL(other_id, 0, list_of_titles[1], list_of_urls[1])
# Now check that we added them
bookmarks = self.GetBookmarkModel()
self.assertEqual(nodes + 2, bookmarks.NodeCount())
bar_child = bookmarks.BookmarkBar()['children'][0]
self.assertEqual(bar_child['type'], 'url')
self.assertEqual(bar_child['name'], list_of_titles[0])
self.assertEqual(bar_child['url'], list_of_urls[0])
other_child = bookmarks.Other()['children'][0]
self.assertEqual(other_child['type'], 'url')
self.assertEqual(other_child['name'], list_of_titles[1])
self.assertEqual(other_child['url'], list_of_urls[1])
def testAddingFoldersToBarAndOther(self):
"""Add folders to the Bookmark Bar and "Other Bookmarks" group."""
url_data = self._GetTestURLs("urls_and_titles")
list_of_titles = url_data.values()
bookmarks = self.GetBookmarkModel()
nodes = bookmarks.NodeCount()
# Add folders to the bar and other
bar_id = bookmarks.BookmarkBar()['id']
self.AddBookmarkGroup(bar_id, 0, list_of_titles[0])
other_id = bookmarks.Other()['id']
self.AddBookmarkGroup(other_id, 0, list_of_titles[1])
# Now check that we added them
bookmarks = self.GetBookmarkModel()
self.assertEqual(nodes + 2, bookmarks.NodeCount())
bar_child = bookmarks.BookmarkBar()['children'][0]
self.assertEqual(bar_child['type'], 'folder')
self.assertEqual(bar_child['name'], list_of_titles[0])
other_child = bookmarks.Other()['children'][0]
self.assertEqual(other_child['type'], 'folder')
self.assertEqual(other_child['name'], list_of_titles[1])
def testAddingFoldersWithChildrenToBarAndOther(self):
"""Add folders with children to the bar and "Other Bookmarks" group."""
url_data = self._GetTestURLs("urls_and_titles")
list_of_urls = url_data.keys()
list_of_titles = url_data.values()
bookmarks = self.GetBookmarkModel()
nodes = bookmarks.NodeCount()
# Add a folder to the bar
bar_id = bookmarks.BookmarkBar()['id']
self.AddBookmarkGroup(bar_id, 0, list_of_titles[0])
# Get a handle on the folder, and add a bookmark in it
bookmarks = self.GetBookmarkModel()
bar_folder_id = bookmarks.BookmarkBar()['children'][0]['id']
self.AddBookmarkURL(bar_folder_id, 0, list_of_titles[1], list_of_urls[1])
# Add a folder to other
other_id = bookmarks.Other()['id']
self.AddBookmarkGroup(other_id, 0, list_of_titles[2])
# Get a handle on the folder, and add a bookmark in it
bookmarks = self.GetBookmarkModel()
other_folder_id = bookmarks.Other()['children'][0]['id']
self.AddBookmarkURL(other_folder_id, 0, list_of_titles[3], list_of_urls[3])
# Verify we have added a folder in the bar and other, and each folder has
# a URL in it
bookmarks = self.GetBookmarkModel()
self.assertEqual(nodes + 4, bookmarks.NodeCount())
bar_child = bookmarks.BookmarkBar()['children'][0]
self.assertEqual(bar_child['type'], 'folder')
self.assertEqual(bar_child['name'], list_of_titles[0])
bar_child_0 = bar_child['children'][0]
self.assertEqual(bar_child_0['type'], 'url')
self.assertEqual(bar_child_0['name'], list_of_titles[1])
self.assertEqual(bar_child_0['url'], list_of_urls[1])
other_child = bookmarks.Other()['children'][0]
self.assertEqual(other_child['type'], 'folder')
self.assertEqual(other_child['name'], list_of_titles[2])
other_child_0 = other_child['children'][0]
self.assertEqual(other_child_0['type'], 'url')
self.assertEqual(other_child_0['name'], list_of_titles[3])
self.assertEqual(other_child_0['url'], list_of_urls[3])
def testOrdering(self):
"""Add/delete a set of bookmarks, and verify their ordering."""
bookmarks = self.GetBookmarkModel()
bar_id = bookmarks.BookmarkBar()['id']
orig_nodes_count = bookmarks.NodeCount()
url_data = self._GetTestURLs("urls_and_titles")
list_of_titles = url_data.values()
for index, (url, name) in enumerate(url_data.iteritems()):
self.AddBookmarkURL(bar_id, index, name, url)
# check that we added them correctly
bookmarks = self.GetBookmarkModel()
self.assertEqual(orig_nodes_count + len(url_data) , bookmarks.NodeCount())
for node, (url, name) in zip(bookmarks.BookmarkBar()['children'],
url_data.iteritems()):
self.assertEqual(node['type'], 'url')
self.assertEqual(node['name'], name)
self.assertEqual(node['url'], url)
# remove a few of them.
remove_indices = [3, 5, 9, 12]
new_list_of_titles = list_of_titles[:]
assert len(remove_indices) <= len(url_data) and \
max(remove_indices) < len(url_data)
for index in remove_indices:
node = bookmarks.FindByTitle(list_of_titles[index])
self.RemoveBookmark(node[0]['id'])
new_list_of_titles.remove(list_of_titles[index])
logging.debug('Removed: %s' % [list_of_titles[x] for x in remove_indices])
# Confirm removal.
bookmarks = self.GetBookmarkModel()
for index in remove_indices:
nodes = bookmarks.FindByTitle(list_of_titles[index])
self.assertEqual(0, len(nodes))
# Confirm that other bookmarks were not removed and their order is intact.
self.assertEqual(len(new_list_of_titles),
len(bookmarks.BookmarkBar()['children']))
for title, node in zip(new_list_of_titles,
bookmarks.BookmarkBar()['children']):
self.assertEqual(title, node['name'])
def testDeepNesting(self):
"""Deep nested folders. Move a bookmark around."""
url_data = self._GetTestURLs("urls_and_titles")
list_of_urls = url_data.keys()
list_of_titles = url_data.values()
bookmarks = self.GetBookmarkModel()
nodes = bookmarks.NodeCount()
bar_folder_id = bookmarks.BookmarkBar()['id']
# Created deep nested folders
num_folders = 15
assert num_folders >= 3
for i in range(num_folders):
self.AddBookmarkGroup(bar_folder_id, 0 ,'Group %d' % i)
bookmarks = self.GetBookmarkModel()
added_nodes = bookmarks.FindByID(bar_folder_id)['children']
self.assertEqual(1, len(added_nodes))
self.assertEqual('Group %d' % i, added_nodes[0]['name'])
bar_folder_id = added_nodes[0]['id']
self.assertEqual(nodes + num_folders, bookmarks.NodeCount())
# Add a bookmark to the leaf folder
a_url = list_of_urls[0]
a_title = list_of_titles[0]
leaf_folder = bookmarks.FindByTitle('Group %d' % (num_folders - 1))[0]
self.AddBookmarkURL(leaf_folder['id'], 0, a_title, a_url)
bookmarks = self.GetBookmarkModel()
nodes = bookmarks.FindByTitle('Group %d' % (num_folders - 1))
self.assertEqual(1, len(nodes))
mynode = nodes[0]['children'][0]
self.assertEqual(a_title, mynode['name'])
self.assertEqual(a_url, mynode['url'])
# Move bookmark to another group, say Group 2
self.ReparentBookmark(bookmarks.FindByTitle(a_title)[0]['id'],
bookmarks.FindByTitle('Group 2')[0]['id'], 0)
bookmarks = self.GetBookmarkModel()
# Bookmark moves to right place
node = bookmarks.FindByTitle('Group 2')[0]
self.assertEqual(2, len(node['children']))
self.assertEqual(a_title, node['children'][0]['name'])
self.assertEqual(a_url, node['children'][0]['url'])
# Bookmark removed from leaf folder
nodes = bookmarks.FindByTitle('Group %d' % (num_folders - 1))
self.assertEqual([], nodes[0]['children'])
def testURLTypes(self):
"""Test bookmarks with different types of URLS."""
bookmarks = self.GetBookmarkModel()
bar_id = bookmarks.BookmarkBar()['id']
orig_nodes_count = bookmarks.NodeCount()
url_data = self._GetTestURLs("url_types")
for index, (url, name) in enumerate(url_data.iteritems()):
self.AddBookmarkURL(bar_id, index, name, url)
# check that we added them correctly
bookmarks = self.GetBookmarkModel()
self.assertEqual(orig_nodes_count + len(url_data), bookmarks.NodeCount())
for node, (url, name) in zip(bookmarks.BookmarkBar()['children'],
url_data.iteritems()):
self.assertEqual(node['type'], 'url')
self.assertEqual(node['name'], name)
self.assertEqual(node['url'], url)
def _VerifyBookmarkURL(self, node, name, url):
"""Verify that node is a bookmark URL of the given name and url."""
self.assertTrue(node)
self.assertEqual(node['type'], 'url')
self.assertEqual(node['name'], name)
self.assertEqual(node['url'], url)
def testDuplicateBookmarks(self):
"""Verify bookmark duplicates."""
url_data = self._GetTestURLs("urls_and_titles")
list_of_urls = url_data.keys()
list_of_titles = url_data.values()
bookmarks = self.GetBookmarkModel()
nodes = bookmarks.NodeCount()
bar_id = bookmarks.BookmarkBar()['id']
# Create some duplicate bookmarks
self.AddBookmarkURL(bar_id, 0, list_of_titles[0], list_of_urls[0])
self.AddBookmarkURL(bar_id, 1, list_of_titles[0], list_of_urls[0])
self.AddBookmarkURL(bar_id, 2, list_of_titles[0], list_of_urls[1])
self.AddBookmarkURL(bar_id, 3, list_of_titles[1], list_of_urls[0])
# Create a folder with any existing bookmark title
self.AddBookmarkGroup(bar_id, 4, list_of_titles[0])
bookmarks = self.GetBookmarkModel()
self.assertEqual(nodes + 5, bookmarks.NodeCount())
# Verify 1st bookmark is created
bar_child = bookmarks.BookmarkBar()['children'][0]
self._VerifyBookmarkURL(bar_child, list_of_titles[0], list_of_urls[0])
# Verify this is a duplicate bookmark of 1st bookmark
bar_child = bookmarks.BookmarkBar()['children'][1]
self._VerifyBookmarkURL(bar_child, list_of_titles[0], list_of_urls[0])
# Verify the bookmark with same title and different URL of 1st bookmark
bar_child = bookmarks.BookmarkBar()['children'][2]
self._VerifyBookmarkURL(bar_child, list_of_titles[0], list_of_urls[1])
# Verify the bookmark with different title and same URL of 1st bookmark
bar_child = bookmarks.BookmarkBar()['children'][3]
self._VerifyBookmarkURL(bar_child, list_of_titles[1], list_of_urls[0])
# Verify Bookmark group got created with same title of 1st bookmark
bar_child = bookmarks.BookmarkBar()['children'][4]
self.assertEqual(bar_child['type'], 'folder')
self.assertEqual(bar_child['name'], list_of_titles[0])
def testBookmarksPersistence(self):
"""Verify that bookmarks and groups persist browser restart."""
# Populate bookmarks and groups
bookmarks = self.GetBookmarkModel()
bar_id = bookmarks.BookmarkBar()['id']
other_id = bookmarks.Other()['id']
self.AddBookmarkURL(bar_id, 0, "Google", "http://www.news.google.com/")
self.AddBookmarkURL(other_id, 0, "Yahoo", "http://www.yahoo.com/")
self.AddBookmarkGroup(bar_id, 0, "MS")
bookmarks = self.GetBookmarkModel()
bar_folder_id = bookmarks.BookmarkBar()['children'][0]['id']
self.AddBookmarkURL(bar_folder_id, 0, "BING", "http://www.bing.com/")
self.AddBookmarkGroup(other_id, 0, "Oracle")
bookmarks = self.GetBookmarkModel()
other_folder_id = bookmarks.Other()['id']
self.AddBookmarkURL(other_folder_id, 0, "DB", "http://www.oracle.com/")
nodes_before = self.GetBookmarkModel().NodeCount()
self.RestartBrowser(clear_profile=False)
# Verify that all bookmarks persist
bookmarks = self.GetBookmarkModel()
node = bookmarks.FindByTitle('Google')
self.assertEqual(nodes_before, bookmarks.NodeCount())
self._VerifyBookmarkURL(node[0], 'Google', 'http://www.news.google.com/')
self._VerifyBookmarkURL(bookmarks.FindByTitle('Yahoo')[0],
'Yahoo', 'http://www.yahoo.com/')
bmb_child = bookmarks.BookmarkBar()['children'][0]
self.assertEqual(bmb_child['type'], 'folder')
self.assertEqual(bmb_child['name'],'MS')
self._VerifyBookmarkURL(bmb_child['children'][0],
'BING', 'http://www.bing.com/')
def testBookmarksManager(self):
"""Verify bookmark title is displayed in bookmarks manager"""
bookmarks = self.GetBookmarkModel()
bar_id = bookmarks.BookmarkBar()['id']
self.AddBookmarkURL(bar_id, 0, 'GoogleNews', 'http://www.news.google.com/')
self.NavigateToURL('chrome://bookmarks/')
self.assertTrue(self.WaitUntil(
lambda: self.FindInPage('GoogleNews', tab_index=0)['match_count'],
expect_retval=1))
if __name__ == '__main__':
pyauto_functional.Main()
|