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

cc / metrics / total_frame_counter_unittest.cc [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/metrics/total_frame_counter.h"
#include "components/viz/common/frame_sinks/begin_frame_args.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace cc {
namespace {

const uint64_t kSourceId = 1;

TEST(TotalFrameCounterTest, Basic) {
  TotalFrameCounter counter;
  uint64_t sequence_number = 1;
  auto frame_time = base::TimeTicks::Now();
  const auto interval = base::Milliseconds(16.67);

  auto args = viz::BeginFrameArgs::Create(
      BEGINFRAME_FROM_HERE, kSourceId, sequence_number++, frame_time,
      frame_time + interval, interval, viz::BeginFrameArgs::NORMAL);
  counter.OnShow(frame_time);
  counter.OnBeginFrame(args);

  auto advance = base::Seconds(1);
  frame_time += advance;
  counter.OnHide(frame_time);
  EXPECT_EQ(counter.total_frames(), 60u);
}

TEST(TotalFrameCounterTest, BeginFrameIntervalChange) {
  TotalFrameCounter counter;
  uint64_t sequence_number = 1;
  auto frame_time = base::TimeTicks::Now();
  // Use intervals that divide evenly into one second to avoid rounding issues.
  auto interval = base::Milliseconds(20);

  // Make the page visible at 50fps.
  auto args = viz::BeginFrameArgs::Create(
      BEGINFRAME_FROM_HERE, kSourceId, sequence_number++, frame_time,
      frame_time + interval, interval, viz::BeginFrameArgs::NORMAL);
  counter.OnShow(frame_time);
  counter.OnBeginFrame(args);

  // After 10 seconds, change the frame rate to be 100fps.
  interval = base::Milliseconds(10);
  frame_time += base::Seconds(10);
  args = viz::BeginFrameArgs::Create(
      BEGINFRAME_FROM_HERE, kSourceId, sequence_number++, frame_time,
      frame_time + interval, interval, viz::BeginFrameArgs::NORMAL);
  counter.OnBeginFrame(args);

  // Hide the page after 10 more seconds.
  auto advance = base::Seconds(10);
  frame_time += advance;
  counter.OnHide(frame_time);
  EXPECT_EQ(counter.total_frames(), 1500u);
}

TEST(TotalFrameCounterTest, VisibilityChange) {
  TotalFrameCounter counter;
  uint64_t sequence_number = 1;
  auto frame_time = base::TimeTicks::Now();
  auto interval = base::Milliseconds(16.67);

  // Make the page visible at the default frame rate.
  auto args = viz::BeginFrameArgs::Create(
      BEGINFRAME_FROM_HERE, kSourceId, sequence_number++, frame_time,
      frame_time + interval, interval, viz::BeginFrameArgs::NORMAL);
  counter.OnShow(frame_time);
  counter.OnBeginFrame(args);

  // Hide the page after 10 seconds.
  frame_time += base::Seconds(10);
  counter.OnHide(frame_time);
  EXPECT_EQ(counter.total_frames(), 600u);

  // After 20 more seconds, make the page visible again and keep it visible for
  // 5 more seconds.
  frame_time += base::Seconds(20);
  counter.OnShow(frame_time);
  args = viz::BeginFrameArgs::Create(
      BEGINFRAME_FROM_HERE, kSourceId, sequence_number++, frame_time,
      frame_time + interval, interval, viz::BeginFrameArgs::NORMAL);
  counter.OnBeginFrame(args);

  frame_time += base::Seconds(5);
  counter.OnHide(frame_time);
  EXPECT_EQ(counter.total_frames(), 900u);
}

}  // namespace
}  // namespace cc