Extract code handling PrinterProviderAPI from PrintPreviewHandler
[chromium-blink-merge.git] / chrome / browser / extensions / menu_manager_unittest.cc
blobf0cf0494291833280a200f30bcd50bbff60098fd
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 <vector>
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/json/json_reader.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/extensions/extension_system_factory.h"
16 #include "chrome/browser/extensions/menu_manager.h"
17 #include "chrome/browser/extensions/test_extension_prefs.h"
18 #include "chrome/browser/extensions/test_extension_system.h"
19 #include "chrome/browser/prefs/pref_service_syncable.h"
20 #include "chrome/common/chrome_paths.h"
21 #include "chrome/common/extensions/api/context_menus.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "content/public/browser/notification_service.h"
24 #include "content/public/common/context_menu_params.h"
25 #include "content/public/test/test_browser_thread.h"
26 #include "extensions/browser/event_router.h"
27 #include "extensions/browser/extension_registry.h"
28 #include "extensions/browser/extension_system.h"
29 #include "extensions/common/extension.h"
30 #include "testing/gmock/include/gmock/gmock.h"
31 #include "testing/gtest/include/gtest/gtest.h"
33 using content::BrowserThread;
34 using testing::_;
35 using testing::AtLeast;
36 using testing::DeleteArg;
37 using testing::InSequence;
38 using testing::Return;
39 using testing::SaveArg;
41 namespace extensions {
43 namespace context_menus = api::context_menus;
45 // Base class for tests.
46 class MenuManagerTest : public testing::Test {
47 public:
48 MenuManagerTest()
49 : ui_thread_(BrowserThread::UI, &message_loop_),
50 file_thread_(BrowserThread::FILE, &message_loop_),
51 profile_(new TestingProfile()),
52 manager_(profile_.get(),
53 ExtensionSystem::Get(profile_.get())->state_store()),
54 prefs_(message_loop_.message_loop_proxy().get()),
55 next_id_(1) {}
57 void TearDown() override {
58 prefs_.pref_service()->CommitPendingWrite();
59 message_loop_.RunUntilIdle();
62 // Returns a test item.
63 MenuItem* CreateTestItem(Extension* extension, bool incognito = false) {
64 MenuItem::Type type = MenuItem::NORMAL;
65 MenuItem::ContextList contexts(MenuItem::ALL);
66 const MenuItem::ExtensionKey key(extension->id());
67 MenuItem::Id id(incognito, key);
68 id.uid = next_id_++;
69 return new MenuItem(id, "test", false, true, type, contexts);
72 // Returns a test item with the given string ID.
73 MenuItem* CreateTestItemWithID(Extension* extension,
74 const std::string& string_id) {
75 MenuItem::Type type = MenuItem::NORMAL;
76 MenuItem::ContextList contexts(MenuItem::ALL);
77 const MenuItem::ExtensionKey key(extension->id());
78 MenuItem::Id id(false, key);
79 id.string_uid = string_id;
80 return new MenuItem(id, "test", false, true, type, contexts);
83 // Creates and returns a test Extension. The caller does *not* own the return
84 // value.
85 Extension* AddExtension(const std::string& name) {
86 scoped_refptr<Extension> extension = prefs_.AddExtension(name);
87 extensions_.push_back(extension);
88 return extension.get();
91 protected:
92 base::MessageLoopForUI message_loop_;
93 content::TestBrowserThread ui_thread_;
94 content::TestBrowserThread file_thread_;
95 scoped_ptr<TestingProfile> profile_;
97 MenuManager manager_;
98 ExtensionList extensions_;
99 TestExtensionPrefs prefs_;
100 int next_id_;
102 private:
103 DISALLOW_COPY_AND_ASSIGN(MenuManagerTest);
106 // Tests adding, getting, and removing items.
107 TEST_F(MenuManagerTest, AddGetRemoveItems) {
108 Extension* extension = AddExtension("test");
110 // Add a new item, make sure you can get it back.
111 MenuItem* item1 = CreateTestItem(extension);
112 ASSERT_TRUE(item1 != NULL);
113 ASSERT_TRUE(manager_.AddContextItem(extension, item1));
114 ASSERT_EQ(item1, manager_.GetItemById(item1->id()));
115 const MenuItem::List* items = manager_.MenuItems(item1->id().extension_key);
116 ASSERT_EQ(1u, items->size());
117 ASSERT_EQ(item1, items->at(0));
119 // Add a second item, make sure it comes back too.
120 MenuItem* item2 = CreateTestItemWithID(extension, "id2");
121 ASSERT_TRUE(manager_.AddContextItem(extension, item2));
122 ASSERT_EQ(item2, manager_.GetItemById(item2->id()));
123 items = manager_.MenuItems(item2->id().extension_key);
124 ASSERT_EQ(2u, items->size());
125 ASSERT_EQ(item1, items->at(0));
126 ASSERT_EQ(item2, items->at(1));
128 // Try adding item 3, then removing it.
129 MenuItem* item3 = CreateTestItem(extension);
130 MenuItem::Id id3 = item3->id();
131 const MenuItem::ExtensionKey extension_key3(item3->id().extension_key);
132 ASSERT_TRUE(manager_.AddContextItem(extension, item3));
133 ASSERT_EQ(item3, manager_.GetItemById(id3));
134 ASSERT_EQ(3u, manager_.MenuItems(extension_key3)->size());
135 ASSERT_TRUE(manager_.RemoveContextMenuItem(id3));
136 ASSERT_EQ(NULL, manager_.GetItemById(id3));
137 ASSERT_EQ(2u, manager_.MenuItems(extension_key3)->size());
139 // Make sure removing a non-existent item returns false.
140 const MenuItem::ExtensionKey key(extension->id());
141 MenuItem::Id id(false, key);
142 id.uid = id3.uid + 50;
143 ASSERT_FALSE(manager_.RemoveContextMenuItem(id));
145 // Make sure adding an item with the same string ID returns false.
146 scoped_ptr<MenuItem> item2too(CreateTestItemWithID(extension, "id2"));
147 ASSERT_FALSE(manager_.AddContextItem(extension, item2too.get()));
149 // But the same string ID should not collide with another extension.
150 Extension* extension2 = AddExtension("test2");
151 MenuItem* item2other = CreateTestItemWithID(extension2, "id2");
152 ASSERT_TRUE(manager_.AddContextItem(extension2, item2other));
155 // Test adding/removing child items.
156 TEST_F(MenuManagerTest, ChildFunctions) {
157 Extension* extension1 = AddExtension("1111");
158 Extension* extension2 = AddExtension("2222");
159 Extension* extension3 = AddExtension("3333");
161 MenuItem* item1 = CreateTestItem(extension1);
162 MenuItem* item2 = CreateTestItem(extension2);
163 MenuItem* item2_child = CreateTestItemWithID(extension2, "2child");
164 MenuItem* item2_grandchild = CreateTestItem(extension2);
166 // This third item we expect to fail inserting, so we use a scoped_ptr to make
167 // sure it gets deleted.
168 scoped_ptr<MenuItem> item3(CreateTestItem(extension3));
170 // Add in the first two items.
171 ASSERT_TRUE(manager_.AddContextItem(extension1, item1));
172 ASSERT_TRUE(manager_.AddContextItem(extension2, item2));
174 MenuItem::Id id1 = item1->id();
175 MenuItem::Id id2 = item2->id();
177 // Try adding item3 as a child of item2 - this should fail because item3 has
178 // a different extension id.
179 ASSERT_FALSE(manager_.AddChildItem(id2, item3.get()));
181 // Add item2_child as a child of item2.
182 MenuItem::Id id2_child = item2_child->id();
183 ASSERT_TRUE(manager_.AddChildItem(id2, item2_child));
184 ASSERT_EQ(1, item2->child_count());
185 ASSERT_EQ(0, item1->child_count());
186 ASSERT_EQ(item2_child, manager_.GetItemById(id2_child));
188 ASSERT_EQ(1u, manager_.MenuItems(item1->id().extension_key)->size());
189 ASSERT_EQ(item1, manager_.MenuItems(item1->id().extension_key)->at(0));
191 // Add item2_grandchild as a child of item2_child, then remove it.
192 MenuItem::Id id2_grandchild = item2_grandchild->id();
193 ASSERT_TRUE(manager_.AddChildItem(id2_child, item2_grandchild));
194 ASSERT_EQ(1, item2->child_count());
195 ASSERT_EQ(1, item2_child->child_count());
196 ASSERT_TRUE(manager_.RemoveContextMenuItem(id2_grandchild));
198 // We should only get 1 thing back when asking for item2's extension id, since
199 // it has a child item.
200 ASSERT_EQ(1u, manager_.MenuItems(item2->id().extension_key)->size());
201 ASSERT_EQ(item2, manager_.MenuItems(item2->id().extension_key)->at(0));
203 // Remove child2_item.
204 ASSERT_TRUE(manager_.RemoveContextMenuItem(id2_child));
205 ASSERT_EQ(1u, manager_.MenuItems(item2->id().extension_key)->size());
206 ASSERT_EQ(item2, manager_.MenuItems(item2->id().extension_key)->at(0));
207 ASSERT_EQ(0, item2->child_count());
210 TEST_F(MenuManagerTest, PopulateFromValue) {
211 Extension* extension = AddExtension("test");
213 bool incognito = true;
214 int type = MenuItem::CHECKBOX;
215 std::string title("TITLE");
216 bool checked = true;
217 bool enabled = true;
218 MenuItem::ContextList contexts;
219 contexts.Add(MenuItem::PAGE);
220 contexts.Add(MenuItem::SELECTION);
221 int contexts_value = 0;
222 ASSERT_TRUE(contexts.ToValue()->GetAsInteger(&contexts_value));
224 base::ListValue* document_url_patterns(new base::ListValue());
225 document_url_patterns->Append(
226 new base::StringValue("http://www.google.com/*"));
227 document_url_patterns->Append(
228 new base::StringValue("http://www.reddit.com/*"));
230 base::ListValue* target_url_patterns(new base::ListValue());
231 target_url_patterns->Append(
232 new base::StringValue("http://www.yahoo.com/*"));
233 target_url_patterns->Append(
234 new base::StringValue("http://www.facebook.com/*"));
236 base::DictionaryValue value;
237 value.SetBoolean("incognito", incognito);
238 value.SetString("string_uid", std::string());
239 value.SetInteger("type", type);
240 value.SetString("title", title);
241 value.SetBoolean("checked", checked);
242 value.SetBoolean("enabled", enabled);
243 value.SetInteger("contexts", contexts_value);
244 value.Set("document_url_patterns", document_url_patterns);
245 value.Set("target_url_patterns", target_url_patterns);
247 std::string error;
248 scoped_ptr<MenuItem> item(MenuItem::Populate(extension->id(), value, &error));
249 ASSERT_TRUE(item.get());
251 EXPECT_EQ(extension->id(), item->extension_id());
252 EXPECT_EQ(incognito, item->incognito());
253 EXPECT_EQ(title, item->title());
254 EXPECT_EQ(checked, item->checked());
255 EXPECT_EQ(item->checked(), item->checked());
256 EXPECT_EQ(enabled, item->enabled());
257 EXPECT_EQ(contexts, item->contexts());
259 URLPatternSet document_url_pattern_set;
260 document_url_pattern_set.Populate(*document_url_patterns,
261 URLPattern::SCHEME_ALL,
262 true,
263 &error);
264 EXPECT_EQ(document_url_pattern_set, item->document_url_patterns());
266 URLPatternSet target_url_pattern_set;
267 target_url_pattern_set.Populate(*target_url_patterns,
268 URLPattern::SCHEME_ALL,
269 true,
270 &error);
271 EXPECT_EQ(target_url_pattern_set, item->target_url_patterns());
274 // Tests that deleting a parent properly removes descendants.
275 TEST_F(MenuManagerTest, DeleteParent) {
276 Extension* extension = AddExtension("1111");
278 // Set up 5 items to add.
279 MenuItem* item1 = CreateTestItem(extension);
280 MenuItem* item2 = CreateTestItem(extension);
281 MenuItem* item3 = CreateTestItemWithID(extension, "id3");
282 MenuItem* item4 = CreateTestItemWithID(extension, "id4");
283 MenuItem* item5 = CreateTestItem(extension);
284 MenuItem* item6 = CreateTestItem(extension);
285 MenuItem::Id item1_id = item1->id();
286 MenuItem::Id item2_id = item2->id();
287 MenuItem::Id item3_id = item3->id();
288 MenuItem::Id item4_id = item4->id();
289 MenuItem::Id item5_id = item5->id();
290 MenuItem::Id item6_id = item6->id();
291 const MenuItem::ExtensionKey key(extension->id());
293 // Add the items in the hierarchy
294 // item1 -> item2 -> item3 -> item4 -> item5 -> item6.
295 ASSERT_TRUE(manager_.AddContextItem(extension, item1));
296 ASSERT_TRUE(manager_.AddChildItem(item1_id, item2));
297 ASSERT_TRUE(manager_.AddChildItem(item2_id, item3));
298 ASSERT_TRUE(manager_.AddChildItem(item3_id, item4));
299 ASSERT_TRUE(manager_.AddChildItem(item4_id, item5));
300 ASSERT_TRUE(manager_.AddChildItem(item5_id, item6));
301 ASSERT_EQ(item1, manager_.GetItemById(item1_id));
302 ASSERT_EQ(item2, manager_.GetItemById(item2_id));
303 ASSERT_EQ(item3, manager_.GetItemById(item3_id));
304 ASSERT_EQ(item4, manager_.GetItemById(item4_id));
305 ASSERT_EQ(item5, manager_.GetItemById(item5_id));
306 ASSERT_EQ(item6, manager_.GetItemById(item6_id));
307 ASSERT_EQ(1u, manager_.MenuItems(key)->size());
308 ASSERT_EQ(6u, manager_.items_by_id_.size());
310 // Remove item6 (a leaf node).
311 ASSERT_TRUE(manager_.RemoveContextMenuItem(item6_id));
312 ASSERT_EQ(item1, manager_.GetItemById(item1_id));
313 ASSERT_EQ(item2, manager_.GetItemById(item2_id));
314 ASSERT_EQ(item3, manager_.GetItemById(item3_id));
315 ASSERT_EQ(item4, manager_.GetItemById(item4_id));
316 ASSERT_EQ(item5, manager_.GetItemById(item5_id));
317 ASSERT_EQ(NULL, manager_.GetItemById(item6_id));
318 ASSERT_EQ(1u, manager_.MenuItems(key)->size());
319 ASSERT_EQ(5u, manager_.items_by_id_.size());
321 // Remove item4 and make sure item5 is gone as well.
322 ASSERT_TRUE(manager_.RemoveContextMenuItem(item4_id));
323 ASSERT_EQ(item1, manager_.GetItemById(item1_id));
324 ASSERT_EQ(item2, manager_.GetItemById(item2_id));
325 ASSERT_EQ(item3, manager_.GetItemById(item3_id));
326 ASSERT_EQ(NULL, manager_.GetItemById(item4_id));
327 ASSERT_EQ(NULL, manager_.GetItemById(item5_id));
328 ASSERT_EQ(1u, manager_.MenuItems(key)->size());
329 ASSERT_EQ(3u, manager_.items_by_id_.size());
331 // Now remove item1 and make sure item2 and item3 are gone as well.
332 ASSERT_TRUE(manager_.RemoveContextMenuItem(item1_id));
333 ASSERT_EQ(NULL, manager_.MenuItems(key));
334 ASSERT_EQ(0u, manager_.items_by_id_.size());
335 ASSERT_EQ(NULL, manager_.GetItemById(item1_id));
336 ASSERT_EQ(NULL, manager_.GetItemById(item2_id));
337 ASSERT_EQ(NULL, manager_.GetItemById(item3_id));
340 // Tests changing parents.
341 TEST_F(MenuManagerTest, ChangeParent) {
342 Extension* extension1 = AddExtension("1111");
344 // First create two items and add them both to the manager.
345 MenuItem* item1 = CreateTestItem(extension1);
346 MenuItem* item2 = CreateTestItem(extension1);
348 ASSERT_TRUE(manager_.AddContextItem(extension1, item1));
349 ASSERT_TRUE(manager_.AddContextItem(extension1, item2));
351 const MenuItem::List* items = manager_.MenuItems(item1->id().extension_key);
352 ASSERT_EQ(2u, items->size());
353 ASSERT_EQ(item1, items->at(0));
354 ASSERT_EQ(item2, items->at(1));
356 // Now create a third item, initially add it as a child of item1, then move
357 // it to be a child of item2.
358 MenuItem* item3 = CreateTestItem(extension1);
360 ASSERT_TRUE(manager_.AddChildItem(item1->id(), item3));
361 ASSERT_EQ(1, item1->child_count());
362 ASSERT_EQ(item3, item1->children()[0]);
364 ASSERT_TRUE(manager_.ChangeParent(item3->id(), &item2->id()));
365 ASSERT_EQ(0, item1->child_count());
366 ASSERT_EQ(1, item2->child_count());
367 ASSERT_EQ(item3, item2->children()[0]);
369 // Move item2 to be a child of item1.
370 ASSERT_TRUE(manager_.ChangeParent(item2->id(), &item1->id()));
371 ASSERT_EQ(1, item1->child_count());
372 ASSERT_EQ(item2, item1->children()[0]);
373 ASSERT_EQ(1, item2->child_count());
374 ASSERT_EQ(item3, item2->children()[0]);
376 // Since item2 was a top-level item but is no longer, we should only have 1
377 // top-level item.
378 items = manager_.MenuItems(item1->id().extension_key);
379 ASSERT_EQ(1u, items->size());
380 ASSERT_EQ(item1, items->at(0));
382 // Move item3 back to being a child of item1, so it's now a sibling of item2.
383 ASSERT_TRUE(manager_.ChangeParent(item3->id(), &item1->id()));
384 ASSERT_EQ(2, item1->child_count());
385 ASSERT_EQ(item2, item1->children()[0]);
386 ASSERT_EQ(item3, item1->children()[1]);
388 // Try switching item3 to be the parent of item1 - this should fail.
389 ASSERT_FALSE(manager_.ChangeParent(item1->id(), &item3->id()));
390 ASSERT_EQ(0, item3->child_count());
391 ASSERT_EQ(2, item1->child_count());
392 ASSERT_EQ(item2, item1->children()[0]);
393 ASSERT_EQ(item3, item1->children()[1]);
394 items = manager_.MenuItems(item1->id().extension_key);
395 ASSERT_EQ(1u, items->size());
396 ASSERT_EQ(item1, items->at(0));
398 // Move item2 to be a top-level item.
399 ASSERT_TRUE(manager_.ChangeParent(item2->id(), NULL));
400 items = manager_.MenuItems(item1->id().extension_key);
401 ASSERT_EQ(2u, items->size());
402 ASSERT_EQ(item1, items->at(0));
403 ASSERT_EQ(item2, items->at(1));
404 ASSERT_EQ(1, item1->child_count());
405 ASSERT_EQ(item3, item1->children()[0]);
407 // Make sure you can't move a node to be a child of another extension's item.
408 Extension* extension2 = AddExtension("2222");
409 MenuItem* item4 = CreateTestItem(extension2);
410 ASSERT_TRUE(manager_.AddContextItem(extension2, item4));
411 ASSERT_FALSE(manager_.ChangeParent(item4->id(), &item1->id()));
412 ASSERT_FALSE(manager_.ChangeParent(item1->id(), &item4->id()));
414 // Make sure you can't make an item be it's own parent.
415 ASSERT_FALSE(manager_.ChangeParent(item1->id(), &item1->id()));
418 // Tests that we properly remove an extension's menu item when that extension is
419 // unloaded.
420 TEST_F(MenuManagerTest, ExtensionUnloadRemovesMenuItems) {
421 content::NotificationService* notifier =
422 content::NotificationService::current();
423 ASSERT_TRUE(notifier != NULL);
425 // Create a test extension.
426 Extension* extension1 = AddExtension("1111");
428 // Create an MenuItem and put it into the manager.
429 MenuItem* item1 = CreateTestItem(extension1);
430 MenuItem::Id id1 = item1->id();
431 ASSERT_EQ(extension1->id(), item1->extension_id());
432 ASSERT_TRUE(manager_.AddContextItem(extension1, item1));
433 ASSERT_EQ(
434 1u, manager_.MenuItems(MenuItem::ExtensionKey(extension1->id()))->size());
436 // Create a menu item with a different extension id and add it to the manager.
437 Extension* extension2 = AddExtension("2222");
438 MenuItem* item2 = CreateTestItem(extension2);
439 ASSERT_NE(item1->extension_id(), item2->extension_id());
440 ASSERT_TRUE(manager_.AddContextItem(extension2, item2));
442 // Notify that the extension was unloaded, and make sure the right item is
443 // gone.
444 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get());
445 registry->TriggerOnUnloaded(extension1,
446 UnloadedExtensionInfo::REASON_DISABLE);
448 ASSERT_EQ(NULL, manager_.MenuItems(MenuItem::ExtensionKey(extension1->id())));
449 ASSERT_EQ(
450 1u, manager_.MenuItems(MenuItem::ExtensionKey(extension2->id()))->size());
451 ASSERT_TRUE(manager_.GetItemById(id1) == NULL);
452 ASSERT_TRUE(manager_.GetItemById(item2->id()) != NULL);
455 namespace {
457 // A mock message service for tests of MenuManager::ExecuteCommand.
458 class MockEventRouter : public EventRouter {
459 public:
460 explicit MockEventRouter(Profile* profile) : EventRouter(profile, NULL) {}
462 MOCK_METHOD6(DispatchEventToExtensionMock,
463 void(const std::string& extension_id,
464 const std::string& event_name,
465 base::ListValue* event_args,
466 content::BrowserContext* source_context,
467 const GURL& event_url,
468 EventRouter::UserGestureState state));
470 virtual void DispatchEventToExtension(const std::string& extension_id,
471 scoped_ptr<Event> event) {
472 DispatchEventToExtensionMock(extension_id,
473 event->event_name,
474 event->event_args.release(),
475 event->restrict_to_browser_context,
476 event->event_url,
477 event->user_gesture);
480 private:
481 DISALLOW_COPY_AND_ASSIGN(MockEventRouter);
484 // A mock ExtensionSystem to serve our MockEventRouter.
485 class MockExtensionSystem : public TestExtensionSystem {
486 public:
487 explicit MockExtensionSystem(Profile* profile)
488 : TestExtensionSystem(profile) {}
490 EventRouter* event_router() override {
491 if (!mock_event_router_)
492 mock_event_router_.reset(new MockEventRouter(profile_));
493 return mock_event_router_.get();
496 private:
497 scoped_ptr<MockEventRouter> mock_event_router_;
499 DISALLOW_COPY_AND_ASSIGN(MockExtensionSystem);
502 KeyedService* BuildMockExtensionSystem(content::BrowserContext* profile) {
503 return new MockExtensionSystem(static_cast<Profile*>(profile));
506 } // namespace
508 // Tests the RemoveAll functionality.
509 TEST_F(MenuManagerTest, RemoveAll) {
510 // Try removing all items for an extension id that doesn't have any items.
511 manager_.RemoveAllContextItems(MenuItem::ExtensionKey("CCCC"));
513 // Add 2 top-level and one child item for extension 1.
514 Extension* extension1 = AddExtension("1111");
515 MenuItem* item1 = CreateTestItem(extension1);
516 MenuItem* item2 = CreateTestItem(extension1);
517 MenuItem* item3 = CreateTestItem(extension1);
518 ASSERT_TRUE(manager_.AddContextItem(extension1, item1));
519 ASSERT_TRUE(manager_.AddContextItem(extension1, item2));
520 ASSERT_TRUE(manager_.AddChildItem(item1->id(), item3));
522 // Add one top-level item for extension 2.
523 Extension* extension2 = AddExtension("2222");
524 MenuItem* item4 = CreateTestItem(extension2);
525 ASSERT_TRUE(manager_.AddContextItem(extension2, item4));
527 const MenuItem::ExtensionKey key1(extension1->id());
528 const MenuItem::ExtensionKey key2(extension2->id());
529 EXPECT_EQ(2u, manager_.MenuItems(key1)->size());
530 EXPECT_EQ(1u, manager_.MenuItems(key2)->size());
532 // Remove extension2's item.
533 manager_.RemoveAllContextItems(key2);
534 EXPECT_EQ(2u, manager_.MenuItems(key1)->size());
535 EXPECT_EQ(NULL, manager_.MenuItems(key2));
537 // Remove extension1's items.
538 manager_.RemoveAllContextItems(key1);
539 EXPECT_EQ(NULL, manager_.MenuItems(key1));
542 // Tests that removing all items one-by-one doesn't leave an entry around.
543 TEST_F(MenuManagerTest, RemoveOneByOne) {
544 // Add 2 test items.
545 Extension* extension1 = AddExtension("1111");
546 MenuItem* item1 = CreateTestItem(extension1);
547 MenuItem* item2 = CreateTestItem(extension1);
548 MenuItem* item3 = CreateTestItemWithID(extension1, "id3");
549 ASSERT_TRUE(manager_.AddContextItem(extension1, item1));
550 ASSERT_TRUE(manager_.AddContextItem(extension1, item2));
551 ASSERT_TRUE(manager_.AddContextItem(extension1, item3));
553 ASSERT_FALSE(manager_.context_items_.empty());
555 manager_.RemoveContextMenuItem(item3->id());
556 manager_.RemoveContextMenuItem(item1->id());
557 manager_.RemoveContextMenuItem(item2->id());
559 ASSERT_TRUE(manager_.context_items_.empty());
562 TEST_F(MenuManagerTest, ExecuteCommand) {
563 TestingProfile profile;
565 MockExtensionSystem* mock_extension_system =
566 static_cast<MockExtensionSystem*>(ExtensionSystemFactory::GetInstance()->
567 SetTestingFactoryAndUse(&profile, &BuildMockExtensionSystem));
568 MockEventRouter* mock_event_router =
569 static_cast<MockEventRouter*>(mock_extension_system->event_router());
571 content::ContextMenuParams params;
572 params.media_type = blink::WebContextMenuData::MediaTypeImage;
573 params.src_url = GURL("http://foo.bar/image.png");
574 params.page_url = GURL("http://foo.bar");
575 params.selection_text = base::ASCIIToUTF16("Hello World");
576 params.is_editable = false;
578 Extension* extension = AddExtension("test");
579 MenuItem* parent = CreateTestItem(extension);
580 MenuItem* item = CreateTestItem(extension);
581 MenuItem::Id parent_id = parent->id();
582 MenuItem::Id id = item->id();
583 ASSERT_TRUE(manager_.AddContextItem(extension, parent));
584 ASSERT_TRUE(manager_.AddChildItem(parent->id(), item));
586 // Use the magic of googlemock to save a parameter to our mock's
587 // DispatchEventToExtension method into event_args.
588 base::ListValue* list = NULL;
590 InSequence s;
591 EXPECT_CALL(*mock_event_router,
592 DispatchEventToExtensionMock(item->extension_id(),
593 MenuManager::kOnContextMenus,
595 &profile,
596 GURL(),
597 EventRouter::USER_GESTURE_ENABLED))
598 .Times(1)
599 .WillOnce(SaveArg<2>(&list));
600 EXPECT_CALL(*mock_event_router,
601 DispatchEventToExtensionMock(
602 item->extension_id(),
603 context_menus::OnClicked::kEventName,
605 &profile,
606 GURL(),
607 EventRouter::USER_GESTURE_ENABLED))
608 .Times(1)
609 .WillOnce(DeleteArg<2>());
611 manager_.ExecuteCommand(&profile, NULL /* web_contents */, params, id);
613 ASSERT_EQ(2u, list->GetSize());
615 base::DictionaryValue* info;
616 ASSERT_TRUE(list->GetDictionary(0, &info));
618 int tmp_id = 0;
619 ASSERT_TRUE(info->GetInteger("menuItemId", &tmp_id));
620 ASSERT_EQ(id.uid, tmp_id);
621 ASSERT_TRUE(info->GetInteger("parentMenuItemId", &tmp_id));
622 ASSERT_EQ(parent_id.uid, tmp_id);
624 std::string tmp;
625 ASSERT_TRUE(info->GetString("mediaType", &tmp));
626 ASSERT_EQ("image", tmp);
627 ASSERT_TRUE(info->GetString("srcUrl", &tmp));
628 ASSERT_EQ(params.src_url.spec(), tmp);
629 ASSERT_TRUE(info->GetString("pageUrl", &tmp));
630 ASSERT_EQ(params.page_url.spec(), tmp);
632 base::string16 tmp16;
633 ASSERT_TRUE(info->GetString("selectionText", &tmp16));
634 ASSERT_EQ(params.selection_text, tmp16);
636 bool bool_tmp = true;
637 ASSERT_TRUE(info->GetBoolean("editable", &bool_tmp));
638 ASSERT_EQ(params.is_editable, bool_tmp);
640 delete list;
643 // Test that there is always only one radio item selected.
644 TEST_F(MenuManagerTest, SanitizeRadioButtons) {
645 Extension* extension = AddExtension("test");
647 // A single unchecked item should get checked
648 MenuItem* item1 = CreateTestItem(extension);
650 item1->set_type(MenuItem::RADIO);
651 item1->SetChecked(false);
652 ASSERT_FALSE(item1->checked());
653 manager_.AddContextItem(extension, item1);
654 ASSERT_TRUE(item1->checked());
656 // In a run of two unchecked items, the first should get selected.
657 item1->SetChecked(false);
658 MenuItem* item2 = CreateTestItem(extension);
659 item2->set_type(MenuItem::RADIO);
660 item2->SetChecked(false);
661 ASSERT_FALSE(item1->checked());
662 ASSERT_FALSE(item2->checked());
663 manager_.AddContextItem(extension, item2);
664 ASSERT_TRUE(item1->checked());
665 ASSERT_FALSE(item2->checked());
667 // If multiple items are checked, only the last item should get checked.
668 item1->SetChecked(true);
669 item2->SetChecked(true);
670 ASSERT_TRUE(item1->checked());
671 ASSERT_TRUE(item2->checked());
672 manager_.ItemUpdated(item1->id());
673 ASSERT_FALSE(item1->checked());
674 ASSERT_TRUE(item2->checked());
676 // If the checked item is removed, the new first item should get checked.
677 item1->SetChecked(false);
678 item2->SetChecked(true);
679 ASSERT_FALSE(item1->checked());
680 ASSERT_TRUE(item2->checked());
681 manager_.RemoveContextMenuItem(item2->id());
682 item2 = NULL;
683 ASSERT_TRUE(item1->checked());
685 // If a checked item is added to a run that already has a checked item,
686 // then the new item should get checked.
687 item1->SetChecked(true);
688 MenuItem* new_item = CreateTestItem(extension);
689 new_item->set_type(MenuItem::RADIO);
690 new_item->SetChecked(true);
691 ASSERT_TRUE(item1->checked());
692 ASSERT_TRUE(new_item->checked());
693 manager_.AddContextItem(extension, new_item);
694 ASSERT_FALSE(item1->checked());
695 ASSERT_TRUE(new_item->checked());
696 // Make sure that children are checked as well.
697 MenuItem* parent = CreateTestItem(extension);
698 manager_.AddContextItem(extension, parent);
699 MenuItem* child1 = CreateTestItem(extension);
700 child1->set_type(MenuItem::RADIO);
701 child1->SetChecked(false);
702 MenuItem* child2 = CreateTestItem(extension);
703 child2->set_type(MenuItem::RADIO);
704 child2->SetChecked(true);
705 ASSERT_FALSE(child1->checked());
706 ASSERT_TRUE(child2->checked());
708 manager_.AddChildItem(parent->id(), child1);
709 ASSERT_TRUE(child1->checked());
711 manager_.AddChildItem(parent->id(), child2);
712 ASSERT_FALSE(child1->checked());
713 ASSERT_TRUE(child2->checked());
715 // Removing the checked item from the children should cause the
716 // remaining child to be checked.
717 manager_.RemoveContextMenuItem(child2->id());
718 child2 = NULL;
719 ASSERT_TRUE(child1->checked());
721 // This should NOT cause |new_item| to be deseleted because
722 // |parent| will be seperating the two runs of radio items.
723 manager_.ChangeParent(child1->id(), NULL);
724 ASSERT_TRUE(new_item->checked());
725 ASSERT_TRUE(child1->checked());
727 // Removing |parent| should cause only |child1| to be selected.
728 manager_.RemoveContextMenuItem(parent->id());
729 parent = NULL;
730 ASSERT_FALSE(new_item->checked());
731 ASSERT_TRUE(child1->checked());
734 // Tests the RemoveAllIncognitoContextItems functionality.
735 TEST_F(MenuManagerTest, RemoveAllIncognito) {
736 Extension* extension1 = AddExtension("1111");
737 // Add 2 top-level and one child item for extension 1
738 // with incognito 'true'.
739 MenuItem* item1 = CreateTestItem(extension1, true);
740 MenuItem* item2 = CreateTestItem(extension1, true);
741 MenuItem* item3 = CreateTestItem(extension1, true);
742 ASSERT_TRUE(manager_.AddContextItem(extension1, item1));
743 ASSERT_TRUE(manager_.AddContextItem(extension1, item2));
744 ASSERT_TRUE(manager_.AddChildItem(item1->id(), item3));
746 // Add 2 top-level and one child item for extension 1
747 // with incognito 'false'.
748 MenuItem* item4 = CreateTestItem(extension1);
749 MenuItem* item5 = CreateTestItem(extension1);
750 MenuItem* item6 = CreateTestItem(extension1);
751 ASSERT_TRUE(manager_.AddContextItem(extension1, item4));
752 ASSERT_TRUE(manager_.AddContextItem(extension1, item5));
753 ASSERT_TRUE(manager_.AddChildItem(item4->id(), item6));
755 // Add one top-level item for extension 2.
756 Extension* extension2 = AddExtension("2222");
757 MenuItem* item7 = CreateTestItem(extension2);
758 ASSERT_TRUE(manager_.AddContextItem(extension2, item7));
760 const MenuItem::ExtensionKey key1(extension1->id());
761 const MenuItem::ExtensionKey key2(extension2->id());
762 EXPECT_EQ(4u, manager_.MenuItems(key1)->size());
763 EXPECT_EQ(1u, manager_.MenuItems(key2)->size());
765 // Remove all context menu items with incognito true.
766 manager_.RemoveAllIncognitoContextItems();
767 EXPECT_EQ(2u, manager_.MenuItems(key1)->size());
768 EXPECT_EQ(1u, manager_.MenuItems(key2)->size());
771 } // namespace extensions