Chromium Code Reviews| Index: chrome/browser/extensions/api/tabs/tabs_test.cc |
| diff --git a/chrome/browser/extensions/api/tabs/tabs_test.cc b/chrome/browser/extensions/api/tabs/tabs_test.cc |
| index 0ce9e1f569b34e96d9624dca7a59b9bb7a828344..bd72a62520e94aec04ed4eb417b779777e558565 100644 |
| --- a/chrome/browser/extensions/api/tabs/tabs_test.cc |
| +++ b/chrome/browser/extensions/api/tabs/tabs_test.cc |
| @@ -22,6 +22,7 @@ |
| #include "chrome/browser/ui/browser_window.h" |
| #include "chrome/test/base/in_process_browser_test.h" |
| #include "chrome/test/base/ui_test_utils.h" |
| +#include "content/public/common/page_zoom.h" |
| #include "ui/gfx/rect.h" |
| namespace extensions { |
| @@ -590,4 +591,229 @@ IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DuplicateTabNoPermission) { |
| EXPECT_FALSE(utils::HasPrivacySensitiveFields(duplicate_result.get())); |
| } |
| +// Provides helper functions for testing the zoom extension API. |
| +class ZoomExtensionAPITestHelper { |
|
not at google - send to devlin
2014/04/11 15:04:25
a more convenient/common pattern is to have
class
paulmeyer
2014/04/11 19:09:49
Done.
|
| + public: |
| + explicit ZoomExtensionAPITestHelper(Browser* browser) |
| + : browser_(browser), |
| + extension_(utils::CreateEmptyExtension()) {} |
| + |
| + // Runs chrome.tabs.setZoom(). |
| + void RunSetZoom(int tab_id, double zoom_factor) { |
| + scoped_refptr<TabsSetZoomFunction> set_zoom_function( |
| + new TabsSetZoomFunction()); |
| + set_zoom_function->set_extension(extension_); |
| + set_zoom_function->set_has_callback(true); |
| + |
| + utils::RunFunction(set_zoom_function.get(), |
| + base::StringPrintf("[%u, %lf]", tab_id, zoom_factor), |
| + browser_, extension_function_test_utils::NONE); |
| + } |
| + |
| + // Runs chrome.tabs.getZoom(). |
| + void RunGetZoom(int tab_id, double* zoom_factor) { |
| + scoped_refptr<TabsGetZoomFunction> get_zoom_function( |
| + new TabsGetZoomFunction()); |
| + get_zoom_function->set_extension(extension_); |
| + get_zoom_function->set_has_callback(true); |
| + |
| + scoped_ptr<base::Value> get_zoom_result( |
| + utils::RunFunctionAndReturnSingleResult( |
| + get_zoom_function.get(), base::StringPrintf("[%u]", tab_id), |
| + browser_)); |
| + |
|
not at google - send to devlin
2014/04/11 15:04:25
ASSERT_TRUE(get_zoom_result.get());
though an eve
paulmeyer
2014/04/11 19:09:49
Done.
|
| + get_zoom_result->GetAsDouble(zoom_factor); |
| + } |
| + |
| + // Runs chrome.tabs.setZoomSettings(). |
| + void RunSetZoomSettings(int tab_id, |
| + const char* mode, |
| + const char* scope) { |
| + scoped_refptr<TabsSetZoomSettingsFunction> set_zoom_settings_function( |
| + new TabsSetZoomSettingsFunction()); |
| + set_zoom_settings_function->set_extension(extension_); |
| + |
| + if (scope) { |
|
not at google - send to devlin
2014/04/11 15:04:25
instead, can you do like
std::string args;
if (sc
paulmeyer
2014/04/11 19:09:49
Done.
|
| + utils::RunFunction(set_zoom_settings_function.get(), |
| + base::StringPrintf("[%u, {\"mode\": \"%s\", " |
| + "\"scope\": \"%s\"}]", |
| + tab_id, mode, scope), |
| + browser_, extension_function_test_utils::NONE); |
| + } else { |
| + utils::RunFunction(set_zoom_settings_function.get(), |
| + base::StringPrintf("[%u, {\"mode\": \"%s\"}]", |
| + tab_id, mode), |
| + browser_, extension_function_test_utils::NONE); |
| + } |
| + } |
| + |
| + // Runs chrome.tabs.getZoomSettings(). |
| + void RunGetZoomSettings(int tab_id, std::string* mode, std::string* scope) { |
| + scoped_refptr<TabsGetZoomSettingsFunction> get_zoom_settings_function( |
| + new TabsGetZoomSettingsFunction()); |
| + get_zoom_settings_function->set_extension(extension_); |
| + get_zoom_settings_function->set_has_callback(true); |
| + |
| + scoped_ptr<base::DictionaryValue> get_zoom_settings_result( |
| + utils::ToDictionary(utils::RunFunctionAndReturnSingleResult( |
| + get_zoom_settings_function.get(), |
| + base::StringPrintf("[%u]", tab_id), |
| + browser_))); |
| + |
| + *mode = utils::GetString(get_zoom_settings_result.get(), "mode"); |
| + *scope = utils::GetString(get_zoom_settings_result.get(), "scope"); |
| + } |
| + |
| + // Runs chrome.tabs.setZoom(), expecting an error. |
| + void RunSetZoomExpectError(int tab_id, |
| + double zoom_factor, |
| + std::string* error) { |
| + scoped_refptr<TabsSetZoomFunction> set_zoom_function( |
| + new TabsSetZoomFunction()); |
| + set_zoom_function->set_extension(extension_); |
| + set_zoom_function->set_has_callback(true); |
| + |
| + *error = utils::RunFunctionAndReturnError( |
| + set_zoom_function.get(), |
| + base::StringPrintf("[%u, %lf]", tab_id, zoom_factor), |
| + browser_); |
| + } |
| + |
| + // Runs chrome.tabs.setZoomSettings(), expecting an error. |
| + void RunSetZoomSettingsExpectError(int tab_id, |
|
not at google - send to devlin
2014/04/11 15:04:25
ideally you'd just have a single general function
paulmeyer
2014/04/11 19:09:49
Yes, the structure doesn't allow for that.
On 201
|
| + const char* mode, |
| + const char* scope, |
| + std::string* error) { |
| + scoped_refptr<TabsSetZoomSettingsFunction> set_zoom_settings_function( |
| + new TabsSetZoomSettingsFunction()); |
| + set_zoom_settings_function->set_extension(extension_); |
| + |
| + *error = utils::RunFunctionAndReturnError( |
| + set_zoom_settings_function.get(), |
| + base::StringPrintf("[%u, {\"mode\": \"%s\", " |
| + "\"scope\": \"%s\"}]", |
| + tab_id, mode, scope), |
| + browser_); |
| + } |
| + |
| + private: |
| + Browser* browser_; |
| + scoped_refptr<Extension> extension_; |
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, SetAndGetZoom) { |
| + ZoomExtensionAPITestHelper helper(browser()); |
| + static const char kNewTestTabArgs[] = "about:blank"; |
|
not at google - send to devlin
2014/04/11 15:04:25
there isn't any advantage having this as "static".
paulmeyer
2014/04/11 19:09:49
Done.
|
| + content::OpenURLParams params(GURL(kNewTestTabArgs), content::Referrer(), |
| + NEW_FOREGROUND_TAB, |
| + content::PAGE_TRANSITION_LINK, false); |
| + content::WebContents* web_contents = browser()->OpenURL(params); |
| + int tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| + |
| + // Test chrome.tabs.setZoom(). |
| + helper.RunSetZoom(tab_id, 0.8); |
| + EXPECT_EQ(0.8, content::ZoomLevelToZoomFactor(web_contents->GetZoomLevel())); |
| + |
| + // Test chrome.tabs.getZoom(). |
| + double zoom_factor = -1; |
| + helper.RunGetZoom(tab_id, &zoom_factor); |
| + EXPECT_EQ(0.8, zoom_factor); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, ZoomSettings) { |
| + ZoomExtensionAPITestHelper helper(browser()); |
| + |
| + static const char kNewTestTabArgsA[] = "data:text/html,A"; |
| + static const char kNewTestTabArgsB[] = "data:text/html,B"; |
| + |
| + content::OpenURLParams params_A(GURL(kNewTestTabArgsA), content::Referrer(), |
| + NEW_FOREGROUND_TAB, |
| + content::PAGE_TRANSITION_LINK, false); |
| + content::OpenURLParams params_B(GURL(kNewTestTabArgsB), content::Referrer(), |
| + NEW_FOREGROUND_TAB, |
| + content::PAGE_TRANSITION_LINK, false); |
| + |
| + // Tabs A1 and A2 are navigated to the same origin, while B is navigated |
| + // to a different one. |
| + content::WebContents* web_contents_A1 = browser()->OpenURL(params_A); |
| + content::WebContents* web_contents_A2 = browser()->OpenURL(params_A); |
| + content::WebContents* web_contents_B = browser()->OpenURL(params_B); |
| + int tab_id_A1 = ExtensionTabUtil::GetTabId(web_contents_A1); |
| + int tab_id_A2 = ExtensionTabUtil::GetTabId(web_contents_A2); |
| + int tab_id_B = ExtensionTabUtil::GetTabId(web_contents_B); |
| + |
| + // Test per-origin automatic zoom settings. |
| + helper.RunSetZoom(tab_id_B, 1); |
| + helper.RunSetZoom(tab_id_A2, 1.1); |
| + EXPECT_EQ(1.1, |
| + content::ZoomLevelToZoomFactor(web_contents_A1->GetZoomLevel())); |
| + EXPECT_EQ(1.1, |
| + content::ZoomLevelToZoomFactor(web_contents_A2->GetZoomLevel())); |
| + EXPECT_EQ(1, |
| + content::ZoomLevelToZoomFactor(web_contents_B->GetZoomLevel())); |
| + |
| + // Test per-tab automatic zoom settings. |
| + helper.RunSetZoomSettings(tab_id_A1, "automatic", "per-tab"); |
| + helper.RunSetZoom(tab_id_A1, 1.2); |
| + EXPECT_EQ(1.2, |
| + content::ZoomLevelToZoomFactor(web_contents_A1->GetZoomLevel())); |
| + EXPECT_EQ(1.1, |
| + content::ZoomLevelToZoomFactor(web_contents_A2->GetZoomLevel())); |
| + |
| + // Test 'manual' mode. |
| + helper.RunSetZoomSettings(tab_id_A1, "manual", NULL); |
| + helper.RunSetZoom(tab_id_A1, 1.3); |
| + EXPECT_EQ(1.3, |
| + content::ZoomLevelToZoomFactor(web_contents_A1->GetZoomLevel())); |
| + EXPECT_EQ(1.1, |
| + content::ZoomLevelToZoomFactor(web_contents_A2->GetZoomLevel())); |
| + |
| + // Test 'disabled' mode. |
| + helper.RunSetZoomSettings(tab_id_A1, "disabled", NULL); |
| + helper.RunSetZoom(tab_id_A2, 1.4); |
| + EXPECT_EQ(1, |
| + content::ZoomLevelToZoomFactor(web_contents_A1->GetZoomLevel())); |
| + EXPECT_EQ(1.4, |
| + content::ZoomLevelToZoomFactor(web_contents_A2->GetZoomLevel())); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetZoomSettings) { |
| + ZoomExtensionAPITestHelper helper(browser()); |
| + static const char kNewTestTabArgs[] = "about:blank"; |
| + content::OpenURLParams params(GURL(kNewTestTabArgs), content::Referrer(), |
| + NEW_FOREGROUND_TAB, |
| + content::PAGE_TRANSITION_LINK, false); |
| + content::WebContents* web_contents = browser()->OpenURL(params); |
| + int tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| + |
| + helper.RunSetZoomSettings(tab_id, "automatic", "per-tab"); |
| + std::string mode; |
| + std::string scope; |
| + helper.RunGetZoomSettings(tab_id, &mode, &scope); |
| + |
| + EXPECT_EQ("automatic", mode); |
| + EXPECT_EQ("per-tab", scope); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, CannotZoomChromeURL) { |
| + ZoomExtensionAPITestHelper helper(browser()); |
| + static const char kNewTestTabArgs[] = "chrome://version"; |
| + content::OpenURLParams params(GURL(kNewTestTabArgs), content::Referrer(), |
| + NEW_FOREGROUND_TAB, |
| + content::PAGE_TRANSITION_LINK, false); |
| + content::WebContents* web_contents = browser()->OpenURL(params); |
| + int tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| + |
| + std::string error; |
| + |
| + // Test chrome.tabs.setZoom(). |
| + helper.RunSetZoomExpectError(tab_id, 3.14159, &error); |
| + EXPECT_TRUE(MatchPattern(error, keys::kCannotZoomChromePagesError)); |
| + |
| + // chrome.tabs.setZoomSettings(). |
| + helper.RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab", &error); |
| + EXPECT_TRUE(MatchPattern(error, |
| + keys::kCannotChangeChromePageZoomSettingsError)); |
| +} |
| + |
| } // namespace extensions |