| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/browser.h" | 5 #include "chrome/browser/browser.h" |
| 6 #include "chrome/browser/browser_window.h" | 6 #include "chrome/browser/browser_window.h" |
| 7 #include "chrome/browser/extensions/extension_apitest.h" | 7 #include "chrome/browser/extensions/extension_apitest.h" |
| 8 #include "chrome/browser/extensions/extension_browser_event_router.h" | 8 #include "chrome/browser/extensions/extension_browser_event_router.h" |
| 9 #include "chrome/browser/extensions/extension_tabs_module.h" | 9 #include "chrome/browser/extensions/extension_tabs_module.h" |
| 10 #include "chrome/browser/extensions/extensions_service.h" | 10 #include "chrome/browser/extensions/extensions_service.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 ui_test_utils::NavigateToURL(browser(), | 55 ui_test_utils::NavigateToURL(browser(), |
| 56 GURL(extension->GetResourceURL("update2.html"))); | 56 GURL(extension->GetResourceURL("update2.html"))); |
| 57 ASSERT_TRUE(catcher.GetNextResult()); | 57 ASSERT_TRUE(catcher.GetNextResult()); |
| 58 } | 58 } |
| 59 | 59 |
| 60 // Test that we received the changes. | 60 // Test that we received the changes. |
| 61 tab_id = browser()->GetSelectedTabContents()->controller().session_id().id(); | 61 tab_id = browser()->GetSelectedTabContents()->controller().session_id().id(); |
| 62 EXPECT_FALSE(action->GetIcon(tab_id).isNull()); | 62 EXPECT_FALSE(action->GetIcon(tab_id).isNull()); |
| 63 } | 63 } |
| 64 | 64 |
| 65 | |
| 66 // Tests old-style pageActions API that is deprecated but we don't want to | 65 // Tests old-style pageActions API that is deprecated but we don't want to |
| 67 // break. | 66 // break. |
| 68 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, OldPageActions) { | 67 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, OldPageActions) { |
| 69 ASSERT_TRUE(RunExtensionTest("old_page_actions")) << message_; | 68 ASSERT_TRUE(RunExtensionTest("old_page_actions")) << message_; |
| 70 | 69 |
| 71 ExtensionsService* service = browser()->profile()->GetExtensionsService(); | 70 ExtensionsService* service = browser()->profile()->GetExtensionsService(); |
| 72 ASSERT_EQ(1u, service->extensions()->size()); | 71 ASSERT_EQ(1u, service->extensions()->size()); |
| 73 Extension* extension = service->extensions()->at(0); | 72 Extension* extension = service->extensions()->at(0); |
| 74 ASSERT_TRUE(extension); | 73 ASSERT_TRUE(extension); |
| 75 | 74 |
| 76 // Have the extension enable the page action. | 75 // Have the extension enable the page action. |
| 77 { | 76 { |
| 78 ResultCatcher catcher; | 77 ResultCatcher catcher; |
| 79 ui_test_utils::NavigateToURL(browser(), | 78 ui_test_utils::NavigateToURL(browser(), |
| 80 GURL(extension->GetResourceURL("page.html"))); | 79 GURL(extension->GetResourceURL("page.html"))); |
| 81 ASSERT_TRUE(catcher.GetNextResult()); | 80 ASSERT_TRUE(catcher.GetNextResult()); |
| 82 } | 81 } |
| 83 | 82 |
| 84 // Simulate the page action being clicked. | 83 // Simulate the page action being clicked. |
| 85 { | 84 { |
| 86 ResultCatcher catcher; | 85 ResultCatcher catcher; |
| 87 int tab_id = | 86 int tab_id = |
| 88 ExtensionTabUtil::GetTabId(browser()->GetSelectedTabContents()); | 87 ExtensionTabUtil::GetTabId(browser()->GetSelectedTabContents()); |
| 89 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted( | 88 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted( |
| 90 browser()->profile(), extension->id(), "action", tab_id, "", 1); | 89 browser()->profile(), extension->id(), "action", tab_id, "", 1); |
| 91 EXPECT_TRUE(catcher.GetNextResult()); | 90 EXPECT_TRUE(catcher.GetNextResult()); |
| 92 } | 91 } |
| 93 } | 92 } |
| 94 | 93 |
| 94 class PageActionPopupTest : public ExtensionApiTest { |
| 95 public: |
| 96 bool RunExtensionTest(const char* extension_name) { |
| 97 last_action_ = NULL; |
| 98 last_visibility_ = false; |
| 99 waiting_ = false; |
| 100 |
| 101 return ExtensionApiTest::RunExtensionTest(extension_name); |
| 102 }; |
| 103 |
| 104 void Observe(NotificationType type, const NotificationSource& source, |
| 105 const NotificationDetails& details) { |
| 106 switch (type.value) { |
| 107 case NotificationType::EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED: { |
| 108 last_action_ = Source<ExtensionAction>(source).ptr(); |
| 109 TabContents* contents = Details<TabContents>(details).ptr(); |
| 110 int tab_id = ExtensionTabUtil::GetTabId(contents); |
| 111 last_visibility_ = last_action_->GetIsVisible(tab_id); |
| 112 if (waiting_) |
| 113 MessageLoopForUI::current()->Quit(); |
| 114 break; |
| 115 } |
| 116 default: |
| 117 ExtensionBrowserTest::Observe(type, source, details); |
| 118 break; |
| 119 } |
| 120 } |
| 121 |
| 122 void WaitForPopupVisibilityChange() { |
| 123 // Wait for EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED to come in. |
| 124 if (!last_action_) { |
| 125 waiting_ = true; |
| 126 ui_test_utils::RunMessageLoop(); |
| 127 waiting_ = false; |
| 128 } |
| 129 } |
| 130 |
| 131 protected: |
| 132 bool waiting_; |
| 133 ExtensionAction* last_action_; |
| 134 bool last_visibility_; |
| 135 }; |
| 136 |
| 137 // TODO(port) |
| 138 #if defined(OS_WIN) |
| 139 // Tests popups in page actions. |
| 140 IN_PROC_BROWSER_TEST_F(PageActionPopupTest, Show) { |
| 141 NotificationRegistrar registrar; |
| 142 registrar.Add(this, |
| 143 NotificationType::EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED, |
| 144 NotificationService::AllSources()); |
| 145 |
| 146 ASSERT_TRUE(RunExtensionTest("page_action_popup")) << message_; |
| 147 |
| 148 ExtensionsService* service = browser()->profile()->GetExtensionsService(); |
| 149 ASSERT_EQ(1u, service->extensions()->size()); |
| 150 Extension* extension = service->extensions()->at(0); |
| 151 ASSERT_TRUE(extension); |
| 152 |
| 153 // Wait for The page action to actually become visible. |
| 154 if (!last_visibility_) |
| 155 last_action_ = NULL; |
| 156 WaitForPopupVisibilityChange(); |
| 157 ASSERT_TRUE(last_visibility_); |
| 158 |
| 159 LocationBarTesting* location_bar = |
| 160 browser()->window()->GetLocationBar()->GetLocationBarForTesting(); |
| 161 ASSERT_EQ(1, location_bar->PageActionVisibleCount()); |
| 162 ExtensionAction* action = location_bar->GetVisiblePageAction(0); |
| 163 ASSERT_TRUE(action == last_action_); |
| 164 |
| 165 { |
| 166 ResultCatcher catcher; |
| 167 location_bar->TestPageActionPressed(0); |
| 168 ASSERT_TRUE(catcher.GetNextResult()); |
| 169 } |
| 170 } |
| 171 #endif |
| OLD | NEW |