1 // Copyright (c) 2012 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 "chrome/browser/ui/bookmarks/recently_used_folders_combo_model.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "components/bookmarks/browser/bookmark_model.h"
13 #include "components/bookmarks/test/bookmark_test_helpers.h"
14 #include "content/public/test/test_browser_thread.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/base/models/combobox_model_observer.h"
18 using bookmarks::BookmarkModel
;
19 using bookmarks::BookmarkNode
;
20 using content::BrowserThread
;
22 // Implementation of ComboboxModelObserver that records when
23 // OnComboboxModelChanged() is invoked.
24 class TestComboboxModelObserver
: public ui::ComboboxModelObserver
{
26 TestComboboxModelObserver() : changed_(false) {}
27 ~TestComboboxModelObserver() override
{}
29 // Returns whether the model changed and clears changed state.
30 bool GetAndClearChanged() {
31 const bool changed
= changed_
;
36 // ui::ComboboxModelObserver:
37 void OnComboboxModelChanged(ui::ComboboxModel
* model
) override
{
44 DISALLOW_COPY_AND_ASSIGN(TestComboboxModelObserver
);
47 class RecentlyUsedFoldersComboModelTest
: public testing::Test
{
49 RecentlyUsedFoldersComboModelTest();
51 void SetUp() override
;
52 void TearDown() override
;
55 BookmarkModel
* GetModel();
58 base::MessageLoopForUI message_loop_
;
59 content::TestBrowserThread ui_thread_
;
60 content::TestBrowserThread file_thread_
;
61 scoped_ptr
<TestingProfile
> profile_
;
63 DISALLOW_COPY_AND_ASSIGN(RecentlyUsedFoldersComboModelTest
);
66 RecentlyUsedFoldersComboModelTest::RecentlyUsedFoldersComboModelTest()
67 : ui_thread_(BrowserThread::UI
, &message_loop_
),
68 file_thread_(BrowserThread::FILE, &message_loop_
) {
71 void RecentlyUsedFoldersComboModelTest::SetUp() {
72 profile_
.reset(new TestingProfile());
73 profile_
->CreateBookmarkModel(true);
74 bookmarks::test::WaitForBookmarkModelToLoad(GetModel());
77 void RecentlyUsedFoldersComboModelTest::TearDown() {
78 // Flush the message loop to make application verifiers happy.
79 message_loop_
.RunUntilIdle();
82 BookmarkModel
* RecentlyUsedFoldersComboModelTest::GetModel() {
83 return BookmarkModelFactory::GetForProfile(profile_
.get());
86 // Verifies there are no duplicate nodes in the model.
87 TEST_F(RecentlyUsedFoldersComboModelTest
, NoDups
) {
88 const BookmarkNode
* new_node
= GetModel()->AddURL(
89 GetModel()->bookmark_bar_node(), 0, base::ASCIIToUTF16("a"),
91 RecentlyUsedFoldersComboModel
model(GetModel(), new_node
);
92 std::set
<base::string16
> items
;
93 for (int i
= 0; i
< model
.GetItemCount(); ++i
) {
94 if (!model
.IsItemSeparatorAt(i
))
95 EXPECT_EQ(0u, items
.count(model
.GetItemAt(i
)));
99 // Verifies that observers are notified on changes.
100 TEST_F(RecentlyUsedFoldersComboModelTest
, NotifyObserver
) {
101 const BookmarkNode
* folder
= GetModel()->AddFolder(
102 GetModel()->bookmark_bar_node(), 0, base::ASCIIToUTF16("a"));
103 const BookmarkNode
* sub_folder
= GetModel()->AddFolder(
104 folder
, 0, base::ASCIIToUTF16("b"));
105 const BookmarkNode
* new_node
= GetModel()->AddURL(
106 sub_folder
, 0, base::ASCIIToUTF16("a"), GURL("http://a"));
107 RecentlyUsedFoldersComboModel
model(GetModel(), new_node
);
108 TestComboboxModelObserver observer
;
109 model
.AddObserver(&observer
);
111 const int initial_count
= model
.GetItemCount();
112 // Remove a folder, it should remove an item from the model too.
113 GetModel()->Remove(folder
, folder
->GetIndexOf(sub_folder
));
114 EXPECT_TRUE(observer
.GetAndClearChanged());
115 const int updated_count
= model
.GetItemCount();
116 EXPECT_LT(updated_count
, initial_count
);
118 // Remove all, which should remove a folder too.
119 GetModel()->RemoveAllUserBookmarks();
120 EXPECT_TRUE(observer
.GetAndClearChanged());
121 EXPECT_LT(model
.GetItemCount(), updated_count
);
123 model
.RemoveObserver(&observer
);