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"
21 scoped_ptr
<PrefService
> PrefServiceForTesting() {
22 scoped_refptr
<user_prefs::PrefRegistrySyncable
> registry(
23 new user_prefs::PrefRegistrySyncable());
24 registry
->RegisterListPref(prefs::kBookmarkEditorExpandedNodes
,
26 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
27 base::PrefServiceFactory factory
;
28 factory
.set_user_prefs(make_scoped_refptr(new TestingPrefStore()));
29 return factory
.Create(registry
.get());
32 class BookmarkExpandedStateTrackerTest
: public testing::Test
{
34 BookmarkExpandedStateTrackerTest();
35 virtual ~BookmarkExpandedStateTrackerTest();
39 virtual void SetUp() OVERRIDE
;
40 virtual void TearDown() OVERRIDE
;
42 base::MessageLoop message_loop_
;
43 test::TestBookmarkClient client_
;
44 scoped_ptr
<PrefService
> prefs_
;
45 scoped_ptr
<BookmarkModel
> model_
;
47 DISALLOW_COPY_AND_ASSIGN(BookmarkExpandedStateTrackerTest
);
50 BookmarkExpandedStateTrackerTest::BookmarkExpandedStateTrackerTest() {}
52 BookmarkExpandedStateTrackerTest::~BookmarkExpandedStateTrackerTest() {}
54 void BookmarkExpandedStateTrackerTest::SetUp() {
55 prefs_
= PrefServiceForTesting();
56 model_
.reset(new BookmarkModel(&client_
, false));
57 model_
->Load(prefs_
.get(),
60 base::MessageLoopProxy::current(),
61 base::MessageLoopProxy::current());
62 test::WaitForBookmarkModelToLoad(model_
.get());
65 void BookmarkExpandedStateTrackerTest::TearDown() {
69 // Various assertions for SetExpandedNodes.
70 TEST_F(BookmarkExpandedStateTrackerTest
, SetExpandedNodes
) {
71 BookmarkExpandedStateTracker
* tracker
= model_
->expanded_state_tracker();
73 // Should start out initially empty.
74 EXPECT_TRUE(tracker
->GetExpandedNodes().empty());
76 BookmarkExpandedStateTracker::Nodes nodes
;
77 nodes
.insert(model_
->bookmark_bar_node());
78 tracker
->SetExpandedNodes(nodes
);
79 EXPECT_EQ(nodes
, tracker
->GetExpandedNodes());
81 // Add a folder and mark it expanded.
82 const BookmarkNode
* n1
= model_
->AddFolder(
83 model_
->bookmark_bar_node(), 0, base::ASCIIToUTF16("x"));
85 tracker
->SetExpandedNodes(nodes
);
86 EXPECT_EQ(nodes
, tracker
->GetExpandedNodes());
88 // Remove the folder, which should remove it from the list of expanded nodes.
89 model_
->Remove(model_
->bookmark_bar_node(), 0);
92 EXPECT_EQ(nodes
, tracker
->GetExpandedNodes());
95 TEST_F(BookmarkExpandedStateTrackerTest
, RemoveAll
) {
96 BookmarkExpandedStateTracker
* tracker
= model_
->expanded_state_tracker();
98 // Add a folder and mark it expanded.
99 const BookmarkNode
* n1
= model_
->AddFolder(
100 model_
->bookmark_bar_node(), 0, base::ASCIIToUTF16("x"));
101 BookmarkExpandedStateTracker::Nodes nodes
;
103 tracker
->SetExpandedNodes(nodes
);
104 // Verify that the node is present.
105 EXPECT_EQ(nodes
, tracker
->GetExpandedNodes());
108 // Verify node is not present.
109 EXPECT_TRUE(tracker
->GetExpandedNodes().empty());