| Index: chrome/browser/predictors/resource_prefetch_predictor_browsertest.cc
|
| diff --git a/chrome/browser/predictors/resource_prefetch_predictor_browsertest.cc b/chrome/browser/predictors/resource_prefetch_predictor_browsertest.cc
|
| index 872022fca4fee41414c8e54cad337b42bfd33aae..44f7cd77e631dacc40df2f8c7ca7510141e3a55c 100644
|
| --- a/chrome/browser/predictors/resource_prefetch_predictor_browsertest.cc
|
| +++ b/chrome/browser/predictors/resource_prefetch_predictor_browsertest.cc
|
| @@ -258,17 +258,30 @@ class LearningObserver : public TestObserver {
|
| };
|
|
|
| // Helper class to track and allow waiting for a single OnPrefetchingFinished
|
| -// event. No learning events should be fired while this observer is active.
|
| +// event. Checks also that {Start,Stop}Prefetching are called with the right
|
| +// argument.
|
| class PrefetchingObserver : public TestObserver {
|
| public:
|
| PrefetchingObserver(ResourcePrefetchPredictor* predictor,
|
| - const GURL& expected_main_frame_url)
|
| - : TestObserver(predictor), main_frame_url_(expected_main_frame_url) {}
|
| + const GURL& expected_main_frame_url,
|
| + bool is_learning_allowed)
|
| + : TestObserver(predictor),
|
| + main_frame_url_(expected_main_frame_url),
|
| + is_learning_allowed_(is_learning_allowed) {}
|
|
|
| // TestObserver:
|
| void OnNavigationLearned(size_t url_visit_count,
|
| const PageRequestSummary& summary) override {
|
| - ADD_FAILURE() << "Prefetching shouldn't activate learning";
|
| + if (!is_learning_allowed_)
|
| + ADD_FAILURE() << "Prefetching shouldn't activate learning";
|
| + }
|
| +
|
| + void OnPrefetchingStarted(const GURL& main_frame_url) override {
|
| + EXPECT_EQ(main_frame_url_, main_frame_url);
|
| + }
|
| +
|
| + void OnPrefetchingStopped(const GURL& main_frame_url) override {
|
| + EXPECT_EQ(main_frame_url_, main_frame_url);
|
| }
|
|
|
| void OnPrefetchingFinished(const GURL& main_frame_url) override {
|
| @@ -281,6 +294,7 @@ class PrefetchingObserver : public TestObserver {
|
| private:
|
| base::RunLoop run_loop_;
|
| GURL main_frame_url_;
|
| + bool is_learning_allowed_;
|
|
|
| DISALLOW_COPY_AND_ASSIGN(PrefetchingObserver);
|
| };
|
| @@ -381,8 +395,18 @@ class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest {
|
| navigation_id_history_.insert(nav);
|
| }
|
|
|
| + void NavigateToURLAndCheckPrefetching(const GURL& main_frame_url) {
|
| + PrefetchingObserver observer(predictor_, main_frame_url, true);
|
| + ui_test_utils::NavigateToURL(browser(), main_frame_url);
|
| + observer.Wait();
|
| + for (auto& kv : resources_) {
|
| + if (kv.second.is_observable)
|
| + kv.second.request.was_cached = true;
|
| + }
|
| + }
|
| +
|
| void PrefetchURL(const GURL& main_frame_url) {
|
| - PrefetchingObserver observer(predictor_, main_frame_url);
|
| + PrefetchingObserver observer(predictor_, main_frame_url, false);
|
| predictor_->StartPrefetching(main_frame_url, PrefetchOrigin::EXTERNAL);
|
| observer.Wait();
|
| for (auto& kv : resources_) {
|
| @@ -637,6 +661,21 @@ class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest {
|
| std::set<NavigationID> navigation_id_history_;
|
| };
|
|
|
| +// Subclass to test PrefetchOrigin::NAVIGATION.
|
| +class ResourcePrefetchPredictorPrefetchingBrowserTest
|
| + : public ResourcePrefetchPredictorBrowserTest {
|
| + protected:
|
| + void SetUpCommandLine(base::CommandLine* command_line) override {
|
| + command_line->AppendSwitchASCII("force-fieldtrials", "trial/group");
|
| + std::string parameter = base::StringPrintf(
|
| + "trial.group:%s/%s", kModeParamName, kPrefetchingMode);
|
| + command_line->AppendSwitchASCII("force-fieldtrial-params", parameter);
|
| + std::string enabled_feature = base::StringPrintf(
|
| + "%s<trial", kSpeculativeResourcePrefetchingFeatureName);
|
| + command_line->AppendSwitchASCII("enable-features", enabled_feature);
|
| + }
|
| +};
|
| +
|
| IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Simple) {
|
| // These resources have default priorities that correspond to
|
| // blink::typeToPriority function.
|
| @@ -899,4 +938,43 @@ IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest,
|
| NavigateToURLAndCheckSubresourcesAllCached(GetURL(kHtmlSubresourcesPath));
|
| }
|
|
|
| +// Makes sure that {Stop,Start}Prefetching are called with the same argument.
|
| +IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorPrefetchingBrowserTest,
|
| + Simple) {
|
| + AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST);
|
| + AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET,
|
| + net::HIGHEST);
|
| + AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
|
| + AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE,
|
| + net::HIGHEST);
|
| +
|
| + GURL main_frame_url = GetURL(kHtmlSubresourcesPath);
|
| + NavigateToURLAndCheckSubresources(main_frame_url);
|
| + ClearCache();
|
| + NavigateToURLAndCheckSubresources(main_frame_url);
|
| + ClearCache();
|
| + NavigateToURLAndCheckPrefetching(main_frame_url);
|
| +}
|
| +
|
| +// Makes sure that {Stop,Start}Prefetching are called with the same argument in
|
| +// presence of redirect.
|
| +IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorPrefetchingBrowserTest,
|
| + Redirect) {
|
| + GURL initial_url = embedded_test_server()->GetURL(kFooHost, kRedirectPath);
|
| + AddRedirectChain(initial_url, {{net::HTTP_MOVED_PERMANENTLY,
|
| + GetURL(kHtmlSubresourcesPath)}});
|
| + AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST);
|
| + AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET,
|
| + net::HIGHEST);
|
| + AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
|
| + AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE,
|
| + net::HIGHEST);
|
| +
|
| + NavigateToURLAndCheckSubresources(initial_url);
|
| + ClearCache();
|
| + NavigateToURLAndCheckSubresources(initial_url);
|
| + ClearCache();
|
| + NavigateToURLAndCheckPrefetching(initial_url);
|
| +}
|
| +
|
| } // namespace predictors
|
|
|