Commit 6db683ca authored by kundaji's avatar kundaji Committed by Commit bot

Tests for "Save Image As..." when Data Saver is enabled.

Verify that request headers specify a passthrough when "Save Image As..."
feature is used with Data Saver enabled, and this header is not specified
with Data Saver disabled.

Override SaveFrameWithHeaders in TestWebContents to keep track of
the headers.

BUG=467163

Review URL: https://codereview.chromium.org/1137563002

Cr-Commit-Position: refs/heads/master@{#329900}
parent 5691d78e
...@@ -4,7 +4,10 @@ ...@@ -4,7 +4,10 @@
#include "chrome/browser/renderer_context_menu/render_view_context_menu.h" #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/pref_service.h" #include "base/prefs/pref_service.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h" #include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/custom_handlers/protocol_handler_registry.h" #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
...@@ -12,11 +15,19 @@ ...@@ -12,11 +15,19 @@
#include "chrome/browser/extensions/menu_manager_factory.h" #include "chrome/browser/extensions/menu_manager_factory.h"
#include "chrome/browser/extensions/test_extension_environment.h" #include "chrome/browser/extensions/test_extension_environment.h"
#include "chrome/browser/extensions/test_extension_prefs.h" #include "chrome/browser/extensions/test_extension_prefs.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h"
#include "chrome/browser/prefs/incognito_mode_prefs.h" #include "chrome/browser/prefs/incognito_mode_prefs.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h" #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h" #include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/test/web_contents_tester.h"
#include "extensions/browser/extension_prefs.h" #include "extensions/browser/extension_prefs.h"
#include "extensions/common/url_pattern.h" #include "extensions/common/url_pattern.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -345,6 +356,43 @@ class RenderViewContextMenuPrefsTest : public ChromeRenderViewHostTestHarness { ...@@ -345,6 +356,43 @@ class RenderViewContextMenuPrefsTest : public ChromeRenderViewHostTestHarness {
return menu; return menu;
} }
void SetupDataReductionProxy(bool enable_data_reduction_proxy) {
drp_test_context_ =
data_reduction_proxy::DataReductionProxyTestContext::Builder()
.WithParamsFlags(
data_reduction_proxy::DataReductionProxyParams::kAllowed |
data_reduction_proxy::DataReductionProxyParams::
kFallbackAllowed |
data_reduction_proxy::DataReductionProxyParams::kPromoAllowed)
.WithMockConfig()
.SkipSettingsInitialization()
.Build();
DataReductionProxyChromeSettings* settings =
DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
profile());
// TODO(bengr): Remove prefs::kProxy registration after M46. See
// http://crbug.com/445599.
PrefRegistrySimple* registry =
drp_test_context_->pref_service()->registry();
registry->RegisterDictionaryPref(prefs::kProxy);
drp_test_context_->pref_service()->SetBoolean(
data_reduction_proxy::prefs::kDataReductionProxyEnabled,
enable_data_reduction_proxy);
drp_test_context_->InitSettings();
settings->InitDataReductionProxySettings(
drp_test_context_->io_data(), drp_test_context_->pref_service(),
drp_test_context_->request_context_getter(),
base::MessageLoopProxy::current());
}
protected:
scoped_ptr<data_reduction_proxy::DataReductionProxyTestContext>
drp_test_context_;
private: private:
scoped_ptr<ProtocolHandlerRegistry> registry_; scoped_ptr<ProtocolHandlerRegistry> registry_;
}; };
...@@ -378,3 +426,44 @@ TEST_F(RenderViewContextMenuPrefsTest, ...@@ -378,3 +426,44 @@ TEST_F(RenderViewContextMenuPrefsTest,
EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_CUSTOM_FIRST)); EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_CUSTOM_FIRST));
} }
// Verify that request headers specify that data reduction proxy should return
// the original non compressed resource when "Save Image As..." is used with
// Data Saver enabled.
TEST_F(RenderViewContextMenuPrefsTest, DataSaverEnabledSaveImageAs) {
SetupDataReductionProxy(true);
content::ContextMenuParams params = CreateParams(MenuItem::IMAGE);
params.unfiltered_link_url = params.link_url;
content::WebContents* wc = web_contents();
scoped_ptr<TestRenderViewContextMenu> menu(
new TestRenderViewContextMenu(wc->GetMainFrame(), params));
menu->ExecuteCommand(IDC_CONTENT_CONTEXT_SAVEIMAGEAS, 0);
const std::string& headers =
content::WebContentsTester::For(web_contents())->GetSaveFrameHeaders();
EXPECT_TRUE(headers.find("X-PSA-Client-Options: v=1,m=1") !=
std::string::npos &&
headers.find("Cache-Control: no-cache") != std::string::npos);
}
// Verify that request headers do not specify pass through when "Save Image
// As..." is used with Data Saver disabled.
TEST_F(RenderViewContextMenuPrefsTest, DataSaverDisabledSaveImageAs) {
SetupDataReductionProxy(false);
content::ContextMenuParams params = CreateParams(MenuItem::IMAGE);
params.unfiltered_link_url = params.link_url;
content::WebContents* wc = web_contents();
scoped_ptr<TestRenderViewContextMenu> menu(
new TestRenderViewContextMenu(wc->GetMainFrame(), params));
menu->ExecuteCommand(IDC_CONTENT_CONTEXT_SAVEIMAGEAS, 0);
const std::string& headers =
content::WebContentsTester::For(web_contents())->GetSaveFrameHeaders();
EXPECT_TRUE(headers.find("X-PSA-Client-Options: v=1,m=1") ==
std::string::npos &&
headers.find("Cache-Control: no-cache") == std::string::npos);
}
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef CONTENT_PUBLIC_TEST_WEB_CONTENTS_TESTER_H_ #ifndef CONTENT_PUBLIC_TEST_WEB_CONTENTS_TESTER_H_
#define CONTENT_PUBLIC_TEST_WEB_CONTENTS_TESTER_H_ #define CONTENT_PUBLIC_TEST_WEB_CONTENTS_TESTER_H_
#include <string>
#include "ui/base/page_transition_types.h" #include "ui/base/page_transition_types.h"
class GURL; class GURL;
...@@ -100,6 +102,10 @@ class WebContentsTester { ...@@ -100,6 +102,10 @@ class WebContentsTester {
const GURL& url, const GURL& url,
const Referrer& referrer, const Referrer& referrer,
ui::PageTransition transition) = 0; ui::PageTransition transition) = 0;
// Returns headers that were passed in the previous SaveFrameWithHeaders(...)
// call.
virtual const std::string& GetSaveFrameHeaders() = 0;
}; };
} // namespace content } // namespace content
......
...@@ -111,6 +111,10 @@ void TestWebContents::TestDidNavigateWithReferrer( ...@@ -111,6 +111,10 @@ void TestWebContents::TestDidNavigateWithReferrer(
rfhi->frame_tree_node()->navigator()->DidNavigate(rfhi, params); rfhi->frame_tree_node()->navigator()->DidNavigate(rfhi, params);
} }
const std::string& TestWebContents::GetSaveFrameHeaders() {
return save_frame_headers_;
}
bool TestWebContents::CrossProcessNavigationPending() { bool TestWebContents::CrossProcessNavigationPending() {
if (base::CommandLine::ForCurrentProcess()->HasSwitch( if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableBrowserSideNavigation)) { switches::kEnableBrowserSideNavigation)) {
...@@ -285,4 +289,10 @@ void TestWebContents::ShowCreatedWidget(int route_id, ...@@ -285,4 +289,10 @@ void TestWebContents::ShowCreatedWidget(int route_id,
void TestWebContents::ShowCreatedFullscreenWidget(int route_id) { void TestWebContents::ShowCreatedFullscreenWidget(int route_id) {
} }
void TestWebContents::SaveFrameWithHeaders(const GURL& url,
const Referrer& referrer,
const std::string& headers) {
save_frame_headers_ = headers;
}
} // namespace content } // namespace content
...@@ -5,12 +5,16 @@ ...@@ -5,12 +5,16 @@
#ifndef CONTENT_TEST_TEST_WEB_CONTENTS_H_ #ifndef CONTENT_TEST_TEST_WEB_CONTENTS_H_
#define CONTENT_TEST_TEST_WEB_CONTENTS_H_ #define CONTENT_TEST_TEST_WEB_CONTENTS_H_
#include <string>
#include "content/browser/web_contents/web_contents_impl.h" #include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/test/web_contents_tester.h" #include "content/public/test/web_contents_tester.h"
#include "content/test/test_render_frame_host.h" #include "content/test/test_render_frame_host.h"
#include "content/test/test_render_view_host.h" #include "content/test/test_render_view_host.h"
#include "ui/base/page_transition_types.h" #include "ui/base/page_transition_types.h"
class GURL;
class Referrer;
class SiteInstanceImpl; class SiteInstanceImpl;
namespace content { namespace content {
...@@ -51,6 +55,7 @@ class TestWebContents : public WebContentsImpl, public WebContentsTester { ...@@ -51,6 +55,7 @@ class TestWebContents : public WebContentsImpl, public WebContentsTester {
const GURL& url, const GURL& url,
const Referrer& referrer, const Referrer& referrer,
ui::PageTransition transition) override; ui::PageTransition transition) override;
const std::string& GetSaveFrameHeaders() override;
// True if a cross-site navigation is pending. // True if a cross-site navigation is pending.
bool CrossProcessNavigationPending(); bool CrossProcessNavigationPending();
...@@ -116,6 +121,9 @@ class TestWebContents : public WebContentsImpl, public WebContentsTester { ...@@ -116,6 +121,9 @@ class TestWebContents : public WebContentsImpl, public WebContentsTester {
bool user_gesture) override; bool user_gesture) override;
void ShowCreatedWidget(int route_id, const gfx::Rect& initial_rect) override; void ShowCreatedWidget(int route_id, const gfx::Rect& initial_rect) override;
void ShowCreatedFullscreenWidget(int route_id) override; void ShowCreatedFullscreenWidget(int route_id) override;
void SaveFrameWithHeaders(const GURL& url,
const Referrer& referrer,
const std::string& headers) override;
RenderViewHostDelegateView* delegate_view_override_; RenderViewHostDelegateView* delegate_view_override_;
...@@ -123,6 +131,7 @@ class TestWebContents : public WebContentsImpl, public WebContentsTester { ...@@ -123,6 +131,7 @@ class TestWebContents : public WebContentsImpl, public WebContentsTester {
bool expect_set_history_offset_and_length_; bool expect_set_history_offset_and_length_;
int expect_set_history_offset_and_length_history_offset_; int expect_set_history_offset_and_length_history_offset_;
int expect_set_history_offset_and_length_history_length_; int expect_set_history_offset_and_length_history_length_;
std::string save_frame_headers_;
}; };
} // namespace content } // namespace content
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment