Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(233)

Side by Side Diff: views/controls/menu/menu_model_adapter_unittest.cc

Issue 7067032: Add MenuModelAdapter to wrap ui::MenuModel with views::MenuDelegate interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Implemented reviewer recommendations. Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « views/controls/menu/menu_model_adapter.cc ('k') | views/test/test_views_delegate.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 "ui/base/l10n/l10n_util.h"
6 #include "ui/base/models/menu_model.h"
7 #include "ui/base/models/menu_model_delegate.h"
8 #include "views/controls/menu/menu_item_view.h"
9 #include "views/controls/menu/menu_model_adapter.h"
10 #include "views/controls/menu/submenu_view.h"
11 #include "views/test/views_test_base.h"
12
13 namespace {
14
15 // Base command id for test menu and its submenu.
16 const int kRootIdBase = 100;
17 const int kSubmenuIdBase = 200;
18
19 // Offset to return for GetFirstItemIndex(). This is an arbitrary
20 // number to ensure that we aren't assuming it is 0.
21 const int kFirstItemIndex = 25;
22
23 class MenuModelBase : public ui::MenuModel {
24 public:
25 MenuModelBase(int command_id_base) : command_id_base_(command_id_base),
26 last_activation_(-1) {
27 }
28
29 // ui::MenuModel implementation:
30
31 virtual bool HasIcons() const OVERRIDE {
32 return false;
33 }
34
35 virtual int GetFirstItemIndex(gfx::NativeMenu native_menu) const OVERRIDE {
36 return kFirstItemIndex;
37 }
38
39 virtual int GetItemCount() const OVERRIDE {
40 return static_cast<int>(items_.size());
41 }
42
43 virtual ItemType GetTypeAt(int index) const OVERRIDE {
44 return items_[index - GetFirstItemIndex(NULL)].type;
45 }
46
47 virtual int GetCommandIdAt(int index) const OVERRIDE {
48 return index - GetFirstItemIndex(NULL) + command_id_base_;
49 }
50
51 string16 GetLabelAt(int index) const OVERRIDE {
52 return items_[index - GetFirstItemIndex(NULL)].label;
53 }
54
55 virtual bool IsItemDynamicAt(int index) const OVERRIDE {
56 return false;
57 }
58
59 virtual const gfx::Font* GetLabelFontAt(int index) const OVERRIDE {
60 return NULL;
61 }
62
63 virtual bool GetAcceleratorAt(int index,
64 ui::Accelerator* accelerator) const OVERRIDE {
65 return false;
66 }
67
68 virtual bool IsItemCheckedAt(int index) const OVERRIDE {
69 return false;
70 }
71
72 virtual int GetGroupIdAt(int index) const OVERRIDE {
73 return 0;
74 }
75
76 virtual bool GetIconAt(int index, SkBitmap* icon) OVERRIDE {
77 return false;
78 }
79
80 virtual ui::ButtonMenuItemModel* GetButtonMenuItemAt(
81 int index) const OVERRIDE {
82 return NULL;
83 }
84
85 virtual bool IsEnabledAt(int index) const OVERRIDE {
86 return true;
87 }
88
89 virtual bool IsVisibleAt(int index) const OVERRIDE {
90 return true;
91 }
92
93 virtual MenuModel* GetSubmenuModelAt(int index) const OVERRIDE {
94 return items_[index - GetFirstItemIndex(NULL)].submenu;
95 }
96
97 virtual void HighlightChangedTo(int index) OVERRIDE {
98 }
99
100 virtual void ActivatedAt(int index) OVERRIDE {
101 set_last_activation(index);
102 }
103
104 virtual void ActivatedAtWithDisposition(int index, int disposition) OVERRIDE {
105 ActivatedAt(index);
106 }
107
108 virtual void MenuWillShow() OVERRIDE {
109 }
110
111 virtual void MenuClosed() OVERRIDE {
112 }
113
114 virtual void SetMenuModelDelegate(
115 ui::MenuModelDelegate* delegate) OVERRIDE {
116 }
117
118 // Item definition.
119 struct Item {
120 Item(ItemType item_type,
121 const std::string& item_label,
122 ui::MenuModel* item_submenu)
123 : type(item_type),
124 label(ASCIIToUTF16(item_label)),
125 submenu(item_submenu) {
126 }
127
128 ItemType type;
129 string16 label;
130 ui::MenuModel* submenu;
131 };
132
133 const Item& GetItemDefinition(int index) {
134 return items_[index];
135 }
136
137 // Access index argument to ActivatedAt() or ActivatedAtWithDisposition().
138 int last_activation() const { return last_activation_; }
139 void set_last_activation(int last_activation) {
140 last_activation_ = last_activation;
141 }
142
143 protected:
144 std::vector<Item> items_;
145
146 private:
147 int command_id_base_;
148 int last_activation_;
149
150 DISALLOW_COPY_AND_ASSIGN(MenuModelBase);
151 };
152
153 class SubmenuModel : public MenuModelBase {
154 public:
155 SubmenuModel() : MenuModelBase(kSubmenuIdBase) {
156 items_.push_back(Item(TYPE_COMMAND, "submenu item 0", NULL));
157 items_.push_back(Item(TYPE_COMMAND, "submenu item 1", NULL));
158 }
159
160 private:
161 DISALLOW_COPY_AND_ASSIGN(SubmenuModel);
162 };
163
164 class RootModel : public MenuModelBase {
165 public:
166 RootModel() : MenuModelBase(kRootIdBase) {
167 submenu_model_.reset(new SubmenuModel);
168
169 items_.push_back(Item(TYPE_COMMAND, "command 0", NULL));
170 items_.push_back(Item(TYPE_CHECK, "check 1", NULL));
171 items_.push_back(Item(TYPE_SEPARATOR, "", NULL));
172 items_.push_back(Item(TYPE_SUBMENU, "submenu 3", submenu_model_.get()));
173 items_.push_back(Item(TYPE_RADIO, "radio 4", NULL));
174 }
175
176 private:
177 scoped_ptr<MenuModel> submenu_model_;
178
179 DISALLOW_COPY_AND_ASSIGN(RootModel);
180 };
181
182 } // namespace
183
184 namespace views {
185
186 typedef ViewsTestBase MenuModelAdapterTest;
187
188 TEST_F(MenuModelAdapterTest, BasicTest) {
189 // Build model and adapter.
190 RootModel model;
191 views::MenuModelAdapter delegate(&model);
192
193 // Create menu. Build menu twice to check that rebuilding works properly.
194 scoped_ptr<views::MenuItemView> menu(new views::MenuItemView(&delegate));
195 delegate.BuildMenu(menu.get());
196 delegate.BuildMenu(menu.get());
197 EXPECT_TRUE(menu->HasSubmenu());
198
199 // Check top level menu items.
200 views::SubmenuView* item_container = menu->GetSubmenu();
201 EXPECT_EQ(5, item_container->child_count());
202
203 for (int i = 0; i < item_container->child_count(); ++i) {
204 const MenuModelBase::Item& model_item = model.GetItemDefinition(i);
205
206 const int id = i + kRootIdBase;
207 MenuItemView* item = menu->GetMenuItemByID(id);
208 if (!item) {
209 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, model_item.type);
210 continue;
211 }
212
213 // Check placement.
214 EXPECT_EQ(i, menu->GetSubmenu()->GetIndexOf(item));
215
216 // Check type.
217 switch (model_item.type) {
218 case ui::MenuModel::TYPE_COMMAND:
219 EXPECT_EQ(views::MenuItemView::NORMAL, item->GetType());
220 break;
221 case ui::MenuModel::TYPE_CHECK:
222 EXPECT_EQ(views::MenuItemView::CHECKBOX, item->GetType());
223 break;
224 case ui::MenuModel::TYPE_RADIO:
225 EXPECT_EQ(views::MenuItemView::RADIO, item->GetType());
226 break;
227 case ui::MenuModel::TYPE_SEPARATOR:
228 case ui::MenuModel::TYPE_BUTTON_ITEM:
229 break;
230 case ui::MenuModel::TYPE_SUBMENU:
231 EXPECT_EQ(views::MenuItemView::SUBMENU, item->GetType());
232 break;
233 }
234
235 // Check activation.
236 static_cast<views::MenuDelegate*>(&delegate)->ExecuteCommand(id);
237 EXPECT_EQ(i + kFirstItemIndex, model.last_activation());
238 model.set_last_activation(-1);
239 }
240
241 // Check submenu items.
242 views::MenuItemView* submenu = menu->GetMenuItemByID(103);
243 views::SubmenuView* subitem_container = submenu->GetSubmenu();
244 EXPECT_EQ(2, subitem_container->child_count());
245
246 for (int i = 0; i < subitem_container->child_count(); ++i) {
247 MenuModelBase* submodel = static_cast<MenuModelBase*>(
248 model.GetSubmenuModelAt(3 + kFirstItemIndex));
249 EXPECT_TRUE(submodel);
250
251 const MenuModelBase::Item& model_item = submodel->GetItemDefinition(i);
252
253 const int id = i + kSubmenuIdBase;
254 MenuItemView* item = menu->GetMenuItemByID(id);
255 if (!item) {
256 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, model_item.type);
257 continue;
258 }
259
260 // Check placement.
261 EXPECT_EQ(i, submenu->GetSubmenu()->GetIndexOf(item));
262
263 // Check type.
264 switch (model_item.type) {
265 case ui::MenuModel::TYPE_COMMAND:
266 EXPECT_EQ(views::MenuItemView::NORMAL, item->GetType());
267 break;
268 case ui::MenuModel::TYPE_CHECK:
269 EXPECT_EQ(views::MenuItemView::CHECKBOX, item->GetType());
270 break;
271 case ui::MenuModel::TYPE_RADIO:
272 EXPECT_EQ(views::MenuItemView::RADIO, item->GetType());
273 break;
274 case ui::MenuModel::TYPE_SEPARATOR:
275 case ui::MenuModel::TYPE_BUTTON_ITEM:
276 break;
277 case ui::MenuModel::TYPE_SUBMENU:
278 EXPECT_EQ(views::MenuItemView::SUBMENU, item->GetType());
279 break;
280 }
281
282 // Check activation.
283 static_cast<views::MenuDelegate*>(&delegate)->ExecuteCommand(id);
284 EXPECT_EQ(i + kFirstItemIndex, submodel->last_activation());
285 submodel->set_last_activation(-1);
286 }
287 }
288
289 } // namespace views
OLDNEW
« no previous file with comments | « views/controls/menu/menu_model_adapter.cc ('k') | views/test/test_views_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698