Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / extension_action / browser_action_apitest.cc
index dc680e3..96a564e 100644 (file)
@@ -3,20 +3,15 @@
 // found in the LICENSE file.
 
 #include "build/build_config.h"
-
-#if defined(TOOLKIT_GTK)
-#include <gtk/gtk.h>
-#endif
-
-#include "chrome/browser/chrome_notification_types.h"
+#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
 #include "chrome/browser/extensions/browser_action_test_util.h"
 #include "chrome/browser/extensions/extension_action.h"
 #include "chrome/browser/extensions/extension_action_icon_factory.h"
 #include "chrome/browser/extensions/extension_action_manager.h"
 #include "chrome/browser/extensions/extension_apitest.h"
-#include "chrome/browser/extensions/extension_service.h"
-#include "chrome/browser/extensions/extension_system.h"
 #include "chrome/browser/extensions/extension_tab_util.h"
+#include "chrome/browser/extensions/extension_toolbar_model.h"
+#include "chrome/browser/extensions/extension_util.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/browser/ui/browser.h"
 #include "chrome/browser/ui/browser_commands.h"
 #include "content/public/browser/notification_service.h"
 #include "content/public/browser/web_contents.h"
 #include "content/public/test/browser_test_utils.h"
+#include "extensions/browser/extension_registry.h"
+#include "extensions/browser/extension_system.h"
+#include "extensions/browser/notification_types.h"
+#include "extensions/browser/process_manager.h"
+#include "extensions/browser/test_extension_registry_observer.h"
+#include "extensions/common/feature_switch.h"
+#include "extensions/test/result_catcher.h"
 #include "grit/theme_resources.h"
 #include "ui/base/resource/resource_bundle.h"
-#include "ui/gfx/rect.h"
-#include "ui/gfx/size.h"
 #include "ui/gfx/image/image_skia.h"
 #include "ui/gfx/image/image_skia_operations.h"
+#include "ui/gfx/rect.h"
+#include "ui/gfx/size.h"
 #include "ui/gfx/skia_util.h"
 
 using content::WebContents;
@@ -52,9 +54,9 @@ gfx::ImageSkia AddBackgroundForViews(const gfx::ImageSkia& icon) {
   ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
   gfx::ImageSkia bg = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION);
   return gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon);
-#endif
-
+#else
   return icon;
+#endif
 }
 
 bool ImagesAreEqualAtScale(const gfx::ImageSkia& i1,
@@ -68,7 +70,7 @@ bool ImagesAreEqualAtScale(const gfx::ImageSkia& i1,
 class BrowserActionApiTest : public ExtensionApiTest {
  public:
   BrowserActionApiTest() {}
-  virtual ~BrowserActionApiTest() {}
+  ~BrowserActionApiTest() override {}
 
  protected:
   BrowserActionTestUtil GetBrowserActionsBar() {
@@ -118,10 +120,8 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, Basic) {
   ui_test_utils::NavigateToURL(browser(),
       test_server()->GetURL("files/extensions/test_file.txt"));
 
-  ExtensionService* service = extensions::ExtensionSystem::Get(
-      browser()->profile())->extension_service();
-  service->toolbar_model()->ExecuteBrowserAction(
-      extension, browser(), NULL, true);
+  ExtensionActionAPI::Get(browser()->profile())->ExecuteExtensionAction(
+      extension, browser(), true);
 
   ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
 }
@@ -174,10 +174,10 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) {
 
   EXPECT_FALSE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
 
-  EXPECT_TRUE(ImagesAreEqualAtScale(
-      AddBackgroundForViews(*action_icon.ToImageSkia()),
-      *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
-      1.0f));
+  EXPECT_TRUE(
+      ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon.ToImageSkia()),
+                            *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
+                            1.0f));
 
   // Tell the extension to update the icon using path.
   GetBrowserActionsBar().Press(0);
@@ -192,10 +192,10 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) {
   EXPECT_FALSE(
       action_icon.ToImageSkia()->HasRepresentation(2.0f));
 
-  EXPECT_TRUE(ImagesAreEqualAtScale(
-      AddBackgroundForViews(*action_icon.ToImageSkia()),
-      *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
-      1.0f));
+  EXPECT_TRUE(
+      ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon.ToImageSkia()),
+                            *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
+                            1.0f));
 
   // Tell the extension to update the icon using dictionary of ImageData
   // objects.
