OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // Test of classes in the tracked_objects.h classes. | 5 // Test of classes in the tracked_objects.h classes. |
6 | 6 |
7 #include "base/tracked_objects.h" | 7 #include "base/tracked_objects.h" |
8 | 8 |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
622 "\"file_name\":\"AnotherFileName\"," | 622 "\"file_name\":\"AnotherFileName\"," |
623 "\"function_name\":\"DifferentLives\"," | 623 "\"function_name\":\"DifferentLives\"," |
624 "\"line_number\":999" | 624 "\"line_number\":999" |
625 "}" | 625 "}" |
626 "}" | 626 "}" |
627 "]" | 627 "]" |
628 "}"; | 628 "}"; |
629 EXPECT_EQ(one_line_result, json); | 629 EXPECT_EQ(one_line_result, json); |
630 } | 630 } |
631 | 631 |
632 TEST_F(TrackedObjectsTest, TrackedTimerMilliseconds) { | |
633 // First make sure we basicallly transfer simple milliseconds values as | |
634 // expected. Most critically, things should not become null. | |
635 int32 kSomeMilliseconds = 243; // Some example times. | |
636 int64 kReallyBigMilliseconds = (1LL << 35) + kSomeMilliseconds; | |
637 | |
638 TrackedTime some = TrackedTime() + | |
639 Duration::FromMilliseconds(kSomeMilliseconds); | |
640 EXPECT_EQ(kSomeMilliseconds, (some - TrackedTime()).InMilliseconds()); | |
641 EXPECT_FALSE(some.is_null()); | |
642 | |
643 // Now create a big time, to check that it is wrapped modulo 2^32. | |
644 base::TimeTicks big = base::TimeTicks() + | |
645 base::TimeDelta::FromMilliseconds(kReallyBigMilliseconds); | |
646 EXPECT_EQ(kReallyBigMilliseconds, (big - base::TimeTicks()).InMilliseconds()); | |
647 | |
648 TrackedTime wrapped_big(big); | |
649 #if defined(USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS) | |
650 // Expect wrapping at 32 bits. | |
651 EXPECT_EQ(kSomeMilliseconds, (wrapped_big - TrackedTime()).InMilliseconds()); | |
652 #else // !USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS) | |
653 // Expect no wrapping at 32 bits. | |
654 EXPECT_EQ(kReallyBigMilliseconds, | |
655 (wrapped_big - TrackedTime()).InMilliseconds()); | |
656 #endif // USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS) | |
657 } | |
658 | |
659 TEST_F(TrackedObjectsTest, TrackedTimerDuration) { | |
660 int kFirstMilliseconds = 793; | |
661 int kSecondMilliseconds = 14889; | |
662 | |
663 Duration first = Duration::FromMilliseconds(kFirstMilliseconds); | |
664 Duration second = Duration::FromMilliseconds(kSecondMilliseconds); | |
665 | |
666 EXPECT_EQ(kFirstMilliseconds, first.InMilliseconds()); | |
667 EXPECT_EQ(kSecondMilliseconds, second.InMilliseconds()); | |
668 | |
669 Duration sum = first + second; | |
670 EXPECT_EQ(kFirstMilliseconds + kSecondMilliseconds, sum.InMilliseconds()); | |
671 } | |
672 | |
673 TEST_F(TrackedObjectsTest, TrackedTimerVsTimeTicks) { | |
674 // Make sure that our 32 bit timer is aligned with the TimeTicks() timer. | |
675 | |
676 // First get a 64 bit timer (which should not be null). | |
677 base::TimeTicks ticks_before = base::TimeTicks::Now(); | |
678 EXPECT_FALSE(ticks_before.is_null()); | |
679 | |
680 // Then get a 32 bit timer that can be be null when it wraps. | |
681 TrackedTime now = TrackedTime::Now(); | |
682 | |
683 // Then get a bracketing time. | |
684 base::TimeTicks ticks_after = base::TimeTicks::Now(); | |
685 EXPECT_FALSE(ticks_after.is_null()); | |
686 | |
687 // Now make sure that we bracketed our tracked time nicely. | |
688 Duration before = now - TrackedTime(ticks_before); | |
689 EXPECT_LE(0, before.InMilliseconds()); | |
690 Duration after = now - TrackedTime(ticks_after); | |
691 EXPECT_GE(0, after.InMilliseconds()); | |
692 } | |
693 | |
694 TEST_F(TrackedObjectsTest, TrackedTimerDisabled) { | |
695 // Check to be sure disabling the collection of data induces a null time | |
696 // (which we know will return much faster). | |
697 if (!ThreadData::InitializeAndSetTrackingStatus(false)) | |
698 return; | |
699 // Since we disabled tracking, we should get a null response. | |
700 TrackedTime track_now = ThreadData::Now(); | |
701 EXPECT_TRUE(track_now.is_null()); | |
702 } | |
703 | |
704 TEST_F(TrackedObjectsTest, TrackedTimerEnabled) { | |
705 if (!ThreadData::InitializeAndSetTrackingStatus(true)) | |
706 return; | |
707 // Make sure that when we enable tracking, we get a real timer result. | |
708 | |
709 // First get a 64 bit timer (which should not be null). | |
710 base::TimeTicks ticks_before = base::TimeTicks::Now(); | |
711 EXPECT_FALSE(ticks_before.is_null()); | |
712 | |
713 // Then get a 32 bit timer that can be null when it wraps. | |
714 // Crtical difference from the TrackedTimerVsTimeTicks test, is that we use | |
715 // ThreadData::Now(). It can sometimes return the null time. | |
716 TrackedTime now = ThreadData::Now(); | |
717 | |
718 // Then get a bracketing time. | |
719 base::TimeTicks ticks_after = base::TimeTicks::Now(); | |
720 EXPECT_FALSE(ticks_after.is_null()); | |
721 | |
722 // Now make sure that we bracketed our tracked time nicely. | |
723 Duration before = now - TrackedTime(ticks_before); | |
724 EXPECT_LE(0, before.InMilliseconds()); | |
725 Duration after = now - TrackedTime(ticks_after); | |
726 EXPECT_GE(0, after.InMilliseconds()); | |
727 } | |
728 | |
729 | 632 |
730 } // namespace tracked_objects | 633 } // namespace tracked_objects |
OLD | NEW |