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/prefs/browser_prefs.h"
9 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/global_error/global_error.h"
12 #include "chrome/browser/ui/global_error/global_error_service.h"
13 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/test/base/browser_with_test_window_test.h"
16 #include "chrome/test/base/menu_model_test.h"
17 #include "chrome/test/base/testing_browser_process.h"
18 #include "chrome/test/base/testing_io_thread_state.h"
19 #include "chrome/test/base/testing_pref_service_syncable.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "testing/gtest/include/gtest/gtest.h"
25 // Error class has a menu item.
26 class MenuError
: public GlobalError
{
28 explicit MenuError(int command_id
)
29 : command_id_(command_id
),
33 int execute_count() { return execute_count_
; }
35 bool HasMenuItem() override
{ return true; }
36 int MenuItemCommandID() override
{ return command_id_
; }
37 base::string16
MenuItemLabel() override
{ return base::string16(); }
38 void ExecuteMenuItem(Browser
* browser
) override
{ execute_count_
++; }
40 bool HasBubbleView() override
{ return false; }
41 bool HasShownBubbleView() override
{ return false; }
42 void ShowBubbleView(Browser
* browser
) override
{ ADD_FAILURE(); }
43 GlobalErrorBubbleViewBase
* GetBubbleView() override
{ return NULL
; }
49 DISALLOW_COPY_AND_ASSIGN(MenuError
);
54 class WrenchMenuModelTest
: public BrowserWithTestWindowTest
,
55 public ui::AcceleratorProvider
{
57 // Don't handle accelerators.
58 bool GetAcceleratorForCommandId(int command_id
,
59 ui::Accelerator
* accelerator
) override
{
64 void SetUp() override
{
65 prefs_
.reset(new TestingPrefServiceSimple());
66 chrome::RegisterLocalState(prefs_
->registry());
68 TestingBrowserProcess::GetGlobal()->SetLocalState(prefs_
.get());
69 testing_io_thread_state_
.reset(new chrome::TestingIOThreadState());
70 BrowserWithTestWindowTest::SetUp();
73 void TearDown() override
{
74 BrowserWithTestWindowTest::TearDown();
75 testing_io_thread_state_
.reset();
76 TestingBrowserProcess::GetGlobal()->SetLocalState(NULL
);
77 DestroyBrowserAndProfile();
81 scoped_ptr
<TestingPrefServiceSimple
> prefs_
;
82 scoped_ptr
<chrome::TestingIOThreadState
> testing_io_thread_state_
;
85 // Copies parts of MenuModelTest::Delegate and combines them with the
86 // WrenchMenuModel since WrenchMenuModel is now a SimpleMenuModel::Delegate and
87 // not derived from SimpleMenuModel.
88 class TestWrenchMenuModel
: public WrenchMenuModel
{
90 TestWrenchMenuModel(ui::AcceleratorProvider
* provider
,
92 : WrenchMenuModel(provider
, browser
),
98 // Testing overrides to ui::SimpleMenuModel::Delegate:
99 bool IsCommandIdChecked(int command_id
) const override
{
100 bool val
= WrenchMenuModel::IsCommandIdChecked(command_id
);
106 bool IsCommandIdEnabled(int command_id
) const override
{
111 void ExecuteCommand(int command_id
, int event_flags
) override
{
116 mutable int checked_count_
;
117 mutable int enable_count_
;
120 TEST_F(WrenchMenuModelTest
, Basics
) {
121 TestWrenchMenuModel
model(this, browser());
122 int itemCount
= model
.GetItemCount();
124 // Verify it has items. The number varies by platform, so we don't check
126 EXPECT_GT(itemCount
, 10);
128 // Execute a couple of the items and make sure it gets back to our delegate.
129 // We can't use CountEnabledExecutable() here because the encoding menu's
130 // delegate is internal, it doesn't use the one we pass in.
131 // Note: The new menu has a spacing separator at the first slot.
132 model
.ActivatedAt(1);
133 EXPECT_TRUE(model
.IsEnabledAt(1));
134 // Make sure to use the index that is not separator in all configurations.
135 model
.ActivatedAt(2);
136 EXPECT_TRUE(model
.IsEnabledAt(2));
137 EXPECT_EQ(model
.execute_count_
, 2);
138 EXPECT_EQ(model
.enable_count_
, 2);
140 model
.execute_count_
= 0;
141 model
.enable_count_
= 0;
143 // Choose something from the bookmark submenu and make sure it makes it back
144 // to the delegate as well.
145 int bookmarksModelIndex
= -1;
146 for (int i
= 0; i
< itemCount
; ++i
) {
147 if (model
.GetTypeAt(i
) == ui::MenuModel::TYPE_SUBMENU
) {
148 bookmarksModelIndex
= i
;
152 EXPECT_GT(bookmarksModelIndex
, -1);
153 ui::MenuModel
* bookmarksModel
= model
.GetSubmenuModelAt(bookmarksModelIndex
);
154 EXPECT_TRUE(bookmarksModel
);
155 // The bookmarks model may be empty until we tell it we're going to show it.
156 bookmarksModel
->MenuWillShow();
157 EXPECT_GT(bookmarksModel
->GetItemCount(), 1);
158 bookmarksModel
->ActivatedAt(1);
159 EXPECT_TRUE(bookmarksModel
->IsEnabledAt(1));
160 EXPECT_EQ(model
.execute_count_
, 1);
161 EXPECT_EQ(model
.enable_count_
, 1);
164 // Tests global error menu items in the wrench menu.
165 TEST_F(WrenchMenuModelTest
, GlobalError
) {
166 // Make sure services required for tests are initialized.
167 GlobalErrorService
* service
=
168 GlobalErrorServiceFactory::GetForProfile(browser()->profile());
169 ProfileOAuth2TokenServiceFactory::GetForProfile(browser()->profile());
170 const int command1
= 1234567;
171 // AddGlobalError takes ownership of error1.
172 MenuError
* error1
= new MenuError(command1
);
173 service
->AddGlobalError(error1
);
174 const int command2
= 1234568;
175 // AddGlobalError takes ownership of error2.
176 MenuError
* error2
= new MenuError(command2
);
177 service
->AddGlobalError(error2
);
179 WrenchMenuModel
model(this, browser());
180 int index1
= model
.GetIndexOfCommandId(command1
);
181 EXPECT_GT(index1
, -1);
182 int index2
= model
.GetIndexOfCommandId(command2
);
183 EXPECT_GT(index2
, -1);
185 EXPECT_TRUE(model
.IsEnabledAt(index1
));
186 EXPECT_EQ(0, error1
->execute_count());
187 model
.ActivatedAt(index1
);
188 EXPECT_EQ(1, error1
->execute_count());
190 EXPECT_TRUE(model
.IsEnabledAt(index2
));
191 EXPECT_EQ(0, error2
->execute_count());
192 model
.ActivatedAt(index2
);
193 EXPECT_EQ(1, error1
->execute_count());
196 class EncodingMenuModelTest
: public BrowserWithTestWindowTest
,
197 public MenuModelTest
{
200 TEST_F(EncodingMenuModelTest
, IsCommandIdCheckedWithNoTabs
) {
201 EncodingMenuModel
model(browser());
202 ASSERT_EQ(NULL
, browser()->tab_strip_model()->GetActiveWebContents());
203 EXPECT_FALSE(model
.IsCommandIdChecked(IDC_ENCODING_WINDOWS1252
));