@@ -210,10 +210,10 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) {
 
   EXPECT_TRUE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
 
-  EXPECT_TRUE(ImagesAreEqualAtScale(
-      AddBackgroundForViews(*action_icon.ToImageSkia()),
-      *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
-      1.0f));
+  EXPECT_TRUE(
+      ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon.ToImageSkia()),
+                            *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
+                            1.0f));
 
   // Tell the extension to update the icon using dictionary of paths.
   GetBrowserActionsBar().Press(0);
@@ -227,10 +227,10 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) {
 
   EXPECT_TRUE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
 
-  EXPECT_TRUE(ImagesAreEqualAtScale(
-      AddBackgroundForViews(*action_icon.ToImageSkia()),
-      *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
-      1.0f));
+  EXPECT_TRUE(
+      ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon.ToImageSkia()),
+                            *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
+                            1.0f));
 
   // Tell the extension to update the icon using dictionary of ImageData
   // objects, but setting only size 19.
@@ -245,10 +245,10 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) {
 
   EXPECT_FALSE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
 
-  EXPECT_TRUE(ImagesAreEqualAtScale(
-      AddBackgroundForViews(*action_icon.ToImageSkia()),
-      *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
-      1.0f));
+  EXPECT_TRUE(
+      ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon.ToImageSkia()),
+                            *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
+                            1.0f));
 
   // Tell the extension to update the icon using dictionary of paths, but
   // setting only size 19.
@@ -263,10 +263,10 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) {
 
   EXPECT_FALSE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
 
-  EXPECT_TRUE(ImagesAreEqualAtScale(
-      AddBackgroundForViews(*action_icon.ToImageSkia()),
-      *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
-      1.0f));
+  EXPECT_TRUE(
+      ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon.ToImageSkia()),
+                            *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
+                            1.0f));
 
   // Tell the extension to update the icon using dictionary of ImageData
   // objects, but setting only size 38.
@@ -288,10 +288,10 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) {
       *action_icon.ToSkBitmap(),
       action_icon_skia->GetRepresentation(2.0f).sk_bitmap()));
 
-  EXPECT_TRUE(ImagesAreEqualAtScale(
-      AddBackgroundForViews(*action_icon_skia),
-      *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
-      2.0f));
+  EXPECT_TRUE(
+      ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon_skia),
+                            *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
+                            2.0f));
 
   // Try setting icon with empty dictionary of ImageData objects.
   GetBrowserActionsBar().Press(0);
@@ -338,8 +338,8 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest,
 // http://code.google.com/p/chromium/issues/detail?id=70829
 // Mac used to be ok, but then mac 10.5 started failing too. =(
 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DISABLED_BrowserActionPopup) {
-  ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
-      "browser_action/popup")));
+  ASSERT_TRUE(
+      LoadExtension(test_data_dir_.AppendASCII("browser_action/popup")));
   BrowserActionTestUtil actions_bar = GetBrowserActionsBar();
   const Extension* extension = GetSingleLoadedExtension();
   ASSERT_TRUE(extension) << message_;
@@ -357,16 +357,16 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DISABLED_BrowserActionPopup) {
   // Simulate a click on the browser action and verify the size of the resulting
   // popup.  The first one tries to be 0x0, so it should be the min values.
   ASSERT_TRUE(OpenPopup(0));
-  EXPECT_EQ(minSize, actions_bar.GetPopupBounds().size());
+  EXPECT_EQ(minSize, actions_bar.GetPopupSize());
   EXPECT_TRUE(actions_bar.HidePopup());
 
   ASSERT_TRUE(OpenPopup(0));
-  EXPECT_EQ(middleSize, actions_bar.GetPopupBounds().size());
+  EXPECT_EQ(middleSize, actions_bar.GetPopupSize());
   EXPECT_TRUE(actions_bar.HidePopup());
 
   // One more time, but this time it should be constrained by the max values.
   ASSERT_TRUE(OpenPopup(0));
-  EXPECT_EQ(maxSize, actions_bar.GetPopupBounds().size());
+  EXPECT_EQ(maxSize, actions_bar.GetPopupSize());
   EXPECT_TRUE(actions_bar.HidePopup());
 }
 
