Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / extensions / menu_manager_unittest.cc
bloba9773c11bff54adecdd6eb2b720c66dedf30e655
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_ptr.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/extensions/extension_system_factory.h"
15 #include "chrome/browser/extensions/menu_manager.h"
16 #include "chrome/browser/extensions/test_extension_prefs.h"
17 #include "chrome/browser/extensions/test_extension_system.h"
18 #include "chrome/browser/prefs/pref_service_syncable.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "chrome/common/extensions/api/context_menus.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/common/context_menu_params.h"
24 #include "content/public/test/test_browser_thread.h"
25 #include "extensions/browser/event_router.h"
26 #include "extensions/browser/event_router_factory.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_.task_runner().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 // MockEventRouter factory function
485 scoped_ptr<KeyedService> MockEventRouterFactoryFunction(
486 content::BrowserContext* context) {
487 return make_scoped_ptr(new MockEventRouter(static_cast<Profile*>(context)));
490 } // namespace
492 // Tests the RemoveAll functionality.
493 TEST_F(MenuManagerTest, RemoveAll) {
494 // Try removing all items for an extension id that doesn't have any items.
495 manager_.RemoveAllContextItems(MenuItem::ExtensionKey("CCCC"));
497 // Add 2 top-level and one child item for extension 1.
498 Extension* extension1 = AddExtension("1111");
499 MenuItem* item1 = CreateTestItem(extension1);
500 MenuItem* item2 = CreateTestItem(extension1);
501 MenuItem* item3 = CreateTestItem(extension1);
502 ASSERT_TRUE(manager_.AddContextItem(extension1, item1));
503 ASSERT_TRUE(manager_.AddContextItem(extension1, item2));
504 ASSERT_TRUE(manager_.AddChildItem(item1->id(), item3));
506 // Add one top-level item for extension 2.
507 Extension* extension2 = AddExtension("2222");
508 MenuItem* item4 = CreateTestItem(extension2);
509 ASSERT_TRUE(manager_.AddContextItem(extension2, item4));
511 const MenuItem::ExtensionKey key1(extension1->id());
512 const MenuItem::ExtensionKey key2(extension2->id());
513 EXPECT_EQ(2u, manager_.MenuItems(key1)->size());
514 EXPECT_EQ(1u, manager_.MenuItems(key2)->size());
516 // Remove extension2's item.
517 manager_.RemoveAllContextItems(key2);
518 EXPECT_EQ(2u, manager_.MenuItems(key1)->size());
519 EXPECT_EQ(NULL, manager_.MenuItems(key2));
521 // Remove extension1's items.
522 manager_.RemoveAllContextItems(key1);
523 EXPECT_EQ(NULL, manager_.MenuItems(key1));
526 // Tests that removing all items one-by-one doesn't leave an entry around.
527 TEST_F(MenuManagerTest, RemoveOneByOne) {
528 // Add 2 test items.
529 Extension* extension1 = AddExtension("1111");
530 MenuItem* item1 = CreateTestItem(extension1);
531 MenuItem* item2 = CreateTestItem(extension1);
532 MenuItem* item3 = CreateTestItemWithID(extension1, "id3");
533 ASSERT_TRUE(manager_.AddContextItem(extension1, item1));
534 ASSERT_TRUE(manager_.AddContextItem(extension1, item2));
535 ASSERT_TRUE(manager_.AddContextItem(extension1, item3));
537 ASSERT_FALSE(manager_.context_items_.empty());
539 manager_.RemoveContextMenuItem(item3->id());
540 manager_.RemoveContextMenuItem(item1->id());
541 manager_.RemoveContextMenuItem(item2->id());
543 ASSERT_TRUE(manager_.context_items_.empty());
546 TEST_F(MenuManagerTest, ExecuteCommand) {
547 TestingProfile profile;
548 MockEventRouter* mock_event_router = static_cast<MockEventRouter*>(
549 EventRouterFactory::GetInstance()->SetTestingFactoryAndUse(
550 &profile, &MockEventRouterFactoryFunction));
552 content::ContextMenuParams params;
553 params.media_type = blink::WebContextMenuData::MediaTypeImage;
554 params.src_url = GURL("http://foo.bar/image.png");
555 params.page_url = GURL("http://foo.bar");
556 params.selection_text = base::ASCIIToUTF16("Hello World");
557 params.is_editable = false;
559 Extension* extension = AddExtension("test");
560 MenuItem* parent = CreateTestItem(extension);
561 MenuItem* item = CreateTestItem(extension);
562 MenuItem::Id parent_id = parent->id();
563 MenuItem::Id id = item->id();
564 ASSERT_TRUE(manager_.AddContextItem(extension, parent));
565 ASSERT_TRUE(manager_.AddChildItem(parent->id(), item));
567 // Use the magic of googlemock to save a parameter to our mock's
568 // DispatchEventToExtension method into event_args.
569 base::ListValue* list = NULL;
571 InSequence s;
572 EXPECT_CALL(*mock_event_router,
573 DispatchEventToExtensionMock(item->extension_id(),
574 MenuManager::kOnContextMenus,
576 &profile,
577 GURL(),
578 EventRouter::USER_GESTURE_ENABLED))
579 .Times(1)
580 .WillOnce(SaveArg<2>(&list));
581 EXPECT_CALL(*mock_event_router,
582 DispatchEventToExtensionMock(
583 item->extension_id(),
584 context_menus::OnClicked::kEventName,
586 &profile,
587 GURL(),
588 EventRouter::USER_GESTURE_ENABLED))
589 .Times(1)
590 .WillOnce(DeleteArg<2>());
592 manager_.ExecuteCommand(&profile, NULL /* web_contents */, params, id);
594 ASSERT_EQ(2u, list->GetSize());
596 base::DictionaryValue* info;
597 ASSERT_TRUE(list->GetDictionary(0, &info));
599 int tmp_id = 0;
600 ASSERT_TRUE(info->GetInteger("menuItemId", &tmp_id));
601 ASSERT_EQ(id.uid, tmp_id);
602 ASSERT_TRUE(info->GetInteger("parentMenuItemId", &tmp_id));
603 ASSERT_EQ(parent_id.uid, tmp_id);
605 std::string tmp;
606 ASSERT_TRUE(info->GetString("mediaType", &tmp));
607 ASSERT_EQ("image", tmp);
608 ASSERT_TRUE(info->GetString("srcUrl", &tmp));
609 ASSERT_EQ(params.src_url.spec(), tmp);
610 ASSERT_TRUE(info->GetString("pageUrl", &tmp));
611 ASSERT_EQ(params.page_url.spec(), tmp);
613 base::string16 tmp16;
614 ASSERT_TRUE(info->GetString("selectionText", &tmp16));
615 ASSERT_EQ(params.selection_text, tmp16);
617 bool bool_tmp = true;
618 ASSERT_TRUE(info->GetBoolean("editable", &bool_tmp));
619 ASSERT_EQ(params.is_editable, bool_tmp);
621 delete list;
624 // Test that there is always only one radio item selected.
625 TEST_F(MenuManagerTest, SanitizeRadioButtons) {
626 Extension* extension = AddExtension("test");
628 // A single unchecked item should get checked
629 MenuItem* item1 = CreateTestItem(extension);
631 item1->set_type(MenuItem::RADIO);
632 item1->SetChecked(false);
633 ASSERT_FALSE(item1->checked());
634 manager_.AddContextItem(extension, item1);
635 ASSERT_TRUE(item1->checked());
637 // In a run of two unchecked items, the first should get selected.
638 item1->SetChecked(false);
639 MenuItem* item2 = CreateTestItem(extension);
640 item2->set_type(MenuItem::RADIO);
641 item2->SetChecked(false);
642 ASSERT_FALSE(item1->checked());
643 ASSERT_FALSE(item2->checked());
644 manager_.AddContextItem(extension, item2);
645 ASSERT_TRUE(item1->checked());
646 ASSERT_FALSE(item2->checked());
648 // If multiple items are checked, only the last item should get checked.
649 item1->SetChecked(true);
650 item2->SetChecked(true);
651 ASSERT_TRUE(item1->checked());
652 ASSERT_TRUE(item2->checked());
653 manager_.ItemUpdated(item1->id());
654 ASSERT_FALSE(item1->checked());
655 ASSERT_TRUE(item2->checked());
657 // If the checked item is removed, the new first item should get checked.
658 item1->SetChecked(false);
659 item2->SetChecked(true);
660 ASSERT_FALSE(item1->checked());
661 ASSERT_TRUE(item2->checked());
662 manager_.RemoveContextMenuItem(item2->id());
663 item2 = NULL;
664 ASSERT_TRUE(item1->checked());
666 // If a checked item is added to a run that already has a checked item,
667 // then the new item should get checked.
668 item1->SetChecked(true);
669 MenuItem* new_item = CreateTestItem(extension);
670 new_item->set_type(MenuItem::RADIO);
671 new_item->SetChecked(true);
672 ASSERT_TRUE(item1->checked());
673 ASSERT_TRUE(new_item->checked());
674 manager_.AddContextItem(extension, new_item);
675 ASSERT_FALSE(item1->checked());
676 ASSERT_TRUE(new_item->checked());
677 // Make sure that children are checked as well.
678 MenuItem* parent = CreateTestItem(extension);
679 manager_.AddContextItem(extension, parent);
680 MenuItem* child1 = CreateTestItem(extension);
681 child1->set_type(MenuItem::RADIO);
682 child1->SetChecked(false);
683 MenuItem* child2 = CreateTestItem(extension);
684 child2->set_type(MenuItem::RADIO);
685 child2->SetChecked(true);
686 ASSERT_FALSE(child1->checked());
687 ASSERT_TRUE(child2->checked());
689 manager_.AddChildItem(parent->id(), child1);
690 ASSERT_TRUE(child1->checked());
692 manager_.AddChildItem(parent->id(), child2);
693 ASSERT_FALSE(child1->checked());
694 ASSERT_TRUE(child2->checked());
696 // Removing the checked item from the children should cause the
697 // remaining child to be checked.
698 manager_.RemoveContextMenuItem(child2->id());
699 child2 = NULL;
700 ASSERT_TRUE(child1->checked());
702 // This should NOT cause |new_item| to be deseleted because
703 // |parent| will be seperating the two runs of radio items.
704 manager_.ChangeParent(child1->id(), NULL);
705 ASSERT_TRUE(new_item->checked());
706 ASSERT_TRUE(child1->checked());
708 // Removing |parent| should cause only |child1| to be selected.
709 manager_.RemoveContextMenuItem(parent->id());
710 parent = NULL;
711 ASSERT_FALSE(new_item->checked());
712 ASSERT_TRUE(child1->checked());
715 // Tests the RemoveAllIncognitoContextItems functionality.
716 TEST_F(MenuManagerTest, RemoveAllIncognito) {
717 Extension* extension1 = AddExtension("1111");
718 // Add 2 top-level and one child item for extension 1
719 // with incognito 'true'.
720 MenuItem* item1 = CreateTestItem(extension1, true);
721 MenuItem* item2 = CreateTestItem(extension1, true);
722 MenuItem* item3 = CreateTestItem(extension1, true);
723 ASSERT_TRUE(manager_.AddContextItem(extension1, item1));
724 ASSERT_TRUE(manager_.AddContextItem(extension1, item2));
725 ASSERT_TRUE(manager_.AddChildItem(item1->id(), item3));
727 // Add 2 top-level and one child item for extension 1
728 // with incognito 'false'.
729 MenuItem* item4 = CreateTestItem(extension1);
730 MenuItem* item5 = CreateTestItem(extension1);
731 MenuItem* item6 = CreateTestItem(extension1);
732 ASSERT_TRUE(manager_.AddContextItem(extension1, item4));
733 ASSERT_TRUE(manager_.AddContextItem(extension1, item5));
734 ASSERT_TRUE(manager_.AddChildItem(item4->id(), item6));
736 // Add one top-level item for extension 2.
737 Extension* extension2 = AddExtension("2222");
738 MenuItem* item7 = CreateTestItem(extension2);
739 ASSERT_TRUE(manager_.AddContextItem(extension2, item7));
741 const MenuItem::ExtensionKey key1(extension1->id());
742 const MenuItem::ExtensionKey key2(extension2->id());
743 EXPECT_EQ(4u, manager_.MenuItems(key1)->size());
744 EXPECT_EQ(1u, manager_.MenuItems(key2)->size());
746 // Remove all context menu items with incognito true.
747 manager_.RemoveAllIncognitoContextItems();
748 EXPECT_EQ(2u, manager_.MenuItems(key1)->size());
749 EXPECT_EQ(1u, manager_.MenuItems(key2)->size());
752 } // namespace extensions