| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/time/time.h" | 5 #include "base/time/time.h" |
| 6 | 6 |
| 7 #include <stdint.h> |
| 7 #include <time.h> | 8 #include <time.h> |
| 8 | |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/threading/platform_thread.h" | 15 #include "base/threading/platform_thread.h" |
| 16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| (...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 683 HighResClockTest(&TimeTicks::Now); | 683 HighResClockTest(&TimeTicks::Now); |
| 684 } | 684 } |
| 685 | 685 |
| 686 // Fails frequently on Android http://crbug.com/352633 with: | 686 // Fails frequently on Android http://crbug.com/352633 with: |
| 687 // Expected: (delta_thread.InMicroseconds()) > (0), actual: 0 vs 0 | 687 // Expected: (delta_thread.InMicroseconds()) > (0), actual: 0 vs 0 |
| 688 #if defined(OS_ANDROID) | 688 #if defined(OS_ANDROID) |
| 689 #define MAYBE_ThreadNow DISABLED_ThreadNow | 689 #define MAYBE_ThreadNow DISABLED_ThreadNow |
| 690 #else | 690 #else |
| 691 #define MAYBE_ThreadNow ThreadNow | 691 #define MAYBE_ThreadNow ThreadNow |
| 692 #endif | 692 #endif |
| 693 TEST(TimeTicks, MAYBE_ThreadNow) { | 693 TEST(ThreadTicks, MAYBE_ThreadNow) { |
| 694 if (TimeTicks::IsThreadNowSupported()) { | 694 if (ThreadTicks::IsSupported()) { |
| 695 TimeTicks begin = TimeTicks::Now(); | 695 TimeTicks begin = TimeTicks::Now(); |
| 696 TimeTicks begin_thread = TimeTicks::ThreadNow(); | 696 ThreadTicks begin_thread = ThreadTicks::Now(); |
| 697 // Make sure that ThreadNow value is non-zero. | 697 // Make sure that ThreadNow value is non-zero. |
| 698 EXPECT_GT(begin_thread, TimeTicks()); | 698 EXPECT_GT(begin_thread, ThreadTicks()); |
| 699 // Sleep for 10 milliseconds to get the thread de-scheduled. | 699 // Sleep for 10 milliseconds to get the thread de-scheduled. |
| 700 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); | 700 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); |
| 701 TimeTicks end_thread = TimeTicks::ThreadNow(); | 701 ThreadTicks end_thread = ThreadTicks::Now(); |
| 702 TimeTicks end = TimeTicks::Now(); | 702 TimeTicks end = TimeTicks::Now(); |
| 703 TimeDelta delta = end - begin; | 703 TimeDelta delta = end - begin; |
| 704 TimeDelta delta_thread = end_thread - begin_thread; | 704 TimeDelta delta_thread = end_thread - begin_thread; |
| 705 // Make sure that some thread time have elapsed. | 705 // Make sure that some thread time have elapsed. |
| 706 EXPECT_GT(delta_thread.InMicroseconds(), 0); | 706 EXPECT_GT(delta_thread.InMicroseconds(), 0); |
| 707 // But the thread time is at least 9ms less than clock time. | 707 // But the thread time is at least 9ms less than clock time. |
| 708 TimeDelta difference = delta - delta_thread; | 708 TimeDelta difference = delta - delta_thread; |
| 709 EXPECT_GE(difference.InMicroseconds(), 9000); | 709 EXPECT_GE(difference.InMicroseconds(), 9000); |
| 710 } | 710 } |
| 711 } | 711 } |
| 712 | 712 |
| 713 TEST(TimeTicks, NowFromSystemTraceTime) { | 713 TEST(TraceTicks, NowFromSystemTraceTime) { |
| 714 // Re-use HighRes test for now since clock properties are identical. | 714 // Re-use HighRes test for now since clock properties are identical. |
| 715 HighResClockTest(&TimeTicks::NowFromSystemTraceTime); | 715 using NowFunction = TimeTicks (*)(void); |
| 716 HighResClockTest(reinterpret_cast<NowFunction>(&TraceTicks::Now)); |
| 716 } | 717 } |
| 717 | 718 |
| 718 TEST(TimeTicks, SnappedToNextTickBasic) { | 719 TEST(TimeTicks, SnappedToNextTickBasic) { |
| 719 base::TimeTicks phase = base::TimeTicks::FromInternalValue(4000); | 720 base::TimeTicks phase = base::TimeTicks::FromInternalValue(4000); |
| 720 base::TimeDelta interval = base::TimeDelta::FromMicroseconds(1000); | 721 base::TimeDelta interval = base::TimeDelta::FromMicroseconds(1000); |
| 721 base::TimeTicks timestamp; | 722 base::TimeTicks timestamp; |
| 722 | 723 |
| 723 // Timestamp in previous interval. | 724 // Timestamp in previous interval. |
| 724 timestamp = base::TimeTicks::FromInternalValue(3500); | 725 timestamp = base::TimeTicks::FromInternalValue(3500); |
| 725 EXPECT_EQ(4000, | 726 EXPECT_EQ(4000, |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 825 exploded.year = 1970; | 826 exploded.year = 1970; |
| 826 exploded.month = 1; | 827 exploded.month = 1; |
| 827 exploded.day_of_week = 0; // Should be unusued. | 828 exploded.day_of_week = 0; // Should be unusued. |
| 828 exploded.day_of_month = 1; | 829 exploded.day_of_month = 1; |
| 829 exploded.hour = 0; | 830 exploded.hour = 0; |
| 830 exploded.minute = 0; | 831 exploded.minute = 0; |
| 831 exploded.second = 0; | 832 exploded.second = 0; |
| 832 exploded.millisecond = 0; | 833 exploded.millisecond = 0; |
| 833 Time t = Time::FromUTCExploded(exploded); | 834 Time t = Time::FromUTCExploded(exploded); |
| 834 // Unix 1970 epoch. | 835 // Unix 1970 epoch. |
| 835 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue()); | 836 EXPECT_EQ(INT64_C(11644473600000000), t.ToInternalValue()); |
| 836 | 837 |
| 837 // We can't test 1601 epoch, since the system time functions on Linux | 838 // We can't test 1601 epoch, since the system time functions on Linux |
| 838 // only compute years starting from 1900. | 839 // only compute years starting from 1900. |
| 839 } | 840 } |
| 840 | 841 |
| 841 // We could define this separately for Time, TimeTicks and TimeDelta but the | 842 // We could define this separately for Time, TimeTicks and TimeDelta but the |
| 842 // definitions would be identical anyway. | 843 // definitions would be identical anyway. |
| 843 template <class Any> | 844 template <class Any> |
| 844 std::string AnyToString(Any any) { | 845 std::string AnyToString(Any any) { |
| 845 std::ostringstream oss; | 846 std::ostringstream oss; |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1093 | 1094 |
| 1094 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { | 1095 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { |
| 1095 std::ostringstream oss; | 1096 std::ostringstream oss; |
| 1096 oss << TimeTicks(); | 1097 oss << TimeTicks(); |
| 1097 EXPECT_TRUE(oss.good()); | 1098 EXPECT_TRUE(oss.good()); |
| 1098 } | 1099 } |
| 1099 | 1100 |
| 1100 } // namespace | 1101 } // namespace |
| 1101 | 1102 |
| 1102 } // namespace base | 1103 } // namespace base |
| OLD | NEW |