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

Unified Diff: chrome/browser/predictors/loading_predictor_unittest.cc

Issue 2887133003: predictors: Refactor resource_prefetch_predictor triggering. (Closed)
Patch Set: . Created 3 years, 7 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
Index: chrome/browser/predictors/loading_predictor_unittest.cc
diff --git a/chrome/browser/predictors/loading_predictor_unittest.cc b/chrome/browser/predictors/loading_predictor_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3f0859d7879c67f7d9c8a4e15e15bf5f39b26f49
--- /dev/null
+++ b/chrome/browser/predictors/loading_predictor_unittest.cc
@@ -0,0 +1,147 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/predictors/loading_predictor.h"
+
+#include <memory>
+#include <string>
+
+#include "base/run_loop.h"
+#include "base/test/histogram_tester.h"
+#include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h"
+#include "chrome/test/base/testing_profile.h"
+#include "content/public/test/test_browser_thread_bundle.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace predictors {
+
+class LoadingPredictorTest : public testing::Test {
+ public:
+ LoadingPredictorTest();
+ ~LoadingPredictorTest() override;
+ void SetUp() override;
+ void TearDown() override;
+
+ protected:
+ std::unique_ptr<LoadingPredictor> predictor_;
+ content::TestBrowserThreadBundle thread_bundle_;
alexilin 2017/05/30 16:19:14 Nit: thread_bundle_ must be the first member. http
Benoit L 2017/05/31 14:34:42 Thanks! Done.
+ std::unique_ptr<TestingProfile> profile_;
+};
+
+LoadingPredictorTest::LoadingPredictorTest()
+ : profile_(base::MakeUnique<TestingProfile>()) {}
+
+LoadingPredictorTest::~LoadingPredictorTest() {
+ profile_ = nullptr;
+ base::RunLoop().RunUntilIdle();
+}
+
+void LoadingPredictorTest::SetUp() {
+ LoadingPredictorConfig config;
+ PopulateTestConfig(&config);
+ predictor_ = base::MakeUnique<LoadingPredictor>(config, profile_.get());
+ predictor_->StartInitialization();
+ base::RunLoop loop;
alexilin 2017/05/30 16:19:14 tiny nit: Why not base::RunLoop.RunUntilIdle() as
Benoit L 2017/05/31 14:34:42 Done.
+ loop.RunUntilIdle(); // Runs the DB lookup.
+}
+
+void LoadingPredictorTest::TearDown() {
+ predictor_ = nullptr;
+ profile_->DestroyHistoryService();
+}
+
+TEST_F(LoadingPredictorTest, TestPrefetchingDurationHistogram) {
+ base::HistogramTester histogram_tester;
+
+ const GURL url = GURL("http://www.google.com/cats");
+ const GURL url2 = GURL("http://www.notgoogle.com/dogs");
+
+ predictor_->PrepareForPageLoad(url, HintOrigin::EXTERNAL);
+ predictor_->CancelPageLoadHint(url);
+ histogram_tester.ExpectTotalCount(
+ internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 1);
+
+ // Mismatched start / end.
+ predictor_->PrepareForPageLoad(url, HintOrigin::EXTERNAL);
+ predictor_->CancelPageLoadHint(url2);
+ // No increment.
+ histogram_tester.ExpectTotalCount(
+ internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 1);
+
+ // Can track a navigation (url2) while one is still in progress (url).
+ predictor_->PrepareForPageLoad(url2, HintOrigin::EXTERNAL);
+ predictor_->CancelPageLoadHint(url2);
+ histogram_tester.ExpectTotalCount(
+ internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 2);
+}
+
+TEST_F(LoadingPredictorTest, TestMainFrameResponseCancelHint) {
alexilin 2017/05/30 16:19:14 nit: s/Cancel/Cancels/
Benoit L 2017/05/31 14:34:42 Done.
+ const GURL url = GURL("http://www.google.com/cats");
+
+ predictor_->PrepareForPageLoad(url, HintOrigin::EXTERNAL);
+ EXPECT_EQ(1UL, predictor_->active_hints_.size());
+
+ auto summary = CreateURLRequestSummary(12, url.spec());
+ predictor_->OnMainFrameResponse(summary);
+ EXPECT_TRUE(predictor_->active_hints_.empty());
+}
+
+TEST_F(LoadingPredictorTest, TestMainFrameRequestCancelStaleNavigations) {
alexilin 2017/05/30 16:19:14 ditto
Benoit L 2017/05/31 14:34:42 Done.
+ const std::string url = "http://www.google.com/cats";
+ const std::string url2 = "http://www.notgoogle.com/dogs";
+ const int tab_id = 12;
+ const auto& active_navigations = predictor_->active_navigations_;
+ const auto& active_hints = predictor_->active_hints_;
+
+ auto summary = CreateURLRequestSummary(tab_id, url);
+ auto navigation_id = CreateNavigationID(tab_id, url);
+
+ predictor_->OnMainFrameRequest(summary);
+ EXPECT_NE(active_navigations.find(navigation_id), active_navigations.end());
+ EXPECT_NE(active_hints.find(GURL(url)), active_hints.end());
+
+ summary = CreateURLRequestSummary(tab_id, url2);
+ predictor_->OnMainFrameRequest(summary);
+ EXPECT_EQ(active_navigations.find(navigation_id), active_navigations.end());
+ EXPECT_EQ(active_hints.find(GURL(url)), active_hints.end());
+
+ auto navigation_id2 = CreateNavigationID(tab_id, url2);
+ EXPECT_NE(active_navigations.find(navigation_id2), active_navigations.end());
+}
+
+TEST_F(LoadingPredictorTest, TestMainFrameResponseClearsNavigation) {
+ const std::string url = "http://www.google.com/cats";
+ const std::string redirected = "http://www.google.com/dogs";
+ const int tab_id = 12;
+ const auto& active_navigations = predictor_->active_navigations_;
+ const auto& active_hints = predictor_->active_hints_;
+
+ auto summary = CreateURLRequestSummary(tab_id, url);
+ auto navigation_id = CreateNavigationID(tab_id, url);
+
+ predictor_->OnMainFrameRequest(summary);
+ EXPECT_NE(active_navigations.find(navigation_id), active_navigations.end());
+ EXPECT_FALSE(active_hints.empty());
+
+ predictor_->OnMainFrameResponse(summary);
+ EXPECT_TRUE(active_navigations.empty());
+ EXPECT_TRUE(active_hints.empty());
+
+ // With redirects.
+ predictor_->OnMainFrameRequest(summary);
+ EXPECT_NE(active_navigations.find(navigation_id), active_navigations.end());
+ EXPECT_FALSE(active_hints.empty());
+
+ summary.redirect_url = GURL(redirected);
+ predictor_->OnMainFrameRedirect(summary);
+ EXPECT_FALSE(active_navigations.empty());
+ EXPECT_FALSE(active_hints.empty());
+
+ summary.navigation_id.main_frame_url = GURL(redirected);
+ predictor_->OnMainFrameResponse(summary);
+ EXPECT_TRUE(active_navigations.empty());
+ EXPECT_TRUE(active_hints.empty());
+}
+
+} // namespace predictors
alexilin 2017/05/30 16:19:14 Another useful usecase: OnMainFrameRequest doesn't
Benoit L 2017/05/31 14:34:42 Done.

Powered by Google App Engine
This is Rietveld 408576698