Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ppapi / shared_impl / time_conversion_unittest.cc
blob1ec70bf3e4a406ae6269f36faa6ee1fd7d866b9c
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <math.h>
6 #include <stdlib.h>
8 #include "ppapi/shared_impl/time_conversion.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace ppapi {
13 // Slop we'll allow in two Time "internal values" to consider them equal.
14 // Double conversion can introduce rounding errors. The internal values are in
15 // microseconds, so an error here is very small.
16 static const int kTimeInternalValueSlop = 2;
18 // Same as above in double-precision seconds units.
19 static const double kTimeSecondsSlop =
20 static_cast<double>(kTimeInternalValueSlop) /
21 base::Time::kMicrosecondsPerSecond;
23 TEST(TimeConversion, Time) {
24 // Should be able to round-trip.
25 base::Time now = base::Time::Now();
26 base::Time converted = ppapi::PPTimeToTime(TimeToPPTime(now));
27 EXPECT_GE(kTimeInternalValueSlop,
28 abs(static_cast<int>((converted - now).ToInternalValue())));
30 // Units should be in seconds.
31 base::Time one_second_from_now = now + base::TimeDelta::FromSeconds(1);
32 double converted_one_second_from_now =
33 ppapi::TimeToPPTime(one_second_from_now) - ppapi::TimeToPPTime(now);
34 EXPECT_GE(kTimeSecondsSlop, fabs(converted_one_second_from_now - 1));
37 TEST(TimeConversion, EventTime) {
38 // Should be able to round-trip.
39 base::Time now = base::Time::Now();
40 double event_now = now.ToDoubleT();
41 double converted =
42 ppapi::EventTimeToPPTimeTicks(ppapi::PPTimeTicksToEventTime(event_now));
43 EXPECT_GE(kTimeSecondsSlop, fabs(converted - event_now));
45 // Units should be in seconds.
46 base::Time one_second_from_now = now + base::TimeDelta::FromSeconds(1);
47 double event_one_second_from_now = one_second_from_now.ToDoubleT();
48 EXPECT_GE(kTimeSecondsSlop,
49 1.0 - ppapi::EventTimeToPPTimeTicks(event_one_second_from_now) -
50 ppapi::EventTimeToPPTimeTicks(event_now));
53 TEST(TimeConversion, EpochTime) {
54 // Should be able to round-trip from epoch time.
55 base::Time epoch = base::Time::UnixEpoch();
56 base::Time converted = ppapi::PPTimeToTime(TimeToPPTime(epoch));
57 EXPECT_GE(kTimeInternalValueSlop,
58 abs(static_cast<int>((converted - epoch).ToInternalValue())));
60 // Units should be in seconds.
61 base::Time one_second_from_epoch = epoch + base::TimeDelta::FromSeconds(1);
62 double converted_one_second_from_epoch =
63 ppapi::TimeToPPTime(one_second_from_epoch) - ppapi::TimeToPPTime(epoch);
64 EXPECT_GE(kTimeSecondsSlop, fabs(converted_one_second_from_epoch - 1));
66 // Epoch time should be equal to a PP_Time of 0.0.
67 EXPECT_GE(kTimeSecondsSlop, fabs(ppapi::TimeToPPTime(epoch) - 0.0));
68 EXPECT_GE(kTimeInternalValueSlop,
69 abs(static_cast<int>(
70 (ppapi::PPTimeToTime(0.0) - epoch).ToInternalValue())));
73 } // namespace ppapi