summaryrefslogtreecommitdiff
path: root/chromium/third_party/WebKit/Source/core/loader/InteractiveDetectorTest.cpp
blob: 8ccffd319c5e54d49d5db078ae16c301632a7648 (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// 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 "core/loader/InteractiveDetector.h"

#include "core/dom/Document.h"
#include "core/paint/FirstMeaningfulPaintDetector.h"
#include "core/testing/DummyPageHolder.h"
#include "core/testing/PageTestBase.h"
#include "platform/CrossThreadFunctional.h"
#include "platform/scheduler/renderer/renderer_scheduler_impl.h"
#include "platform/testing/TestingPlatformSupportWithMockScheduler.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace blink {

class NetworkActivityCheckerForTest
    : public InteractiveDetector::NetworkActivityChecker {
 public:
  NetworkActivityCheckerForTest(Document* document)
      : InteractiveDetector::NetworkActivityChecker(document) {}

  virtual void SetActiveConnections(int active_connections) {
    active_connections_ = active_connections;
  };
  int GetActiveConnections() override;

 private:
  int active_connections_ = 0;
};

int NetworkActivityCheckerForTest::GetActiveConnections() {
  return active_connections_;
}

struct TaskTiming {
  double start;
  double end;
  TaskTiming(double start, double end) : start(start), end(end) {}
};

class InteractiveDetectorTest : public ::testing::Test {
 public:
  InteractiveDetectorTest() {
    platform_->AdvanceClockSeconds(1);
    dummy_page_holder_ = DummyPageHolder::Create();

    Document* document = &dummy_page_holder_->GetDocument();

    detector_ = new InteractiveDetector(
        *document, new NetworkActivityCheckerForTest(document));

    // By this time, the DummyPageHolder has created an InteractiveDetector, and
    // sent DOMContentLoadedEnd. We overwrite it with our new
    // InteractiveDetector, which won't have received any timestamps.
    Supplement<Document>::ProvideTo(
        *document, InteractiveDetector::SupplementName(), detector_.Get());

    // Ensure the document is using the injected InteractiveDetector.
    DCHECK_EQ(detector_, InteractiveDetector::From(*document));
  }

  // Public because it's executed on a task queue.
  void DummyTaskWithDuration(double duration_seconds) {
    platform_->AdvanceClockSeconds(duration_seconds);
    dummy_task_end_time_ = CurrentTimeTicksInSeconds();
  }

 protected:
  InteractiveDetector* GetDetector() { return detector_; }

  double GetDummyTaskEndTime() { return dummy_task_end_time_; }

  NetworkActivityCheckerForTest* GetNetworkActivityChecker() {
    // We know in this test context that network_activity_checker_ is an
    // instance of NetworkActivityCheckerForTest, so this static_cast is safe.
    return static_cast<NetworkActivityCheckerForTest*>(
        detector_->network_activity_checker_.get());
  }

  void SimulateNavigationStart(double nav_start_time) {
    RunTillTimestamp(nav_start_time);
    detector_->SetNavigationStartTime(nav_start_time);
  }

  void SimulateLongTask(double start, double end) {
    CHECK(end - start >= 0.05);
    RunTillTimestamp(end);
    detector_->OnLongTaskDetected(start, end);
  }

  void SimulateDOMContentLoadedEnd(double dcl_time) {
    RunTillTimestamp(dcl_time);
    detector_->OnDomContentLoadedEnd(dcl_time);
  }

  void SimulateFMPDetected(double fmp_time, double detection_time) {
    RunTillTimestamp(detection_time);
    detector_->OnFirstMeaningfulPaintDetected(
        fmp_time, FirstMeaningfulPaintDetector::kNoUserInput);
  }

  void SimulateInteractiveInvalidatingInput(double timestamp) {
    RunTillTimestamp(timestamp);
    detector_->OnInvalidatingInputEvent(timestamp);
  }

  void RunTillTimestamp(double target_time) {
    double current_time = CurrentTimeTicksInSeconds();
    platform_->RunForPeriodSeconds(std::max(0.0, target_time - current_time));
  }

  int GetActiveConnections() {
    return GetNetworkActivityChecker()->GetActiveConnections();
  }

  void SetActiveConnections(int active_connections) {
    GetNetworkActivityChecker()->SetActiveConnections(active_connections);
  }

  void SimulateResourceLoadBegin(double load_begin_time) {
    RunTillTimestamp(load_begin_time);
    detector_->OnResourceLoadBegin(load_begin_time);
    // ActiveConnections is incremented after detector runs OnResourceLoadBegin;
    SetActiveConnections(GetActiveConnections() + 1);
  }

  void SimulateResourceLoadEnd(double load_finish_time) {
    RunTillTimestamp(load_finish_time);
    int active_connections = GetActiveConnections();
    SetActiveConnections(active_connections - 1);
    detector_->OnResourceLoadEnd(load_finish_time);
  }

  double GetInteractiveTime() { return detector_->GetInteractiveTime(); }

  ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
      platform_;

 private:
  Persistent<InteractiveDetector> detector_;
  std::unique_ptr<DummyPageHolder> dummy_page_holder_;
  double dummy_task_end_time_ = 0.0;
};

// Note: The tests currently assume kTimeToInteractiveWindowSeconds is 5
// seconds. The window size is unlikely to change, and this makes the test
// scenarios significantly easier to write.

// Note: Some of the tests are named W_X_Y_Z, where W, X, Y, Z can any of the
// following events:
// FMP: First Meaningful Paint
// DCL: DomContentLoadedEnd
// FmpDetect: Detection of FMP. FMP is not detected in realtime.
// LT: Long Task
// The name shows the ordering of these events in the test.

TEST_F(InteractiveDetectorTest, FMP_DCL_FmpDetect) {
  double t0 = CurrentTimeTicksInSeconds();
  SimulateNavigationStart(t0);
  // Network is forever quiet for this test.
  SetActiveConnections(1);
  SimulateDOMContentLoadedEnd(t0 + 3.0);
  SimulateFMPDetected(/* fmp_time */ t0 + 5.0, /* detection_time */ t0 + 7.0);
  // Run until 5 seconds after FMP.
  RunTillTimestamp((t0 + 5.0) + 5.0 + 0.1);
  // Reached TTI at FMP.
  EXPECT_EQ(GetInteractiveTime(), t0 + 5.0);
}

TEST_F(InteractiveDetectorTest, DCL_FMP_FmpDetect) {
  double t0 = CurrentTimeTicksInSeconds();
  SimulateNavigationStart(t0);
  // Network is forever quiet for this test.
  SetActiveConnections(1);
  SimulateDOMContentLoadedEnd(t0 + 5.0);
  SimulateFMPDetected(/* fmp_time */ t0 + 3.0, /* detection_time */ t0 + 7.0);
  // Run until 5 seconds after FMP.
  RunTillTimestamp((t0 + 3.0) + 5.0 + 0.1);
  // Reached TTI at DCL.
  EXPECT_EQ(GetInteractiveTime(), t0 + 5.0);
}

TEST_F(InteractiveDetectorTest, InstantDetectionAtFmpDetectIfPossible) {
  double t0 = CurrentTimeTicksInSeconds();
  SimulateNavigationStart(t0);
  // Network is forever quiet for this test.
  SetActiveConnections(1);
  SimulateDOMContentLoadedEnd(t0 + 5.0);
  SimulateFMPDetected(/* fmp_time */ t0 + 3.0, /* detection_time */ t0 + 10.0);
  // Although we just detected FMP, the FMP timestamp is more than
  // kTimeToInteractiveWindowSeconds earlier. We should instantaneously
  // detect that we reached TTI at DCL.
  EXPECT_EQ(GetInteractiveTime(), t0 + 5.0);
}

TEST_F(InteractiveDetectorTest, FmpDetectFiresAfterLateLongTask) {
  double t0 = CurrentTimeTicksInSeconds();
  SimulateNavigationStart(t0);
  // Network is forever quiet for this test.
  SetActiveConnections(1);
  SimulateDOMContentLoadedEnd(t0 + 3.0);
  SimulateLongTask(t0 + 9.0, t0 + 9.1);
  SimulateFMPDetected(/* fmp_time */ t0 + 3.0, /* detection_time */ t0 + 10.0);
  // There is a 5 second quiet window after fmp_time - the long task is 6s
  // seconds after fmp_time. We should instantly detect we reached TTI at FMP.
  EXPECT_EQ(GetInteractiveTime(), t0 + 3.0);
}

TEST_F(InteractiveDetectorTest, FMP_FmpDetect_DCL) {
  double t0 = CurrentTimeTicksInSeconds();
  SimulateNavigationStart(t0);
  // Network is forever quiet for this test.
  SetActiveConnections(1);
  SimulateFMPDetected(/* fmp_time */ t0 + 3.0, /* detection_time */ t0 + 5.0);
  SimulateDOMContentLoadedEnd(t0 + 9.0);
  // TTI reached at DCL.
  EXPECT_EQ(GetInteractiveTime(), t0 + 9.0);
}

TEST_F(InteractiveDetectorTest, LongTaskBeforeFMPDoesNotAffectTTI) {
  double t0 = CurrentTimeTicksInSeconds();
  SimulateNavigationStart(t0);
  // Network is forever quiet for this test.
  SetActiveConnections(1);
  SimulateDOMContentLoadedEnd(t0 + 3.0);
  SimulateLongTask(t0 + 5.1, t0 + 5.2);
  SimulateFMPDetected(/* fmp_time */ t0 + 8.0, /* detection_time */ t0 + 9.0);
  // Run till 5 seconds after FMP.
  RunTillTimestamp((t0 + 8.0) + 5.0 + 0.1);
  // TTI reached at FMP.
  EXPECT_EQ(GetInteractiveTime(), t0 + 8.0);
}

TEST_F(InteractiveDetectorTest, DCLDoesNotResetTimer) {
  double t0 = CurrentTimeTicksInSeconds();
  SimulateNavigationStart(t0);
  // Network is forever quiet for this test.
  SetActiveConnections(1);
  SimulateFMPDetected(/* fmp_time */ t0 + 3.0, /* detection_time */ t0 + 4.0);
  SimulateLongTask(t0 + 5.0, t0 + 5.1);
  SimulateDOMContentLoadedEnd(t0 + 8.0);
  // Run till 5 seconds after long task end.
  RunTillTimestamp((t0 + 5.1) + 5.0 + 0.1);
  // TTI Reached at DCL.
  EXPECT_EQ(GetInteractiveTime(), t0 + 8.0);
}

TEST_F(InteractiveDetectorTest, DCL_FMP_FmpDetect_LT) {
  double t0 = CurrentTimeTicksInSeconds();
  SimulateNavigationStart(t0);
  // Network is forever quiet for this test.
  SetActiveConnections(1);
  SimulateDOMContentLoadedEnd(t0 + 3.0);
  SimulateFMPDetected(/* fmp_time */ t0 + 4.0, /* detection_time */ t0 + 5.0);
  SimulateLongTask(t0 + 7.0, t0 + 7.1);
  // Run till 5 seconds after long task end.
  RunTillTimestamp((t0 + 7.1) + 5.0 + 0.1);
  // TTI reached at long task end.
  EXPECT_EQ(GetInteractiveTime(), t0 + 7.1);
}

TEST_F(InteractiveDetectorTest, DCL_FMP_LT_FmpDetect) {
  double t0 = CurrentTimeTicksInSeconds();
  SimulateNavigationStart(t0);
  // Network is forever quiet for this test.
  SetActiveConnections(1);
  SimulateDOMContentLoadedEnd(t0 + 3.0);
  SimulateLongTask(t0 + 7.0, t0 + 7.1);
  SimulateFMPDetected(/* fmp_time */ t0 + 3.0, /* detection_time */ t0 + 5.0);
  // Run till 5 seconds after long task end.
  RunTillTimestamp((t0 + 7.1) + 5.0 + 0.1);
  // TTI reached at long task end.
  EXPECT_EQ(GetInteractiveTime(), t0 + 7.1);
}

TEST_F(InteractiveDetectorTest, FMP_FmpDetect_LT_DCL) {
  double t0 = CurrentTimeTicksInSeconds();
  SimulateNavigationStart(t0);
  // Network is forever quiet for this test.
  SetActiveConnections(1);
  SimulateFMPDetected(/* fmp_time */ t0 + 3.0, /* detection_time */ t0 + 4.0);
  SimulateLongTask(t0 + 7.0, t0 + 7.1);
  SimulateDOMContentLoadedEnd(t0 + 8.0);
  // Run till 5 seconds after long task end.
  RunTillTimestamp((t0 + 7.1) + 5.0 + 0.1);
  // TTI reached at DCL. Note that we do not need to wait for DCL + 5 seconds.
  EXPECT_EQ(GetInteractiveTime(), t0 + 8.0);
}

TEST_F(InteractiveDetectorTest, DclIsMoreThan5sAfterFMP) {
  double t0 = CurrentTimeTicksInSeconds();
  SimulateNavigationStart(t0);
  // Network is forever quiet for this test.
  SetActiveConnections(1);
  SimulateFMPDetected(/* fmp_time */ t0 + 3.0, /* detection_time */ t0 + 4.0);
  SimulateLongTask(t0 + 7.0, t0 + 7.1);  // Long task 1.
  SimulateDOMContentLoadedEnd(t0 + 10.0);
  // Have not reached TTI yet.
  EXPECT_EQ(GetInteractiveTime(), 0.0);
  SimulateLongTask(t0 + 11.0, t0 + 11.1);  // Long task 2.
  // Run till long task 2 end + 5 seconds.
  RunTillTimestamp((t0 + 11.1) + 5.0 + 0.1);
  // TTI reached at long task 2 end.
  EXPECT_EQ(GetInteractiveTime(), (t0 + 11.1));
}

TEST_F(InteractiveDetectorTest, NetworkBusyBlocksTTIEvenWhenMainThreadQuiet) {
  double t0 = CurrentTimeTicksInSeconds();
  SimulateNavigationStart(t0);
  SetActiveConnections(1);
  SimulateDOMContentLoadedEnd(t0 + 2.0);
  SimulateResourceLoadBegin(t0 + 3.4);  // Request 2 start.
  SimulateResourceLoadBegin(t0 + 3.5);  // Request 3 start. Network busy.
  SimulateFMPDetected(/* fmp_time */ t0 + 3.0, /* detection_time */ t0 + 4.0);
  SimulateLongTask(t0 + 7.0, t0 + 7.1);  // Long task 1.
  SimulateResourceLoadEnd(t0 + 12.2);    // Network quiet.
  // Network busy kept page from reaching TTI..
  EXPECT_EQ(GetInteractiveTime(), 0.0);
  SimulateLongTask(t0 + 13.0, t0 + 13.1);  // Long task 2.
  // Run till 5 seconds after long task 2 end.
  RunTillTimestamp((t0 + 13.1) + 5.0 + 0.1);
  EXPECT_EQ(GetInteractiveTime(), (t0 + 13.1));
}

TEST_F(InteractiveDetectorTest, LongEnoughQuietWindowBetweenFMPAndFmpDetect) {
  double t0 = CurrentTimeTicksInSeconds();
  SimulateNavigationStart(t0);
  SetActiveConnections(1);
  SimulateDOMContentLoadedEnd(t0 + 2.0);
  SimulateLongTask(t0 + 2.1, t0 + 2.2);  // Long task 1.
  SimulateLongTask(t0 + 8.2, t0 + 8.3);  // Long task 2.
  SimulateResourceLoadBegin(t0 + 8.4);   // Request 2 start.
  SimulateResourceLoadBegin(t0 + 8.5);   // Request 3 start. Network busy.
  SimulateFMPDetected(/* fmp_time */ t0 + 3.0, /* detection_time */ t0 + 10.0);
  // Even though network is currently busy and we have long task finishing
  // recently, we should be able to detect that the page already achieved TTI at
  // FMP.
  EXPECT_EQ(GetInteractiveTime(), t0 + 3.0);
}

TEST_F(InteractiveDetectorTest, NetworkBusyEndIsNotTTI) {
  double t0 = CurrentTimeTicksInSeconds();
  SimulateNavigationStart(t0);
  SetActiveConnections(1);
  SimulateDOMContentLoadedEnd(t0 + 2.0);
  SimulateResourceLoadBegin(t0 + 3.4);  // Request 2 start.
  SimulateResourceLoadBegin(t0 + 3.5);  // Request 3 start. Network busy.
  SimulateFMPDetected(/* fmp_time */ t0 + 3.0, /* detection_time */ t0 + 4.0);
  SimulateLongTask(t0 + 7.0, t0 + 7.1);    // Long task 1.
  SimulateLongTask(t0 + 13.0, t0 + 13.1);  // Long task 2.
  SimulateResourceLoadEnd(t0 + 14.0);      // Network quiet.
  // Run till 5 seconds after network busy end.
  RunTillTimestamp((t0 + 14.0) + 5.0 + 0.1);
  // TTI reached at long task 2 end, NOT at network busy end.
  EXPECT_EQ(GetInteractiveTime(), t0 + 13.1);
}

TEST_F(InteractiveDetectorTest, LateLongTaskWithLateFMPDetection) {
  double t0 = CurrentTimeTicksInSeconds();
  SimulateNavigationStart(t0);
  SetActiveConnections(1);
  SimulateDOMContentLoadedEnd(t0 + 2.0);
  SimulateResourceLoadBegin(t0 + 3.4);     // Request 2 start.
  SimulateResourceLoadBegin(t0 + 3.5);     // Request 3 start. Network busy.
  SimulateLongTask(t0 + 7.0, t0 + 7.1);    // Long task 1.
  SimulateResourceLoadEnd(t0 + 8.0);       // Network quiet.
  SimulateLongTask(t0 + 14.0, t0 + 14.1);  // Long task 2.
  SimulateFMPDetected(/* fmp_time */ t0 + 3.0, /* detection_time */ t0 + 20.0);
  // TTI reached at long task 1 end, NOT at long task 2 end.
  EXPECT_EQ(GetInteractiveTime(), t0 + 7.1);
}

TEST_F(InteractiveDetectorTest, IntermittentNetworkBusyBlocksTTI) {
  double t0 = CurrentTimeTicksInSeconds();
  SimulateNavigationStart(t0);
  SetActiveConnections(1);
  SimulateDOMContentLoadedEnd(t0 + 2.0);
  SimulateFMPDetected(/* fmp_time */ t0 + 3.0, /* detection_time */ t0 + 4.0);
  SimulateLongTask(t0 + 7.0, t0 + 7.1);  // Long task 1.
  SimulateResourceLoadBegin(t0 + 7.9);   // Active connections: 2
  // Network busy start.
  SimulateResourceLoadBegin(t0 + 8.0);  // Active connections: 3.
  // Network busy end.
  SimulateResourceLoadEnd(t0 + 8.5);  // Active connections: 2.
  // Network busy start.
  SimulateResourceLoadBegin(t0 + 11.0);  // Active connections: 3.
  // Network busy end.
  SimulateResourceLoadEnd(t0 + 12.0);      // Active connections: 2.
  SimulateLongTask(t0 + 14.0, t0 + 14.1);  // Long task 2.
  // Run till 5 seconds after long task 2 end.
  RunTillTimestamp((t0 + 14.1) + 5.0 + 0.1);
  // TTI reached at long task 2 end.
  EXPECT_EQ(GetInteractiveTime(), t0 + 14.1);
}

TEST_F(InteractiveDetectorTest, InvalidatingUserInput) {
  double t0 = CurrentTimeTicksInSeconds();
  SimulateNavigationStart(t0);
  // Network is forever quiet for this test.
  SetActiveConnections(1);
  SimulateDOMContentLoadedEnd(t0 + 2.0);
  SimulateFMPDetected(/* fmp_time */ t0 + 3.0, /* detection_time */ t0 + 4.0);
  SimulateInteractiveInvalidatingInput(t0 + 5.0);
  SimulateLongTask(t0 + 7.0, t0 + 7.1);  // Long task 1.
  // Run till 5 seconds after long task 2 end.
  RunTillTimestamp((t0 + 7.1) + 5.0 + 0.1);
  // We still detect interactive time on the blink side even if there is an
  // invalidating user input. Page Load Metrics filters out this value in the
  // browser process for UMA reporting.
  EXPECT_EQ(GetInteractiveTime(), t0 + 7.1);
  EXPECT_EQ(GetDetector()->GetFirstInvalidatingInputTime(), t0 + 5.0);
}

TEST_F(InteractiveDetectorTest, InvalidatedFMP) {
  double t0 = CurrentTimeTicksInSeconds();
  SimulateNavigationStart(t0);
  // Network is forever quiet for this test.
  SetActiveConnections(1);
  SimulateInteractiveInvalidatingInput(t0 + 1.0);
  SimulateDOMContentLoadedEnd(t0 + 2.0);
  RunTillTimestamp(t0 + 4.0);  // FMP Detection time.
  GetDetector()->OnFirstMeaningfulPaintDetected(
      t0 + 3.0, FirstMeaningfulPaintDetector::kHadUserInput);
  // Run till 5 seconds after FMP.
  RunTillTimestamp((t0 + 3.0) + 5.0 + 0.1);
  // Since FMP was invalidated, we do not have TTI or TTI Detection Time.
  EXPECT_EQ(GetInteractiveTime(), 0.0);
  EXPECT_EQ(GetDetector()->GetInteractiveDetectionTime(), 0.0);
  // Invalidating input timestamp is available.
  EXPECT_EQ(GetDetector()->GetFirstInvalidatingInputTime(), t0 + 1.0);
}

TEST_F(InteractiveDetectorTest, TaskLongerThan5sBlocksTTI) {
  double t0 = CurrentTimeTicksInSeconds();
  GetDetector()->SetNavigationStartTime(t0);

  SimulateDOMContentLoadedEnd(t0 + 2.0);
  SimulateFMPDetected(t0 + 3.0, t0 + 4.0);

  // Post a task with 6 seconds duration.
  PostCrossThreadTask(
      *platform_->CurrentThread()->GetWebTaskRunner(), FROM_HERE,
      CrossThreadBind(&InteractiveDetectorTest::DummyTaskWithDuration,
                      CrossThreadUnretained(this), 6.0));

  platform_->RunUntilIdle();

  // We should be able to detect TTI 5s after the end of long task.
  platform_->RunForPeriodSeconds(5.1);
  EXPECT_EQ(GetDetector()->GetInteractiveTime(), GetDummyTaskEndTime());
}

TEST_F(InteractiveDetectorTest, LongTaskAfterTTIDoesNothing) {
  double t0 = CurrentTimeTicksInSeconds();
  GetDetector()->SetNavigationStartTime(t0);

  SimulateDOMContentLoadedEnd(2.0);
  SimulateFMPDetected(t0 + 3.0, t0 + 4.0);

  // Long task 1.
  PostCrossThreadTask(
      *platform_->CurrentThread()->GetWebTaskRunner(), FROM_HERE,
      CrossThreadBind(&InteractiveDetectorTest::DummyTaskWithDuration,
                      CrossThreadUnretained(this), 0.1));

  platform_->RunUntilIdle();

  double long_task_1_end_time = GetDummyTaskEndTime();
  // We should be able to detect TTI 5s after the end of long task.
  platform_->RunForPeriodSeconds(5.1);
  EXPECT_EQ(GetDetector()->GetInteractiveTime(), long_task_1_end_time);

  // Long task 2.
  PostCrossThreadTask(
      *platform_->CurrentThread()->GetWebTaskRunner(), FROM_HERE,
      CrossThreadBind(&InteractiveDetectorTest::DummyTaskWithDuration,
                      CrossThreadUnretained(this), 0.1));

  platform_->RunUntilIdle();
  // Wait 5 seconds to see if TTI time changes.
  platform_->RunForPeriodSeconds(5.1);
  // TTI time should not change.
  EXPECT_EQ(GetDetector()->GetInteractiveTime(), long_task_1_end_time);
}

}  // namespace blink