1 // Copyright (c) 2006-2008 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 // Test of classes in the tracked_objects.h classes.
7 #include "base/tracked_objects.h"
9 #include "base/message_loop.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace tracked_objects
{
14 class TrackedObjectsTest
: public testing::Test
{
16 MessageLoop message_loop_
;
19 TEST_F(TrackedObjectsTest
, MinimalStartupShutdown
) {
20 // Minimal test doesn't even create any tasks.
21 if (!ThreadData::StartTracking(true))
24 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread.
25 ThreadData
* data
= ThreadData::current();
26 EXPECT_TRUE(ThreadData::first()); // Now class was constructed.
28 EXPECT_TRUE(!data
->next());
29 EXPECT_EQ(data
, ThreadData::current());
30 ThreadData::BirthMap birth_map
;
31 data
->SnapshotBirthMap(&birth_map
);
32 EXPECT_EQ(0u, birth_map
.size());
33 ThreadData::DeathMap death_map
;
34 data
->SnapshotDeathMap(&death_map
);
35 EXPECT_EQ(0u, death_map
.size());
36 ThreadData::ShutdownSingleThreadedCleanup();
38 // Do it again, just to be sure we reset state completely.
39 ThreadData::StartTracking(true);
40 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread.
41 data
= ThreadData::current();
42 EXPECT_TRUE(ThreadData::first()); // Now class was constructed.
44 EXPECT_TRUE(!data
->next());
45 EXPECT_EQ(data
, ThreadData::current());
47 data
->SnapshotBirthMap(&birth_map
);
48 EXPECT_EQ(0u, birth_map
.size());
50 data
->SnapshotDeathMap(&death_map
);
51 EXPECT_EQ(0u, death_map
.size());
52 ThreadData::ShutdownSingleThreadedCleanup();
55 class NoopTracked
: public tracked_objects::Tracked
{
58 TEST_F(TrackedObjectsTest
, TinyStartupShutdown
) {
59 if (!ThreadData::StartTracking(true))
62 // Instigate tracking on a single tracked object, or our thread.
65 const ThreadData
* data
= ThreadData::first();
67 EXPECT_TRUE(!data
->next());
68 EXPECT_EQ(data
, ThreadData::current());
69 ThreadData::BirthMap birth_map
;
70 data
->SnapshotBirthMap(&birth_map
);
71 EXPECT_EQ(1u, birth_map
.size()); // 1 birth location.
72 EXPECT_EQ(1, birth_map
.begin()->second
->birth_count()); // 1 birth.
73 ThreadData::DeathMap death_map
;
74 data
->SnapshotDeathMap(&death_map
);
75 EXPECT_EQ(0u, death_map
.size()); // No deaths.
78 // Now instigate a birth, and a death.
79 delete new NoopTracked
;
82 data
->SnapshotBirthMap(&birth_map
);
83 EXPECT_EQ(1u, birth_map
.size()); // 1 birth location.
84 EXPECT_EQ(2, birth_map
.begin()->second
->birth_count()); // 2 births.
86 data
->SnapshotDeathMap(&death_map
);
87 EXPECT_EQ(1u, death_map
.size()); // 1 location.
88 EXPECT_EQ(1, death_map
.begin()->second
.count()); // 1 death.
90 // The births were at the same location as the one known death.
91 EXPECT_EQ(birth_map
.begin()->second
, death_map
.begin()->first
);
93 ThreadData::ShutdownSingleThreadedCleanup();
96 } // namespace tracked_objects