Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / bookmarks / recently_used_folders_combo_model_unittest.cc
blob9e986572953e907d16582f8da9d4b2971ee39a5a
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 "components/bookmarks/browser/bookmark_model.h"
11 #include "components/bookmarks/test/bookmark_test_helpers.h"
12 #include "components/bookmarks/test/test_bookmark_client.h"
13 #include "content/public/test/test_browser_thread.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/base/models/combobox_model_observer.h"
17 using bookmarks::BookmarkModel;
18 using bookmarks::BookmarkNode;
19 using bookmarks::TestBookmarkClient;
20 using content::BrowserThread;
22 // Implementation of ComboboxModelObserver that records when
23 // OnComboboxModelChanged() is invoked.
24 class TestComboboxModelObserver : public ui::ComboboxModelObserver {
25 public:
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_;
32 changed_ = false;
33 return changed;
36 // ui::ComboboxModelObserver:
37 void OnComboboxModelChanged(ui::ComboboxModel* model) override {
38 changed_ = true;
41 private:
42 bool changed_;
44 DISALLOW_COPY_AND_ASSIGN(TestComboboxModelObserver);
47 class RecentlyUsedFoldersComboModelTest : public testing::Test {
48 public:
49 RecentlyUsedFoldersComboModelTest();
51 private:
52 base::MessageLoopForUI message_loop_;
53 content::TestBrowserThread ui_thread_;
54 content::TestBrowserThread file_thread_;
56 DISALLOW_COPY_AND_ASSIGN(RecentlyUsedFoldersComboModelTest);
59 RecentlyUsedFoldersComboModelTest::RecentlyUsedFoldersComboModelTest()
60 : ui_thread_(BrowserThread::UI, &message_loop_),
61 file_thread_(BrowserThread::FILE, &message_loop_) {
64 // Verifies there are no duplicate nodes in the model.
65 TEST_F(RecentlyUsedFoldersComboModelTest, NoDups) {
66 TestBookmarkClient client;
67 scoped_ptr<BookmarkModel> bookmark_model(client.CreateModel());
68 const BookmarkNode* new_node = bookmark_model->AddURL(
69 bookmark_model->bookmark_bar_node(), 0, base::ASCIIToUTF16("a"),
70 GURL("http://a"));
71 RecentlyUsedFoldersComboModel model(bookmark_model.get(), new_node);
72 std::set<base::string16> items;
73 for (int i = 0; i < model.GetItemCount(); ++i) {
74 if (!model.IsItemSeparatorAt(i))
75 EXPECT_EQ(0u, items.count(model.GetItemAt(i)));
79 // Verifies that observers are notified on changes.
80 TEST_F(RecentlyUsedFoldersComboModelTest, NotifyObserver) {
81 TestBookmarkClient client;
82 scoped_ptr<BookmarkModel> bookmark_model(client.CreateModel());
83 const BookmarkNode* folder = bookmark_model->AddFolder(
84 bookmark_model->bookmark_bar_node(), 0, base::ASCIIToUTF16("a"));
85 const BookmarkNode* sub_folder = bookmark_model->AddFolder(
86 folder, 0, base::ASCIIToUTF16("b"));
87 const BookmarkNode* new_node = bookmark_model->AddURL(
88 sub_folder, 0, base::ASCIIToUTF16("a"), GURL("http://a"));
89 RecentlyUsedFoldersComboModel model(bookmark_model.get(), new_node);
90 TestComboboxModelObserver observer;
91 model.AddObserver(&observer);
93 const int initial_count = model.GetItemCount();
94 // Remove a folder, it should remove an item from the model too.
95 bookmark_model->Remove(sub_folder);
96 EXPECT_TRUE(observer.GetAndClearChanged());
97 const int updated_count = model.GetItemCount();
98 EXPECT_LT(updated_count, initial_count);
100 // Remove all, which should remove a folder too.
101 bookmark_model->RemoveAllUserBookmarks();
102 EXPECT_TRUE(observer.GetAndClearChanged());
103 EXPECT_LT(model.GetItemCount(), updated_count);
105 model.RemoveObserver(&observer);