Fix missing GN dependencies.
[chromium-blink-merge.git] / components / enhanced_bookmarks / enhanced_bookmark_model_unittest.cc
blobc7c29f9c79c881109ddfbedc3f7c386f19cf30de
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
3 // found in the LICENSE file.
5 #include "components/enhanced_bookmarks/enhanced_bookmark_model.h"
7 #include "base/base64.h"
8 #include "base/macros.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "components/bookmarks/browser/bookmark_model.h"
16 #include "components/bookmarks/browser/bookmark_node.h"
17 #include "components/bookmarks/test/test_bookmark_client.h"
18 #include "components/enhanced_bookmarks/enhanced_bookmark_model_observer.h"
19 #include "components/enhanced_bookmarks/proto/metadata.pb.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "url/gurl.h"
23 using bookmarks::BookmarkModel;
24 using bookmarks::BookmarkNode;
25 using enhanced_bookmarks::EnhancedBookmarkModel;
27 namespace {
28 const std::string BOOKMARK_URL("http://example.com/index.html");
29 const std::string IMAGE_URL("http://example.com/image.jpg");
30 } // namespace
32 class EnhancedBookmarkModelTest
33 : public testing::Test,
34 public enhanced_bookmarks::EnhancedBookmarkModelObserver {
35 public:
36 EnhancedBookmarkModelTest()
37 : loaded_calls_(0),
38 shutting_down_calls_(0),
39 added_calls_(0),
40 removed_calls_(0),
41 changed_calls_(0),
42 all_user_nodes_removed_calls_(0),
43 remote_id_changed_calls_(0),
44 last_added_(NULL),
45 last_removed_(NULL),
46 last_changed_(NULL),
47 last_remote_id_node_(NULL) {}
48 ~EnhancedBookmarkModelTest() override {}
50 void SetUp() override {
51 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_DEFAULT));
52 bookmark_client_.reset(new bookmarks::TestBookmarkClient());
53 bookmark_model_.reset(bookmark_client_->CreateModel().release());
54 model_.reset(new EnhancedBookmarkModel(bookmark_model_.get(), "v1.0"));
55 model_->AddObserver(this);
58 void TearDown() override {
59 if (model_)
60 model_->Shutdown();
61 model_.reset();
62 bookmark_model_.reset();
63 bookmark_client_.reset();
64 message_loop_.reset();
67 protected:
68 const BookmarkNode* AddBookmark() {
69 return AddBookmark("Some title", bookmark_model_->other_node());
72 const BookmarkNode* AddFolder() {
73 return AddFolder("Some title", bookmark_model_->other_node());
76 const BookmarkNode* AddBookmark(const std::string& name,
77 const BookmarkNode* parent) {
78 return model_->AddURL(parent,
79 0, // index.
80 base::ASCIIToUTF16(name),
81 GURL(BOOKMARK_URL),
82 base::Time::Now());
85 const BookmarkNode* AddFolder(const std::string& name,
86 const BookmarkNode* parent) {
87 return model_->AddFolder(parent, 0, base::ASCIIToUTF16(name));
90 std::string GetVersion(const BookmarkNode* node) {
91 return GetMetaInfoField(node, "stars.version");
94 std::string GetId(const BookmarkNode* node) {
95 return GetMetaInfoField(node, "stars.id");
98 std::string GetOldId(const BookmarkNode* node) {
99 return GetMetaInfoField(node, "stars.oldId");
102 std::string GetMetaInfoField(const BookmarkNode* node,
103 const std::string& name) {
104 std::string value;
105 if (!node->GetMetaInfo(name, &value))
106 return std::string();
107 return value;
110 scoped_ptr<base::MessageLoop> message_loop_;
111 scoped_ptr<bookmarks::TestBookmarkClient> bookmark_client_;
112 scoped_ptr<BookmarkModel> bookmark_model_;
113 scoped_ptr<EnhancedBookmarkModel> model_;
115 // EnhancedBookmarkModelObserver implementation:
116 void EnhancedBookmarkModelLoaded() override { loaded_calls_++; }
117 void EnhancedBookmarkModelShuttingDown() override { shutting_down_calls_++; }
118 void EnhancedBookmarkAdded(const BookmarkNode* node) override {
119 added_calls_++;
120 last_added_ = node;
122 void EnhancedBookmarkRemoved(const BookmarkNode* node) override {
123 removed_calls_++;
124 last_removed_ = node;
126 void EnhancedBookmarkNodeChanged(const BookmarkNode* node) override {
127 changed_calls_++;
128 last_changed_ = node;
130 void EnhancedBookmarkAllUserNodesRemoved() override {
131 all_user_nodes_removed_calls_++;
133 void EnhancedBookmarkRemoteIdChanged(const BookmarkNode* node,
134 const std::string& old_remote_id,
135 const std::string& remote_id) override {
136 remote_id_changed_calls_++;
137 last_remote_id_node_ = node;
138 last_old_remote_id_ = old_remote_id;
139 last_remote_id_ = remote_id;
142 // Observer call counters:
143 int loaded_calls_;
144 int shutting_down_calls_;
145 int added_calls_;
146 int removed_calls_;
147 int changed_calls_;
148 int all_user_nodes_removed_calls_;
149 int remote_id_changed_calls_;
151 // Observer parameter cache:
152 const BookmarkNode* last_added_;
153 const BookmarkNode* last_removed_;
154 const BookmarkNode* last_changed_;
155 const BookmarkNode* last_remote_id_node_;
156 std::string last_old_remote_id_;
157 std::string last_remote_id_;
159 private:
160 DISALLOW_COPY_AND_ASSIGN(EnhancedBookmarkModelTest);
163 TEST_F(EnhancedBookmarkModelTest, TestEmptySnippet) {
164 const BookmarkNode* node = AddBookmark();
166 std::string snippet(model_->GetSnippet(node));
167 EXPECT_EQ(snippet, "");
170 TEST_F(EnhancedBookmarkModelTest, TestSnippet) {
171 const BookmarkNode* node = AddBookmark();
173 // Binary serialize the protobuf.
174 image::collections::PageData data;
175 data.set_snippet("I'm happy!");
176 ASSERT_TRUE(data.IsInitialized());
177 std::string output;
178 bool result = data.SerializeToString(&output);
179 ASSERT_TRUE(result);
181 // base64 encode the output.
182 std::string encoded;
183 base::Base64Encode(output, &encoded);
184 bookmark_model_->SetNodeMetaInfo(node, "stars.pageData", encoded);
186 std::string snippet(model_->GetSnippet(node));
187 EXPECT_EQ(snippet, "I'm happy!");
190 TEST_F(EnhancedBookmarkModelTest, TestBadEncodingSnippet) {
191 const BookmarkNode* node = AddBookmark();
193 // Binary serialize the protobuf.
194 image::collections::PageData data;
195 data.set_snippet("You are happy!");
196 ASSERT_TRUE(data.IsInitialized());
197 std::string output;
198 bool result = data.SerializeToString(&output);
199 ASSERT_TRUE(result);
201 // don't base 64 encode the output.
202 bookmark_model_->SetNodeMetaInfo(node, "stars.pageData", output);
204 std::string snippet(model_->GetSnippet(node));
205 EXPECT_EQ(snippet, "");
208 TEST_F(EnhancedBookmarkModelTest, TestOriginalImage) {
209 const BookmarkNode* node = AddBookmark();
211 image::collections::ImageData data;
212 // Intentionally make raw pointer.
213 image::collections::ImageData_ImageInfo* info =
214 new image::collections::ImageData_ImageInfo;
215 info->set_url("http://example.com/foobar");
216 info->set_width(15);
217 info->set_height(55);
218 // This method consumes the pointer.
219 data.set_allocated_original_info(info);
221 std::string output;
222 bool result = data.SerializePartialToString(&output);
223 ASSERT_TRUE(result);
225 // base64 encode the output.
226 std::string encoded;
227 base::Base64Encode(output, &encoded);
228 bookmark_model_->SetNodeMetaInfo(node, "stars.imageData", encoded);
230 GURL url;
231 int width;
232 int height;
233 result = model_->GetOriginalImage(node, &url, &width, &height);
234 ASSERT_TRUE(result);
235 EXPECT_EQ(url, GURL("http://example.com/foobar"));
236 EXPECT_EQ(width, 15);
237 EXPECT_EQ(height, 55);
240 TEST_F(EnhancedBookmarkModelTest, TestThumbnailImage) {
241 const BookmarkNode* node = AddBookmark();
243 image::collections::ImageData data;
244 // Intentionally make raw pointer.
245 image::collections::ImageData_ImageInfo* info =
246 new image::collections::ImageData_ImageInfo;
247 info->set_url("http://example.com/foobar");
248 info->set_width(15);
249 info->set_height(55);
250 // This method consumes the pointer.
251 data.set_allocated_thumbnail_info(info);
253 std::string output;
254 bool result = data.SerializePartialToString(&output);
255 ASSERT_TRUE(result);
257 // base64 encode the output.
258 std::string encoded;
259 base::Base64Encode(output, &encoded);
260 bookmark_model_->SetNodeMetaInfo(node, "stars.imageData", encoded);
262 GURL url;
263 int width;
264 int height;
265 result = model_->GetThumbnailImage(node, &url, &width, &height);
266 ASSERT_TRUE(result);
267 EXPECT_EQ(url, GURL("http://example.com/foobar"));
268 EXPECT_EQ(width, 15);
269 EXPECT_EQ(height, 55);
272 TEST_F(EnhancedBookmarkModelTest, TestOriginalImageMissingDimensions) {
273 const BookmarkNode* node = AddBookmark();
275 image::collections::ImageData data;
276 // Intentionally make raw pointer.
277 image::collections::ImageData_ImageInfo* info =
278 new image::collections::ImageData_ImageInfo;
279 info->set_url("http://example.com/foobar");
280 // This method consumes the pointer.
281 data.set_allocated_original_info(info);
283 std::string output;
284 bool result = data.SerializePartialToString(&output);
285 ASSERT_TRUE(result);
287 // base64 encode the output.
288 std::string encoded;
289 base::Base64Encode(output, &encoded);
290 bookmark_model_->SetNodeMetaInfo(node, "stars.imageData", encoded);
292 GURL url;
293 int width;
294 int height;
295 result = model_->GetOriginalImage(node, &url, &width, &height);
296 ASSERT_FALSE(result);
299 TEST_F(EnhancedBookmarkModelTest, TestOriginalImageBadUrl) {
300 const BookmarkNode* node = AddBookmark();
302 image::collections::ImageData data;
303 // Intentionally make raw pointer.
304 image::collections::ImageData_ImageInfo* info =
305 new image::collections::ImageData_ImageInfo;
306 info->set_url("asdf. 13r");
307 info->set_width(15);
308 info->set_height(55);
309 // This method consumes the pointer.
310 data.set_allocated_original_info(info);
312 std::string output;
313 bool result = data.SerializePartialToString(&output);
314 ASSERT_TRUE(result);
316 // base64 encode the output.
317 std::string encoded;
318 base::Base64Encode(output, &encoded);
319 bookmark_model_->SetNodeMetaInfo(node, "stars.imageData", encoded);
321 GURL url;
322 int width;
323 int height;
324 result = model_->GetOriginalImage(node, &url, &width, &height);
325 ASSERT_FALSE(result);
328 TEST_F(EnhancedBookmarkModelTest, TestEncodeDecode) {
329 const BookmarkNode* node = AddBookmark();
331 bool result =
332 model_->SetOriginalImage(node, GURL("http://example.com/i.jpg"), 22, 33);
333 ASSERT_TRUE(result);
335 GURL url;
336 int width;
337 int height;
338 result = model_->GetOriginalImage(node, &url, &width, &height);
339 ASSERT_TRUE(result);
340 EXPECT_EQ(url, GURL("http://example.com/i.jpg"));
341 EXPECT_EQ(width, 22);
342 EXPECT_EQ(height, 33);
343 EXPECT_EQ("v1.0", GetVersion(node));
346 TEST_F(EnhancedBookmarkModelTest, TestDoubleEncodeDecode) {
347 const BookmarkNode* node = AddBookmark();
349 // Encode some information.
350 bool result =
351 model_->SetOriginalImage(node, GURL("http://example.com/i.jpg"), 22, 33);
352 ASSERT_TRUE(result);
353 // Encode some different information.
354 result =
355 model_->SetOriginalImage(node, GURL("http://example.com/i.jpg"), 33, 44);
356 ASSERT_TRUE(result);
358 GURL url;
359 int width;
360 int height;
361 result = model_->GetOriginalImage(node, &url, &width, &height);
362 ASSERT_TRUE(result);
363 EXPECT_EQ(url, GURL("http://example.com/i.jpg"));
364 EXPECT_EQ(width, 33);
365 EXPECT_EQ(height, 44);
366 EXPECT_EQ("v1.0", GetVersion(node));
369 TEST_F(EnhancedBookmarkModelTest, TestRemoteId) {
370 const BookmarkNode* node = AddBookmark();
371 // Verify that the remote id starts with the correct prefix.
372 EXPECT_TRUE(StartsWithASCII(model_->GetRemoteId(node), "ebc_", true));
374 // Getting the remote id for nodes that don't have them should return the
375 // empty string.
376 const BookmarkNode* existing_node =
377 bookmark_model_->AddURL(bookmark_model_->other_node(),
379 base::ASCIIToUTF16("Title"),
380 GURL(GURL(BOOKMARK_URL)));
381 EXPECT_TRUE(model_->GetRemoteId(existing_node).empty());
383 // Folder nodes should not have a remote id set on creation.
384 const BookmarkNode* folder_node = AddFolder();
385 EXPECT_TRUE(model_->GetRemoteId(folder_node).empty());
388 TEST_F(EnhancedBookmarkModelTest, TestEmptyDescription) {
389 const BookmarkNode* node = AddBookmark();
391 std::string description(model_->GetDescription(node));
392 EXPECT_EQ(description, "");
395 TEST_F(EnhancedBookmarkModelTest, TestDescription) {
396 const BookmarkNode* node = AddBookmark();
397 const std::string description("This is the most useful description of all.");
399 // Set the description.
400 model_->SetDescription(node, description);
402 // Check the description is the one that was set.
403 EXPECT_EQ(model_->GetDescription(node), description);
404 EXPECT_EQ("v1.0", GetVersion(node));
407 // If there is no notes field, the description should fall back on the snippet.
408 TEST_F(EnhancedBookmarkModelTest, TestDescriptionFallback) {
409 const BookmarkNode* node = AddBookmark();
411 // Binary serialize the protobuf.
412 image::collections::PageData data;
413 data.set_snippet("Joe Bar Team");
414 ASSERT_TRUE(data.IsInitialized());
415 std::string output;
416 bool result = data.SerializeToString(&output);
417 ASSERT_TRUE(result);
419 // base64 encode the output.
420 std::string encoded;
421 base::Base64Encode(output, &encoded);
422 bookmark_model_->SetNodeMetaInfo(node, "stars.pageData", encoded);
424 // The snippet is used as the description.
425 std::string snippet(model_->GetSnippet(node));
426 EXPECT_EQ("Joe Bar Team", model_->GetDescription(node));
428 // Set the description.
429 const std::string description("This is the most useful description of all.");
430 model_->SetDescription(node, description);
432 // Check the description is the one that was set.
433 EXPECT_EQ(model_->GetDescription(node), description);
436 // Makes sure that the stars.version field is set every time
437 // EnhancedBookmarkModel makes a change to a node.
438 TEST_F(EnhancedBookmarkModelTest, TestVersionField) {
439 const BookmarkNode* node =
440 bookmark_model_->AddURL(bookmark_model_->other_node(), 0,
441 base::ASCIIToUTF16("Title"), GURL(BOOKMARK_URL));
442 EXPECT_EQ("", GetVersion(node));
444 model_->SetDescription(node, "foo");
445 EXPECT_EQ("v1.0", GetVersion(node));
447 // Add a suffix to the version to set.
448 model_->SetVersionSuffix("alpha");
450 model_->SetDescription(node, "foo");
451 // Since the description didn't actually change, the version field should
452 // not either.
453 EXPECT_EQ("v1.0", GetVersion(node));
455 model_->SetDescription(node, "bar");
456 EXPECT_EQ("v1.0/alpha", GetVersion(node));
459 // Verifies that duplicate nodes are reset when the model is created.
460 TEST_F(EnhancedBookmarkModelTest, ResetDuplicateNodesOnInitialization) {
461 model_->Shutdown();
463 const BookmarkNode* parent = bookmark_model_->other_node();
464 const BookmarkNode* node1 = bookmark_model_->AddURL(
465 parent, 0, base::ASCIIToUTF16("Some title"), GURL(BOOKMARK_URL));
466 const BookmarkNode* node2 = bookmark_model_->AddURL(
467 parent, 0, base::ASCIIToUTF16("Some title"), GURL(BOOKMARK_URL));
468 const BookmarkNode* node3 = bookmark_model_->AddURL(
469 parent, 0, base::ASCIIToUTF16("Some title"), GURL(BOOKMARK_URL));
470 const BookmarkNode* node4 = bookmark_model_->AddURL(
471 parent, 0, base::ASCIIToUTF16("Some title"), GURL(BOOKMARK_URL));
473 bookmark_model_->SetNodeMetaInfo(node1, "stars.id", "c_1");
474 bookmark_model_->SetNodeMetaInfo(node2, "stars.id", "c_2");
475 bookmark_model_->SetNodeMetaInfo(node3, "stars.id", "c_1");
476 bookmark_model_->SetNodeMetaInfo(node4, "stars.id", "c_1");
477 EXPECT_EQ("c_1", GetId(node1));
478 EXPECT_EQ("c_2", GetId(node2));
479 EXPECT_EQ("c_1", GetId(node3));
480 EXPECT_EQ("c_1", GetId(node4));
482 model_.reset(new EnhancedBookmarkModel(bookmark_model_.get(), "v2.0"));
483 base::RunLoop().RunUntilIdle();
484 EXPECT_EQ("c_2", GetId(node2));
485 EXPECT_EQ("", GetId(node1));
486 EXPECT_EQ("", GetId(node3));
487 EXPECT_EQ("", GetId(node4));
488 EXPECT_EQ("c_1", GetOldId(node1));
489 EXPECT_EQ("c_1", GetOldId(node3));
490 EXPECT_EQ("c_1", GetOldId(node4));
491 EXPECT_EQ("v2.0", GetVersion(node1));
492 EXPECT_EQ("v2.0", GetVersion(node3));
493 EXPECT_EQ("v2.0", GetVersion(node4));
496 // Verifies that duplicate nodes are reset if one is created.
497 TEST_F(EnhancedBookmarkModelTest, ResetDuplicateAddedNodes) {
498 BookmarkNode::MetaInfoMap meta_info;
499 meta_info["stars.id"] = "c_1";
500 const BookmarkNode* parent = bookmark_model_->other_node();
502 const BookmarkNode* node1 =
503 bookmark_model_->AddURLWithCreationTimeAndMetaInfo(
504 parent,
506 base::ASCIIToUTF16("Some title"),
507 GURL(BOOKMARK_URL),
508 base::Time::Now(),
509 &meta_info);
510 EXPECT_EQ("c_1", GetId(node1));
512 const BookmarkNode* node2 =
513 bookmark_model_->AddURLWithCreationTimeAndMetaInfo(
514 parent,
516 base::ASCIIToUTF16("Some title"),
517 GURL(BOOKMARK_URL),
518 base::Time::Now(),
519 &meta_info);
521 base::RunLoop().RunUntilIdle();
522 EXPECT_EQ("", GetId(node1));
523 EXPECT_EQ("", GetId(node2));
524 EXPECT_EQ("c_1", GetOldId(node1));
525 EXPECT_EQ("c_1", GetOldId(node2));
526 EXPECT_EQ("v1.0", GetVersion(node1));
527 EXPECT_EQ("v1.0", GetVersion(node2));
530 // Verifies that duplicate nodes are reset if an id is changed to a duplicate
531 // value.
532 TEST_F(EnhancedBookmarkModelTest, ResetDuplicateChangedNodes) {
533 const BookmarkNode* node1 = AddBookmark();
534 const BookmarkNode* node2 = AddBookmark();
536 bookmark_model_->SetNodeMetaInfo(node1, "stars.id", "c_1");
537 EXPECT_EQ("c_1", GetId(node1));
539 bookmark_model_->SetNodeMetaInfo(node2, "stars.id", "c_1");
540 base::RunLoop().RunUntilIdle();
541 EXPECT_EQ("", GetId(node1));
542 EXPECT_EQ("", GetId(node2));
543 EXPECT_EQ("c_1", GetOldId(node1));
544 EXPECT_EQ("c_1", GetOldId(node2));
545 EXPECT_EQ("v1.0", GetVersion(node1));
546 EXPECT_EQ("v1.0", GetVersion(node2));
549 TEST_F(EnhancedBookmarkModelTest, SetMultipleMetaInfo) {
550 const BookmarkNode* node = AddBookmark();
551 BookmarkNode::MetaInfoMap meta_info;
552 meta_info["a"] = "aa";
553 meta_info["b"] = "bb";
555 model_->SetVersionSuffix("1");
556 model_->SetMultipleMetaInfo(node, meta_info);
557 EXPECT_EQ("aa", GetMetaInfoField(node, "a"));
558 EXPECT_EQ("bb", GetMetaInfoField(node, "b"));
559 EXPECT_EQ("v1.0/1", GetVersion(node));
561 // Not present fields does not erase the fields already set on the node.
562 meta_info["a"] = "aaa";
563 model_->SetVersionSuffix("2");
564 model_->SetMultipleMetaInfo(node, meta_info);
565 EXPECT_EQ("aaa", GetMetaInfoField(node, "a"));
566 EXPECT_EQ("bb", GetMetaInfoField(node, "b"));
567 EXPECT_EQ("v1.0/2", GetVersion(node));
569 // Not actually changing any values should not set the version field.
570 model_->SetVersionSuffix("3");
571 model_->SetMultipleMetaInfo(node, meta_info);
572 EXPECT_EQ("v1.0/2", GetVersion(node));
575 TEST_F(EnhancedBookmarkModelTest, ObserverShuttingDownEvent) {
576 EXPECT_EQ(0, shutting_down_calls_);
577 model_->Shutdown();
578 EXPECT_EQ(1, shutting_down_calls_);
579 model_.reset();
582 TEST_F(EnhancedBookmarkModelTest, ObserverNodeAddedEvent) {
583 EXPECT_EQ(0, added_calls_);
584 const BookmarkNode* node = AddBookmark();
585 EXPECT_EQ(1, added_calls_);
586 EXPECT_EQ(node, last_added_);
588 const BookmarkNode* folder = AddFolder();
589 EXPECT_EQ(2, added_calls_);
590 EXPECT_EQ(folder, last_added_);
593 TEST_F(EnhancedBookmarkModelTest, ObserverNodeRemovedEvent) {
594 const BookmarkNode* node = AddBookmark();
595 const BookmarkNode* folder = AddFolder();
597 EXPECT_EQ(0, removed_calls_);
598 bookmark_model_->Remove(node);
599 EXPECT_EQ(1, removed_calls_);
600 EXPECT_EQ(node, last_removed_);
602 bookmark_model_->Remove(folder);
603 EXPECT_EQ(2, removed_calls_);
604 EXPECT_EQ(folder, last_removed_);
607 TEST_F(EnhancedBookmarkModelTest, ObserverNodeChangedEvent) {
608 const BookmarkNode* node = AddBookmark();
610 EXPECT_EQ(0, changed_calls_);
611 bookmark_model_->SetTitle(node, base::ASCIIToUTF16("New Title"));
612 EXPECT_EQ(1, changed_calls_);
613 EXPECT_EQ(node, last_changed_);
616 TEST_F(EnhancedBookmarkModelTest, ObserverAllUserNodesRemovedEvent) {
617 AddBookmark();
618 AddFolder();
619 EXPECT_EQ(0, all_user_nodes_removed_calls_);
620 bookmark_model_->RemoveAllUserBookmarks();
621 EXPECT_EQ(0, removed_calls_);
622 EXPECT_EQ(1, all_user_nodes_removed_calls_);
625 TEST_F(EnhancedBookmarkModelTest, ObserverRemoteIdChangedEvent) {
626 const BookmarkNode* node1 = AddFolder();
627 const BookmarkNode* node2 = AddFolder();
629 EXPECT_EQ(0, remote_id_changed_calls_);
630 bookmark_model_->SetNodeMetaInfo(node1, "stars.id", "c_1");
631 base::RunLoop().RunUntilIdle();
632 EXPECT_EQ(1, remote_id_changed_calls_);
633 EXPECT_EQ(node1, last_remote_id_node_);
634 EXPECT_EQ("", last_old_remote_id_);
635 EXPECT_EQ("c_1", last_remote_id_);
637 bookmark_model_->SetNodeMetaInfo(node2, "stars.id", "c_2");
638 base::RunLoop().RunUntilIdle();
639 EXPECT_EQ(2, remote_id_changed_calls_);
640 EXPECT_EQ(node2, last_remote_id_node_);
641 EXPECT_EQ("", last_old_remote_id_);
642 EXPECT_EQ("c_2", last_remote_id_);
644 bookmark_model_->SetNodeMetaInfo(node1, "stars.id", "c_3");
645 base::RunLoop().RunUntilIdle();
646 EXPECT_EQ(3, remote_id_changed_calls_);
647 EXPECT_EQ(node1, last_remote_id_node_);
648 EXPECT_EQ("c_1", last_old_remote_id_);
649 EXPECT_EQ("c_3", last_remote_id_);
651 // Set to duplicate ids.
652 bookmark_model_->SetNodeMetaInfo(node2, "stars.id", "c_3");
653 EXPECT_EQ(4, remote_id_changed_calls_);
654 EXPECT_EQ(node2, last_remote_id_node_);
655 EXPECT_EQ("c_2", last_old_remote_id_);
656 EXPECT_EQ("c_3", last_remote_id_);
657 base::RunLoop().RunUntilIdle();
658 EXPECT_EQ(6, remote_id_changed_calls_);
659 EXPECT_EQ("", last_remote_id_);
662 TEST_F(EnhancedBookmarkModelTest, ShutDownWhileResetDuplicationScheduled) {
663 const BookmarkNode* node1 = AddBookmark();
664 const BookmarkNode* node2 = AddBookmark();
665 bookmark_model_->SetNodeMetaInfo(node1, "stars.id", "c_1");
666 bookmark_model_->SetNodeMetaInfo(node2, "stars.id", "c_1");
667 model_->Shutdown();
668 model_.reset();
669 base::RunLoop().RunUntilIdle();
672 TEST_F(EnhancedBookmarkModelTest, NodeRemovedWhileResetDuplicationScheduled) {
673 const BookmarkNode* node1 = AddBookmark();
674 const BookmarkNode* node2 = AddBookmark();
675 bookmark_model_->SetNodeMetaInfo(node1, "stars.id", "c_1");
676 bookmark_model_->SetNodeMetaInfo(node2, "stars.id", "c_1");
677 bookmark_model_->Remove(node1);
678 base::RunLoop().RunUntilIdle();
681 TEST_F(EnhancedBookmarkModelTest,
682 RemoveParentShouldRemoveChildrenFromMaps) {
683 const BookmarkNode* parent = AddFolder();
684 const BookmarkNode* node = AddBookmark("Title", parent);
685 std::string remote_id = GetId(node);
686 EXPECT_EQ(node, model_->BookmarkForRemoteId(remote_id));
688 bookmark_model_->Remove(parent);
689 EXPECT_FALSE(model_->BookmarkForRemoteId(remote_id));
692 TEST_F(EnhancedBookmarkModelTest, AddsRemoteIdToNonClonedKeys) {
693 const std::set<std::string>& non_cloned_keys =
694 bookmark_model_->non_cloned_keys();
695 EXPECT_TRUE(non_cloned_keys.find("stars.id") != non_cloned_keys.end());
698 TEST_F(EnhancedBookmarkModelTest, RemoveImageData) {
699 const BookmarkNode* node = AddBookmark();
700 model_->SetAllImages(node, GURL(IMAGE_URL), 64, 64, GURL(IMAGE_URL), 16, 16);
702 GURL url;
703 int width, height;
704 EXPECT_TRUE(model_->GetOriginalImage(node, &url, &width, &height));
705 EXPECT_TRUE(model_->GetThumbnailImage(node, &url, &width, &height));
707 model_->RemoveImageData(node);
708 EXPECT_FALSE(model_->GetOriginalImage(node, &url, &width, &height));
709 EXPECT_FALSE(model_->GetThumbnailImage(node, &url, &width, &height));
711 std::string meta_info = GetMetaInfoField(node, "stars.imageData");
712 std::string decoded;
713 ASSERT_TRUE(base::Base64Decode(meta_info, &decoded));
714 image::collections::ImageData data;
715 ASSERT_TRUE(data.ParseFromString(decoded));
716 EXPECT_TRUE(data.user_removed_image());