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

Side by Side Diff: components/policy/core/browser/managed_bookmarks_tracker_unittest.cc

Issue 769153007: Managed bookmarks for supervised users (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix build (Android & unit_tests) Created 5 years, 10 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/policy/core/browser/managed_bookmarks_tracker.h" 5 #include "components/policy/core/browser/managed_bookmarks_tracker.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 21 matching lines...) Expand all
32 32
33 namespace policy { 33 namespace policy {
34 34
35 class ManagedBookmarksTrackerTest : public testing::Test { 35 class ManagedBookmarksTrackerTest : public testing::Test {
36 public: 36 public:
37 ManagedBookmarksTrackerTest() : managed_node_(NULL) {} 37 ManagedBookmarksTrackerTest() : managed_node_(NULL) {}
38 ~ManagedBookmarksTrackerTest() override {} 38 ~ManagedBookmarksTrackerTest() override {}
39 39
40 void SetUp() override { 40 void SetUp() override {
41 prefs_.registry()->RegisterListPref(bookmarks::prefs::kManagedBookmarks); 41 prefs_.registry()->RegisterListPref(bookmarks::prefs::kManagedBookmarks);
42 prefs_.registry()->RegisterListPref(bookmarks::prefs::kSupervisedBookmarks);
42 prefs_.registry()->RegisterListPref( 43 prefs_.registry()->RegisterListPref(
43 bookmarks::prefs::kBookmarkEditorExpandedNodes); 44 bookmarks::prefs::kBookmarkEditorExpandedNodes);
44 } 45 }
45 46
46 void TearDown() override { 47 void TearDown() override {
47 if (model_) 48 if (model_)
48 model_->RemoveObserver(&observer_); 49 model_->RemoveObserver(&observer_);
49 } 50 }
50 51
51 void CreateModel() { 52 void CreateModel(bool is_supervised) {
52 // Simulate the creation of the managed node by the BookmarkClient. 53 // Simulate the creation of the managed node by the BookmarkClient.
53 BookmarkPermanentNode* managed_node = new BookmarkPermanentNode(100); 54 BookmarkPermanentNode* managed_node = new BookmarkPermanentNode(100);
54 policy::ManagedBookmarksTracker::LoadInitial( 55 policy::ManagedBookmarksTracker::LoadInitial(
55 managed_node, prefs_.GetList(bookmarks::prefs::kManagedBookmarks), 101); 56 managed_node,
57 prefs_.GetList(ManagedBookmarksTracker::GetPrefName(is_supervised)),
58 101);
56 managed_node->set_visible(!managed_node->empty()); 59 managed_node->set_visible(!managed_node->empty());
57 managed_node->SetTitle(l10n_util::GetStringUTF16( 60 managed_node->SetTitle(l10n_util::GetStringUTF16(
58 IDS_BOOKMARK_BAR_MANAGED_FOLDER_DEFAULT_NAME)); 61 is_supervised ? IDS_BOOKMARK_BAR_SUPERVISED_FOLDER_DEFAULT_NAME
62 : IDS_BOOKMARK_BAR_MANAGED_FOLDER_DEFAULT_NAME));
59 63
60 bookmarks::BookmarkPermanentNodeList extra_nodes; 64 bookmarks::BookmarkPermanentNodeList extra_nodes;
61 extra_nodes.push_back(managed_node); 65 extra_nodes.push_back(managed_node);
62 client_.SetExtraNodesToLoad(extra_nodes.Pass()); 66 client_.SetExtraNodesToLoad(extra_nodes.Pass());
63 67
64 model_.reset(new BookmarkModel(&client_)); 68 model_.reset(new BookmarkModel(&client_));
65 model_->AddObserver(&observer_); 69 model_->AddObserver(&observer_);
66 EXPECT_CALL(observer_, BookmarkModelLoaded(model_.get(), _)); 70 EXPECT_CALL(observer_, BookmarkModelLoaded(model_.get(), _));
67 model_->Load(&prefs_, 71 model_->Load(&prefs_,
68 std::string(), 72 std::string(),
69 base::FilePath(), 73 base::FilePath(),
70 base::MessageLoopProxy::current(), 74 base::MessageLoopProxy::current(),
71 base::MessageLoopProxy::current()); 75 base::MessageLoopProxy::current());
72 bookmarks::test::WaitForBookmarkModelToLoad(model_.get()); 76 bookmarks::test::WaitForBookmarkModelToLoad(model_.get());
73 Mock::VerifyAndClearExpectations(&observer_); 77 Mock::VerifyAndClearExpectations(&observer_);
74 78
75 ASSERT_EQ(1u, client_.extra_nodes().size()); 79 ASSERT_EQ(1u, client_.extra_nodes().size());
76 managed_node_ = client_.extra_nodes()[0]; 80 managed_node_ = client_.extra_nodes()[0];
77 ASSERT_EQ(managed_node, managed_node_); 81 ASSERT_EQ(managed_node, managed_node_);
78 82
79 managed_bookmarks_tracker_.reset(new ManagedBookmarksTracker( 83 managed_bookmarks_tracker_.reset(new ManagedBookmarksTracker(
80 model_.get(), 84 model_.get(),
81 &prefs_, 85 &prefs_,
86 is_supervised,
82 base::Bind(&ManagedBookmarksTrackerTest::GetManagementDomain))); 87 base::Bind(&ManagedBookmarksTrackerTest::GetManagementDomain)));
83 managed_bookmarks_tracker_->Init(managed_node_); 88 managed_bookmarks_tracker_->Init(managed_node_);
84 } 89 }
85 90
86 const BookmarkNode* managed_node() { 91 const BookmarkNode* managed_node() {
87 return managed_node_; 92 return managed_node_;
88 } 93 }
89 94
90 bool IsManaged(const BookmarkNode* node) { 95 bool IsManaged(const BookmarkNode* node) {
91 return node && node->HasAncestor(managed_node_); 96 return node && node->HasAncestor(managed_node_);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 base::MessageLoop loop_; 171 base::MessageLoop loop_;
167 TestingPrefServiceSimple prefs_; 172 TestingPrefServiceSimple prefs_;
168 bookmarks::TestBookmarkClient client_; 173 bookmarks::TestBookmarkClient client_;
169 scoped_ptr<BookmarkModel> model_; 174 scoped_ptr<BookmarkModel> model_;
170 bookmarks::MockBookmarkModelObserver observer_; 175 bookmarks::MockBookmarkModelObserver observer_;
171 BookmarkPermanentNode* managed_node_; 176 BookmarkPermanentNode* managed_node_;
172 scoped_ptr<ManagedBookmarksTracker> managed_bookmarks_tracker_; 177 scoped_ptr<ManagedBookmarksTracker> managed_bookmarks_tracker_;
173 }; 178 };
174 179
175 TEST_F(ManagedBookmarksTrackerTest, Empty) { 180 TEST_F(ManagedBookmarksTrackerTest, Empty) {
176 CreateModel(); 181 CreateModel(false /* is_supervised */);
177 EXPECT_TRUE(model_->bookmark_bar_node()->empty()); 182 EXPECT_TRUE(model_->bookmark_bar_node()->empty());
178 EXPECT_TRUE(model_->other_node()->empty()); 183 EXPECT_TRUE(model_->other_node()->empty());
179 EXPECT_TRUE(managed_node()->empty()); 184 EXPECT_TRUE(managed_node()->empty());
180 EXPECT_FALSE(managed_node()->IsVisible()); 185 EXPECT_FALSE(managed_node()->IsVisible());
181 } 186 }
182 187
183 TEST_F(ManagedBookmarksTrackerTest, LoadInitial) { 188 TEST_F(ManagedBookmarksTrackerTest, LoadInitial) {
184 // Set a policy before loading the model. 189 // Set a policy before loading the model.
185 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks, CreateTestTree()); 190 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks, CreateTestTree());
186 CreateModel(); 191 CreateModel(false /* is_supervised */);
187 EXPECT_TRUE(model_->bookmark_bar_node()->empty()); 192 EXPECT_TRUE(model_->bookmark_bar_node()->empty());
188 EXPECT_TRUE(model_->other_node()->empty()); 193 EXPECT_TRUE(model_->other_node()->empty());
189 EXPECT_FALSE(managed_node()->empty()); 194 EXPECT_FALSE(managed_node()->empty());
190 EXPECT_TRUE(managed_node()->IsVisible()); 195 EXPECT_TRUE(managed_node()->IsVisible());
191 196
192 scoped_ptr<base::DictionaryValue> expected(CreateExpectedTree()); 197 scoped_ptr<base::DictionaryValue> expected(CreateExpectedTree());
193 EXPECT_TRUE(NodeMatchesValue(managed_node(), expected.get())); 198 EXPECT_TRUE(NodeMatchesValue(managed_node(), expected.get()));
194 } 199 }
195 200
201 TEST_F(ManagedBookmarksTrackerTest, SupervisedTrackerIgnoresManagedPref) {
202 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks, CreateTestTree());
203 CreateModel(true /* is_supervised */);
204 EXPECT_TRUE(managed_node()->empty());
205 EXPECT_FALSE(managed_node()->IsVisible());
206 }
207
208 TEST_F(ManagedBookmarksTrackerTest, SupervisedTrackerHandlesSupervisedPref) {
209 prefs_.SetManagedPref(bookmarks::prefs::kSupervisedBookmarks,
210 CreateTestTree());
211 CreateModel(true /* is_supervised */);
212 EXPECT_FALSE(managed_node()->empty());
213 EXPECT_TRUE(managed_node()->IsVisible());
214 // Don't bother checking the actual contents, the non-supervised tests cover
215 // that already.
216 }
217
196 TEST_F(ManagedBookmarksTrackerTest, SwapNodes) { 218 TEST_F(ManagedBookmarksTrackerTest, SwapNodes) {
197 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks, CreateTestTree()); 219 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks, CreateTestTree());
198 CreateModel(); 220 CreateModel(false /* is_supervised */);
199 221
200 // Swap the Google bookmark with the Folder. 222 // Swap the Google bookmark with the Folder.
201 scoped_ptr<base::ListValue> updated(CreateTestTree()); 223 scoped_ptr<base::ListValue> updated(CreateTestTree());
202 scoped_ptr<base::Value> removed; 224 scoped_ptr<base::Value> removed;
203 ASSERT_TRUE(updated->Remove(0, &removed)); 225 ASSERT_TRUE(updated->Remove(0, &removed));
204 updated->Append(removed.release()); 226 updated->Append(removed.release());
205 227
206 // These two nodes should just be swapped. 228 // These two nodes should just be swapped.
207 const BookmarkNode* parent = managed_node(); 229 const BookmarkNode* parent = managed_node();
208 EXPECT_CALL(observer_, BookmarkNodeMoved(model_.get(), parent, 1, parent, 0)); 230 EXPECT_CALL(observer_, BookmarkNodeMoved(model_.get(), parent, 1, parent, 0));
209 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks, 231 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks,
210 updated->DeepCopy()); 232 updated->DeepCopy());
211 Mock::VerifyAndClearExpectations(&observer_); 233 Mock::VerifyAndClearExpectations(&observer_);
212 234
213 // Verify the final tree. 235 // Verify the final tree.
214 scoped_ptr<base::DictionaryValue> expected( 236 scoped_ptr<base::DictionaryValue> expected(
215 CreateFolder(GetManagedFolderTitle(), updated.release())); 237 CreateFolder(GetManagedFolderTitle(), updated.release()));
216 EXPECT_TRUE(NodeMatchesValue(managed_node(), expected.get())); 238 EXPECT_TRUE(NodeMatchesValue(managed_node(), expected.get()));
217 } 239 }
218 240
219 TEST_F(ManagedBookmarksTrackerTest, RemoveNode) { 241 TEST_F(ManagedBookmarksTrackerTest, RemoveNode) {
220 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks, CreateTestTree()); 242 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks, CreateTestTree());
221 CreateModel(); 243 CreateModel(false /* is_supervised */);
222 244
223 // Remove the Folder. 245 // Remove the Folder.
224 scoped_ptr<base::ListValue> updated(CreateTestTree()); 246 scoped_ptr<base::ListValue> updated(CreateTestTree());
225 ASSERT_TRUE(updated->Remove(1, NULL)); 247 ASSERT_TRUE(updated->Remove(1, NULL));
226 248
227 const BookmarkNode* parent = managed_node(); 249 const BookmarkNode* parent = managed_node();
228 EXPECT_CALL(observer_, BookmarkNodeRemoved(model_.get(), parent, 1, _, _)); 250 EXPECT_CALL(observer_, BookmarkNodeRemoved(model_.get(), parent, 1, _, _));
229 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks, 251 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks,
230 updated->DeepCopy()); 252 updated->DeepCopy());
231 Mock::VerifyAndClearExpectations(&observer_); 253 Mock::VerifyAndClearExpectations(&observer_);
232 254
233 // Verify the final tree. 255 // Verify the final tree.
234 scoped_ptr<base::DictionaryValue> expected( 256 scoped_ptr<base::DictionaryValue> expected(
235 CreateFolder(GetManagedFolderTitle(), updated.release())); 257 CreateFolder(GetManagedFolderTitle(), updated.release()));
236 EXPECT_TRUE(NodeMatchesValue(managed_node(), expected.get())); 258 EXPECT_TRUE(NodeMatchesValue(managed_node(), expected.get()));
237 } 259 }
238 260
239 TEST_F(ManagedBookmarksTrackerTest, CreateNewNodes) { 261 TEST_F(ManagedBookmarksTrackerTest, CreateNewNodes) {
240 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks, CreateTestTree()); 262 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks, CreateTestTree());
241 CreateModel(); 263 CreateModel(false /* is_supervised */);
242 264
243 // Put all the nodes inside another folder. 265 // Put all the nodes inside another folder.
244 scoped_ptr<base::ListValue> updated(new base::ListValue); 266 scoped_ptr<base::ListValue> updated(new base::ListValue);
245 updated->Append(CreateFolder("Container", CreateTestTree())); 267 updated->Append(CreateFolder("Container", CreateTestTree()));
246 268
247 EXPECT_CALL(observer_, BookmarkNodeAdded(model_.get(), _, _)).Times(5); 269 EXPECT_CALL(observer_, BookmarkNodeAdded(model_.get(), _, _)).Times(5);
248 // The remaining nodes have been pushed to positions 1 and 2; they'll both be 270 // The remaining nodes have been pushed to positions 1 and 2; they'll both be
249 // removed when at position 1. 271 // removed when at position 1.
250 const BookmarkNode* parent = managed_node(); 272 const BookmarkNode* parent = managed_node();
251 EXPECT_CALL(observer_, BookmarkNodeRemoved(model_.get(), parent, 1, _, _)) 273 EXPECT_CALL(observer_, BookmarkNodeRemoved(model_.get(), parent, 1, _, _))
252 .Times(2); 274 .Times(2);
253 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks, 275 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks,
254 updated->DeepCopy()); 276 updated->DeepCopy());
255 Mock::VerifyAndClearExpectations(&observer_); 277 Mock::VerifyAndClearExpectations(&observer_);
256 278
257 // Verify the final tree. 279 // Verify the final tree.
258 scoped_ptr<base::DictionaryValue> expected( 280 scoped_ptr<base::DictionaryValue> expected(
259 CreateFolder(GetManagedFolderTitle(), updated.release())); 281 CreateFolder(GetManagedFolderTitle(), updated.release()));
260 EXPECT_TRUE(NodeMatchesValue(managed_node(), expected.get())); 282 EXPECT_TRUE(NodeMatchesValue(managed_node(), expected.get()));
261 } 283 }
262 284
263 TEST_F(ManagedBookmarksTrackerTest, RemoveAll) { 285 TEST_F(ManagedBookmarksTrackerTest, RemoveAll) {
264 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks, CreateTestTree()); 286 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks, CreateTestTree());
265 CreateModel(); 287 CreateModel(false /* is_supervised */);
266 EXPECT_TRUE(managed_node()->IsVisible()); 288 EXPECT_TRUE(managed_node()->IsVisible());
267 289
268 // Remove the policy. 290 // Remove the policy.
269 const BookmarkNode* parent = managed_node(); 291 const BookmarkNode* parent = managed_node();
270 EXPECT_CALL(observer_, BookmarkNodeRemoved(model_.get(), parent, 0, _, _)) 292 EXPECT_CALL(observer_, BookmarkNodeRemoved(model_.get(), parent, 0, _, _))
271 .Times(2); 293 .Times(2);
272 prefs_.RemoveManagedPref(bookmarks::prefs::kManagedBookmarks); 294 prefs_.RemoveManagedPref(bookmarks::prefs::kManagedBookmarks);
273 Mock::VerifyAndClearExpectations(&observer_); 295 Mock::VerifyAndClearExpectations(&observer_);
274 296
275 EXPECT_TRUE(managed_node()->empty()); 297 EXPECT_TRUE(managed_node()->empty());
276 EXPECT_FALSE(managed_node()->IsVisible()); 298 EXPECT_FALSE(managed_node()->IsVisible());
277 } 299 }
278 300
279 TEST_F(ManagedBookmarksTrackerTest, IsManaged) { 301 TEST_F(ManagedBookmarksTrackerTest, IsManaged) {
280 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks, CreateTestTree()); 302 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks, CreateTestTree());
281 CreateModel(); 303 CreateModel(false /* is_supervised */);
282 304
283 EXPECT_FALSE(IsManaged(model_->root_node())); 305 EXPECT_FALSE(IsManaged(model_->root_node()));
284 EXPECT_FALSE(IsManaged(model_->bookmark_bar_node())); 306 EXPECT_FALSE(IsManaged(model_->bookmark_bar_node()));
285 EXPECT_FALSE(IsManaged(model_->other_node())); 307 EXPECT_FALSE(IsManaged(model_->other_node()));
286 EXPECT_FALSE(IsManaged(model_->mobile_node())); 308 EXPECT_FALSE(IsManaged(model_->mobile_node()));
287 EXPECT_TRUE(IsManaged(managed_node())); 309 EXPECT_TRUE(IsManaged(managed_node()));
288 310
289 const BookmarkNode* parent = managed_node(); 311 const BookmarkNode* parent = managed_node();
290 ASSERT_EQ(2, parent->child_count()); 312 ASSERT_EQ(2, parent->child_count());
291 EXPECT_TRUE(IsManaged(parent->GetChild(0))); 313 EXPECT_TRUE(IsManaged(parent->GetChild(0)));
292 EXPECT_TRUE(IsManaged(parent->GetChild(1))); 314 EXPECT_TRUE(IsManaged(parent->GetChild(1)));
293 315
294 parent = parent->GetChild(1); 316 parent = parent->GetChild(1);
295 ASSERT_EQ(2, parent->child_count()); 317 ASSERT_EQ(2, parent->child_count());
296 EXPECT_TRUE(IsManaged(parent->GetChild(0))); 318 EXPECT_TRUE(IsManaged(parent->GetChild(0)));
297 EXPECT_TRUE(IsManaged(parent->GetChild(1))); 319 EXPECT_TRUE(IsManaged(parent->GetChild(1)));
298 } 320 }
299 321
300 TEST_F(ManagedBookmarksTrackerTest, RemoveAllUserBookmarksDoesntRemoveManaged) { 322 TEST_F(ManagedBookmarksTrackerTest, RemoveAllUserBookmarksDoesntRemoveManaged) {
301 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks, CreateTestTree()); 323 prefs_.SetManagedPref(bookmarks::prefs::kManagedBookmarks, CreateTestTree());
302 CreateModel(); 324 CreateModel(false /* is_supervised */);
303 EXPECT_EQ(2, managed_node()->child_count()); 325 EXPECT_EQ(2, managed_node()->child_count());
304 326
305 EXPECT_CALL(observer_, 327 EXPECT_CALL(observer_,
306 BookmarkNodeAdded(model_.get(), model_->bookmark_bar_node(), 0)); 328 BookmarkNodeAdded(model_.get(), model_->bookmark_bar_node(), 0));
307 EXPECT_CALL(observer_, 329 EXPECT_CALL(observer_,
308 BookmarkNodeAdded(model_.get(), model_->bookmark_bar_node(), 1)); 330 BookmarkNodeAdded(model_.get(), model_->bookmark_bar_node(), 1));
309 model_->AddURL(model_->bookmark_bar_node(), 331 model_->AddURL(model_->bookmark_bar_node(),
310 0, 332 0,
311 base::ASCIIToUTF16("Test"), 333 base::ASCIIToUTF16("Test"),
312 GURL("http://google.com/")); 334 GURL("http://google.com/"));
313 model_->AddFolder( 335 model_->AddFolder(
314 model_->bookmark_bar_node(), 1, base::ASCIIToUTF16("Test Folder")); 336 model_->bookmark_bar_node(), 1, base::ASCIIToUTF16("Test Folder"));
315 EXPECT_EQ(2, model_->bookmark_bar_node()->child_count()); 337 EXPECT_EQ(2, model_->bookmark_bar_node()->child_count());
316 Mock::VerifyAndClearExpectations(&observer_); 338 Mock::VerifyAndClearExpectations(&observer_);
317 339
318 EXPECT_CALL(observer_, BookmarkAllUserNodesRemoved(model_.get(), _)); 340 EXPECT_CALL(observer_, BookmarkAllUserNodesRemoved(model_.get(), _));
319 model_->RemoveAllUserBookmarks(); 341 model_->RemoveAllUserBookmarks();
320 EXPECT_EQ(2, managed_node()->child_count()); 342 EXPECT_EQ(2, managed_node()->child_count());
321 EXPECT_EQ(0, model_->bookmark_bar_node()->child_count()); 343 EXPECT_EQ(0, model_->bookmark_bar_node()->child_count());
322 Mock::VerifyAndClearExpectations(&observer_); 344 Mock::VerifyAndClearExpectations(&observer_);
323 } 345 }
324 346
325 } // namespace policy 347 } // namespace policy
OLDNEW
« no previous file with comments | « components/policy/core/browser/managed_bookmarks_tracker.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698