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/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace tracked_objects { | 13 namespace tracked_objects { |
14 | 14 |
15 class TrackedObjectsTest : public testing::Test { | 15 class TrackedObjectsTest : public testing::Test { |
16 public: | 16 public: |
17 MessageLoop message_loop_; | 17 MessageLoop message_loop_; |
18 }; | 18 }; |
19 | 19 |
20 TEST_F(TrackedObjectsTest, MinimalStartupShutdown) { | 20 TEST_F(TrackedObjectsTest, MinimalStartupShutdown) { |
21 // Minimal test doesn't even create any tasks. | 21 // Minimal test doesn't even create any tasks. |
22 if (!ThreadData::StartTracking(true)) | 22 if (!ThreadData::StartTracking(true)) |
23 return; | 23 return; |
24 | 24 |
25 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread. | 25 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread. |
26 ThreadData* data = ThreadData::current(); | 26 ThreadData* data = ThreadData::Get(); |
27 EXPECT_TRUE(ThreadData::first()); // Now class was constructed. | 27 EXPECT_TRUE(ThreadData::first()); // Now class was constructed. |
28 EXPECT_TRUE(data); | 28 EXPECT_TRUE(data); |
29 EXPECT_TRUE(!data->next()); | 29 EXPECT_TRUE(!data->next()); |
30 EXPECT_EQ(data, ThreadData::current()); | 30 EXPECT_EQ(data, ThreadData::Get()); |
31 ThreadData::BirthMap birth_map; | 31 ThreadData::BirthMap birth_map; |
32 data->SnapshotBirthMap(&birth_map); | 32 data->SnapshotBirthMap(&birth_map); |
33 EXPECT_EQ(0u, birth_map.size()); | 33 EXPECT_EQ(0u, birth_map.size()); |
34 ThreadData::DeathMap death_map; | 34 ThreadData::DeathMap death_map; |
35 data->SnapshotDeathMap(&death_map); | 35 data->SnapshotDeathMap(&death_map); |
36 EXPECT_EQ(0u, death_map.size()); | 36 EXPECT_EQ(0u, death_map.size()); |
37 ThreadData::ShutdownSingleThreadedCleanup(); | 37 ThreadData::ShutdownSingleThreadedCleanup(); |
38 | 38 |
39 // Do it again, just to be sure we reset state completely. | 39 // Do it again, just to be sure we reset state completely. |
40 ThreadData::StartTracking(true); | 40 ThreadData::StartTracking(true); |
41 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread. | 41 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread. |
42 data = ThreadData::current(); | 42 data = ThreadData::Get(); |
43 EXPECT_TRUE(ThreadData::first()); // Now class was constructed. | 43 EXPECT_TRUE(ThreadData::first()); // Now class was constructed. |
44 EXPECT_TRUE(data); | 44 EXPECT_TRUE(data); |
45 EXPECT_TRUE(!data->next()); | 45 EXPECT_TRUE(!data->next()); |
46 EXPECT_EQ(data, ThreadData::current()); | 46 EXPECT_EQ(data, ThreadData::Get()); |
47 birth_map.clear(); | 47 birth_map.clear(); |
48 data->SnapshotBirthMap(&birth_map); | 48 data->SnapshotBirthMap(&birth_map); |
49 EXPECT_EQ(0u, birth_map.size()); | 49 EXPECT_EQ(0u, birth_map.size()); |
50 death_map.clear(); | 50 death_map.clear(); |
51 data->SnapshotDeathMap(&death_map); | 51 data->SnapshotDeathMap(&death_map); |
52 EXPECT_EQ(0u, death_map.size()); | 52 EXPECT_EQ(0u, death_map.size()); |
53 ThreadData::ShutdownSingleThreadedCleanup(); | 53 ThreadData::ShutdownSingleThreadedCleanup(); |
54 } | 54 } |
55 | 55 |
56 TEST_F(TrackedObjectsTest, TinyStartupShutdown) { | 56 TEST_F(TrackedObjectsTest, TinyStartupShutdown) { |
57 if (!ThreadData::StartTracking(true)) | 57 if (!ThreadData::StartTracking(true)) |
58 return; | 58 return; |
59 | 59 |
60 // Instigate tracking on a single tracked object, or our thread. | 60 // Instigate tracking on a single tracked object, or our thread. |
61 const Location& location = FROM_HERE; | 61 const Location& location = FROM_HERE; |
62 ThreadData::TallyABirthIfActive(location); | 62 ThreadData::TallyABirthIfActive(location); |
63 | 63 |
64 const ThreadData* data = ThreadData::first(); | 64 const ThreadData* data = ThreadData::first(); |
65 ASSERT_TRUE(data); | 65 ASSERT_TRUE(data); |
66 EXPECT_TRUE(!data->next()); | 66 EXPECT_TRUE(!data->next()); |
67 EXPECT_EQ(data, ThreadData::current()); | 67 EXPECT_EQ(data, ThreadData::Get()); |
68 ThreadData::BirthMap birth_map; | 68 ThreadData::BirthMap birth_map; |
69 data->SnapshotBirthMap(&birth_map); | 69 data->SnapshotBirthMap(&birth_map); |
70 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. | 70 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. |
71 EXPECT_EQ(1, birth_map.begin()->second->birth_count()); // 1 birth. | 71 EXPECT_EQ(1, birth_map.begin()->second->birth_count()); // 1 birth. |
72 ThreadData::DeathMap death_map; | 72 ThreadData::DeathMap death_map; |
73 data->SnapshotDeathMap(&death_map); | 73 data->SnapshotDeathMap(&death_map); |
74 EXPECT_EQ(0u, death_map.size()); // No deaths. | 74 EXPECT_EQ(0u, death_map.size()); // No deaths. |
75 | 75 |
76 | 76 |
77 // Now instigate a birth, and a death. | 77 // Now instigate a birth, and a death. |
78 const Births* second_birth = ThreadData::TallyABirthIfActive(location); | 78 const Births* second_birth = ThreadData::TallyABirthIfActive(location); |
79 ThreadData::TallyADeathIfActive( | 79 ThreadData::TallyADeathIfActive( |
80 second_birth, | 80 second_birth, |
81 base::TimeDelta::FromSeconds(1) /* Bogus duration. */); | 81 base::TimeTicks::TimeTicks(), /* Bogus post_time. */ |
| 82 base::TimeTicks::TimeTicks(), /* Bogus delayed_start_time. */ |
| 83 base::TimeTicks::TimeTicks() /* Bogus start_run_time. */); |
82 | 84 |
83 birth_map.clear(); | 85 birth_map.clear(); |
84 data->SnapshotBirthMap(&birth_map); | 86 data->SnapshotBirthMap(&birth_map); |
85 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. | 87 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. |
86 EXPECT_EQ(2, birth_map.begin()->second->birth_count()); // 2 births. | 88 EXPECT_EQ(2, birth_map.begin()->second->birth_count()); // 2 births. |
87 death_map.clear(); | 89 death_map.clear(); |
88 data->SnapshotDeathMap(&death_map); | 90 data->SnapshotDeathMap(&death_map); |
89 EXPECT_EQ(1u, death_map.size()); // 1 location. | 91 EXPECT_EQ(1u, death_map.size()); // 1 location. |
90 EXPECT_EQ(1, death_map.begin()->second.count()); // 1 death. | 92 EXPECT_EQ(1, death_map.begin()->second.count()); // 1 death. |
91 | 93 |
92 // The births were at the same location as the one known death. | 94 // The births were at the same location as the one known death. |
93 EXPECT_EQ(birth_map.begin()->second, death_map.begin()->first); | 95 EXPECT_EQ(birth_map.begin()->second, death_map.begin()->first); |
94 | 96 |
95 ThreadData::ShutdownSingleThreadedCleanup(); | 97 ThreadData::ShutdownSingleThreadedCleanup(); |
96 } | 98 } |
97 | 99 |
98 } // namespace tracked_objects | 100 } // namespace tracked_objects |
OLD | NEW |