Commit e58a502d authored by Sergio Villar Senin's avatar Sergio Villar Senin Committed by Commit Bot

OnionSoup: Migrate WebViewTest to TestMockTimeTaskRunner

This is a followup of http://crrev.com/c/1590101. In this CL WebViewTest is
moved out of time function overrides. TestMockTimeTaskRunner is used instead.

The migration is far from trivial. Some other classes had to be migrated first,
InteractiveDetector (with test) and DocumentLoader among others. In order to
simplify a bit the migration, a new attribute was added to WebNavigationParams
carrying the clock to be used by DocumentLoader. That clock is precisely the one
being used by TestMockTimeTaskRunner.

That modifications in DocumentLoader had some side effects in order non directly
related tests, in particular CSSAnimationTests. There is a pretty tiny rounding
error in TimeTicks <-> double conversions that force us to replace an exact
comparison (EXPECT_DOUBLE_EQ) by an approximation (EXPECT_NEAR) with a very tiny
margin error.

Bug: 919383
Change-Id: Ie1bf2402a9cf55f312ba4cf5f129097e25ed4ae1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1588650
Commit-Queue: Sergio Villar <svillar@igalia.com>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#664697}
parent 17a43185
...@@ -39,6 +39,10 @@ ...@@ -39,6 +39,10 @@
#include "base/memory/scoped_refptr.h" #include "base/memory/scoped_refptr.h"
#endif #endif
namespace base {
class TickClock;
}
namespace blink { namespace blink {
class KURL; class KURL;
...@@ -316,6 +320,9 @@ struct BLINK_EXPORT WebNavigationParams { ...@@ -316,6 +320,9 @@ struct BLINK_EXPORT WebNavigationParams {
}; };
WebVector<std::unique_ptr<PrefetchedSignedExchange>> WebVector<std::unique_ptr<PrefetchedSignedExchange>>
prefetched_signed_exchanges; prefetched_signed_exchanges;
// An optional tick clock to be used for document loader timing. This is used
// for testing.
const base::TickClock* tick_clock = nullptr;
}; };
} // namespace blink } // namespace blink
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "third_party/blink/renderer/core/animation/css/css_animations.h" #include "third_party/blink/renderer/core/animation/css/css_animations.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/core/animation/animation.h" #include "third_party/blink/renderer/core/animation/animation.h"
#include "third_party/blink/renderer/core/animation/element_animations.h" #include "third_party/blink/renderer/core/animation/element_animations.h"
#include "third_party/blink/renderer/core/dom/node_computed_style.h" #include "third_party/blink/renderer/core/dom/node_computed_style.h"
...@@ -80,13 +81,13 @@ TEST_F(CSSAnimationsTest, RetargetedTransition) { ...@@ -80,13 +81,13 @@ TEST_F(CSSAnimationsTest, RetargetedTransition) {
element->setAttribute(html_names::kClassAttr, "contrast2"); element->setAttribute(html_names::kClassAttr, "contrast2");
GetPage().Animator().ServiceScriptedAnimations(CurrentTimeTicks()); GetPage().Animator().ServiceScriptedAnimations(CurrentTimeTicks());
UpdateAllLifecyclePhasesForTest(); UpdateAllLifecyclePhasesForTest();
EXPECT_DOUBLE_EQ(0.6, GetContrastFilterAmount(element)); EXPECT_NEAR(0.6, GetContrastFilterAmount(element), 0.00000000001);
// As it has been retargeted, advancing halfway should go to 0.3. // As it has been retargeted, advancing halfway should go to 0.3.
AdvanceClockSeconds(0.5); AdvanceClockSeconds(0.5);
GetPage().Animator().ServiceScriptedAnimations(CurrentTimeTicks()); GetPage().Animator().ServiceScriptedAnimations(CurrentTimeTicks());
UpdateAllLifecyclePhasesForTest(); UpdateAllLifecyclePhasesForTest();
EXPECT_DOUBLE_EQ(0.3, GetContrastFilterAmount(element)); EXPECT_NEAR(0.3, GetContrastFilterAmount(element), 0.00000000001);
} }
// Test that when an incompatible in progress compositor transition // Test that when an incompatible in progress compositor transition
......
...@@ -130,11 +130,14 @@ void LoadFrame(WebLocalFrame* frame, const std::string& url) { ...@@ -130,11 +130,14 @@ void LoadFrame(WebLocalFrame* frame, const std::string& url) {
void LoadHTMLString(WebLocalFrame* frame, void LoadHTMLString(WebLocalFrame* frame,
const std::string& html, const std::string& html,
const WebURL& base_url) { const WebURL& base_url,
const base::TickClock* clock) {
auto* impl = To<WebLocalFrameImpl>(frame); auto* impl = To<WebLocalFrameImpl>(frame);
impl->CommitNavigation( std::unique_ptr<WebNavigationParams> navigation_params =
WebNavigationParams::CreateWithHTMLString(html, base_url), WebNavigationParams::CreateWithHTMLString(html, base_url);
nullptr /* extra_data */); navigation_params->tick_clock = clock;
impl->CommitNavigation(std::move(navigation_params),
nullptr /* extra_data */);
PumpPendingRequestsForFrameToLoad(frame); PumpPendingRequestsForFrameToLoad(frame);
} }
......
...@@ -83,6 +83,10 @@ ...@@ -83,6 +83,10 @@
EXPECT_FLOAT_EQ((expected).Height(), (actual).Height()); \ EXPECT_FLOAT_EQ((expected).Height(), (actual).Height()); \
} while (false) } while (false)
namespace base {
class TickClock;
}
namespace cc { namespace cc {
class AnimationHost; class AnimationHost;
} }
...@@ -109,7 +113,8 @@ void LoadFrame(WebLocalFrame*, const std::string& url); ...@@ -109,7 +113,8 @@ void LoadFrame(WebLocalFrame*, const std::string& url);
// Same as above, but for WebLocalFrame::LoadHTMLString(). // Same as above, but for WebLocalFrame::LoadHTMLString().
void LoadHTMLString(WebLocalFrame*, void LoadHTMLString(WebLocalFrame*,
const std::string& html, const std::string& html,
const WebURL& base_url); const WebURL& base_url,
const base::TickClock* clock = nullptr);
// Same as above, but for WebLocalFrame::RequestFromHistoryItem/Load. // Same as above, but for WebLocalFrame::RequestFromHistoryItem/Load.
void LoadHistoryItem(WebLocalFrame*, void LoadHistoryItem(WebLocalFrame*,
const WebHistoryItem&, const WebHistoryItem&,
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include <utility> #include <utility>
#include "base/auto_reset.h" #include "base/auto_reset.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/time/default_tick_clock.h"
#include "services/metrics/public/cpp/ukm_builders.h" #include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h" #include "services/metrics/public/cpp/ukm_recorder.h"
#include "third_party/blink/public/common/origin_policy/origin_policy.h" #include "third_party/blink/public/common/origin_policy/origin_policy.h"
...@@ -133,7 +134,9 @@ DocumentLoader::DocumentLoader( ...@@ -133,7 +134,9 @@ DocumentLoader::DocumentLoader(
devtools_navigation_token_(params_->devtools_navigation_token), devtools_navigation_token_(params_->devtools_navigation_token),
had_sticky_activation_(params_->is_user_activated), had_sticky_activation_(params_->is_user_activated),
was_discarded_(params_->was_discarded), was_discarded_(params_->was_discarded),
use_counter_() { use_counter_(),
clock_(params_->tick_clock ? params_->tick_clock
: base::DefaultTickClock::GetInstance()) {
DCHECK(frame_); DCHECK(frame_);
// TODO(nasko): How should this work with OOPIF? // TODO(nasko): How should this work with OOPIF?
...@@ -170,7 +173,7 @@ DocumentLoader::DocumentLoader( ...@@ -170,7 +173,7 @@ DocumentLoader::DocumentLoader(
document_load_timing_.SetInputStart(timings.input_start); document_load_timing_.SetInputStart(timings.input_start);
if (timings.navigation_start.is_null()) { if (timings.navigation_start.is_null()) {
// If we don't have any navigation timings yet, it starts now. // If we don't have any navigation timings yet, it starts now.
document_load_timing_.SetNavigationStart(CurrentTimeTicks()); document_load_timing_.SetNavigationStart(clock_->NowTicks());
} else { } else {
document_load_timing_.SetNavigationStart(timings.navigation_start); document_load_timing_.SetNavigationStart(timings.navigation_start);
if (!timings.redirect_start.is_null()) { if (!timings.redirect_start.is_null()) {
...@@ -665,7 +668,7 @@ void DocumentLoader::FinishedLoading(TimeTicks finish_time) { ...@@ -665,7 +668,7 @@ void DocumentLoader::FinishedLoading(TimeTicks finish_time) {
if (response_end_time.is_null()) if (response_end_time.is_null())
response_end_time = time_of_last_data_received_; response_end_time = time_of_last_data_received_;
if (response_end_time.is_null()) if (response_end_time.is_null())
response_end_time = CurrentTimeTicks(); response_end_time = clock_->NowTicks();
GetTiming().SetResponseEnd(response_end_time); GetTiming().SetResponseEnd(response_end_time);
if (!frame_) if (!frame_)
...@@ -994,7 +997,7 @@ void DocumentLoader::HandleData(const char* data, size_t length) { ...@@ -994,7 +997,7 @@ void DocumentLoader::HandleData(const char* data, size_t length) {
DCHECK(data); DCHECK(data);
DCHECK(length); DCHECK(length);
DCHECK(!frame_->GetPage()->Paused()); DCHECK(!frame_->GetPage()->Paused());
time_of_last_data_received_ = CurrentTimeTicks(); time_of_last_data_received_ = clock_->NowTicks();
if (listing_ftp_directory_ || loading_mhtml_archive_) { if (listing_ftp_directory_ || loading_mhtml_archive_) {
data_buffer_->Append(data, length); data_buffer_->Append(data, length);
...@@ -1109,7 +1112,7 @@ void DocumentLoader::LoadEmpty() { ...@@ -1109,7 +1112,7 @@ void DocumentLoader::LoadEmpty() {
// stop this loader. // stop this loader.
if (!frame_) if (!frame_)
return; return;
FinishedLoading(CurrentTimeTicks()); FinishedLoading(clock_->NowTicks());
} }
void DocumentLoader::StartLoading() { void DocumentLoader::StartLoading() {
...@@ -1229,7 +1232,7 @@ void DocumentLoader::StartLoadingInternal() { ...@@ -1229,7 +1232,7 @@ void DocumentLoader::StartLoadingInternal() {
body_loader_->StartLoadingBody(this, false /* use_isolated_code_cache */); body_loader_->StartLoadingBody(this, false /* use_isolated_code_cache */);
if (body_loader_) { if (body_loader_) {
// If we did not finish synchronously, load empty document instead. // If we did not finish synchronously, load empty document instead.
FinishedLoading(CurrentTimeTicks()); FinishedLoading(clock_->NowTicks());
} }
// FinishedLoading call above must commit navigation for mhtml archive. // FinishedLoading call above must commit navigation for mhtml archive.
CHECK_GE(state_, kCommitted); CHECK_GE(state_, kCommitted);
......
...@@ -66,6 +66,10 @@ ...@@ -66,6 +66,10 @@
#include <memory> #include <memory>
namespace base {
class TickClock;
}
namespace blink { namespace blink {
class ApplicationCacheHost; class ApplicationCacheHost;
...@@ -277,6 +281,9 @@ class CORE_EXPORT DocumentLoader ...@@ -277,6 +281,9 @@ class CORE_EXPORT DocumentLoader
void CountUse(mojom::WebFeature) override; void CountUse(mojom::WebFeature) override;
void CountDeprecation(mojom::WebFeature) override; void CountDeprecation(mojom::WebFeature) override;
// The caller owns the |clock| which must outlive the DocumentLoader.
void SetTickClockForTesting(const base::TickClock* clock) { clock_ = clock; }
protected: protected:
bool had_transient_activation() const { return had_transient_activation_; } bool had_transient_activation() const { return had_transient_activation_; }
...@@ -488,6 +495,8 @@ class CORE_EXPORT DocumentLoader ...@@ -488,6 +495,8 @@ class CORE_EXPORT DocumentLoader
UseCounterHelper use_counter_; UseCounterHelper use_counter_;
Dactyloscoper dactyloscoper_; Dactyloscoper dactyloscoper_;
const base::TickClock* clock_;
}; };
DECLARE_WEAK_IDENTIFIER_MAP(DocumentLoader); DECLARE_WEAK_IDENTIFIER_MAP(DocumentLoader);
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "third_party/blink/renderer/core/loader/interactive_detector.h" #include "third_party/blink/renderer/core/loader/interactive_detector.h"
#include "base/time/default_tick_clock.h"
#include "third_party/blink/renderer/core/dom/document.h" #include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/events/event.h" #include "third_party/blink/renderer/core/dom/events/event.h"
#include "third_party/blink/renderer/core/frame/local_frame.h" #include "third_party/blink/renderer/core/frame/local_frame.h"
...@@ -49,6 +50,7 @@ InteractiveDetector::InteractiveDetector( ...@@ -49,6 +50,7 @@ InteractiveDetector::InteractiveDetector(
NetworkActivityChecker* network_activity_checker) NetworkActivityChecker* network_activity_checker)
: Supplement<Document>(document), : Supplement<Document>(document),
ContextLifecycleObserver(&document), ContextLifecycleObserver(&document),
clock_(base::DefaultTickClock::GetInstance()),
network_activity_checker_(network_activity_checker), network_activity_checker_(network_activity_checker),
time_to_interactive_timer_( time_to_interactive_timer_(
document.GetTaskRunner(TaskType::kInternalDefault), document.GetTaskRunner(TaskType::kInternalDefault),
...@@ -100,7 +102,7 @@ void InteractiveDetector::StartOrPostponeCITimer(TimeTicks timer_fire_time) { ...@@ -100,7 +102,7 @@ void InteractiveDetector::StartOrPostponeCITimer(TimeTicks timer_fire_time) {
if (timer_fire_time < time_to_interactive_timer_fire_time_) if (timer_fire_time < time_to_interactive_timer_fire_time_)
return; return;
TimeDelta delay = timer_fire_time - CurrentTimeTicks(); TimeDelta delay = timer_fire_time - clock_->NowTicks();
time_to_interactive_timer_fire_time_ = timer_fire_time; time_to_interactive_timer_fire_time_ = timer_fire_time;
if (delay <= TimeDelta()) { if (delay <= TimeDelta()) {
...@@ -266,22 +268,21 @@ void InteractiveDetector::EndNetworkQuietPeriod(TimeTicks current_time) { ...@@ -266,22 +268,21 @@ void InteractiveDetector::EndNetworkQuietPeriod(TimeTicks current_time) {
} }
// The optional opt_current_time, if provided, saves us a call to // The optional opt_current_time, if provided, saves us a call to
// CurrentTimeTicksInSeconds. // clock_->NowTicks().
void InteractiveDetector::UpdateNetworkQuietState( void InteractiveDetector::UpdateNetworkQuietState(
double request_count, double request_count,
base::Optional<TimeTicks> opt_current_time) { base::Optional<TimeTicks> opt_current_time) {
if (request_count <= kNetworkQuietMaximumConnections && if (request_count <= kNetworkQuietMaximumConnections &&
active_network_quiet_window_start_.is_null()) { active_network_quiet_window_start_.is_null()) {
// Not using `value_or(CurrentTimeTicksInSeconds())` here because // Not using `value_or(clock_->NowTicks())` here because arguments to
// arguments to functions are eagerly evaluated, which always call // functions are eagerly evaluated, which always call clock_->NowTicks.
// CurrentTimeTicksInSeconds.
TimeTicks current_time = TimeTicks current_time =
opt_current_time ? opt_current_time.value() : CurrentTimeTicks(); opt_current_time ? opt_current_time.value() : clock_->NowTicks();
BeginNetworkQuietPeriod(current_time); BeginNetworkQuietPeriod(current_time);
} else if (request_count > kNetworkQuietMaximumConnections && } else if (request_count > kNetworkQuietMaximumConnections &&
!active_network_quiet_window_start_.is_null()) { !active_network_quiet_window_start_.is_null()) {
TimeTicks current_time = TimeTicks current_time =
opt_current_time ? opt_current_time.value() : CurrentTimeTicks(); opt_current_time ? opt_current_time.value() : clock_->NowTicks();
EndNetworkQuietPeriod(current_time); EndNetworkQuietPeriod(current_time);
} }
} }
...@@ -298,7 +299,7 @@ void InteractiveDetector::OnResourceLoadBegin( ...@@ -298,7 +299,7 @@ void InteractiveDetector::OnResourceLoadBegin(
} }
// The optional load_finish_time, if provided, saves us a call to // The optional load_finish_time, if provided, saves us a call to
// CurrentTimeTicksInSeconds. // clock_->NowTicks.
void InteractiveDetector::OnResourceLoadEnd( void InteractiveDetector::OnResourceLoadEnd(
base::Optional<TimeTicks> load_finish_time) { base::Optional<TimeTicks> load_finish_time) {
if (!GetSupplementable()) if (!GetSupplementable())
...@@ -331,7 +332,7 @@ void InteractiveDetector::OnFirstMeaningfulPaintDetected( ...@@ -331,7 +332,7 @@ void InteractiveDetector::OnFirstMeaningfulPaintDetected(
page_event_times_.first_meaningful_paint = fmp_time; page_event_times_.first_meaningful_paint = fmp_time;
page_event_times_.first_meaningful_paint_invalidated = page_event_times_.first_meaningful_paint_invalidated =
user_input_before_fmp == FirstMeaningfulPaintDetector::kHadUserInput; user_input_before_fmp == FirstMeaningfulPaintDetector::kHadUserInput;
if (CurrentTimeTicks() - fmp_time >= kTimeToInteractiveWindow) { if (clock_->NowTicks() - fmp_time >= kTimeToInteractiveWindow) {
// We may have reached TTCI already. Check right away. // We may have reached TTCI already. Check right away.
CheckTimeToInteractiveReached(); CheckTimeToInteractiveReached();
} else { } else {
...@@ -364,7 +365,7 @@ void InteractiveDetector::OnInvalidatingInputEvent( ...@@ -364,7 +365,7 @@ void InteractiveDetector::OnInvalidatingInputEvent(
} }
void InteractiveDetector::OnPageHiddenChanged(bool is_hidden) { void InteractiveDetector::OnPageHiddenChanged(bool is_hidden) {
visibility_change_events_.push_back({CurrentTimeTicks(), is_hidden}); visibility_change_events_.push_back({clock_->NowTicks(), is_hidden});
} }
void InteractiveDetector::TimeToInteractiveTimerFired(TimerBase*) { void InteractiveDetector::TimeToInteractiveTimerFired(TimerBase*) {
...@@ -474,7 +475,7 @@ void InteractiveDetector::CheckTimeToInteractiveReached() { ...@@ -474,7 +475,7 @@ void InteractiveDetector::CheckTimeToInteractiveReached() {
page_event_times_.dom_content_loaded_end.is_null()) page_event_times_.dom_content_loaded_end.is_null())
return; return;
const TimeTicks current_time = CurrentTimeTicks(); const TimeTicks current_time = clock_->NowTicks();
if (current_time - page_event_times_.first_meaningful_paint < if (current_time - page_event_times_.first_meaningful_paint <
kTimeToInteractiveWindow) { kTimeToInteractiveWindow) {
// Too close to FMP to determine Time to Interactive. // Too close to FMP to determine Time to Interactive.
...@@ -492,7 +493,7 @@ void InteractiveDetector::CheckTimeToInteractiveReached() { ...@@ -492,7 +493,7 @@ void InteractiveDetector::CheckTimeToInteractiveReached() {
interactive_time_ = std::max( interactive_time_ = std::max(
{interactive_candidate, page_event_times_.dom_content_loaded_end}); {interactive_candidate, page_event_times_.dom_content_loaded_end});
interactive_detection_time_ = CurrentTimeTicks(); interactive_detection_time_ = clock_->NowTicks();
OnTimeToInteractiveDetected(); OnTimeToInteractiveDetected();
} }
...@@ -530,4 +531,13 @@ void InteractiveDetector::Trace(Visitor* visitor) { ...@@ -530,4 +531,13 @@ void InteractiveDetector::Trace(Visitor* visitor) {
ContextLifecycleObserver::Trace(visitor); ContextLifecycleObserver::Trace(visitor);
} }
void InteractiveDetector::SetTickClockForTesting(const base::TickClock* clock) {
clock_ = clock;
}
void InteractiveDetector::SetTaskRunnerForTesting(
scoped_refptr<base::SingleThreadTaskRunner> task_runner_for_testing) {
time_to_interactive_timer_.MoveToNewTaskRunner(task_runner_for_testing);
}
} // namespace blink } // namespace blink
...@@ -19,6 +19,10 @@ ...@@ -19,6 +19,10 @@
#include "third_party/blink/renderer/platform/timer.h" #include "third_party/blink/renderer/platform/timer.h"
#include "third_party/blink/renderer/platform/wtf/pod_interval.h" #include "third_party/blink/renderer/platform/wtf/pod_interval.h"
namespace base {
class TickClock;
} // namespace base
namespace blink { namespace blink {
class Document; class Document;
...@@ -116,9 +120,16 @@ class CORE_EXPORT InteractiveDetector ...@@ -116,9 +120,16 @@ class CORE_EXPORT InteractiveDetector
void Trace(Visitor*) override; void Trace(Visitor*) override;
void SetTaskRunnerForTesting(
scoped_refptr<base::SingleThreadTaskRunner> task_runner_for_testing);
// The caller owns the |clock| which must outlive the InteractiveDetector.
void SetTickClockForTesting(const base::TickClock* clock);
private: private:
friend class InteractiveDetectorTest; friend class InteractiveDetectorTest;
const base::TickClock* clock_;
TimeTicks interactive_time_; TimeTicks interactive_time_;
TimeTicks interactive_detection_time_; TimeTicks interactive_detection_time_;
......
...@@ -45,6 +45,9 @@ class InteractiveDetectorTest : public testing::Test { ...@@ -45,6 +45,9 @@ class InteractiveDetectorTest : public testing::Test {
detector_ = MakeGarbageCollected<InteractiveDetector>( detector_ = MakeGarbageCollected<InteractiveDetector>(
*document, new NetworkActivityCheckerForTest(document)); *document, new NetworkActivityCheckerForTest(document));
auto test_task_runner = platform_->test_task_runner();
detector_->SetTaskRunnerForTesting(test_task_runner);
detector_->SetTickClockForTesting(test_task_runner->GetMockTickClock());
// By this time, the DummyPageHolder has created an InteractiveDetector, and // By this time, the DummyPageHolder has created an InteractiveDetector, and
// sent DOMContentLoadedEnd. We overwrite it with our new // sent DOMContentLoadedEnd. We overwrite it with our new
...@@ -58,7 +61,7 @@ class InteractiveDetectorTest : public testing::Test { ...@@ -58,7 +61,7 @@ class InteractiveDetectorTest : public testing::Test {
// Public because it's executed on a task queue. // Public because it's executed on a task queue.
void DummyTaskWithDuration(double duration_seconds) { void DummyTaskWithDuration(double duration_seconds) {
platform_->AdvanceClockSeconds(duration_seconds); platform_->AdvanceClockSeconds(duration_seconds);
dummy_task_end_time_ = CurrentTimeTicks(); dummy_task_end_time_ = Now();
} }
protected: protected:
...@@ -101,7 +104,7 @@ class InteractiveDetectorTest : public testing::Test { ...@@ -101,7 +104,7 @@ class InteractiveDetectorTest : public testing::Test {
} }
void RunTillTimestamp(TimeTicks target_time) { void RunTillTimestamp(TimeTicks target_time) {
TimeTicks current_time = CurrentTimeTicks(); TimeTicks current_time = Now();
platform_->RunForPeriod(std::max(TimeDelta(), target_time - current_time)); platform_->RunForPeriod(std::max(TimeDelta(), target_time - current_time));
} }
...@@ -127,6 +130,8 @@ class InteractiveDetectorTest : public testing::Test { ...@@ -127,6 +130,8 @@ class InteractiveDetectorTest : public testing::Test {
detector_->OnResourceLoadEnd(load_finish_time); detector_->OnResourceLoadEnd(load_finish_time);
} }
base::TimeTicks Now() { return platform_->test_task_runner()->NowTicks(); }
TimeTicks GetInteractiveTime() { return detector_->GetInteractiveTime(); } TimeTicks GetInteractiveTime() { return detector_->GetInteractiveTime(); }
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler> ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
...@@ -151,7 +156,7 @@ class InteractiveDetectorTest : public testing::Test { ...@@ -151,7 +156,7 @@ class InteractiveDetectorTest : public testing::Test {
// The name shows the ordering of these events in the test. // The name shows the ordering of these events in the test.
TEST_F(InteractiveDetectorTest, FMP_DCL_FmpDetect) { TEST_F(InteractiveDetectorTest, FMP_DCL_FmpDetect) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
// Network is forever quiet for this test. // Network is forever quiet for this test.
SetActiveConnections(1); SetActiveConnections(1);
...@@ -166,7 +171,7 @@ TEST_F(InteractiveDetectorTest, FMP_DCL_FmpDetect) { ...@@ -166,7 +171,7 @@ TEST_F(InteractiveDetectorTest, FMP_DCL_FmpDetect) {
} }
TEST_F(InteractiveDetectorTest, DCL_FMP_FmpDetect) { TEST_F(InteractiveDetectorTest, DCL_FMP_FmpDetect) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
// Network is forever quiet for this test. // Network is forever quiet for this test.
SetActiveConnections(1); SetActiveConnections(1);
...@@ -181,7 +186,7 @@ TEST_F(InteractiveDetectorTest, DCL_FMP_FmpDetect) { ...@@ -181,7 +186,7 @@ TEST_F(InteractiveDetectorTest, DCL_FMP_FmpDetect) {
} }
TEST_F(InteractiveDetectorTest, InstantDetectionAtFmpDetectIfPossible) { TEST_F(InteractiveDetectorTest, InstantDetectionAtFmpDetectIfPossible) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
// Network is forever quiet for this test. // Network is forever quiet for this test.
SetActiveConnections(1); SetActiveConnections(1);
...@@ -195,7 +200,7 @@ TEST_F(InteractiveDetectorTest, InstantDetectionAtFmpDetectIfPossible) { ...@@ -195,7 +200,7 @@ TEST_F(InteractiveDetectorTest, InstantDetectionAtFmpDetectIfPossible) {
} }
TEST_F(InteractiveDetectorTest, FmpDetectFiresAfterLateLongTask) { TEST_F(InteractiveDetectorTest, FmpDetectFiresAfterLateLongTask) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
// Network is forever quiet for this test. // Network is forever quiet for this test.
SetActiveConnections(1); SetActiveConnections(1);
...@@ -210,7 +215,7 @@ TEST_F(InteractiveDetectorTest, FmpDetectFiresAfterLateLongTask) { ...@@ -210,7 +215,7 @@ TEST_F(InteractiveDetectorTest, FmpDetectFiresAfterLateLongTask) {
} }
TEST_F(InteractiveDetectorTest, FMP_FmpDetect_DCL) { TEST_F(InteractiveDetectorTest, FMP_FmpDetect_DCL) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
// Network is forever quiet for this test. // Network is forever quiet for this test.
SetActiveConnections(1); SetActiveConnections(1);
...@@ -222,7 +227,7 @@ TEST_F(InteractiveDetectorTest, FMP_FmpDetect_DCL) { ...@@ -222,7 +227,7 @@ TEST_F(InteractiveDetectorTest, FMP_FmpDetect_DCL) {
} }
TEST_F(InteractiveDetectorTest, LongTaskBeforeFMPDoesNotAffectTTI) { TEST_F(InteractiveDetectorTest, LongTaskBeforeFMPDoesNotAffectTTI) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
// Network is forever quiet for this test. // Network is forever quiet for this test.
SetActiveConnections(1); SetActiveConnections(1);
...@@ -239,7 +244,7 @@ TEST_F(InteractiveDetectorTest, LongTaskBeforeFMPDoesNotAffectTTI) { ...@@ -239,7 +244,7 @@ TEST_F(InteractiveDetectorTest, LongTaskBeforeFMPDoesNotAffectTTI) {
} }
TEST_F(InteractiveDetectorTest, DCLDoesNotResetTimer) { TEST_F(InteractiveDetectorTest, DCLDoesNotResetTimer) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
// Network is forever quiet for this test. // Network is forever quiet for this test.
SetActiveConnections(1); SetActiveConnections(1);
...@@ -256,7 +261,7 @@ TEST_F(InteractiveDetectorTest, DCLDoesNotResetTimer) { ...@@ -256,7 +261,7 @@ TEST_F(InteractiveDetectorTest, DCLDoesNotResetTimer) {
} }
TEST_F(InteractiveDetectorTest, DCL_FMP_FmpDetect_LT) { TEST_F(InteractiveDetectorTest, DCL_FMP_FmpDetect_LT) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
// Network is forever quiet for this test. // Network is forever quiet for this test.
SetActiveConnections(1); SetActiveConnections(1);
...@@ -273,7 +278,7 @@ TEST_F(InteractiveDetectorTest, DCL_FMP_FmpDetect_LT) { ...@@ -273,7 +278,7 @@ TEST_F(InteractiveDetectorTest, DCL_FMP_FmpDetect_LT) {
} }
TEST_F(InteractiveDetectorTest, DCL_FMP_LT_FmpDetect) { TEST_F(InteractiveDetectorTest, DCL_FMP_LT_FmpDetect) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
// Network is forever quiet for this test. // Network is forever quiet for this test.
SetActiveConnections(1); SetActiveConnections(1);
...@@ -290,7 +295,7 @@ TEST_F(InteractiveDetectorTest, DCL_FMP_LT_FmpDetect) { ...@@ -290,7 +295,7 @@ TEST_F(InteractiveDetectorTest, DCL_FMP_LT_FmpDetect) {
} }
TEST_F(InteractiveDetectorTest, FMP_FmpDetect_LT_DCL) { TEST_F(InteractiveDetectorTest, FMP_FmpDetect_LT_DCL) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
// Network is forever quiet for this test. // Network is forever quiet for this test.
SetActiveConnections(1); SetActiveConnections(1);
...@@ -307,7 +312,7 @@ TEST_F(InteractiveDetectorTest, FMP_FmpDetect_LT_DCL) { ...@@ -307,7 +312,7 @@ TEST_F(InteractiveDetectorTest, FMP_FmpDetect_LT_DCL) {
} }
TEST_F(InteractiveDetectorTest, DclIsMoreThan5sAfterFMP) { TEST_F(InteractiveDetectorTest, DclIsMoreThan5sAfterFMP) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
// Network is forever quiet for this test. // Network is forever quiet for this test.
SetActiveConnections(1); SetActiveConnections(1);
...@@ -328,7 +333,7 @@ TEST_F(InteractiveDetectorTest, DclIsMoreThan5sAfterFMP) { ...@@ -328,7 +333,7 @@ TEST_F(InteractiveDetectorTest, DclIsMoreThan5sAfterFMP) {
} }
TEST_F(InteractiveDetectorTest, NetworkBusyBlocksTTIEvenWhenMainThreadQuiet) { TEST_F(InteractiveDetectorTest, NetworkBusyBlocksTTIEvenWhenMainThreadQuiet) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
SetActiveConnections(1); SetActiveConnections(1);
SimulateDOMContentLoadedEnd(t0 + TimeDelta::FromSeconds(2)); SimulateDOMContentLoadedEnd(t0 + TimeDelta::FromSeconds(2));
...@@ -353,7 +358,7 @@ TEST_F(InteractiveDetectorTest, NetworkBusyBlocksTTIEvenWhenMainThreadQuiet) { ...@@ -353,7 +358,7 @@ TEST_F(InteractiveDetectorTest, NetworkBusyBlocksTTIEvenWhenMainThreadQuiet) {
} }
TEST_F(InteractiveDetectorTest, LongEnoughQuietWindowBetweenFMPAndFmpDetect) { TEST_F(InteractiveDetectorTest, LongEnoughQuietWindowBetweenFMPAndFmpDetect) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
SetActiveConnections(1); SetActiveConnections(1);
SimulateDOMContentLoadedEnd(t0 + TimeDelta::FromSeconds(2)); SimulateDOMContentLoadedEnd(t0 + TimeDelta::FromSeconds(2));
...@@ -374,7 +379,7 @@ TEST_F(InteractiveDetectorTest, LongEnoughQuietWindowBetweenFMPAndFmpDetect) { ...@@ -374,7 +379,7 @@ TEST_F(InteractiveDetectorTest, LongEnoughQuietWindowBetweenFMPAndFmpDetect) {
} }
TEST_F(InteractiveDetectorTest, NetworkBusyEndIsNotTTI) { TEST_F(InteractiveDetectorTest, NetworkBusyEndIsNotTTI) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
SetActiveConnections(1); SetActiveConnections(1);
SimulateDOMContentLoadedEnd(t0 + TimeDelta::FromSeconds(2)); SimulateDOMContentLoadedEnd(t0 + TimeDelta::FromSeconds(2));
...@@ -397,7 +402,7 @@ TEST_F(InteractiveDetectorTest, NetworkBusyEndIsNotTTI) { ...@@ -397,7 +402,7 @@ TEST_F(InteractiveDetectorTest, NetworkBusyEndIsNotTTI) {
} }
TEST_F(InteractiveDetectorTest, LateLongTaskWithLateFMPDetection) { TEST_F(InteractiveDetectorTest, LateLongTaskWithLateFMPDetection) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
SetActiveConnections(1); SetActiveConnections(1);
SimulateDOMContentLoadedEnd(t0 + TimeDelta::FromSeconds(2)); SimulateDOMContentLoadedEnd(t0 + TimeDelta::FromSeconds(2));
...@@ -417,7 +422,7 @@ TEST_F(InteractiveDetectorTest, LateLongTaskWithLateFMPDetection) { ...@@ -417,7 +422,7 @@ TEST_F(InteractiveDetectorTest, LateLongTaskWithLateFMPDetection) {
} }
TEST_F(InteractiveDetectorTest, IntermittentNetworkBusyBlocksTTI) { TEST_F(InteractiveDetectorTest, IntermittentNetworkBusyBlocksTTI) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
SetActiveConnections(1); SetActiveConnections(1);
SimulateDOMContentLoadedEnd(t0 + TimeDelta::FromSeconds(2)); SimulateDOMContentLoadedEnd(t0 + TimeDelta::FromSeconds(2));
...@@ -449,7 +454,7 @@ TEST_F(InteractiveDetectorTest, IntermittentNetworkBusyBlocksTTI) { ...@@ -449,7 +454,7 @@ TEST_F(InteractiveDetectorTest, IntermittentNetworkBusyBlocksTTI) {
} }
TEST_F(InteractiveDetectorTest, InvalidatingUserInput) { TEST_F(InteractiveDetectorTest, InvalidatingUserInput) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
// Network is forever quiet for this test. // Network is forever quiet for this test.
SetActiveConnections(1); SetActiveConnections(1);
...@@ -471,7 +476,7 @@ TEST_F(InteractiveDetectorTest, InvalidatingUserInput) { ...@@ -471,7 +476,7 @@ TEST_F(InteractiveDetectorTest, InvalidatingUserInput) {
} }
TEST_F(InteractiveDetectorTest, InvalidatingUserInputClampedAtNavStart) { TEST_F(InteractiveDetectorTest, InvalidatingUserInputClampedAtNavStart) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
// Network is forever quiet for this test. // Network is forever quiet for this test.
SetActiveConnections(1); SetActiveConnections(1);
...@@ -490,7 +495,7 @@ TEST_F(InteractiveDetectorTest, InvalidatingUserInputClampedAtNavStart) { ...@@ -490,7 +495,7 @@ TEST_F(InteractiveDetectorTest, InvalidatingUserInputClampedAtNavStart) {
} }
TEST_F(InteractiveDetectorTest, InvalidatedFMP) { TEST_F(InteractiveDetectorTest, InvalidatedFMP) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
SimulateNavigationStart(t0); SimulateNavigationStart(t0);
// Network is forever quiet for this test. // Network is forever quiet for this test.
SetActiveConnections(1); SetActiveConnections(1);
...@@ -514,7 +519,7 @@ TEST_F(InteractiveDetectorTest, InvalidatedFMP) { ...@@ -514,7 +519,7 @@ TEST_F(InteractiveDetectorTest, InvalidatedFMP) {
} }
TEST_F(InteractiveDetectorTest, TaskLongerThan5sBlocksTTI) { TEST_F(InteractiveDetectorTest, TaskLongerThan5sBlocksTTI) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
GetDetector()->SetNavigationStartTime(t0); GetDetector()->SetNavigationStartTime(t0);
SimulateDOMContentLoadedEnd(t0 + TimeDelta::FromSeconds(2)); SimulateDOMContentLoadedEnd(t0 + TimeDelta::FromSeconds(2));
...@@ -534,7 +539,7 @@ TEST_F(InteractiveDetectorTest, TaskLongerThan5sBlocksTTI) { ...@@ -534,7 +539,7 @@ TEST_F(InteractiveDetectorTest, TaskLongerThan5sBlocksTTI) {
} }
TEST_F(InteractiveDetectorTest, LongTaskAfterTTIDoesNothing) { TEST_F(InteractiveDetectorTest, LongTaskAfterTTIDoesNothing) {
TimeTicks t0 = CurrentTimeTicks(); TimeTicks t0 = Now();
GetDetector()->SetNavigationStartTime(t0); GetDetector()->SetNavigationStartTime(t0);
SimulateDOMContentLoadedEnd(t0 + TimeDelta::FromSeconds(2)); SimulateDOMContentLoadedEnd(t0 + TimeDelta::FromSeconds(2));
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "third_party/blink/renderer/core/timing/event_timing.h" #include "third_party/blink/renderer/core/timing/event_timing.h"
#include "base/time/tick_clock.h"
#include "third_party/blink/renderer/core/dom/events/event.h" #include "third_party/blink/renderer/core/dom/events/event.h"
#include "third_party/blink/renderer/core/events/pointer_event.h" #include "third_party/blink/renderer/core/events/pointer_event.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h" #include "third_party/blink/renderer/core/frame/local_dom_window.h"
...@@ -13,6 +14,15 @@ ...@@ -13,6 +14,15 @@
#include "third_party/blink/renderer/platform/runtime_enabled_features.h" #include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/wtf/time.h" #include "third_party/blink/renderer/platform/wtf/time.h"
namespace {
const base::TickClock* g_clock_for_testing = nullptr;
static base::TimeTicks Now() {
return g_clock_for_testing ? g_clock_for_testing->NowTicks()
: CurrentTimeTicks();
}
} // namespace
namespace blink { namespace blink {
bool ShouldLogEvent(const Event& event) { bool ShouldLogEvent(const Event& event) {
...@@ -69,11 +79,11 @@ std::unique_ptr<EventTiming> EventTiming::Create(LocalDOMWindow* window, ...@@ -69,11 +79,11 @@ std::unique_ptr<EventTiming> EventTiming::Create(LocalDOMWindow* window,
if (!should_report_for_event_timing && !should_log_event) if (!should_report_for_event_timing && !should_log_event)
return nullptr; return nullptr;
TimeTicks processing_start = CurrentTimeTicks();
TimeTicks event_timestamp = TimeTicks event_timestamp =
event.IsPointerEvent() ? ToPointerEvent(&event)->OldestPlatformTimeStamp() event.IsPointerEvent() ? ToPointerEvent(&event)->OldestPlatformTimeStamp()
: event.PlatformTimeStamp(); : event.PlatformTimeStamp();
base::TimeTicks processing_start = Now();
if (should_log_event) { if (should_log_event) {
Document* document = Document* document =
DynamicTo<Document>(performance->GetExecutionContext()); DynamicTo<Document>(performance->GetExecutionContext());
...@@ -93,8 +103,13 @@ std::unique_ptr<EventTiming> EventTiming::Create(LocalDOMWindow* window, ...@@ -93,8 +103,13 @@ std::unique_ptr<EventTiming> EventTiming::Create(LocalDOMWindow* window,
void EventTiming::DidDispatchEvent(const Event& event) { void EventTiming::DidDispatchEvent(const Event& event) {
performance_->RegisterEventTiming(event.type(), event_timestamp_, performance_->RegisterEventTiming(event.type(), event_timestamp_,
processing_start_, CurrentTimeTicks(), processing_start_, Now(),
event.cancelable()); event.cancelable());
} }
// static
void EventTiming::SetTickClockForTesting(const base::TickClock* clock) {
g_clock_for_testing = clock;
}
} // namespace blink } // namespace blink
...@@ -35,6 +35,9 @@ class CORE_EXPORT EventTiming final { ...@@ -35,6 +35,9 @@ class CORE_EXPORT EventTiming final {
// Notifies the Performance object that the event has been dispatched. // Notifies the Performance object that the event has been dispatched.
void DidDispatchEvent(const Event&); void DidDispatchEvent(const Event&);
// The caller owns the |clock| which must outlive the EventTiming.
static void SetTickClockForTesting(const base::TickClock* clock);
private: private:
// The time the first event handler or default action started to execute. // The time the first event handler or default action started to execute.
TimeTicks processing_start_; TimeTicks processing_start_;
......
...@@ -205,6 +205,7 @@ _CONFIG = [ ...@@ -205,6 +205,7 @@ _CONFIG = [
'base::Clock', 'base::Clock',
'base::DefaultClock', 'base::DefaultClock',
'base::DefaultTickClock', 'base::DefaultTickClock',
'base::TestMockTimeTaskRunner',
'base::TickClock', 'base::TickClock',
# cc painting types. # cc painting types.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment