tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestRTCStatsTimestampMaker.cpp (4398B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include <cmath>
      8 
      9 #include "gtest/gtest.h"
     10 #include "libwebrtcglue/SystemTime.h"
     11 
     12 using namespace mozilla;
     13 using dom::PerformanceService;
     14 using dom::RTCStatsTimestamp;
     15 using dom::RTCStatsTimestampMaker;
     16 
     17 static constexpr auto kWebrtcTimeOffset = webrtc::Timestamp::Seconds(123456789);
     18 
     19 TEST(RTCStatsTimestampMakerRealtimeClock, ConvertTimestampToNtpTime)
     20 {
     21  auto maker = RTCStatsTimestampMaker::Create();
     22  RTCStatsTimestampMakerRealtimeClock clock(maker);
     23  constexpr auto ntpTo1Jan1970Ms = webrtc::kNtpJan1970 * 1000LL;
     24  for (int i = 1000; i < 20000; i += 93) {
     25    const auto t = kWebrtcTimeOffset + webrtc::TimeDelta::Micros(i);
     26    const auto ntp = clock.ConvertTimestampToNtpTime(t);
     27    // Because of precision differences, these round to a specific millisecond
     28    // slightly differently.
     29    EXPECT_NEAR(ntp.ToMs() - ntpTo1Jan1970Ms,
     30                RTCStatsTimestamp::FromRealtime(maker, t).To1Jan1970().ms(),
     31                1.0)
     32        << " for i=" << i;
     33  }
     34 }
     35 
     36 TEST(RTCStatsTimestampMaker, ConvertNtpToDomTime)
     37 {
     38  auto maker = RTCStatsTimestampMaker::Create();
     39  RTCStatsTimestampMakerRealtimeClock clock(maker);
     40  for (int i = 1000; i < 20000; i += 93) {
     41    const auto t = kWebrtcTimeOffset + webrtc::TimeDelta::Micros(i);
     42    const auto ntp = clock.ConvertTimestampToNtpTime(t);
     43    const auto dom =
     44        RTCStatsTimestamp::FromNtp(maker, webrtc::Timestamp::Millis(ntp.ToMs()))
     45            .ToDom();
     46    // Because of precision differences, these round to a specific millisecond
     47    // slightly differently.
     48    EXPECT_NEAR(std::lround(dom),
     49                std::lround(RTCStatsTimestamp::FromRealtime(maker, t).ToDom()),
     50                1.0)
     51        << " for i=" << i;
     52  }
     53 }
     54 
     55 TEST(RTCStatsTimestampMaker, ConvertMozTime)
     56 {
     57  auto maker = RTCStatsTimestampMaker::Create();
     58  const auto start = TimeStamp::Now();
     59  RTCStatsTimestampMakerRealtimeClock clock(maker);
     60  for (int i = 1000; i < 20000; i += 93) {
     61    const auto duration = TimeDuration::FromMicroseconds(i);
     62    const auto time = RTCStatsTimestamp::FromMozTime(maker, start + duration);
     63    EXPECT_EQ(duration.ToMicroseconds(),
     64              (time.ToMozTime() - start).ToMicroseconds())
     65        << " for i=" << i;
     66  }
     67 }
     68 
     69 TEST(RTCStatsTimestampMaker, ConvertRealtime)
     70 {
     71  auto maker = RTCStatsTimestampMaker::Create();
     72  const auto start = kWebrtcTimeOffset;
     73  RTCStatsTimestampMakerRealtimeClock clock(maker);
     74  for (int i = 1000; i < 20000; i += 93) {
     75    const auto duration = webrtc::TimeDelta::Micros(i);
     76    const auto time = RTCStatsTimestamp::FromRealtime(maker, start + duration);
     77    // Because of precision differences, these round to a specific Microsecond
     78    // slightly differently.
     79    EXPECT_NEAR(duration.us(), (time.ToRealtime() - start).us(), 1)
     80        << " for i=" << i;
     81  }
     82 }
     83 
     84 TEST(RTCStatsTimestampMaker, Convert1Jan1970)
     85 {
     86  auto maker = RTCStatsTimestampMaker::Create();
     87  const auto start =
     88      kWebrtcTimeOffset +
     89      webrtc::TimeDelta::Millis(PerformanceService::GetOrCreate()->TimeOrigin(
     90          WebrtcSystemTimeBase()));
     91  RTCStatsTimestampMakerRealtimeClock clock(maker);
     92  for (int i = 1000; i < 20000; i += 93) {
     93    const auto duration = webrtc::TimeDelta::Micros(i);
     94    const auto time = RTCStatsTimestamp::From1Jan1970(maker, start + duration);
     95    // Because of precision differences, these round to a specific Microsecond
     96    // slightly differently.
     97    EXPECT_NEAR(duration.us(), (time.To1Jan1970() - start).us(), 1)
     98        << " for i=" << i;
     99  }
    100 }
    101 
    102 TEST(RTCStatsTimestampMaker, ConvertDomRealtime)
    103 {
    104  auto maker = RTCStatsTimestampMaker::Create();
    105  const auto start = kWebrtcTimeOffset;
    106  RTCStatsTimestampMakerRealtimeClock clock(maker);
    107  for (int i = 1000; i < 20000; i += 93) {
    108    const auto duration = webrtc::TimeDelta::Micros(i);
    109    const auto time =
    110        RTCStatsTimestamp::FromDomRealtime(maker, start + duration);
    111    // Because of precision differences, these round to a specific Microsecond
    112    // slightly differently.
    113    EXPECT_NEAR(duration.us(), (time.ToDomRealtime() - start).us(), 1)
    114        << " for i=" << i;
    115  }
    116 }