Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: components/enhanced_bookmarks/enhanced_bookmark_model_unittest.cc

Issue 563363002: Only set remote id during url node creation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: EnhancedBookmarkModelObserver Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/enhanced_bookmarks/enhanced_bookmark_model.h" 5 #include "components/enhanced_bookmarks/enhanced_bookmark_model.h"
6 6
7 #include "base/base64.h" 7 #include "base/base64.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "components/bookmarks/browser/bookmark_model.h" 12 #include "components/bookmarks/browser/bookmark_model.h"
13 #include "components/bookmarks/browser/bookmark_node.h" 13 #include "components/bookmarks/browser/bookmark_node.h"
14 #include "components/bookmarks/test/test_bookmark_client.h" 14 #include "components/bookmarks/test/test_bookmark_client.h"
15 #include "components/enhanced_bookmarks/proto/metadata.pb.h" 15 #include "components/enhanced_bookmarks/proto/metadata.pb.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "url/gurl.h" 17 #include "url/gurl.h"
18 18
19 namespace { 19 namespace {
20 20
21 using enhanced_bookmarks::EnhancedBookmarkModel;
22
21 const std::string BOOKMARK_URL("http://example.com/index.html"); 23 const std::string BOOKMARK_URL("http://example.com/index.html");
22 24
23 class EnhancedBookmarkModelTest : public testing::Test { 25 class EnhancedBookmarkModelTest : public testing::Test {
24 public: 26 public:
25 EnhancedBookmarkModelTest() {} 27 EnhancedBookmarkModelTest() {}
26 virtual ~EnhancedBookmarkModelTest() {} 28 virtual ~EnhancedBookmarkModelTest() {}
27 29
28 virtual void SetUp() OVERRIDE { 30 virtual void SetUp() OVERRIDE {
29 bookmarks::TestBookmarkClient bookmark_client; 31 bookmarks::TestBookmarkClient bookmark_client;
30 bookmark_model_.reset(bookmark_client.CreateModel().release()); 32 bookmark_model_.reset(bookmark_client.CreateModel().release());
31 model_.reset(new enhanced_bookmarks::EnhancedBookmarkModel( 33 model_.reset(new EnhancedBookmarkModel(bookmark_model_.get(), "v1.0"));
32 bookmark_model_.get(), "v1.0"));
33 } 34 }
34 35
35 virtual void TearDown() OVERRIDE { 36 virtual void TearDown() OVERRIDE {
37 model_->ShutDown();
36 model_.reset(); 38 model_.reset();
37 bookmark_model_.reset(); 39 bookmark_model_.reset();
38 } 40 }
39 41
40 protected: 42 protected:
41 const BookmarkNode* AddBookmark() { 43 const BookmarkNode* AddBookmark() {
42 return AddBookmark("Some title", bookmark_model_->other_node()); 44 return AddBookmark("Some title", bookmark_model_->other_node());
43 } 45 }
44 46
45 const BookmarkNode* AddFolder() { 47 const BookmarkNode* AddFolder() {
46 return AddFolder("Some title", bookmark_model_->other_node()); 48 return AddFolder("Some title", bookmark_model_->other_node());
47 } 49 }
48 50
49 const BookmarkNode* AddBookmark(const std::string& name, 51 const BookmarkNode* AddBookmark(const std::string& name,
50 const BookmarkNode* parent) { 52 const BookmarkNode* parent) {
51 return bookmark_model_->AddURL(parent, 53 return model_->AddURL(parent,
52 0, // index. 54 0, // index.
53 base::ASCIIToUTF16(name), 55 base::ASCIIToUTF16(name),
54 GURL(BOOKMARK_URL)); 56 GURL(BOOKMARK_URL),
57 base::Time::Now());
55 } 58 }
56 59
57 const BookmarkNode* AddFolder(const std::string& name, 60 const BookmarkNode* AddFolder(const std::string& name,
58 const BookmarkNode* parent) { 61 const BookmarkNode* parent) {
59 return bookmark_model_->AddFolder(parent, 0, base::ASCIIToUTF16(name)); 62 return model_->AddFolder(parent, 0, base::ASCIIToUTF16(name));
60 } 63 }
61 64
62 std::string GetVersionForNode(const BookmarkNode* node) { 65 std::string GetVersion(const BookmarkNode* node) {
63 std::string version; 66 return GetMetaInfoField(node, "stars.version");
64 if (!node->GetMetaInfo("stars.version", &version)) 67 }
68
69 std::string GetId(const BookmarkNode* node) {
70 return GetMetaInfoField(node, "stars.id");
71 }
72
73 std::string GetOldId(const BookmarkNode* node) {
74 return GetMetaInfoField(node, "stars.oldId");
75 }
76
77 std::string GetMetaInfoField(const BookmarkNode* node,
78 const std::string& name) {
79 std::string value;
80 if (!node->GetMetaInfo(name, &value))
65 return std::string(); 81 return std::string();
66 return version; 82 return value;
67 } 83 }
68 84
69 scoped_ptr<BookmarkModel> bookmark_model_; 85 scoped_ptr<BookmarkModel> bookmark_model_;
70 scoped_ptr<enhanced_bookmarks::EnhancedBookmarkModel> model_; 86 scoped_ptr<EnhancedBookmarkModel> model_;
71 87
72 private: 88 private:
73 DISALLOW_COPY_AND_ASSIGN(EnhancedBookmarkModelTest); 89 DISALLOW_COPY_AND_ASSIGN(EnhancedBookmarkModelTest);
74 }; 90 };
75 91
76 TEST_F(EnhancedBookmarkModelTest, TestEmptySnippet) { 92 TEST_F(EnhancedBookmarkModelTest, TestEmptySnippet) {
77 const BookmarkNode* node = AddBookmark(); 93 const BookmarkNode* node = AddBookmark();
78 94
79 std::string snippet(model_->GetSnippet(node)); 95 std::string snippet(model_->GetSnippet(node));
80 EXPECT_EQ(snippet, ""); 96 EXPECT_EQ(snippet, "");
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 ASSERT_TRUE(result); 262 ASSERT_TRUE(result);
247 263
248 GURL url; 264 GURL url;
249 int width; 265 int width;
250 int height; 266 int height;
251 result = model_->GetOriginalImage(node, &url, &width, &height); 267 result = model_->GetOriginalImage(node, &url, &width, &height);
252 ASSERT_TRUE(result); 268 ASSERT_TRUE(result);
253 EXPECT_EQ(url, GURL("http://example.com/i.jpg")); 269 EXPECT_EQ(url, GURL("http://example.com/i.jpg"));
254 EXPECT_EQ(width, 22); 270 EXPECT_EQ(width, 22);
255 EXPECT_EQ(height, 33); 271 EXPECT_EQ(height, 33);
256 EXPECT_EQ("v1.0", GetVersionForNode(node)); 272 EXPECT_EQ("v1.0", GetVersion(node));
257 } 273 }
258 274
259 TEST_F(EnhancedBookmarkModelTest, TestDoubleEncodeDecode) { 275 TEST_F(EnhancedBookmarkModelTest, TestDoubleEncodeDecode) {
260 const BookmarkNode* node = AddBookmark(); 276 const BookmarkNode* node = AddBookmark();
261 277
262 // Encode some information. 278 // Encode some information.
263 bool result = 279 bool result =
264 model_->SetOriginalImage(node, GURL("http://example.com/i.jpg"), 22, 33); 280 model_->SetOriginalImage(node, GURL("http://example.com/i.jpg"), 22, 33);
265 ASSERT_TRUE(result); 281 ASSERT_TRUE(result);
266 // Encode some different information. 282 // Encode some different information.
267 result = 283 result =
268 model_->SetOriginalImage(node, GURL("http://example.com/i.jpg"), 33, 44); 284 model_->SetOriginalImage(node, GURL("http://example.com/i.jpg"), 33, 44);
269 ASSERT_TRUE(result); 285 ASSERT_TRUE(result);
270 286
271 GURL url; 287 GURL url;
272 int width; 288 int width;
273 int height; 289 int height;
274 result = model_->GetOriginalImage(node, &url, &width, &height); 290 result = model_->GetOriginalImage(node, &url, &width, &height);
275 ASSERT_TRUE(result); 291 ASSERT_TRUE(result);
276 EXPECT_EQ(url, GURL("http://example.com/i.jpg")); 292 EXPECT_EQ(url, GURL("http://example.com/i.jpg"));
277 EXPECT_EQ(width, 33); 293 EXPECT_EQ(width, 33);
278 EXPECT_EQ(height, 44); 294 EXPECT_EQ(height, 44);
279 EXPECT_EQ("v1.0", GetVersionForNode(node)); 295 EXPECT_EQ("v1.0", GetVersion(node));
280 } 296 }
281 297
282 TEST_F(EnhancedBookmarkModelTest, TestRemoteId) { 298 TEST_F(EnhancedBookmarkModelTest, TestRemoteId) {
283 const BookmarkNode* node = AddBookmark(); 299 const BookmarkNode* node = AddBookmark();
300 // Verify that the remote id starts with the correct prefix.
301 EXPECT_TRUE(StartsWithASCII(model_->GetRemoteId(node), "ebc_", true));
302
303 // Getting the remote id for nodes that don't have them should return the
304 // empty string.
305 const BookmarkNode* existing_node =
306 bookmark_model_->AddURL(bookmark_model_->other_node(),
307 0,
308 base::ASCIIToUTF16("Title"),
309 GURL(GURL(BOOKMARK_URL)));
310 EXPECT_TRUE(model_->GetRemoteId(existing_node).empty());
311
312 // Folder nodes should not have a remote id set on creation.
284 const BookmarkNode* folder_node = AddFolder(); 313 const BookmarkNode* folder_node = AddFolder();
285 314 EXPECT_TRUE(model_->GetRemoteId(folder_node).empty());
286 std::string remote_id = model_->GetRemoteId(node);
287 // First call creates the UUID, second call should return the same.
288 EXPECT_EQ(remote_id, model_->GetRemoteId(node));
289
290 // Verify that the remote id starts with the correct prefix.
291 EXPECT_TRUE(StartsWithASCII(remote_id, "ebc_", true));
292 std::string folder_remote_id = model_->GetRemoteId(folder_node);
293 EXPECT_TRUE(StartsWithASCII(folder_remote_id, "ebf_", true));
294
295 // Verifiy version field was set.
296 EXPECT_EQ("v1.0", GetVersionForNode(node));
297 EXPECT_EQ("v1.0", GetVersionForNode(folder_node));
298 } 315 }
299 316
300 TEST_F(EnhancedBookmarkModelTest, TestEmptyDescription) { 317 TEST_F(EnhancedBookmarkModelTest, TestEmptyDescription) {
301 const BookmarkNode* node = AddBookmark(); 318 const BookmarkNode* node = AddBookmark();
302 319
303 std::string description(model_->GetDescription(node)); 320 std::string description(model_->GetDescription(node));
304 EXPECT_EQ(description, ""); 321 EXPECT_EQ(description, "");
305 } 322 }
306 323
307 TEST_F(EnhancedBookmarkModelTest, TestDescription) { 324 TEST_F(EnhancedBookmarkModelTest, TestDescription) {
308 const BookmarkNode* node = AddBookmark(); 325 const BookmarkNode* node = AddBookmark();
309 const std::string description("This is the most useful description of all."); 326 const std::string description("This is the most useful description of all.");
310 327
311 // Set the description. 328 // Set the description.
312 model_->SetDescription(node, description); 329 model_->SetDescription(node, description);
313 330
314 // Check the description is the one that was set. 331 // Check the description is the one that was set.
315 EXPECT_EQ(model_->GetDescription(node), description); 332 EXPECT_EQ(model_->GetDescription(node), description);
316 EXPECT_EQ("v1.0", GetVersionForNode(node)); 333 EXPECT_EQ("v1.0", GetVersion(node));
317 } 334 }
318 335
319 // If there is no notes field, the description should fall back on the snippet. 336 // If there is no notes field, the description should fall back on the snippet.
320 TEST_F(EnhancedBookmarkModelTest, TestDescriptionFallback) { 337 TEST_F(EnhancedBookmarkModelTest, TestDescriptionFallback) {
321 const BookmarkNode* node = AddBookmark(); 338 const BookmarkNode* node = AddBookmark();
322 339
323 // Binary serialize the protobuf. 340 // Binary serialize the protobuf.
324 image::collections::PageData data; 341 image::collections::PageData data;
325 data.set_snippet("Joe Bar Team"); 342 data.set_snippet("Joe Bar Team");
326 ASSERT_TRUE(data.IsInitialized()); 343 ASSERT_TRUE(data.IsInitialized());
(...skipping 15 matching lines...) Expand all
342 model_->SetDescription(node, description); 359 model_->SetDescription(node, description);
343 360
344 // Check the description is the one that was set. 361 // Check the description is the one that was set.
345 EXPECT_EQ(model_->GetDescription(node), description); 362 EXPECT_EQ(model_->GetDescription(node), description);
346 } 363 }
347 364
348 // Makes sure that the stars.version field is set every time 365 // Makes sure that the stars.version field is set every time
349 // EnhancedBookmarkModel makes a change to a node. 366 // EnhancedBookmarkModel makes a change to a node.
350 TEST_F(EnhancedBookmarkModelTest, TestVersionField) { 367 TEST_F(EnhancedBookmarkModelTest, TestVersionField) {
351 const BookmarkNode* node = AddBookmark(); 368 const BookmarkNode* node = AddBookmark();
352 EXPECT_EQ("", GetVersionForNode(node)); 369 EXPECT_EQ("", GetVersion(node));
353 370
354 model_->SetDescription(node, "foo"); 371 model_->SetDescription(node, "foo");
355 EXPECT_EQ("v1.0", GetVersionForNode(node)); 372 EXPECT_EQ("v1.0", GetVersion(node));
356 373
357 // Add a suffix to the version to set. 374 // Add a suffix to the version to set.
358 model_->SetVersionSuffix("alpha"); 375 model_->SetVersionSuffix("alpha");
359 376
360 model_->SetDescription(node, "foo"); 377 model_->SetDescription(node, "foo");
361 // Since the description didn't actually change, the version field should 378 // Since the description didn't actually change, the version field should
362 // not either. 379 // not either.
363 EXPECT_EQ("v1.0", GetVersionForNode(node)); 380 EXPECT_EQ("v1.0", GetVersion(node));
364 381
365 model_->SetDescription(node, "bar"); 382 model_->SetDescription(node, "bar");
366 EXPECT_EQ("v1.0/alpha", GetVersionForNode(node)); 383 EXPECT_EQ("v1.0/alpha", GetVersion(node));
367 } 384 }
368 385
369 // Verifies that the stars.userEdit field is set appropriately when editing a 386 // Verifies that duplicate nodes are reset when the model is created.
370 // node. 387 TEST_F(EnhancedBookmarkModelTest, ResetDuplicateNodesOnInitialization) {
371 TEST_F(EnhancedBookmarkModelTest, TestUserEdit) { 388 model_->ShutDown();
372 const BookmarkNode* node = AddBookmark();
373 389
374 model_->SetDescription(node, "foo"); 390 const BookmarkNode* parent = bookmark_model_->other_node();
375 std::string user_edit; 391 const BookmarkNode* node1 = bookmark_model_->AddURL(
376 ASSERT_TRUE(node->GetMetaInfo("stars.userEdit", &user_edit)); 392 parent, 0, base::ASCIIToUTF16("Some title"), GURL(BOOKMARK_URL));
377 EXPECT_EQ("true", user_edit); 393 const BookmarkNode* node2 = bookmark_model_->AddURL(
394 parent, 0, base::ASCIIToUTF16("Some title"), GURL(BOOKMARK_URL));
395 const BookmarkNode* node3 = bookmark_model_->AddURL(
396 parent, 0, base::ASCIIToUTF16("Some title"), GURL(BOOKMARK_URL));
397 const BookmarkNode* node4 = bookmark_model_->AddURL(
398 parent, 0, base::ASCIIToUTF16("Some title"), GURL(BOOKMARK_URL));
399
400 bookmark_model_->SetNodeMetaInfo(node1, "stars.id", "c_1");
401 bookmark_model_->SetNodeMetaInfo(node2, "stars.id", "c_2");
402 bookmark_model_->SetNodeMetaInfo(node3, "stars.id", "c_1");
403 bookmark_model_->SetNodeMetaInfo(node4, "stars.id", "c_1");
404 EXPECT_EQ("c_1", GetId(node1));
405 EXPECT_EQ("c_2", GetId(node2));
406 EXPECT_EQ("c_1", GetId(node3));
407 EXPECT_EQ("c_1", GetId(node4));
408
409 model_.reset(new EnhancedBookmarkModel(bookmark_model_.get(), "v2.0"));
410
411 EXPECT_EQ("c_2", GetId(node2));
412 EXPECT_EQ("", GetId(node1));
413 EXPECT_EQ("", GetId(node3));
414 EXPECT_EQ("", GetId(node4));
415 EXPECT_EQ("c_1", GetOldId(node1));
416 EXPECT_EQ("c_1", GetOldId(node3));
417 EXPECT_EQ("c_1", GetOldId(node4));
418 EXPECT_EQ("v2.0", GetVersion(node1));
419 EXPECT_EQ("v2.0", GetVersion(node3));
420 EXPECT_EQ("v2.0", GetVersion(node4));
421 }
422
423 // Verifies that duplicate nodes are reset if one is created.
424 TEST_F(EnhancedBookmarkModelTest, ResetDuplicateAddedNodes) {
425 BookmarkNode::MetaInfoMap meta_info;
426 meta_info["stars.id"] = "c_1";
427 const BookmarkNode* parent = bookmark_model_->other_node();
428
429 const BookmarkNode* node1 =
430 bookmark_model_->AddURLWithCreationTimeAndMetaInfo(
431 parent,
432 0,
433 base::ASCIIToUTF16("Some title"),
434 GURL(BOOKMARK_URL),
435 base::Time::Now(),
436 &meta_info);
437 EXPECT_EQ("c_1", GetId(node1));
438
439 const BookmarkNode* node2 =
440 bookmark_model_->AddURLWithCreationTimeAndMetaInfo(
441 parent,
442 0,
443 base::ASCIIToUTF16("Some title"),
444 GURL(BOOKMARK_URL),
445 base::Time::Now(),
446 &meta_info);
447 EXPECT_EQ("", GetId(node1));
448 EXPECT_EQ("", GetId(node2));
449 EXPECT_EQ("c_1", GetOldId(node1));
450 EXPECT_EQ("c_1", GetOldId(node2));
451 EXPECT_EQ("v1.0", GetVersion(node1));
452 EXPECT_EQ("v1.0", GetVersion(node2));
453 }
454
455 // Verifies that duplicate nodes are reset if an id is changed to a duplicate
456 // value.
457 TEST_F(EnhancedBookmarkModelTest, ResetDuplicateChangedNodes) {
458 const BookmarkNode* node1 = AddBookmark();
459 const BookmarkNode* node2 = AddBookmark();
460
461 bookmark_model_->SetNodeMetaInfo(node1, "stars.id", "c_1");
462 EXPECT_EQ("c_1", GetId(node1));
463
464 bookmark_model_->SetNodeMetaInfo(node2, "stars.id", "c_1");
465 EXPECT_EQ("", GetId(node1));
466 EXPECT_EQ("", GetId(node2));
467 EXPECT_EQ("c_1", GetOldId(node1));
468 EXPECT_EQ("c_1", GetOldId(node2));
469 EXPECT_EQ("v1.0", GetVersion(node1));
470 EXPECT_EQ("v1.0", GetVersion(node2));
378 } 471 }
379 472
noyau (Ping after 24h) 2014/09/18 08:43:22 Can you add tests to make sure the observer method
Rune Fevang 2014/09/19 01:18:07 Done.
380 } // namespace 473 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698