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

gin / time_clamper_unittest.cc [blame]

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

#include "gin/time_clamper.h"

#include "base/time/time.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace gin {

TEST(TimeClamperTest, CurrentClockTimeMilliseconds) {
  TimeClamper clamper(1);
  EXPECT_EQ(0u, clamper.ClampToMillis(base::Time::UnixEpoch()));

  // 1 us after unix epoch should round to 0.
  base::Time time = base::Time::UnixEpoch() + base::Microseconds(1);
  EXPECT_EQ(0u, clamper.ClampToMillis(time));

  time = base::Time::UnixEpoch() + base::Milliseconds(1) -
         base::Microseconds(TimeClamper::kResolutionMicros - 1);
  EXPECT_EQ(0u, clamper.ClampToMillis(time));

  time = base::Time::UnixEpoch() + base::Milliseconds(1);
  EXPECT_EQ(1u, clamper.ClampToMillis(time));
}

#if !BUILDFLAG(IS_ANDROID)
TEST(TimeClamperTest, CurrentClockTimeMillisecondsThreshold) {
  // This test assumes a time clamp of 5. If the clamp changes the time values
  // will need to be adjusted.
  ASSERT_EQ(5, TimeClamper::kResolutionMicros);
  TimeClamper clamper(1);
  base::Time time = base::Time::UnixEpoch() + base::Milliseconds(1) -
                    base::Microseconds(TimeClamper::kResolutionMicros);
  EXPECT_EQ(0u, clamper.ClampToMillis(time));

  time = base::Time::UnixEpoch() + base::Milliseconds(1) -
         base::Microseconds(TimeClamper::kResolutionMicros) +
         base::Microseconds(1);
  EXPECT_EQ(0u, clamper.ClampToMillis(time));

  time = base::Time::UnixEpoch() + base::Milliseconds(1) -
         base::Microseconds(TimeClamper::kResolutionMicros) +
         base::Microseconds(2);
  EXPECT_EQ(1u, clamper.ClampToMillis(time));
}

TEST(TimeClamperTest, CurrentClockTimeMillisecondsThresholdNegative) {
  // This test assumes a time clamp of 5. If the clamp changes the time values
  // will need to be adjusted.
  // be adjusted.
  ASSERT_EQ(5, TimeClamper::kResolutionMicros);
  TimeClamper clamper(1);
  base::Time time = base::Time::UnixEpoch() + base::Milliseconds(-1) +
                    base::Microseconds(TimeClamper::kResolutionMicros);
  EXPECT_EQ(0u, clamper.ClampToMillis(time));

  time = base::Time::UnixEpoch() + base::Milliseconds(-1) +
         base::Microseconds(TimeClamper::kResolutionMicros) -
         base::Microseconds(1);
  EXPECT_EQ(0u, clamper.ClampToMillis(time));

  time = base::Time::UnixEpoch() + base::Milliseconds(-1) +
         base::Microseconds(TimeClamper::kResolutionMicros) -
         base::Microseconds(2);
  EXPECT_EQ(static_cast<int64_t>(-1), clamper.ClampToMillis(time));
}

TEST(TimeClamperTest, CurrentClockTimeMillisecondsHighResolution) {
  TimeClamper clamper(1);
  EXPECT_EQ(0, clamper.ClampToMillisHighResolution(base::Time::UnixEpoch()));

  base::Time time = base::Time::UnixEpoch() + base::Microseconds(1);
  EXPECT_EQ(0, clamper.ClampToMillisHighResolution(time));

  time = base::Time::UnixEpoch() +
         base::Microseconds(TimeClamper::kResolutionMicros);
  EXPECT_EQ(0.005, clamper.ClampToMillisHighResolution(time));

  time = base::Time::UnixEpoch() + base::Milliseconds(1) -
         base::Microseconds(TimeClamper::kResolutionMicros - 1);
  EXPECT_EQ(0.995, clamper.ClampToMillisHighResolution(time));

  time = base::Time::UnixEpoch() + base::Milliseconds(1) -
         base::Microseconds(TimeClamper::kResolutionMicros - 3);
  EXPECT_EQ(1, clamper.ClampToMillisHighResolution(time));

  time = base::Time::UnixEpoch() + base::Milliseconds(1);
  EXPECT_EQ(1, clamper.ClampToMillisHighResolution(time));

  time =
      base::Time::UnixEpoch() + base::Milliseconds(1) + base::Microseconds(1);
  EXPECT_EQ(1.005, clamper.ClampToMillisHighResolution(time));
}
#endif

}  // namespace gin