Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / toolbar / wrench_menu_model_unittest.cc
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.
4
5 #include "chrome/browser/ui/toolbar/wrench_menu_model.h"
6
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"
22
23 namespace {
24
25 // Error class has a menu item.
26 class MenuError : public GlobalError {
27  public:
28   explicit MenuError(int command_id)
29       : command_id_(command_id),
30         execute_count_(0) {
31   }
32
33   int execute_count() { return execute_count_; }
34
35   virtual bool HasMenuItem() OVERRIDE { return true; }
36   virtual int MenuItemCommandID() OVERRIDE { return command_id_; }
37   virtual base::string16 MenuItemLabel() OVERRIDE { return base::string16(); }
38   virtual void ExecuteMenuItem(Browser* browser) OVERRIDE { execute_count_++; }
39
40   virtual bool HasBubbleView() OVERRIDE { return false; }
41   virtual bool HasShownBubbleView() OVERRIDE { return false; }
42   virtual void ShowBubbleView(Browser* browser) OVERRIDE { ADD_FAILURE(); }
43   virtual GlobalErrorBubbleViewBase* GetBubbleView() OVERRIDE { return NULL; }
44
45  private:
46   int command_id_;
47   int execute_count_;
48
49   DISALLOW_COPY_AND_ASSIGN(MenuError);
50 };
51
52 } // namespace
53
54 class WrenchMenuModelTest : public BrowserWithTestWindowTest,
55                             public ui::AcceleratorProvider {
56  public:
57   // Don't handle accelerators.
58   virtual bool GetAcceleratorForCommandId(
59       int command_id,
60       ui::Accelerator* accelerator) OVERRIDE { return false; }
61
62  protected:
63   virtual void SetUp() OVERRIDE {
64     prefs_.reset(new TestingPrefServiceSimple());
65     chrome::RegisterLocalState(prefs_->registry());
66
67     TestingBrowserProcess::GetGlobal()->SetLocalState(prefs_.get());
68     testing_io_thread_state_.reset(new chrome::TestingIOThreadState());
69     BrowserWithTestWindowTest::SetUp();
70   }
71
72   virtual void TearDown() OVERRIDE {
73     BrowserWithTestWindowTest::TearDown();
74     testing_io_thread_state_.reset();
75     TestingBrowserProcess::GetGlobal()->SetLocalState(NULL);
76     DestroyBrowserAndProfile();
77   }
78
79  private:
80   scoped_ptr<TestingPrefServiceSimple> prefs_;
81   scoped_ptr<chrome::TestingIOThreadState> testing_io_thread_state_;
82 };
83
84 // Copies parts of MenuModelTest::Delegate and combines them with the
85 // WrenchMenuModel since WrenchMenuModel is now a SimpleMenuModel::Delegate and
86 // not derived from SimpleMenuModel.
87 class TestWrenchMenuModel : public WrenchMenuModel {
88  public:
89   TestWrenchMenuModel(ui::AcceleratorProvider* provider,
90                       Browser* browser)
91       : WrenchMenuModel(provider, browser),
92         execute_count_(0),
93         checked_count_(0),
94         enable_count_(0) {
95   }
96
97   // Testing overrides to ui::SimpleMenuModel::Delegate:
98   virtual bool IsCommandIdChecked(int command_id) const OVERRIDE {
99     bool val = WrenchMenuModel::IsCommandIdChecked(command_id);
100     if (val)
101       checked_count_++;
102     return val;
103   }
104
105   virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE {
106     ++enable_count_;
107     return true;
108   }
109
110   virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE {
111     ++execute_count_;
112   }
113
114   int execute_count_;
115   mutable int checked_count_;
116   mutable int enable_count_;
117 };
118
119 TEST_F(WrenchMenuModelTest, Basics) {
120   TestWrenchMenuModel model(this, browser());
121   int itemCount = model.GetItemCount();
122
123   // Verify it has items. The number varies by platform, so we don't check
124   // the exact number.
125   EXPECT_GT(itemCount, 10);
126
127   // Execute a couple of the items and make sure it gets back to our delegate.
128   // We can't use CountEnabledExecutable() here because the encoding menu's
129   // delegate is internal, it doesn't use the one we pass in.
130   // Note: The new menu has a spacing separator at the first slot.
131   model.ActivatedAt(1);
132   EXPECT_TRUE(model.IsEnabledAt(1));
133   // Make sure to use the index that is not separator in all configurations.
134   model.ActivatedAt(2);
135   EXPECT_TRUE(model.IsEnabledAt(2));
136   EXPECT_EQ(model.execute_count_, 2);
137   EXPECT_EQ(model.enable_count_, 2);
138
139   model.execute_count_ = 0;
140   model.enable_count_ = 0;
141
142   // Choose something from the bookmark submenu and make sure it makes it back
143   // to the delegate as well.
144   int bookmarksModelIndex = -1;
145   for (int i = 0; i < itemCount; ++i) {
146     if (model.GetTypeAt(i) == ui::MenuModel::TYPE_SUBMENU) {
147       bookmarksModelIndex = i;
148       break;
149     }
150   }
151   EXPECT_GT(bookmarksModelIndex, -1);
152   ui::MenuModel* bookmarksModel = model.GetSubmenuModelAt(bookmarksModelIndex);
153   EXPECT_TRUE(bookmarksModel);
154   // The bookmarks model may be empty until we tell it we're going to show it.
155   bookmarksModel->MenuWillShow();
156   EXPECT_GT(bookmarksModel->GetItemCount(), 1);
157   bookmarksModel->ActivatedAt(1);
158   EXPECT_TRUE(bookmarksModel->IsEnabledAt(1));
159   EXPECT_EQ(model.execute_count_, 1);
160   EXPECT_EQ(model.enable_count_, 1);
161 }
162
163 // Tests global error menu items in the wrench menu.
164 TEST_F(WrenchMenuModelTest, GlobalError) {
165   // Make sure services required for tests are initialized.
166   GlobalErrorService* service =
167       GlobalErrorServiceFactory::GetForProfile(browser()->profile());
168   ProfileOAuth2TokenServiceFactory::GetForProfile(browser()->profile());
169   const int command1 = 1234567;
170   // AddGlobalError takes ownership of error1.
171   MenuError* error1 = new MenuError(command1);
172   service->AddGlobalError(error1);
173   const int command2 = 1234568;
174   // AddGlobalError takes ownership of error2.
175   MenuError* error2 = new MenuError(command2);
176   service->AddGlobalError(error2);
177
178   WrenchMenuModel model(this, browser());
179   int index1 = model.GetIndexOfCommandId(command1);
180   EXPECT_GT(index1, -1);
181   int index2 = model.GetIndexOfCommandId(command2);
182   EXPECT_GT(index2, -1);
183
184   EXPECT_TRUE(model.IsEnabledAt(index1));
185   EXPECT_EQ(0, error1->execute_count());
186   model.ActivatedAt(index1);
187   EXPECT_EQ(1, error1->execute_count());
188
189   EXPECT_TRUE(model.IsEnabledAt(index2));
190   EXPECT_EQ(0, error2->execute_count());
191   model.ActivatedAt(index2);
192   EXPECT_EQ(1, error1->execute_count());
193 }
194
195 class EncodingMenuModelTest : public BrowserWithTestWindowTest,
196                               public MenuModelTest {
197 };
198
199 TEST_F(EncodingMenuModelTest, IsCommandIdCheckedWithNoTabs) {
200   EncodingMenuModel model(browser());
201   ASSERT_EQ(NULL, browser()->tab_strip_model()->GetActiveWebContents());
202   EXPECT_FALSE(model.IsCommandIdChecked(IDC_ENCODING_ISO88591));
203 }