1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/offline_pages/core/background_snapshot_controller.h"
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/test_mock_time_task_runner.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace offline_pages {
class BackgroundSnapshotControllerTest
: public testing::Test,
public BackgroundSnapshotController::Client {
public:
BackgroundSnapshotControllerTest();
~BackgroundSnapshotControllerTest() override;
BackgroundSnapshotController* controller() { return controller_.get(); }
int snapshot_count() { return snapshot_count_; }
// testing::Test
void SetUp() override;
void TearDown() override;
// BackgroundSnapshotController::Client
void StartSnapshot() override;
// Utility methods.
// Runs until all of the tasks that are not delayed are gone from the task
// queue.
void PumpLoop();
// Fast-forwards virtual time by |delta|, causing tasks with a remaining
// delay less than or equal to |delta| to be executed.
void FastForwardBy(base::TimeDelta delta);
private:
std::unique_ptr<BackgroundSnapshotController> controller_;
scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
bool snapshot_started_;
int snapshot_count_;
};
BackgroundSnapshotControllerTest::BackgroundSnapshotControllerTest()
: task_runner_(new base::TestMockTimeTaskRunner),
snapshot_started_(true),
snapshot_count_(0) {}
BackgroundSnapshotControllerTest::~BackgroundSnapshotControllerTest() = default;
void BackgroundSnapshotControllerTest::SetUp() {
controller_ =
std::make_unique<BackgroundSnapshotController>(task_runner_, this, false);
snapshot_started_ = true;
}
void BackgroundSnapshotControllerTest::TearDown() {
controller_.reset();
}
void BackgroundSnapshotControllerTest::StartSnapshot() {
snapshot_count_++;
}
void BackgroundSnapshotControllerTest::PumpLoop() {
task_runner_->RunUntilIdle();
}
void BackgroundSnapshotControllerTest::FastForwardBy(base::TimeDelta delta) {
task_runner_->FastForwardBy(delta);
}
TEST_F(BackgroundSnapshotControllerTest, OnLoad) {
// Onload should make snapshot after its delay.
controller()->DocumentOnLoadCompletedInPrimaryMainFrame();
PumpLoop();
EXPECT_EQ(0, snapshot_count());
FastForwardBy(base::Milliseconds(
controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
EXPECT_EQ(1, snapshot_count());
}
TEST_F(BackgroundSnapshotControllerTest, Stop) {
// OnDOM should make snapshot after a delay.
controller()->DocumentOnLoadCompletedInPrimaryMainFrame();
PumpLoop();
EXPECT_EQ(0, snapshot_count());
controller()->Stop();
FastForwardBy(base::Milliseconds(
controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
// Should not start snapshots
EXPECT_EQ(0, snapshot_count());
// Also should not start snapshot.
controller()->DocumentOnLoadCompletedInPrimaryMainFrame();
EXPECT_EQ(0, snapshot_count());
}
// This simulated a Reset while there is ongoing snapshot, which is reported
// as done later. That reporting should have no effect nor crash.
TEST_F(BackgroundSnapshotControllerTest, ClientReset) {
controller()->DocumentOnLoadCompletedInPrimaryMainFrame();
FastForwardBy(base::Milliseconds(
controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
EXPECT_EQ(1, snapshot_count());
// This normally happens when navigation starts.
controller()->Reset();
// Next snapshot should be initiated when new document is loaded.
controller()->DocumentOnLoadCompletedInPrimaryMainFrame();
FastForwardBy(base::Milliseconds(
controller()->GetDelayAfterDocumentOnLoadCompletedForTest()));
// No snapshot since session was reset.
EXPECT_EQ(2, snapshot_count());
}
} // namespace offline_pages
|