Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1691)

Unified Diff: chrome/browser/prefetch/prefetch_browsertest.cc

Issue 459933002: Add prefetch behavior test. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/prefetch/prefetch_browsertest.cc
diff --git a/chrome/browser/prefetch/prefetch_browsertest.cc b/chrome/browser/prefetch/prefetch_browsertest.cc
index bd8ba5b0a89f37e40af0e911094ae62e55b4807c..0f371fc86a5b18472eefa26966780cf50b3ad3f3 100644
--- a/chrome/browser/prefetch/prefetch_browsertest.cc
+++ b/chrome/browser/prefetch/prefetch_browsertest.cc
@@ -6,6 +6,7 @@
#include "base/prefs/pref_service.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
+#include "chrome/browser/net/prediction_options.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
@@ -17,39 +18,50 @@
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
+#include "net/base/network_change_notifier.h"
#include "net/url_request/url_request_filter.h"
#include "net/url_request/url_request_job.h"
+using chrome_browser_net::NetworkPredictionOptions;
using content::BrowserThread;
+using net::NetworkChangeNotifier;
namespace {
const char kPrefetchPage[] = "files/prerender/simple_prefetch.html";
+class MockNetworkChangeNotifierWIFI : public NetworkChangeNotifier {
+ public:
+ virtual ConnectionType GetCurrentConnectionType() const OVERRIDE {
+ return NetworkChangeNotifier::CONNECTION_WIFI;
+ }
+};
+
+class MockNetworkChangeNotifier4G : public NetworkChangeNotifier {
+ public:
+ virtual ConnectionType GetCurrentConnectionType() const OVERRIDE {
+ return NetworkChangeNotifier::CONNECTION_4G;
+ }
+};
+
class PrefetchBrowserTestBase : public InProcessBrowserTest {
public:
- explicit PrefetchBrowserTestBase(bool do_predictive_networking,
- bool do_prefetch_field_trial)
- : do_predictive_networking_(do_predictive_networking),
- do_prefetch_field_trial_(do_prefetch_field_trial) {}
+ explicit PrefetchBrowserTestBase(bool disabled_via_field_trial)
+ : disabled_via_field_trial_(disabled_via_field_trial) {}
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
- if (do_prefetch_field_trial_) {
+ if (disabled_via_field_trial_) {
command_line->AppendSwitchASCII(switches::kForceFieldTrials,
"Prefetch/ExperimentDisabled/");
- } else {
- command_line->AppendSwitchASCII(switches::kForceFieldTrials,
- "Prefetch/ExperimentEnabled/");
}
}
- virtual void SetUpOnMainThread() OVERRIDE {
- browser()->profile()->GetPrefs()->SetBoolean(
- prefs::kNetworkPredictionEnabled, do_predictive_networking_);
+ void SetPreference(NetworkPredictionOptions value) {
+ browser()->profile()->GetPrefs()->SetInteger(
+ prefs::kNetworkPredictionOptions, value);
}
bool RunPrefetchExperiment(bool expect_success, Browser* browser) {
- CHECK(test_server()->Start());
GURL url = test_server()->GetURL(kPrefetchPage);
const base::string16 expected_title =
@@ -62,32 +74,17 @@ class PrefetchBrowserTestBase : public InProcessBrowserTest {
}
private:
- bool do_predictive_networking_;
- bool do_prefetch_field_trial_;
-};
-
-class PrefetchBrowserTestPredictionOnExpOn : public PrefetchBrowserTestBase {
- public:
- PrefetchBrowserTestPredictionOnExpOn()
- : PrefetchBrowserTestBase(true, true) {}
-};
-
-class PrefetchBrowserTestPredictionOnExpOff : public PrefetchBrowserTestBase {
- public:
- PrefetchBrowserTestPredictionOnExpOff()
- : PrefetchBrowserTestBase(true, false) {}
+ bool disabled_via_field_trial_;
};
-class PrefetchBrowserTestPredictionOffExpOn : public PrefetchBrowserTestBase {
+class PrefetchBrowserTestPrediction : public PrefetchBrowserTestBase {
public:
- PrefetchBrowserTestPredictionOffExpOn()
- : PrefetchBrowserTestBase(false, true) {}
+ PrefetchBrowserTestPrediction() : PrefetchBrowserTestBase(false) {}
};
-class PrefetchBrowserTestPredictionOffExpOff : public PrefetchBrowserTestBase {
+class PrefetchBrowserTestPredictionDisabled : public PrefetchBrowserTestBase {
public:
- PrefetchBrowserTestPredictionOffExpOff()
- : PrefetchBrowserTestBase(false, false) {}
+ PrefetchBrowserTestPredictionDisabled() : PrefetchBrowserTestBase(true) {}
};
// URLRequestJob (and associated handler) which hangs.
@@ -134,29 +131,59 @@ void CreateHangingRequestInterceptorOnIO(const GURL& url,
url, never_respond_handler.Pass());
}
-// Privacy option is on, experiment is on. Prefetch should succeed.
-IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPredictionOnExpOn, PredOnExpOn) {
+// Prefetch is disabled via field experiment. Prefetch should be dropped.
+IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPredictionDisabled,
+ ExperimentDisabled) {
+ CHECK(test_server()->Start());
+ EXPECT_TRUE(RunPrefetchExperiment(false, browser()));
+ // Should not prefetch even if preference is ALWAYS.
+ SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_ALWAYS);
EXPECT_TRUE(RunPrefetchExperiment(false, browser()));
}
-// Privacy option is on, experiment is off. Prefetch should be dropped.
-IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPredictionOnExpOff, PredOnExpOff) {
- EXPECT_TRUE(RunPrefetchExperiment(true, browser()));
-}
+// Prefetch should be allowed depending on preference and network type.
+IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPrediction, PreferenceWorks) {
+ CHECK(test_server()->Start());
+ // Set real NetworkChangeNotifier singleton aside.
+ scoped_ptr<NetworkChangeNotifier::DisableForTest> disable_for_test(
+ new NetworkChangeNotifier::DisableForTest);
+
+ // Preference defaults to WIFI_ONLY: prefetch when not on cellular.
+ {
+ scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI);
+ EXPECT_TRUE(RunPrefetchExperiment(true, browser()));
+ }
+ {
+ scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G);
+ EXPECT_TRUE(RunPrefetchExperiment(false, browser()));
+ }
-// Privacy option is off, experiment is on. Prefetch should be dropped.
-IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPredictionOffExpOn, PredOffExpOn) {
- EXPECT_TRUE(RunPrefetchExperiment(false, browser()));
-}
+ // Set preference to ALWAYS: always prefetch.
+ SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_ALWAYS);
+ {
+ scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI);
+ EXPECT_TRUE(RunPrefetchExperiment(true, browser()));
+ }
+ {
+ scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G);
+ EXPECT_TRUE(RunPrefetchExperiment(true, browser()));
+ }
-// Privacy option is off, experiment is off. Prefetch should be dropped.
-IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPredictionOffExpOff, PredOffExpOff) {
- EXPECT_TRUE(RunPrefetchExperiment(false, browser()));
+ // Set preference to NEVER: never prefetch.
+ SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_NEVER);
+ {
+ scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI);
+ EXPECT_TRUE(RunPrefetchExperiment(false, browser()));
+ }
+ {
+ scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G);
+ EXPECT_TRUE(RunPrefetchExperiment(false, browser()));
+ }
}
// Bug 339909: When in incognito mode the browser crashed due to an
// uninitialized preference member. Verify that it no longer does.
-IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPredictionOnExpOff, IncognitoTest) {
+IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPrediction, IncognitoTest) {
Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
Browser* incognito_browser = new Browser(
Browser::CreateParams(incognito_profile, browser()->host_desktop_type()));
@@ -165,6 +192,7 @@ IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPredictionOnExpOff, IncognitoTest) {
// WebContents for the incognito browser.
ui_test_utils::OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
+ CHECK(test_server()->Start());
EXPECT_TRUE(RunPrefetchExperiment(true, incognito_browser));
}
@@ -173,8 +201,7 @@ IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPredictionOnExpOff, IncognitoTest) {
// - if a prefetch is in progress, but the originating renderer is destroyed,
// that the pending prefetch request is cleaned up cleanly and does not
// result in a crash.
-IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPredictionOnExpOff,
- PrefetchFromBrowser) {
+IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPrediction, PrefetchFromBrowser) {
const GURL kHangingUrl("http://hanging-url.com");
base::RunLoop loop_;
BrowserThread::PostTask(BrowserThread::IO,
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698