| 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..540adbee84c92be9eeca5bbb36b97f2fec6dff66 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,236 @@ IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DuplicateTabNoPermission) {
|
| EXPECT_FALSE(utils::HasPrivacySensitiveFields(duplicate_result.get()));
|
| }
|
|
|
| +// Provides helper functions for testing the zoom extension API.
|
| +class ExtensionTabsZoomTest : public ExtensionTabsTest {
|
| + public:
|
| + void Init() {
|
| + 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().
|
| + bool 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()));
|
| +
|
| + if (!get_zoom_result.get())
|
| + return testing::AssertionFailure() << "no result";
|
| + if (!get_zoom_result->GetAsDouble(zoom_factor))
|
| + return testing::AssertionFailure() << "result was not a double";
|
| +
|
| + return true;
|
| + }
|
| +
|
| + // 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_);
|
| +
|
| + std:: string args;
|
| + if (scope) {
|
| + args = base::StringPrintf("[%u, {\"mode\": \"%s\", \"scope\": \"%s\"}]",
|
| + tab_id, mode, scope);
|
| + } else {
|
| + args = base::StringPrintf("[%u, {\"mode\": \"%s\"}]", tab_id, mode);
|
| + }
|
| +
|
| + utils::RunFunction(set_zoom_settings_function.get(), args,
|
| + browser(), extension_function_test_utils::NONE);
|
| + }
|
| +
|
| + // Runs chrome.tabs.getZoomSettings().
|
| + bool 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())));
|
| +
|
| + if (!get_zoom_settings_result.get())
|
| + return testing::AssertionFailure() << "no result";
|
| +
|
| + *mode = utils::GetString(get_zoom_settings_result.get(), "mode");
|
| + *scope = utils::GetString(get_zoom_settings_result.get(), "scope");
|
| +
|
| + return true;
|
| + }
|
| +
|
| + // 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,
|
| + 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:
|
| + scoped_refptr<Extension> extension_;
|
| +};
|
| +
|
| +IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, SetAndGetZoom) {
|
| + Init();
|
| + 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);
|
| +
|
| + // Test chrome.tabs.setZoom().
|
| + RunSetZoom(tab_id, 0.8);
|
| + EXPECT_EQ(0.8, content::ZoomLevelToZoomFactor(web_contents->GetZoomLevel()));
|
| +
|
| + // Test chrome.tabs.getZoom().
|
| + double zoom_factor = -1;
|
| + EXPECT_TRUE(RunGetZoom(tab_id, &zoom_factor));
|
| + EXPECT_EQ(0.8, zoom_factor);
|
| +}
|
| +
|
| +IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, ZoomSettings) {
|
| + Init();
|
| +
|
| + const char kNewTestTabArgsA[] = "data:text/html,A";
|
| + 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.
|
| + RunSetZoom(tab_id_B, 1);
|
| + 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.
|
| + RunSetZoomSettings(tab_id_A1, "automatic", "per-tab");
|
| + 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.
|
| + RunSetZoomSettings(tab_id_A1, "manual", NULL);
|
| + 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.
|
| + RunSetZoomSettings(tab_id_A1, "disabled", NULL);
|
| + 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(ExtensionTabsZoomTest, GetZoomSettings) {
|
| + Init();
|
| + 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);
|
| +
|
| + RunSetZoomSettings(tab_id, "automatic", "per-tab");
|
| + std::string mode;
|
| + std::string scope;
|
| + EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope));
|
| +
|
| + EXPECT_EQ("automatic", mode);
|
| + EXPECT_EQ("per-tab", scope);
|
| +}
|
| +
|
| +IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, CannotZoomChromeURL) {
|
| + Init();
|
| + 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().
|
| + RunSetZoomExpectError(tab_id, 3.14159, &error);
|
| + EXPECT_TRUE(MatchPattern(error, keys::kCannotZoomChromePagesError));
|
| +
|
| + // chrome.tabs.setZoomSettings().
|
| + RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab", &error);
|
| + EXPECT_TRUE(MatchPattern(error,
|
| + keys::kCannotChangeChromePageZoomSettingsError));
|
| +}
|
| +
|
| } // namespace extensions
|
|
|