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/bookmarks/browser/bookmark_expanded_state_tracker.h"
7 #include "base/files/file_path.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/prefs/pref_service_factory.h"
11 #include "base/prefs/testing_pref_store.h"
12 #include "base/run_loop.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "components/bookmarks/browser/bookmark_model.h"
15 #include "components/bookmarks/common/bookmark_pref_names.h"
16 #include "components/bookmarks/test/bookmark_test_helpers.h"
17 #include "components/bookmarks/test/test_bookmark_client.h"
18 #include "components/pref_registry/pref_registry_syncable.h"
19 #include "testing/gtest/include/gtest/gtest.h"
23 scoped_ptr
<PrefService
> PrefServiceForTesting() {
24 scoped_refptr
<user_prefs::PrefRegistrySyncable
> registry(
25 new user_prefs::PrefRegistrySyncable());
26 registry
->RegisterListPref(prefs::kBookmarkEditorExpandedNodes
,
28 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
29 registry
->RegisterListPref(prefs::kManagedBookmarks
,
30 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
31 base::PrefServiceFactory factory
;
32 factory
.set_user_prefs(make_scoped_refptr(new TestingPrefStore()));
33 return factory
.Create(registry
.get());
36 class BookmarkExpandedStateTrackerTest
: public testing::Test
{
38 BookmarkExpandedStateTrackerTest();
39 virtual ~BookmarkExpandedStateTrackerTest();
43 virtual void SetUp() OVERRIDE
;
44 virtual void TearDown() OVERRIDE
;
46 base::MessageLoop message_loop_
;
47 test::TestBookmarkClient client_
;
48 scoped_ptr
<PrefService
> prefs_
;
49 scoped_ptr
<BookmarkModel
> model_
;
51 DISALLOW_COPY_AND_ASSIGN(BookmarkExpandedStateTrackerTest
);
54 BookmarkExpandedStateTrackerTest::BookmarkExpandedStateTrackerTest() {}
56 BookmarkExpandedStateTrackerTest::~BookmarkExpandedStateTrackerTest() {}
58 void BookmarkExpandedStateTrackerTest::SetUp() {
59 prefs_
= PrefServiceForTesting();
60 model_
.reset(new BookmarkModel(&client_
));
61 model_
->Load(prefs_
.get(),
64 base::MessageLoopProxy::current(),
65 base::MessageLoopProxy::current());
66 test::WaitForBookmarkModelToLoad(model_
.get());
69 void BookmarkExpandedStateTrackerTest::TearDown() {
73 // Various assertions for SetExpandedNodes.
74 TEST_F(BookmarkExpandedStateTrackerTest
, SetExpandedNodes
) {
75 BookmarkExpandedStateTracker
* tracker
= model_
->expanded_state_tracker();
77 // Should start out initially empty.
78 EXPECT_TRUE(tracker
->GetExpandedNodes().empty());
80 BookmarkExpandedStateTracker::Nodes nodes
;
81 nodes
.insert(model_
->bookmark_bar_node());
82 tracker
->SetExpandedNodes(nodes
);
83 EXPECT_EQ(nodes
, tracker
->GetExpandedNodes());
85 // Add a folder and mark it expanded.
86 const BookmarkNode
* n1
= model_
->AddFolder(
87 model_
->bookmark_bar_node(), 0, base::ASCIIToUTF16("x"));
89 tracker
->SetExpandedNodes(nodes
);
90 EXPECT_EQ(nodes
, tracker
->GetExpandedNodes());
92 // Remove the folder, which should remove it from the list of expanded nodes.
93 model_
->Remove(model_
->bookmark_bar_node(), 0);
96 EXPECT_EQ(nodes
, tracker
->GetExpandedNodes());
99 TEST_F(BookmarkExpandedStateTrackerTest
, RemoveAllUserBookmarks
) {
100 BookmarkExpandedStateTracker
* tracker
= model_
->expanded_state_tracker();
102 // Add a folder and mark it expanded.
103 const BookmarkNode
* n1
= model_
->AddFolder(
104 model_
->bookmark_bar_node(), 0, base::ASCIIToUTF16("x"));
105 BookmarkExpandedStateTracker::Nodes nodes
;
107 tracker
->SetExpandedNodes(nodes
);
108 // Verify that the node is present.
109 EXPECT_EQ(nodes
, tracker
->GetExpandedNodes());
111 model_
->RemoveAllUserBookmarks();
112 // Verify node is not present.
113 EXPECT_TRUE(tracker
->GetExpandedNodes().empty());
116 } // namespace bookmarks