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 "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 namespace tracked_objects { | 13 namespace tracked_objects { |
13 | 14 |
14 class TrackedObjectsTest : public testing::Test { | 15 class TrackedObjectsTest : public testing::Test { |
15 public: | 16 public: |
16 MessageLoop message_loop_; | 17 MessageLoop message_loop_; |
17 }; | 18 }; |
18 | 19 |
19 TEST_F(TrackedObjectsTest, MinimalStartupShutdown) { | 20 TEST_F(TrackedObjectsTest, MinimalStartupShutdown) { |
(...skipping 25 matching lines...) Expand all Loading... |
45 EXPECT_EQ(data, ThreadData::current()); | 46 EXPECT_EQ(data, ThreadData::current()); |
46 birth_map.clear(); | 47 birth_map.clear(); |
47 data->SnapshotBirthMap(&birth_map); | 48 data->SnapshotBirthMap(&birth_map); |
48 EXPECT_EQ(0u, birth_map.size()); | 49 EXPECT_EQ(0u, birth_map.size()); |
49 death_map.clear(); | 50 death_map.clear(); |
50 data->SnapshotDeathMap(&death_map); | 51 data->SnapshotDeathMap(&death_map); |
51 EXPECT_EQ(0u, death_map.size()); | 52 EXPECT_EQ(0u, death_map.size()); |
52 ThreadData::ShutdownSingleThreadedCleanup(); | 53 ThreadData::ShutdownSingleThreadedCleanup(); |
53 } | 54 } |
54 | 55 |
55 class NoopTracked : public tracked_objects::Tracked { | |
56 }; | |
57 | |
58 TEST_F(TrackedObjectsTest, TinyStartupShutdown) { | 56 TEST_F(TrackedObjectsTest, TinyStartupShutdown) { |
59 if (!ThreadData::StartTracking(true)) | 57 if (!ThreadData::StartTracking(true)) |
60 return; | 58 return; |
61 | 59 |
62 // Instigate tracking on a single tracked object, or our thread. | 60 // Instigate tracking on a single tracked object, or our thread. |
63 const Location& location = FROM_HERE; | 61 const Location& location = FROM_HERE; |
64 NoopTracked tracked; | 62 ThreadData::TallyABirthIfActive(location); |
65 tracked.SetBirthPlace(location); | |
66 | 63 |
67 const ThreadData* data = ThreadData::first(); | 64 const ThreadData* data = ThreadData::first(); |
68 ASSERT_TRUE(data); | 65 ASSERT_TRUE(data); |
69 EXPECT_TRUE(!data->next()); | 66 EXPECT_TRUE(!data->next()); |
70 EXPECT_EQ(data, ThreadData::current()); | 67 EXPECT_EQ(data, ThreadData::current()); |
71 ThreadData::BirthMap birth_map; | 68 ThreadData::BirthMap birth_map; |
72 data->SnapshotBirthMap(&birth_map); | 69 data->SnapshotBirthMap(&birth_map); |
73 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. | 70 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. |
74 EXPECT_EQ(1, birth_map.begin()->second->birth_count()); // 1 birth. | 71 EXPECT_EQ(1, birth_map.begin()->second->birth_count()); // 1 birth. |
75 ThreadData::DeathMap death_map; | 72 ThreadData::DeathMap death_map; |
76 data->SnapshotDeathMap(&death_map); | 73 data->SnapshotDeathMap(&death_map); |
77 EXPECT_EQ(0u, death_map.size()); // No deaths. | 74 EXPECT_EQ(0u, death_map.size()); // No deaths. |
78 | 75 |
79 | 76 |
80 // Now instigate a birth, and a death. | 77 // Now instigate a birth, and a death. |
81 NoopTracked* new_tracked = new NoopTracked; | 78 const Births* second_birth = ThreadData::TallyABirthIfActive(location); |
82 new_tracked->SetBirthPlace(location); | 79 ThreadData::TallyADeathIfActive( |
83 delete new_tracked; | 80 second_birth, |
| 81 base::TimeDelta::FromSeconds(1) /* Bogus duration. */); |
84 | 82 |
85 birth_map.clear(); | 83 birth_map.clear(); |
86 data->SnapshotBirthMap(&birth_map); | 84 data->SnapshotBirthMap(&birth_map); |
87 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. | 85 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. |
88 EXPECT_EQ(2, birth_map.begin()->second->birth_count()); // 2 births. | 86 EXPECT_EQ(2, birth_map.begin()->second->birth_count()); // 2 births. |
89 death_map.clear(); | 87 death_map.clear(); |
90 data->SnapshotDeathMap(&death_map); | 88 data->SnapshotDeathMap(&death_map); |
91 EXPECT_EQ(1u, death_map.size()); // 1 location. | 89 EXPECT_EQ(1u, death_map.size()); // 1 location. |
92 EXPECT_EQ(1, death_map.begin()->second.count()); // 1 death. | 90 EXPECT_EQ(1, death_map.begin()->second.count()); // 1 death. |
93 | 91 |
94 // The births were at the same location as the one known death. | 92 // The births were at the same location as the one known death. |
95 EXPECT_EQ(birth_map.begin()->second, death_map.begin()->first); | 93 EXPECT_EQ(birth_map.begin()->second, death_map.begin()->first); |
96 | 94 |
97 ThreadData::ShutdownSingleThreadedCleanup(); | 95 ThreadData::ShutdownSingleThreadedCleanup(); |
98 } | 96 } |
99 | 97 |
100 } // namespace tracked_objects | 98 } // namespace tracked_objects |
OLD | NEW |