Skip to content

Commit

Permalink
Use mockable time in CompositorFrameReportingController
Browse files Browse the repository at this point in the history
This CL adds the possibility to inject a base::TickClock* into
CompositorFrameReportingController, so its timing can be mocked by
tests. CompositorFrameReportingControllerTest is also updated to make
use of this.

Bug: 1054009,1057193
Change-Id: I6ece91efe32395a37c14cd4ca4c59da9f2e05da0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2159894
Reviewed-by: Sadrul Chowdhury <sadrul@chromium.org>
Commit-Queue: Mohsen Izadi <mohsen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#762104}
  • Loading branch information
Mohsen Izadi authored and Commit Bot committed Apr 23, 2020
1 parent 247cab0 commit 8461d62
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cc/metrics/compositor_frame_reporting_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ CompositorFrameReportingController::SubmittedCompositorFrame::
SubmittedCompositorFrame(SubmittedCompositorFrame&& other) = default;

base::TimeTicks CompositorFrameReportingController::Now() const {
return base::TimeTicks::Now();
return tick_clock_->NowTicks();
}

bool CompositorFrameReportingController::HasReporterAt(
Expand Down
7 changes: 7 additions & 0 deletions cc/metrics/compositor_frame_reporting_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <memory>
#include <vector>

#include "base/time/default_tick_clock.h"
#include "base/time/time.h"
#include "cc/cc_export.h"
#include "cc/metrics/compositor_frame_reporter.h"
Expand Down Expand Up @@ -77,6 +78,10 @@ class CC_EXPORT CompositorFrameReportingController {
virtual void AddActiveTracker(FrameSequenceTrackerType type);
virtual void RemoveActiveTracker(FrameSequenceTrackerType type);

void set_tick_clock(const base::TickClock* tick_clock) {
tick_clock_ = tick_clock;
}

std::unique_ptr<CompositorFrameReporter>* reporters() { return reporters_; }

protected:
Expand Down Expand Up @@ -120,6 +125,8 @@ class CC_EXPORT CompositorFrameReportingController {
// DO NOT reorder this line and the one above. The latency_ukm_reporter_ must
// outlive the objects in |submitted_compositor_frames_|.
base::circular_deque<SubmittedCompositorFrame> submitted_compositor_frames_;

const base::TickClock* tick_clock_ = base::DefaultTickClock::GetInstance();
};
} // namespace cc

Expand Down
14 changes: 9 additions & 5 deletions cc/metrics/compositor_frame_reporting_controller_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/macros.h"
#include "base/strings/strcat.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/simple_test_tick_clock.h"
#include "cc/input/scroll_input_type.h"
#include "cc/metrics/event_metrics.h"
#include "components/viz/common/frame_timing_details.h"
Expand Down Expand Up @@ -56,6 +57,8 @@ class TestCompositorFrameReportingController
class CompositorFrameReportingControllerTest : public testing::Test {
public:
CompositorFrameReportingControllerTest() : current_id_(1, 1) {
test_tick_clock_.SetNowTicks(base::TimeTicks::Now());
reporting_controller_.set_tick_clock(&test_tick_clock_);
args_ = SimulateBeginFrameArgs(current_id_);
}

Expand Down Expand Up @@ -137,20 +140,21 @@ class CompositorFrameReportingControllerTest : public testing::Test {
}

base::TimeTicks AdvanceNowByMs(int64_t advance_ms) {
now_ += base::TimeDelta::FromMicroseconds(advance_ms);
return now_;
test_tick_clock_.Advance(base::TimeDelta::FromMicroseconds(advance_ms));
return test_tick_clock_.NowTicks();
}

protected:
// This should be defined before |reporting_controller_| so it is created
// before and destroyed after that.
base::SimpleTestTickClock test_tick_clock_;

TestCompositorFrameReportingController reporting_controller_;
viz::BeginFrameArgs args_;
viz::BeginFrameId current_id_;
viz::BeginFrameId last_activated_id_;
base::TimeTicks begin_main_start_;
viz::FrameTokenGenerator next_token_;

private:
base::TimeTicks now_ = base::TimeTicks::Now();
};

TEST_F(CompositorFrameReportingControllerTest, ActiveReporterCounts) {
Expand Down

0 comments on commit 8461d62

Please sign in to comment.