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 7e1e7c6f3ce25c27c16ec06435144b3bae2960ea..ef084794457e3fbc203393e29fb3c9841a45ebf8 100644 |
| --- a/chrome/browser/extensions/api/tabs/tabs_test.cc |
| +++ b/chrome/browser/extensions/api/tabs/tabs_test.cc |
| @@ -20,8 +20,10 @@ |
| #include "chrome/browser/ui/browser.h" |
| #include "chrome/browser/ui/browser_commands.h" |
| #include "chrome/browser/ui/browser_window.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.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 "content/public/common/url_constants.h" |
| #include "ui/gfx/rect.h" |
| @@ -591,4 +593,257 @@ IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DuplicateTabNoPermission) { |
| EXPECT_FALSE(utils::HasPrivacySensitiveFields(duplicate_result.get())); |
| } |
| +// Provides helper functions for testing the zoom extension API. |
|
Devlin
2014/06/16 19:04:48
nit: Please change this to something like "Tester
wjmaclean
2014/06/18 19:03:53
Done.
|
| +class ExtensionTabsZoomTest : public ExtensionTabsTest { |
|
Devlin
2014/06/16 19:04:49
It seems odd that we subclass an empty subclass.
wjmaclean
2014/06/18 19:03:52
Agreed. However, I assume other tests in this same
|
| + public: |
| + void Init() { |
|
Devlin
2014/06/16 19:04:48
Why not do this in Test::SetUp()?
wjmaclean
2014/06/18 19:03:53
Done.
|
| + extension_ = utils::CreateEmptyExtension(); |
| + } |
| + |
| + // Runs chrome.tabs.setZoom(). |
| + void RunSetZoom(int tab_id, double zoom_factor) { |
|
Devlin
2014/06/16 19:04:48
This is personal opinion, but for large tester cla
wjmaclean
2014/06/18 19:03:53
Done.
|
| + 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); |
|
Devlin
2014/06/16 19:04:48
nit: One argument per line.
wjmaclean
2014/06/18 19:03:53
Done.
|
| + content::RunAllPendingInMessageLoop(); |
|
Devlin
2014/06/16 19:04:48
RunFuction() should be synchronous. Is there anot
wjmaclean
2014/06/18 19:03:53
Removed. This was added during a debugging phase,
|
| + } |
| + |
| + // Runs chrome.tabs.getZoom(). |
| + bool RunGetZoom(int tab_id, double* zoom_factor) { |
|
Devlin
2014/06/16 19:04:48
Please make this return a testing::AssertionResult
wjmaclean
2014/06/18 19:03:53
Done.
|
| + 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, |
|
Devlin
2014/06/16 19:04:48
nit: One per line.
wjmaclean
2014/06/18 19:03:52
Done.
|
| + browser(), extension_function_test_utils::NONE); |
| + content::RunAllPendingInMessageLoop(); |
| + } |
| + |
| + // Runs chrome.tabs.getZoomSettings(). |
| + bool RunGetZoomSettings(int tab_id, std::string* mode, std::string* scope) { |
|
Devlin
2014/06/16 19:04:48
Return testing::AssertionResult.
Devlin
2014/06/16 19:04:48
Please DCHECK string and scope.
wjmaclean
2014/06/18 19:03:53
Done.
wjmaclean
2014/06/18 19:03:53
Done.
|
| + 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, |
|
Devlin
2014/06/16 19:04:48
Both of the ExpectError functions are only called
wjmaclean
2014/06/18 19:03:53
Seemed too good to just use once ;-) Have added mo
|
| + 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()); |
| + } |
| + |
| + content::WebContents* OpenUrlAndWaitForLoad(const GURL& url) { |
| + ui_test_utils::NavigateToURLWithDisposition( |
| + browser(), url, NEW_FOREGROUND_TAB, |
| + ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| + return browser()->tab_strip_model()->GetActiveWebContents(); |
| + } |
| + |
| + private: |
| + scoped_refptr<Extension> extension_; |
|
Devlin
2014/06/16 19:04:48
Thanks for making this private :) - lots of tests
wjmaclean
2014/06/18 19:03:53
Paul did this, so I won't claim to take credit for
|
| +}; |
| + |
| +namespace { |
| +double GetZoomLevel(const content::WebContents* web_contents) { |
| + return ZoomController::FromWebContents(web_contents)->GetZoomLevel(); |
| +} |
| +} // namespace |
| + |
| +IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, SetAndGetZoom) { |
| + Init(); |
| + const char kNewTestTabArgs[] = "about:blank"; |
| + content::OpenURLParams params(GURL(kNewTestTabArgs), content::Referrer(), |
|
Devlin
2014/06/16 19:04:48
one per line.
wjmaclean
2014/06/18 19:03:53
Done.
|
| + 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); |
|
Devlin
2014/06/16 19:04:48
Please const this, i.e.
const double kZoomLevel =
wjmaclean
2014/06/18 19:03:53
Done.
|
| + EXPECT_EQ(0.8, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents))); |
| + |
| + // Test chrome.tabs.getZoom(). |
|
Devlin
2014/06/16 19:04:48
Maybe also test this before it's set, just for com
wjmaclean
2014/06/18 19:03:53
Done.
|
| + 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 = OpenUrlAndWaitForLoad(params_A.url); |
| + content::WebContents* web_contents_A2 = OpenUrlAndWaitForLoad(params_A.url); |
| + content::WebContents* web_contents_B = OpenUrlAndWaitForLoad(params_B.url); |
| + |
| + 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.f); |
| + RunSetZoom(tab_id_A2, 1.1f); |
| + EXPECT_FLOAT_EQ( |
| + 1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1))); |
| + EXPECT_FLOAT_EQ( |
| + 1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2))); |
| + EXPECT_FLOAT_EQ(1.f, |
|
Devlin
2014/06/16 19:04:48
maybe ASSERT_FLOAT_EQ this before any set zooms ar
wjmaclean
2014/06/18 19:03:53
Done.
|
| + content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_B))); |
| + |
| + // Test per-tab automatic zoom settings. |
| + RunSetZoomSettings(tab_id_A1, "automatic", "per-tab"); |
| + RunSetZoom(tab_id_A1, 1.2f); |
| + EXPECT_FLOAT_EQ( |
| + 1.2f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1))); |
| + EXPECT_FLOAT_EQ( |
| + 1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2))); |
| + |
| + // Test 'manual' mode. |
| + RunSetZoomSettings(tab_id_A1, "manual", NULL); |
| + RunSetZoom(tab_id_A1, 1.3f); |
| + EXPECT_FLOAT_EQ( |
| + 1.3f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1))); |
| + EXPECT_FLOAT_EQ( |
| + 1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2))); |
| + |
| + // Test 'disabled' mode, which will reset A1's zoom to 1.f. |
| + RunSetZoomSettings(tab_id_A1, "disabled", NULL); |
| + RunSetZoom(tab_id_A2, 1.4f); |
| + EXPECT_FLOAT_EQ( |
| + 1.f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1))); |
| + EXPECT_FLOAT_EQ( |
| + 1.4f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2))); |
| +} |
| + |
| +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); |
| + |
| + std::string mode; |
| + std::string scope; |
| + |
| + EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope)); |
| + EXPECT_EQ("automatic", mode); |
| + EXPECT_EQ("per-origin", scope); |
| + |
| + RunSetZoomSettings(tab_id, "automatic", "per-tab"); |
| + 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 |