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

Side by Side Diff: chrome/browser/bookmarks/chrome_bookmark_client_unittest.cc

Issue 319543003: Create the managed_node at the ChromeBookmarkClient. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: added unittest Created 6 years, 6 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 | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/bookmarks/chrome_bookmark_client.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h"
10 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
11 #include "chrome/browser/bookmarks/chrome_bookmark_client.h"
12 #include "chrome/test/base/testing_pref_service_syncable.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "components/bookmarks/browser/bookmark_model.h"
15 #include "components/bookmarks/browser/bookmark_node.h"
16 #include "components/bookmarks/common/bookmark_pref_names.h"
17 #include "components/bookmarks/test/bookmark_test_helpers.h"
18 #include "components/bookmarks/test/mock_bookmark_model_observer.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "grit/components_strings.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/base/l10n/l10n_util.h"
24
25 using testing::Mock;
26 using testing::_;
27
28 class ChromeBookmarkClientTest : public testing::Test {
29 public:
30 ChromeBookmarkClientTest() : client_(NULL), model_(NULL) {}
31 virtual ~ChromeBookmarkClientTest() {}
32
33 virtual void SetUp() OVERRIDE {
34 prefs_ = profile_.GetTestingPrefService();
35 ASSERT_FALSE(prefs_->HasPrefPath(prefs::kManagedBookmarks));
36
37 prefs_->SetManagedPref(prefs::kManagedBookmarks, CreateTestTree());
38 ResetModel();
39
40 // The managed node always exists.
41 ASSERT_TRUE(client_->managed_node());
42 ASSERT_TRUE(client_->managed_node()->parent() == model_->root_node());
43 EXPECT_NE(-1, model_->root_node()->GetIndexOf(client_->managed_node()));
44 }
45
46 virtual void TearDown() OVERRIDE {
47 model_->RemoveObserver(&observer_);
48 }
49
50 void ResetModel() {
51 profile_.CreateBookmarkModel(false);
52 client_ =
53 BookmarkModelFactory::GetChromeBookmarkClientForProfile(&profile_);
54 ASSERT_TRUE(client_);
55 model_ = client_->model();
56 test::WaitForBookmarkModelToLoad(model_);
57 model_->AddObserver(&observer_);
58 }
59
60 static base::DictionaryValue* CreateBookmark(const std::string& title,
61 const std::string& url) {
62 EXPECT_TRUE(GURL(url).is_valid());
63 base::DictionaryValue* dict = new base::DictionaryValue();
64 dict->SetString("name", title);
65 dict->SetString("url", GURL(url).spec());
66 return dict;
67 }
68
69 static base::DictionaryValue* CreateFolder(const std::string& title,
70 base::ListValue* children) {
71 base::DictionaryValue* dict = new base::DictionaryValue();
72 dict->SetString("name", title);
73 dict->Set("children", children);
74 return dict;
75 }
76
77 static base::ListValue* CreateTestTree() {
78 base::ListValue* folder = new base::ListValue();
79 base::ListValue* empty = new base::ListValue();
80 folder->Append(CreateFolder("Empty", empty));
81 folder->Append(CreateBookmark("Youtube", "http://youtube.com/"));
82
83 base::ListValue* list = new base::ListValue();
84 list->Append(CreateBookmark("Google", "http://google.com/"));
85 list->Append(CreateFolder("Folder", folder));
86
87 return list;
88 }
89
90 static base::DictionaryValue* CreateExpectedTree() {
91 return CreateFolder(GetManagedFolderTitle(), CreateTestTree());
92 }
93
94 static std::string GetManagedFolderTitle() {
95 return l10n_util::GetStringUTF8(
96 IDS_BOOKMARK_BAR_MANAGED_FOLDER_DEFAULT_NAME);
97 }
98
99 static bool NodeMatchesValue(const BookmarkNode* node,
100 const base::DictionaryValue* dict) {
101 base::string16 title;
102 if (!dict->GetString("name", &title) || node->GetTitle() != title)
103 return false;
104
105 if (node->is_folder()) {
106 const base::ListValue* children = NULL;
107 if (!dict->GetList("children", &children) ||
108 node->child_count() != static_cast<int>(children->GetSize())) {
109 return false;
110 }
111 for (int i = 0; i < node->child_count(); ++i) {
112 const base::DictionaryValue* child = NULL;
113 if (!children->GetDictionary(i, &child) ||
114 !NodeMatchesValue(node->GetChild(i), child)) {
115 return false;
116 }
117 }
118 } else if (node->is_url()) {
119 std::string url;
120 if (!dict->GetString("url", &url) || node->url() != GURL(url))
121 return false;
122 } else {
123 return false;
124 }
125 return true;
126 }
127
128 content::TestBrowserThreadBundle thread_bundle_;
129 TestingProfile profile_;
130 TestingPrefServiceSyncable* prefs_;
131 MockBookmarkModelObserver observer_;
132 ChromeBookmarkClient* client_;
133 BookmarkModel* model_;
134 };
sky 2014/06/05 16:03:56 private: DISALLOW...
Joao da Silva 2014/06/05 16:57:13 Done.
135
136 TEST_F(ChromeBookmarkClientTest, EmptyManagedNode) {
137 // Verifies that the managed node is empty and invisible when the policy is
138 // not set.
139 model_->RemoveObserver(&observer_);
140 prefs_->RemoveManagedPref(prefs::kManagedBookmarks);
141 ResetModel();
142
143 EXPECT_TRUE(client_->managed_node()->empty());
sky 2014/06/05 16:03:56 Maybe an: ASSERT_TRUE(client_->managed_node() != N
Joao da Silva 2014/06/05 16:57:13 Done.
144 EXPECT_FALSE(client_->managed_node()->IsVisible());
145 }
146
147 TEST_F(ChromeBookmarkClientTest, LoadInitial) {
148 // Verifies that the initial load picks up the initial policy too.
149 EXPECT_TRUE(model_->bookmark_bar_node()->empty());
150 EXPECT_TRUE(model_->other_node()->empty());
151 EXPECT_FALSE(client_->managed_node()->empty());
152 EXPECT_TRUE(client_->managed_node()->IsVisible());
153
154 scoped_ptr<base::DictionaryValue> expected(CreateExpectedTree());
155 EXPECT_TRUE(NodeMatchesValue(client_->managed_node(), expected.get()));
156 }
157
158 TEST_F(ChromeBookmarkClientTest, SwapNodes) {
159 // Swap the Google bookmark with the Folder.
160 scoped_ptr<base::ListValue> updated(CreateTestTree());
161 scoped_ptr<base::Value> removed;
162 ASSERT_TRUE(updated->Remove(0, &removed));
163 updated->Append(removed.release());
164
165 // These two nodes should just be swapped.
166 const BookmarkNode* parent = client_->managed_node();
167 EXPECT_CALL(observer_, BookmarkNodeMoved(model_, parent, 1, parent, 0));
168 prefs_->SetManagedPref(prefs::kManagedBookmarks, updated->DeepCopy());
169 Mock::VerifyAndClearExpectations(&observer_);
170
171 // Verify the final tree.
172 scoped_ptr<base::DictionaryValue> expected(
173 CreateFolder(GetManagedFolderTitle(), updated.release()));
174 EXPECT_TRUE(NodeMatchesValue(client_->managed_node(), expected.get()));
175 }
176
177 TEST_F(ChromeBookmarkClientTest, RemoveNode) {
178 // Remove the Folder.
179 scoped_ptr<base::ListValue> updated(CreateTestTree());
180 ASSERT_TRUE(updated->Remove(1, NULL));
181
182 const BookmarkNode* parent = client_->managed_node();
183 EXPECT_CALL(observer_, BookmarkNodeRemoved(model_, parent, 1, _, _));
184 prefs_->SetManagedPref(prefs::kManagedBookmarks, updated->DeepCopy());
185 Mock::VerifyAndClearExpectations(&observer_);
186
187 // Verify the final tree.
188 scoped_ptr<base::DictionaryValue> expected(
189 CreateFolder(GetManagedFolderTitle(), updated.release()));
190 EXPECT_TRUE(NodeMatchesValue(client_->managed_node(), expected.get()));
191 }
192
193 TEST_F(ChromeBookmarkClientTest, CreateNewNodes) {
194 // Put all the nodes inside another folder.
195 scoped_ptr<base::ListValue> updated(new base::ListValue);
196 updated->Append(CreateFolder("Container", CreateTestTree()));
197
198 EXPECT_CALL(observer_, BookmarkNodeAdded(model_, _, _)).Times(5);
199 // The remaining nodes have been pushed to positions 1 and 2; they'll both be
200 // removed when at position 1.
201 const BookmarkNode* parent = client_->managed_node();
202 EXPECT_CALL(observer_, BookmarkNodeRemoved(model_, parent, 1, _, _))
203 .Times(2);
204 prefs_->SetManagedPref(prefs::kManagedBookmarks, updated->DeepCopy());
205 Mock::VerifyAndClearExpectations(&observer_);
206
207 // Verify the final tree.
208 scoped_ptr<base::DictionaryValue> expected(
209 CreateFolder(GetManagedFolderTitle(), updated.release()));
210 EXPECT_TRUE(NodeMatchesValue(client_->managed_node(), expected.get()));
211 }
212
213 TEST_F(ChromeBookmarkClientTest, RemoveAll) {
214 // Remove the policy.
215 const BookmarkNode* parent = client_->managed_node();
216 EXPECT_CALL(observer_, BookmarkNodeRemoved(model_, parent, 0, _, _))
217 .Times(2);
218 prefs_->RemoveManagedPref(prefs::kManagedBookmarks);
219 Mock::VerifyAndClearExpectations(&observer_);
220
221 EXPECT_TRUE(client_->managed_node()->empty());
222 EXPECT_FALSE(client_->managed_node()->IsVisible());
223 }
224
225 TEST_F(ChromeBookmarkClientTest, IsAManagedNode) {
226 EXPECT_FALSE(client_->IsAManagedNode(model_->root_node()));
227 EXPECT_FALSE(client_->IsAManagedNode(model_->bookmark_bar_node()));
228 EXPECT_FALSE(client_->IsAManagedNode(model_->other_node()));
229 EXPECT_FALSE(client_->IsAManagedNode(model_->mobile_node()));
230 EXPECT_TRUE(client_->IsAManagedNode(client_->managed_node()));
231
232 const BookmarkNode* parent = client_->managed_node();
233 ASSERT_EQ(2, parent->child_count());
234 EXPECT_TRUE(client_->IsAManagedNode(parent->GetChild(0)));
235 EXPECT_TRUE(client_->IsAManagedNode(parent->GetChild(1)));
236
237 parent = parent->GetChild(1);
238 ASSERT_EQ(2, parent->child_count());
239 EXPECT_TRUE(client_->IsAManagedNode(parent->GetChild(0)));
240 EXPECT_TRUE(client_->IsAManagedNode(parent->GetChild(1)));
241 }
242
243 TEST_F(ChromeBookmarkClientTest, RemoveAllDoesntRemoveManaged) {
244 EXPECT_EQ(2, client_->managed_node()->child_count());
245
246 EXPECT_CALL(observer_,
247 BookmarkNodeAdded(model_, model_->bookmark_bar_node(), 0));
248 EXPECT_CALL(observer_,
249 BookmarkNodeAdded(model_, model_->bookmark_bar_node(), 1));
250 model_->AddURL(model_->bookmark_bar_node(),
251 0,
252 base::ASCIIToUTF16("Test"),
253 GURL("http://google.com/"));
254 model_->AddFolder(
255 model_->bookmark_bar_node(), 1, base::ASCIIToUTF16("Test Folder"));
256 EXPECT_EQ(2, model_->bookmark_bar_node()->child_count());
257 Mock::VerifyAndClearExpectations(&observer_);
258
259 EXPECT_CALL(observer_, BookmarkAllNodesRemoved(model_, _));
260 model_->RemoveAll();
261 EXPECT_EQ(2, client_->managed_node()->child_count());
262 EXPECT_EQ(0, model_->bookmark_bar_node()->child_count());
263 Mock::VerifyAndClearExpectations(&observer_);
264 }
OLDNEW
« no previous file with comments | « chrome/browser/bookmarks/chrome_bookmark_client.cc ('k') | chrome/browser/policy/managed_bookmarks_policy_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698