Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / toolbar / wrench_menu_model_unittest.cc
blob218c3abea1d56d468d1e8a2e099028fcb5c2a086
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/toolbar/wrench_menu_model.h"
7 #include "chrome/app/chrome_command_ids.h"
8 #include "chrome/browser/defaults.h"
9 #include "chrome/browser/prefs/browser_prefs.h"
10 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/global_error/global_error.h"
13 #include "chrome/browser/ui/global_error/global_error_service.h"
14 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/browser/upgrade_detector.h"
17 #include "chrome/test/base/browser_with_test_window_test.h"
18 #include "chrome/test/base/menu_model_test.h"
19 #include "chrome/test/base/testing_browser_process.h"
20 #include "chrome/test/base/testing_io_thread_state.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "components/syncable_prefs/testing_pref_service_syncable.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 namespace {
27 // Error class has a menu item.
28 class MenuError : public GlobalError {
29 public:
30 explicit MenuError(int command_id)
31 : command_id_(command_id),
32 execute_count_(0) {
35 int execute_count() { return execute_count_; }
37 bool HasMenuItem() override { return true; }
38 int MenuItemCommandID() override { return command_id_; }
39 base::string16 MenuItemLabel() override { return base::string16(); }
40 void ExecuteMenuItem(Browser* browser) override { execute_count_++; }
42 bool HasBubbleView() override { return false; }
43 bool HasShownBubbleView() override { return false; }
44 void ShowBubbleView(Browser* browser) override { ADD_FAILURE(); }
45 GlobalErrorBubbleViewBase* GetBubbleView() override { return NULL; }
47 private:
48 int command_id_;
49 int execute_count_;
51 DISALLOW_COPY_AND_ASSIGN(MenuError);
54 } // namespace
56 class WrenchMenuModelTest : public BrowserWithTestWindowTest,
57 public ui::AcceleratorProvider {
58 public:
59 // Don't handle accelerators.
60 bool GetAcceleratorForCommandId(int command_id,
61 ui::Accelerator* accelerator) override {
62 return false;
65 protected:
66 void SetUp() override {
67 prefs_.reset(new TestingPrefServiceSimple());
68 chrome::RegisterLocalState(prefs_->registry());
70 TestingBrowserProcess::GetGlobal()->SetLocalState(prefs_.get());
71 testing_io_thread_state_.reset(new chrome::TestingIOThreadState());
72 BrowserWithTestWindowTest::SetUp();
75 void TearDown() override {
76 BrowserWithTestWindowTest::TearDown();
77 testing_io_thread_state_.reset();
78 TestingBrowserProcess::GetGlobal()->SetLocalState(NULL);
79 DestroyBrowserAndProfile();
82 private:
83 scoped_ptr<TestingPrefServiceSimple> prefs_;
84 scoped_ptr<chrome::TestingIOThreadState> testing_io_thread_state_;
87 // Copies parts of MenuModelTest::Delegate and combines them with the
88 // WrenchMenuModel since WrenchMenuModel is now a SimpleMenuModel::Delegate and
89 // not derived from SimpleMenuModel.
90 class TestWrenchMenuModel : public WrenchMenuModel {
91 public:
92 TestWrenchMenuModel(ui::AcceleratorProvider* provider,
93 Browser* browser)
94 : WrenchMenuModel(provider, browser),
95 execute_count_(0),
96 checked_count_(0),
97 enable_count_(0) {
100 // Testing overrides to ui::SimpleMenuModel::Delegate:
101 bool IsCommandIdChecked(int command_id) const override {
102 bool val = WrenchMenuModel::IsCommandIdChecked(command_id);
103 if (val)
104 checked_count_++;
105 return val;
108 bool IsCommandIdEnabled(int command_id) const override {
109 ++enable_count_;
110 return true;
113 void ExecuteCommand(int command_id, int event_flags) override {
114 ++execute_count_;
117 int execute_count_;
118 mutable int checked_count_;
119 mutable int enable_count_;
122 TEST_F(WrenchMenuModelTest, Basics) {
123 TestWrenchMenuModel model(this, browser());
124 int itemCount = model.GetItemCount();
126 // Verify it has items. The number varies by platform, so we don't check
127 // the exact number.
128 EXPECT_GT(itemCount, 10);
130 UpgradeDetector* detector = UpgradeDetector::GetInstance();
131 detector->NotifyUpgradeRecommended();
132 EXPECT_TRUE(detector->notify_upgrade());
133 EXPECT_EQ(browser_defaults::kShowUpgradeMenuItem,
134 model.IsCommandIdVisible(IDC_UPGRADE_DIALOG));
136 // Execute a couple of the items and make sure it gets back to our delegate.
137 // We can't use CountEnabledExecutable() here because the encoding menu's
138 // delegate is internal, it doesn't use the one we pass in.
139 // Note: The new menu has a spacing separator at the first slot.
140 model.ActivatedAt(1);
141 EXPECT_TRUE(model.IsEnabledAt(1));
142 // Make sure to use the index that is not separator in all configurations.
143 model.ActivatedAt(itemCount - 1);
144 EXPECT_TRUE(model.IsEnabledAt(itemCount - 1));
146 EXPECT_EQ(model.execute_count_, 2);
147 EXPECT_EQ(model.enable_count_, 2);
149 model.execute_count_ = 0;
150 model.enable_count_ = 0;
152 // Choose something from the bookmark submenu and make sure it makes it back
153 // to the delegate as well.
154 int bookmarksModelIndex = -1;
155 for (int i = 0; i < itemCount; ++i) {
156 if (model.GetTypeAt(i) == ui::MenuModel::TYPE_SUBMENU) {
157 // The bookmarks submenu comes after the Tabs and Downloads items.
158 bookmarksModelIndex = i + 2;
159 break;
162 EXPECT_GT(bookmarksModelIndex, -1);
163 ui::MenuModel* bookmarksModel = model.GetSubmenuModelAt(bookmarksModelIndex);
164 EXPECT_TRUE(bookmarksModel);
165 // The bookmarks model may be empty until we tell it we're going to show it.
166 bookmarksModel->MenuWillShow();
167 EXPECT_GT(bookmarksModel->GetItemCount(), 1);
169 // Bookmark manager item.
170 bookmarksModel->ActivatedAt(4);
171 EXPECT_TRUE(bookmarksModel->IsEnabledAt(4));
172 EXPECT_EQ(model.execute_count_, 1);
173 EXPECT_EQ(model.enable_count_, 1);
176 // Tests global error menu items in the wrench menu.
177 TEST_F(WrenchMenuModelTest, GlobalError) {
178 // Make sure services required for tests are initialized.
179 GlobalErrorService* service =
180 GlobalErrorServiceFactory::GetForProfile(browser()->profile());
181 ProfileOAuth2TokenServiceFactory::GetForProfile(browser()->profile());
182 const int command1 = 1234567;
183 // AddGlobalError takes ownership of error1.
184 MenuError* error1 = new MenuError(command1);
185 service->AddGlobalError(error1);
186 const int command2 = 1234568;
187 // AddGlobalError takes ownership of error2.
188 MenuError* error2 = new MenuError(command2);
189 service->AddGlobalError(error2);
191 WrenchMenuModel model(this, browser());
192 int index1 = model.GetIndexOfCommandId(command1);
193 EXPECT_GT(index1, -1);
194 int index2 = model.GetIndexOfCommandId(command2);
195 EXPECT_GT(index2, -1);
197 EXPECT_TRUE(model.IsEnabledAt(index1));
198 EXPECT_EQ(0, error1->execute_count());
199 model.ActivatedAt(index1);
200 EXPECT_EQ(1, error1->execute_count());
202 EXPECT_TRUE(model.IsEnabledAt(index2));
203 EXPECT_EQ(0, error2->execute_count());
204 model.ActivatedAt(index2);
205 EXPECT_EQ(1, error1->execute_count());
208 class EncodingMenuModelTest : public BrowserWithTestWindowTest,
209 public MenuModelTest {
212 TEST_F(EncodingMenuModelTest, IsCommandIdCheckedWithNoTabs) {
213 EncodingMenuModel model(browser());
214 ASSERT_EQ(NULL, browser()->tab_strip_model()->GetActiveWebContents());
215 EXPECT_FALSE(model.IsCommandIdChecked(IDC_ENCODING_WINDOWS1252));