@@ -474,6 +474,7 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, IncognitoBasic) {
   // Open an incognito window and test that the browser action isn't there by
   // default.
   Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
+  base::RunLoop().RunUntilIdle();  // Wait for profile initialization.
   Browser* incognito_browser =
       new Browser(Browser::CreateParams(incognito_profile,
                                         browser()->host_desktop_type()));
@@ -482,15 +483,15 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, IncognitoBasic) {
             BrowserActionTestUtil(incognito_browser).NumberOfBrowserActions());
 
   // Now enable the extension in incognito mode, and test that the browser
-  // action shows up. Note that we don't update the existing window at the
-  // moment, so we just create a new one.
-  extensions::ExtensionSystem::Get(browser()->profile())->extension_service()->
-      extension_prefs()->SetIsIncognitoEnabled(extension->id(), true);
+  // action shows up.
+  // SetIsIncognitoEnabled() requires a reload of the extension, so we have to
+  // wait for it.
+  TestExtensionRegistryObserver registry_observer(
+      ExtensionRegistry::Get(profile()), extension->id());
+  extensions::util::SetIsIncognitoEnabled(
+      extension->id(), browser()->profile(), true);
+  registry_observer.WaitForExtensionLoaded();
 
-  chrome::CloseWindow(incognito_browser);
-  incognito_browser =
-      new Browser(Browser::CreateParams(incognito_profile,
-                                        browser()->host_desktop_type()));
   ASSERT_EQ(1,
             BrowserActionTestUtil(incognito_browser).NumberOfBrowserActions());
 
@@ -498,100 +499,61 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, IncognitoBasic) {
   // incognito.
 }
 
-IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, IncognitoDragging) {
-  ExtensionService* service = extensions::ExtensionSystem::Get(
-      browser()->profile())->extension_service();
-
-  // The tooltips for each respective browser action.
-  const char kTooltipA[] = "Make this page red";
-  const char kTooltipB[] = "grow";
-  const char kTooltipC[] = "Test setPopup()";
-
-  const size_t size_before = service->extensions()->size();
-
-  const Extension* extension_a = LoadExtension(test_data_dir_.AppendASCII(
-      "browser_action/basics"));
-  const Extension* extension_b = LoadExtension(test_data_dir_.AppendASCII(
-      "browser_action/popup"));
-  const Extension* extension_c = LoadExtension(test_data_dir_.AppendASCII(
-      "browser_action/add_popup"));
-  ASSERT_TRUE(extension_a);
-  ASSERT_TRUE(extension_b);
-  ASSERT_TRUE(extension_c);
-
-  // Test that there are 3 browser actions in the toolbar.
-  ASSERT_EQ(size_before + 3, service->extensions()->size());
-  ASSERT_EQ(3, GetBrowserActionsBar().NumberOfBrowserActions());
-
-  // Now enable 2 of the extensions in incognito mode, and test that the browser
-  // actions show up.
-  service->extension_prefs()->SetIsIncognitoEnabled(extension_a->id(), true);
-  service->extension_prefs()->SetIsIncognitoEnabled(extension_c->id(), true);
+// Tests that events are dispatched to the correct profile for split mode
+// extensions.
+IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, IncognitoSplit) {
+  ResultCatcher catcher;
+  const Extension* extension = LoadExtensionWithFlags(
+      test_data_dir_.AppendASCII("browser_action/split_mode"),
+      kFlagEnableIncognito);
+  ASSERT_TRUE(extension) << message_;
 
+  // Open an incognito window.
   Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
   Browser* incognito_browser =
       new Browser(Browser::CreateParams(incognito_profile,
                                         browser()->host_desktop_type()));
-  BrowserActionTestUtil incognito_bar(incognito_browser);
-
+  base::RunLoop().RunUntilIdle();  // Wait for profile initialization.
   // Navigate just to have a tab in this window, otherwise wonky things happen.
   ui_test_utils::OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
+  ASSERT_EQ(1,
+            BrowserActionTestUtil(incognito_browser).NumberOfBrowserActions());
 
-  ASSERT_EQ(2, incognito_bar.NumberOfBrowserActions());
-
-  // Ensure that the browser actions are in the right order (ABC).
-  EXPECT_EQ(kTooltipA, GetBrowserActionsBar().GetTooltip(0));
-  EXPECT_EQ(kTooltipB, GetBrowserActionsBar().GetTooltip(1));
-  EXPECT_EQ(kTooltipC, GetBrowserActionsBar().GetTooltip(2));
-
-  EXPECT_EQ(kTooltipA, incognito_bar.GetTooltip(0));
-  EXPECT_EQ(kTooltipC, incognito_bar.GetTooltip(1));
-
-  // Now rearrange them and ensure that they are rearranged correctly in both
-  // regular and incognito mode.
-
-  // ABC -> CAB
-  service->toolbar_model()->MoveBrowserAction(extension_c, 0);
-
-  EXPECT_EQ(kTooltipC, GetBrowserActionsBar().GetTooltip(0));
-  EXPECT_EQ(kTooltipA, GetBrowserActionsBar().GetTooltip(1));
-  EXPECT_EQ(kTooltipB, GetBrowserActionsBar().GetTooltip(2));
-
-  EXPECT_EQ(kTooltipC, incognito_bar.GetTooltip(0));
-  EXPECT_EQ(kTooltipA, incognito_bar.GetTooltip(1));
-
-  // CAB -> CBA
-  service->toolbar_model()->MoveBrowserAction(extension_b, 1);
-
-  EXPECT_EQ(kTooltipC, GetBrowserActionsBar().GetTooltip(0));
-  EXPECT_EQ(kTooltipB, GetBrowserActionsBar().GetTooltip(1));
-  EXPECT_EQ(kTooltipA, GetBrowserActionsBar().GetTooltip(2));
+  // A click in the regular profile should open a tab in the regular profile.
+  ExtensionActionAPI* extension_action_api =
+      ExtensionActionAPI::Get(browser()->profile());
+  extension_action_api->ExecuteExtensionAction(
+      extension, browser(), true);
+  ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
 
-  EXPECT_EQ(kTooltipC, incognito_bar.GetTooltip(0));
-  EXPECT_EQ(kTooltipA, incognito_bar.GetTooltip(1));
+  // A click in the incognito profile should open a tab in the
+  // incognito profile.
+  extension_action_api->ExecuteExtensionAction(
+      extension, incognito_browser, true);
+  ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
 }
 
 // Disabled because of failures (crashes) on ASAN bot.
 // See http://crbug.com/98861.
 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DISABLED_CloseBackgroundPage) {
-  ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
-      "browser_action/close_background")));
+  ASSERT_TRUE(LoadExtension(
+      test_data_dir_.AppendASCII("browser_action/close_background")));
   const Extension* extension = GetSingleLoadedExtension();
 
   // There is a background page and a browser action with no badge text.
-  ExtensionProcessManager* manager =
-      extensions::ExtensionSystem::Get(browser()->profile())->process_manager();
+  extensions::ProcessManager* manager =
+      extensions::ProcessManager::Get(browser()->profile());
   ASSERT_TRUE(manager->GetBackgroundHostForExtension(extension->id()));
   ExtensionAction* action = GetBrowserAction(*extension);
   ASSERT_EQ("", action->GetBadgeText(ExtensionAction::kDefaultTabId));
 
   content::WindowedNotificationObserver host_destroyed_observer(
-      chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
+      extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
       content::NotificationService::AllSources());
 
   // Click the browser action.
-  extensions::ExtensionSystem::Get(browser()->profile())->extension_service()->
-      toolbar_model()->ExecuteBrowserAction(extension, browser(), NULL, true);
+  ExtensionActionAPI::Get(browser()->profile())->ExecuteExtensionAction(
+      extension, browser(), true);
 
   // It can take a moment for the background page to actually get destroyed
   // so we wait for the notification before checking that it's really gone
@@ -688,9 +650,7 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, TestTriggerBrowserAction) {
       "window.domAutomationController.send(document.body.style."
       "backgroundColor);";
   std::string result;
-  const std::string frame_xpath;
-  EXPECT_TRUE(content::ExecuteScriptInFrameAndExtractString(
-      tab, frame_xpath, script, &result));
+  EXPECT_TRUE(content::ExecuteScriptAndExtractString(tab, script, &result));
   EXPECT_EQ(result, "red");
 }