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

Side by Side Diff: chrome/browser/extensions/api/extension_action/browser_action_apitest.cc

Issue 25305002: Implement initial chrome.browserAction.openPopup API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: finnur@ comments Created 7 years, 2 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #if defined(TOOLKIT_GTK) 7 #if defined(TOOLKIT_GTK)
8 #include <gtk/gtk.h> 8 #include <gtk/gtk.h>
9 #endif 9 #endif
10 10
11 #include "chrome/browser/chrome_notification_types.h" 11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/extensions/browser_action_test_util.h" 12 #include "chrome/browser/extensions/browser_action_test_util.h"
13 #include "chrome/browser/extensions/extension_action.h" 13 #include "chrome/browser/extensions/extension_action.h"
14 #include "chrome/browser/extensions/extension_action_icon_factory.h" 14 #include "chrome/browser/extensions/extension_action_icon_factory.h"
15 #include "chrome/browser/extensions/extension_action_manager.h" 15 #include "chrome/browser/extensions/extension_action_manager.h"
16 #include "chrome/browser/extensions/extension_apitest.h" 16 #include "chrome/browser/extensions/extension_apitest.h"
17 #include "chrome/browser/extensions/extension_service.h" 17 #include "chrome/browser/extensions/extension_service.h"
18 #include "chrome/browser/extensions/extension_system.h" 18 #include "chrome/browser/extensions/extension_system.h"
19 #include "chrome/browser/extensions/extension_tab_util.h" 19 #include "chrome/browser/extensions/extension_tab_util.h"
20 #include "chrome/browser/extensions/extension_test_message_listener.h"
20 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/ui/browser.h" 22 #include "chrome/browser/ui/browser.h"
22 #include "chrome/browser/ui/browser_commands.h" 23 #include "chrome/browser/ui/browser_commands.h"
23 #include "chrome/browser/ui/browser_window.h" 24 #include "chrome/browser/ui/browser_window.h"
24 #include "chrome/browser/ui/tabs/tab_strip_model.h" 25 #include "chrome/browser/ui/tabs/tab_strip_model.h"
26 #include "chrome/common/extensions/permissions/permissions_data.h"
25 #include "chrome/common/url_constants.h" 27 #include "chrome/common/url_constants.h"
26 #include "chrome/test/base/ui_test_utils.h" 28 #include "chrome/test/base/ui_test_utils.h"
27 #include "content/public/browser/notification_service.h" 29 #include "content/public/browser/notification_service.h"
28 #include "content/public/browser/web_contents.h" 30 #include "content/public/browser/web_contents.h"
29 #include "content/public/test/browser_test_utils.h" 31 #include "content/public/test/browser_test_utils.h"
30 #include "grit/theme_resources.h" 32 #include "grit/theme_resources.h"
31 #include "ui/base/resource/resource_bundle.h" 33 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/gfx/rect.h" 34 #include "ui/gfx/rect.h"
33 #include "ui/gfx/size.h" 35 #include "ui/gfx/size.h"
34 #include "ui/gfx/image/image_skia.h" 36 #include "ui/gfx/image/image_skia.h"
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 GetBrowserActionsBar().Press(index); 85 GetBrowserActionsBar().Press(index);
84 popup_observer.Wait(); 86 popup_observer.Wait();
85 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); 87 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
86 return GetBrowserActionsBar().HasPopup(); 88 return GetBrowserActionsBar().HasPopup();
87 } 89 }
88 90
89 ExtensionAction* GetBrowserAction(const Extension& extension) { 91 ExtensionAction* GetBrowserAction(const Extension& extension) {
90 return ExtensionActionManager::Get(browser()->profile())-> 92 return ExtensionActionManager::Get(browser()->profile())->
91 GetBrowserAction(extension); 93 GetBrowserAction(extension);
92 } 94 }
95
96 bool IsGrantedForTab(const Extension* extension,
97 const content::WebContents* web_contents) {
98 return PermissionsData::HasAPIPermissionForTab(
99 extension,
100 SessionID::IdForTab(web_contents),
101 APIPermission::kTab);
102 }
93 }; 103 };
94 104
95 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, Basic) { 105 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, Basic) {
96 ASSERT_TRUE(test_server()->Start()); 106 ASSERT_TRUE(test_server()->Start());
97 ASSERT_TRUE(RunExtensionTest("browser_action/basics")) << message_; 107 ASSERT_TRUE(RunExtensionTest("browser_action/basics")) << message_;
98 const Extension* extension = GetSingleLoadedExtension(); 108 const Extension* extension = GetSingleLoadedExtension();
99 ASSERT_TRUE(extension) << message_; 109 ASSERT_TRUE(extension) << message_;
100 110
101 // Test that there is a browser action in the toolbar. 111 // Test that there is a browser action in the toolbar.
102 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions()); 112 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
(...skipping 10 matching lines...) Expand all
113 ASSERT_EQ("badge", action->GetBadgeText(ExtensionAction::kDefaultTabId)); 123 ASSERT_EQ("badge", action->GetBadgeText(ExtensionAction::kDefaultTabId));
114 ASSERT_EQ(SkColorSetARGB(255, 255, 255, 255), 124 ASSERT_EQ(SkColorSetARGB(255, 255, 255, 255),
115 action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId)); 125 action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
116 126
117 // Simulate the browser action being clicked. 127 // Simulate the browser action being clicked.
118 ui_test_utils::NavigateToURL(browser(), 128 ui_test_utils::NavigateToURL(browser(),
119 test_server()->GetURL("files/extensions/test_file.txt")); 129 test_server()->GetURL("files/extensions/test_file.txt"));
120 130
121 ExtensionService* service = extensions::ExtensionSystem::Get( 131 ExtensionService* service = extensions::ExtensionSystem::Get(
122 browser()->profile())->extension_service(); 132 browser()->profile())->extension_service();
123 service->toolbar_model()->ExecuteBrowserAction(extension, browser(), NULL); 133 service->toolbar_model()->ExecuteBrowserAction(
134 extension, browser(), NULL, true);
124 135
125 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); 136 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
126 } 137 }
127 138
128 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) { 139 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) {
129 ASSERT_TRUE(RunExtensionTest("browser_action/no_icon")) << message_; 140 ASSERT_TRUE(RunExtensionTest("browser_action/no_icon")) << message_;
130 const Extension* extension = GetSingleLoadedExtension(); 141 const Extension* extension = GetSingleLoadedExtension();
131 ASSERT_TRUE(extension) << message_; 142 ASSERT_TRUE(extension) << message_;
132 143
133 #if defined (OS_MACOSX) 144 #if defined (OS_MACOSX)
(...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after
583 ASSERT_TRUE(manager->GetBackgroundHostForExtension(extension->id())); 594 ASSERT_TRUE(manager->GetBackgroundHostForExtension(extension->id()));
584 ExtensionAction* action = GetBrowserAction(*extension); 595 ExtensionAction* action = GetBrowserAction(*extension);
585 ASSERT_EQ("", action->GetBadgeText(ExtensionAction::kDefaultTabId)); 596 ASSERT_EQ("", action->GetBadgeText(ExtensionAction::kDefaultTabId));
586 597
587 content::WindowedNotificationObserver host_destroyed_observer( 598 content::WindowedNotificationObserver host_destroyed_observer(
588 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, 599 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
589 content::NotificationService::AllSources()); 600 content::NotificationService::AllSources());
590 601
591 // Click the browser action. 602 // Click the browser action.
592 extensions::ExtensionSystem::Get(browser()->profile())->extension_service()-> 603 extensions::ExtensionSystem::Get(browser()->profile())->extension_service()->
593 toolbar_model()->ExecuteBrowserAction(extension, browser(), NULL); 604 toolbar_model()->ExecuteBrowserAction(extension, browser(), NULL, true);
594 605
595 // It can take a moment for the background page to actually get destroyed 606 // It can take a moment for the background page to actually get destroyed
596 // so we wait for the notification before checking that it's really gone 607 // so we wait for the notification before checking that it's really gone
597 // and the badge text has been set. 608 // and the badge text has been set.
598 host_destroyed_observer.Wait(); 609 host_destroyed_observer.Wait();
599 ASSERT_FALSE(manager->GetBackgroundHostForExtension(extension->id())); 610 ASSERT_FALSE(manager->GetBackgroundHostForExtension(extension->id()));
600 ASSERT_EQ("X", action->GetBadgeText(ExtensionAction::kDefaultTabId)); 611 ASSERT_EQ("X", action->GetBadgeText(ExtensionAction::kDefaultTabId));
601 } 612 }
602 613
603 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BadgeBackgroundColor) { 614 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BadgeBackgroundColor) {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
686 const std::string script = 697 const std::string script =
687 "window.domAutomationController.send(document.body.style." 698 "window.domAutomationController.send(document.body.style."
688 "backgroundColor);"; 699 "backgroundColor);";
689 std::string result; 700 std::string result;
690 const std::string frame_xpath; 701 const std::string frame_xpath;
691 EXPECT_TRUE(content::ExecuteScriptInFrameAndExtractString( 702 EXPECT_TRUE(content::ExecuteScriptInFrameAndExtractString(
692 tab, frame_xpath, script, &result)); 703 tab, frame_xpath, script, &result));
693 EXPECT_EQ(result, "red"); 704 EXPECT_EQ(result, "red");
694 } 705 }
695 706
707
708 // http://crbug.com/177163
709 #if defined(OS_WIN) && !defined(NDEBUG)
710 #define MAYBE_TestOpenPopup DISABLED_TestOpenPopup
711 #else
712 #define MAYBE_TestOpenPopup TestOpenPopup
713 #endif
714 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, MAYBE_TestOpenPopup) {
715 browser()->window()->Show();
Finnur 2013/10/18 10:31:01 I thought this was implicit in the test framework.
justinlin 2013/10/19 06:01:48 OK, thought they might be needed, maybe it's not r
716 ExtensionTestMessageListener listener("ready", true);
717 {
718 // Show first popup in original window and expect it to have loaded.
719 content::WindowedNotificationObserver frame_observer(
720 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
721 content::NotificationService::AllSources());
722
723 ASSERT_TRUE(RunExtensionSubtest("browser_action/open_popup",
724 "open_popup_succeeds.html")) << message_;
725 frame_observer.Wait();
726 EXPECT_TRUE(GetBrowserActionsBar().HasPopup());
727 }
728
729 EXPECT_TRUE(listener.WaitUntilSatisfied());
730
731 {
732 // Open a new window.
733 content::WindowedNotificationObserver frame_observer(
734 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
735 content::NotificationService::AllSources());
736 browser()->OpenURL(content::OpenURLParams(
737 GURL("about:"), content::Referrer(), NEW_WINDOW,
738 content::PAGE_TRANSITION_TYPED, false));
739 #if defined(OS_WIN)
740 // Hide all the buttons to test that it opens even when browser action is
741 // in the overflow bucket.
742 // TODO(justinlin): Implement for other platforms.
743 GetBrowserActionsBar().SetIconVisibilityCount(0);
744 #endif
745 frame_observer.Wait();
746 }
747
748 ResultCatcher catcher;
749 {
750 content::WindowedNotificationObserver frame_observer(
751 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
752 content::NotificationService::AllSources());
753 // Show second popup in new window.
754 listener.Reply("");
755 frame_observer.Wait();
756 EXPECT_TRUE(GetBrowserActionsBar().HasPopup());
757 }
758 ASSERT_TRUE(catcher.GetNextResult()) << message_;
759 }
760
761 // http://crbug.com/177163
762 #if defined(OS_WIN) && !defined(NDEBUG)
763 #define MAYBE_TestOpenPopupIncognito DISABLED_TestOpenPopupIncognito
764 #else
765 #define MAYBE_TestOpenPopupIncognito TestOpenPopupIncognito
766 #endif
767 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, MAYBE_TestOpenPopupIncognito) {
768 browser()->window()->Show();
769 content::WindowedNotificationObserver frame_observer(
770 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
771 content::NotificationService::AllSources());
772 ASSERT_TRUE(RunExtensionSubtest("browser_action/open_popup",
773 "open_popup_succeeds.html",
774 kFlagEnableIncognito | kFlagUseIncognito))
775 << message_;
776 frame_observer.Wait();
777 EXPECT_TRUE(GetBrowserActionsBar().HasPopup());
778 }
779
780 // http://crbug.com/177163
781 #if defined(OS_WIN) && !defined(NDEBUG)
782 #define MAYBE_TestOpenPopupIncognitoNotAllowed DISABLED_TestOpenPopupIncognitoNo tAllowed
783 #else
784 #define MAYBE_TestOpenPopupIncognitoNotAllowed TestOpenPopupIncognitoNotAllowed
785 #endif
786 // Test that the popup does not show in an incognito window if extension is not
787 // enabled for incognito.
788 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest,
789 MAYBE_TestOpenPopupIncognitoNotAllowed) {
790 browser()->window()->Show();
791 {
792 // Open an incognito window.
793 content::WindowedNotificationObserver frame_observer(
794 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
795 content::NotificationService::AllSources());
796 browser()->OpenURL(content::OpenURLParams(
797 GURL("about:"), content::Referrer(), OFF_THE_RECORD,
798 content::PAGE_TRANSITION_TYPED, false));
799 frame_observer.Wait();
800 }
801
802 {
803 // Load popup.
804 content::WindowedNotificationObserver frame_observer(
805 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
806 content::NotificationService::AllSources());
807 ASSERT_TRUE(RunExtensionSubtest("browser_action/open_popup",
808 "open_popup_succeeds.html")) << message_;
809 frame_observer.Wait();
810 EXPECT_TRUE(GetBrowserActionsBar().HasPopup());
811 }
812 }
813
814 // http://crbug.com/177163
815 #if defined(OS_WIN) && !defined(NDEBUG)
816 #define MAYBE_TestOpenPopupDoesNotCloseOtherPopups DISABLED_TestOpenPopupDoesNot CloseOtherPopups
817 #else
818 #define MAYBE_TestOpenPopupDoesNotCloseOtherPopups TestOpenPopupDoesNotCloseOthe rPopups
819 #endif
820 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest,
821 MAYBE_TestOpenPopupDoesNotCloseOtherPopups) {
822 browser()->window()->Show();
823 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
824 "browser_action/popup")));
825 const Extension* extension = GetSingleLoadedExtension();
Finnur 2013/10/18 10:31:01 Maybe ASSERT the extension pointer?
justinlin 2013/10/19 06:01:48 Done. Doesn't really seem needed though, since if
Finnur 2013/10/21 08:12:12 If it fails, it means the env is to blame (not you
826
827 ExtensionTestMessageListener listener("ready", true);
828 {
829 // Show first popup in original window and expect it to have loaded.
830 content::WindowedNotificationObserver frame_observer(
831 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
832 content::NotificationService::AllSources());
833
834 ASSERT_TRUE(RunExtensionSubtest("browser_action/open_popup",
835 "open_popup_fails.html")) << message_;
836 frame_observer.Wait();
837 }
838
839 EXPECT_TRUE(listener.WaitUntilSatisfied());
840
841 {
842 content::WindowedNotificationObserver frame_observer(
843 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
844 content::NotificationService::AllSources());
845 ASSERT_TRUE(OpenPopup(0));
846 frame_observer.Wait();
847 EXPECT_TRUE(GetBrowserActionsBar().HasPopup());
848 const int tab_id = ExtensionTabUtil::GetTabId(
849 browser()->tab_strip_model()->GetActiveWebContents());
850 ASSERT_TRUE(GetBrowserAction(*extension)->HasPopup(tab_id));
851 }
852
853 ResultCatcher catcher;
854 listener.Reply("");
855 ASSERT_TRUE(catcher.GetNextResult()) << message_;
856 }
857
858 // http://crbug.com/177163
859 #if defined(OS_WIN) && !defined(NDEBUG)
860 #define MAYBE_TestOpenPopupDoesNotGrant DISABLED_TestOpenPopupDoesNotGrant
861 #else
862 #define MAYBE_TestOpenPopupDoesNotGrant TestOpenPopupDoesNotGrant
863 #endif
864 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, MAYBE_TestOpenPopupDoesNotGrant) {
865 browser()->window()->Show();
866 content::WindowedNotificationObserver frame_observer(
867 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
868 content::NotificationService::AllSources());
869 ASSERT_TRUE(RunExtensionSubtest("browser_action/open_popup",
870 "open_popup_succeeds.html")) << message_;
871 frame_observer.Wait();
872
873 ExtensionService* service = extensions::ExtensionSystem::Get(
874 browser()->profile())->extension_service();
875 ASSERT_FALSE(IsGrantedForTab(
876 service->GetExtensionById(last_loaded_extension_id_, false),
877 browser()->tab_strip_model()->GetActiveWebContents()));
878 }
879
696 } // namespace 880 } // namespace
697 } // namespace extensions 881 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698