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

Unified Diff: chrome/browser/android/offline_pages/recent_tab_helper_unittest.cc

Issue 2657013002: Introduce ThreadTaskRunnerHandle::OverrideForTesting and TestMockTimeTaskRunner::ScopedContext. (Closed)
Patch Set: fix RecentTabHelperTest crash? Created 3 years, 10 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/android/offline_pages/recent_tab_helper_unittest.cc
diff --git a/chrome/browser/android/offline_pages/recent_tab_helper_unittest.cc b/chrome/browser/android/offline_pages/recent_tab_helper_unittest.cc
index c9626478f5cdfd0a4854ed755e20cc60122392c0..eb27eb829280c7816444934b476ebaa6fece35b9 100644
--- a/chrome/browser/android/offline_pages/recent_tab_helper_unittest.cc
+++ b/chrome/browser/android/offline_pages/recent_tab_helper_unittest.cc
@@ -4,12 +4,13 @@
#include "chrome/browser/android/offline_pages/recent_tab_helper.h"
+#include <memory>
+
#include "base/memory/ptr_util.h"
-#include "base/run_loop.h"
#include "base/strings/string16.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/scoped_feature_list.h"
-#include "base/test/test_mock_time_task_runner.h"
+#include "base/test/scoped_mock_time_message_loop_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
#include "chrome/browser/android/offline_pages/request_coordinator_factory.h"
@@ -43,14 +44,12 @@ class TestDelegate: public RecentTabHelper::Delegate {
explicit TestDelegate(
OfflinePageTestArchiver::Observer* observer,
- scoped_refptr<base::SingleThreadTaskRunner> task_runner,
int tab_id,
bool tab_id_result);
~TestDelegate() override {}
std::unique_ptr<OfflinePageArchiver> CreatePageArchiver(
content::WebContents* web_contents) override;
- scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() override;
// There is no expectations that tab_id is always present.
bool GetTabId(content::WebContents* web_contents, int* tab_id) override;
bool IsLowEndDevice() override { return is_low_end_device_; }
@@ -66,7 +65,6 @@ class TestDelegate: public RecentTabHelper::Delegate {
private:
OfflinePageTestArchiver::Observer* observer_; // observer owns this.
- scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
int tab_id_;
bool tab_id_result_;
@@ -87,13 +85,14 @@ class RecentTabHelperTest
~RecentTabHelperTest() override {}
void SetUp() override;
+ void TearDown() override;
const std::vector<OfflinePageItem>& GetAllPages();
void FailLoad(const GURL& url);
- // Runs default thread.
+ // Runs main thread.
void RunUntilIdle();
- // Moves forward the snapshot controller's task runner.
+ // Advances main thread time to trigger the snapshot controller's timeouts.
void FastForwardSnapshotController();
// Navigates to the URL and commit as if it has been typed in the address bar.
@@ -121,10 +120,6 @@ class RecentTabHelperTest
return nullptr;
}
- scoped_refptr<base::TestMockTimeTaskRunner>& task_runner() {
- return task_runner_;
- }
-
size_t page_added_count() { return page_added_count_; }
size_t model_removed_count() { return model_removed_count_; }
@@ -155,9 +150,15 @@ class RecentTabHelperTest
size_t model_removed_count_;
std::vector<OfflinePageItem> all_pages_;
bool all_pages_needs_updating_;
- scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
base::test::ScopedFeatureList scoped_feature_list_;
+ // Mocks the RenderViewHostTestHarness' main thread runner. Needs to be delay
+ // initialized in SetUp() -- can't be a simple member -- since
+ // RenderViewHostTestHarness only initializes its main thread environment in
+ // its SetUp() :(.
+ std::unique_ptr<base::ScopedMockTimeMessageLoopTaskRunner>
+ mocked_main_runner_;
+
base::WeakPtrFactory<RecentTabHelperTest> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(RecentTabHelperTest);
@@ -165,11 +166,9 @@ class RecentTabHelperTest
TestDelegate::TestDelegate(
OfflinePageTestArchiver::Observer* observer,
- scoped_refptr<base::SingleThreadTaskRunner> task_runner,
int tab_id,
bool tab_id_result)
: observer_(observer),
- task_runner_(task_runner),
tab_id_(tab_id),
tab_id_result_(tab_id_result) {
}
@@ -183,10 +182,7 @@ std::unique_ptr<OfflinePageArchiver> TestDelegate::CreatePageArchiver(
return std::move(archiver);
}
-scoped_refptr<base::SingleThreadTaskRunner> TestDelegate::GetTaskRunner() {
- return task_runner_;
-}
- // There is no expectations that tab_id is always present.
+// There is no expectations that tab_id is always present.
bool TestDelegate::GetTabId(content::WebContents* web_contents, int* tab_id) {
*tab_id = tab_id_;
return tab_id_result_;
@@ -199,12 +195,14 @@ RecentTabHelperTest::RecentTabHelperTest()
page_added_count_(0),
model_removed_count_(0),
all_pages_needs_updating_(true),
- task_runner_(new base::TestMockTimeTaskRunner),
weak_ptr_factory_(this) {}
void RecentTabHelperTest::SetUp() {
ChromeRenderViewHostTestHarness::SetUp();
+ mocked_main_runner_ =
+ base::MakeUnique<base::ScopedMockTimeMessageLoopTaskRunner>();
+
scoped_feature_list_.InitAndEnableFeature(kOffliningRecentPagesFeature);
// Sets up the factories for testing.
OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse(
@@ -218,7 +216,7 @@ void RecentTabHelperTest::SetUp() {
recent_tab_helper_ = RecentTabHelper::FromWebContents(web_contents());
std::unique_ptr<TestDelegate> test_delegate(
- new TestDelegate(this, task_runner(), kTabId, true));
+ new TestDelegate(this, kTabId, true));
default_test_delegate_ = test_delegate.get();
recent_tab_helper_->SetDelegate(std::move(test_delegate));
@@ -226,6 +224,11 @@ void RecentTabHelperTest::SetUp() {
model_->AddObserver(this);
}
+void RecentTabHelperTest::TearDown() {
+ mocked_main_runner_.reset();
+ ChromeRenderViewHostTestHarness::TearDown();
+}
+
void RecentTabHelperTest::FailLoad(const GURL& url) {
controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED,
std::string());
@@ -252,12 +255,12 @@ void RecentTabHelperTest::OnGetAllPagesDone(
}
void RecentTabHelperTest::RunUntilIdle() {
- base::RunLoop().RunUntilIdle();
+ (*mocked_main_runner_)->RunUntilIdle();
}
void RecentTabHelperTest::FastForwardSnapshotController() {
- const size_t kLongDelayMs = 100*1000;
- task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(kLongDelayMs));
+ constexpr base::TimeDelta kLongDelay = base::TimeDelta::FromSeconds(100);
+ (*mocked_main_runner_)->FastForwardBy(kLongDelay);
}
void RecentTabHelperTest::NavigateAndCommitTyped(const GURL& url) {
@@ -289,7 +292,6 @@ TEST_F(RecentTabHelperTest, LastNCaptureAfterLoad) {
recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
// Move the snapshot controller's time forward so it gets past timeouts.
FastForwardSnapshotController();
- RunUntilIdle();
EXPECT_TRUE(model()->is_loaded());
EXPECT_EQ(0U, page_added_count());
ASSERT_EQ(0U, GetAllPages().size());
@@ -319,7 +321,6 @@ TEST_F(RecentTabHelperTest, NoLastNCaptureIfTabHiddenTooEarlyInPageLoad) {
recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
// Move the snapshot controller's time forward so it gets past timeouts.
FastForwardSnapshotController();
- RunUntilIdle();
EXPECT_EQ(0U, page_added_count());
ASSERT_EQ(0U, GetAllPages().size());
}
@@ -328,8 +329,8 @@ TEST_F(RecentTabHelperTest, NoLastNCaptureIfTabHiddenTooEarlyInPageLoad) {
// ignored from both last_n and downloads.
TEST_F(RecentTabHelperTest, NoTabIdNoCapture) {
// Create delegate that returns 'false' as TabId retrieval result.
- recent_tab_helper()->SetDelegate(base::MakeUnique<TestDelegate>(
- this, task_runner(), kTabId, false));
+ recent_tab_helper()->SetDelegate(
+ base::MakeUnique<TestDelegate>(this, kTabId, false));
NavigateAndCommit(kTestPageUrl);
recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
@@ -375,7 +376,6 @@ TEST_F(RecentTabHelperTest, TwoCapturesSamePageLoad) {
// Set page loading state to the 1st snapshot-able stage. No capture so far.
recent_tab_helper()->DocumentAvailableInMainFrame();
FastForwardSnapshotController();
- RunUntilIdle();
EXPECT_TRUE(model()->is_loaded());
EXPECT_EQ(0U, page_added_count());
@@ -392,7 +392,6 @@ TEST_F(RecentTabHelperTest, TwoCapturesSamePageLoad) {
// capture should happen.
recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
FastForwardSnapshotController();
- RunUntilIdle();
EXPECT_EQ(1U, page_added_count());
EXPECT_EQ(0U, model_removed_count());
ASSERT_EQ(1U, GetAllPages().size());
@@ -465,7 +464,6 @@ TEST_F(RecentTabHelperTest, TwoCapturesDifferentPageLoadsSameUrl) {
NavigateAndCommitTyped(kTestPageUrl);
recent_tab_helper()->DocumentAvailableInMainFrame();
FastForwardSnapshotController();
- RunUntilIdle();
EXPECT_EQ(1U, page_added_count());
EXPECT_EQ(0U, model_removed_count());
ASSERT_EQ(1U, GetAllPages().size());
@@ -531,7 +529,6 @@ TEST_F(RecentTabHelperTest, TwoCapturesDifferentPageLoadsDifferentUrls) {
NavigateAndCommitTyped(kTestPageUrlOther);
recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
FastForwardSnapshotController();
- RunUntilIdle();
EXPECT_EQ(1U, page_added_count());
EXPECT_EQ(0U, model_removed_count());
ASSERT_EQ(1U, GetAllPages().size());
@@ -642,14 +639,12 @@ TEST_F(RecentTabHelperTest, DownloadRequestEarlyInLoad) {
const ClientId client_id = NewDownloadClientId();
recent_tab_helper()->ObserveAndDownloadCurrentPage(client_id, 153L);
FastForwardSnapshotController();
- RunUntilIdle();
EXPECT_TRUE(model()->is_loaded());
ASSERT_EQ(0U, GetAllPages().size());
// Minimally load the page. First capture should occur.
recent_tab_helper()->DocumentAvailableInMainFrame();
FastForwardSnapshotController();
- RunUntilIdle();
ASSERT_EQ(1U, GetAllPages().size());
const OfflinePageItem& early_page = GetAllPages()[0];
EXPECT_EQ(kTestPageUrl, early_page.url);
@@ -659,7 +654,6 @@ TEST_F(RecentTabHelperTest, DownloadRequestEarlyInLoad) {
// Fully load the page. A second capture should replace the first one.
recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
FastForwardSnapshotController();
- RunUntilIdle();
EXPECT_EQ(2U, page_added_count());
EXPECT_EQ(1U, model_removed_count());
ASSERT_EQ(1U, GetAllPages().size());
@@ -676,7 +670,6 @@ TEST_F(RecentTabHelperTest, DownloadRequestLaterInLoad) {
NavigateAndCommit(kTestPageUrl);
recent_tab_helper()->DocumentAvailableInMainFrame();
FastForwardSnapshotController();
- RunUntilIdle();
EXPECT_TRUE(model()->is_loaded());
ASSERT_EQ(0U, GetAllPages().size());
@@ -691,7 +684,6 @@ TEST_F(RecentTabHelperTest, DownloadRequestLaterInLoad) {
recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
FastForwardSnapshotController();
- RunUntilIdle();
EXPECT_EQ(2U, page_added_count());
EXPECT_EQ(1U, model_removed_count());
ASSERT_EQ(1U, GetAllPages().size());
@@ -703,7 +695,6 @@ TEST_F(RecentTabHelperTest, DownloadRequestAfterFullyLoad) {
NavigateAndCommit(kTestPageUrl);
recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
FastForwardSnapshotController();
- RunUntilIdle();
EXPECT_TRUE(model()->is_loaded());
ASSERT_EQ(0U, GetAllPages().size());
@@ -793,7 +784,6 @@ TEST_F(RecentTabHelperTest, OverlappingDownloadRequestsAreIgnored) {
// Finish loading the page. Only the first request should be executed.
recent_tab_helper()->DocumentOnLoadCompletedInMainFrame();
FastForwardSnapshotController();
- RunUntilIdle();
EXPECT_EQ(1U, page_added_count());
EXPECT_EQ(0U, model_removed_count());
ASSERT_EQ(1U, GetAllPages().size());

Powered by Google App Engine
This is Rietveld 408576698