license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 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. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
| 5 | // Test of classes in the tracked_objects.h classes. |
| 6 | |
| 7 | #include "base/tracked_objects.h" |
| 8 | #include "base/logging.h" |
| 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | |
| 11 | namespace tracked_objects { |
| 12 | |
| 13 | class TrackedObjectsTest : public testing::Test { |
| 14 | }; |
| 15 | |
| 16 | TEST(TrackedObjectsTest, MinimalStartupShutdown) { |
| 17 | // Minimal test doesn't even create any tasks. |
| 18 | if (!ThreadData::StartTracking(true)) |
| 19 | return; |
| 20 | |
| 21 | EXPECT_FALSE(ThreadData::first()); // No activity even on this thread. |
| 22 | ThreadData* data = ThreadData::current(); |
| 23 | EXPECT_TRUE(ThreadData::first()); // Now class was constructed. |
| 24 | EXPECT_TRUE(data); |
| 25 | EXPECT_TRUE(!data->next()); |
| 26 | EXPECT_EQ(data, ThreadData::current()); |
| 27 | ThreadData::BirthMap birth_map; |
| 28 | data->SnapshotBirthMap(&birth_map); |
| 29 | EXPECT_EQ(0u, birth_map.size()); |
| 30 | ThreadData::DeathMap death_map; |
| 31 | data->SnapshotDeathMap(&death_map); |
| 32 | EXPECT_EQ(0u, death_map.size()); |
| 33 | ThreadData::ShutdownSingleThreadedCleanup(); |
| 34 | |
| 35 | // Do it again, just to be sure we reset state completely. |
| 36 | ThreadData::StartTracking(true); |
| 37 | EXPECT_FALSE(ThreadData::first()); // No activity even on this thread. |
| 38 | data = ThreadData::current(); |
| 39 | EXPECT_TRUE(ThreadData::first()); // Now class was constructed. |
| 40 | EXPECT_TRUE(data); |
| 41 | EXPECT_TRUE(!data->next()); |
| 42 | EXPECT_EQ(data, ThreadData::current()); |
| 43 | birth_map.clear(); |
| 44 | data->SnapshotBirthMap(&birth_map); |
| 45 | EXPECT_EQ(0u, birth_map.size()); |
| 46 | death_map.clear(); |
| 47 | data->SnapshotDeathMap(&death_map); |
| 48 | EXPECT_EQ(0u, death_map.size()); |
| 49 | ThreadData::ShutdownSingleThreadedCleanup(); |
| 50 | } |
| 51 | |
| 52 | class NoopTask : public Task { |
| 53 | public: |
| 54 | void Run() {} |
| 55 | }; |
| 56 | |
| 57 | TEST(TrackedObjectsTest, TinyStartupShutdown) { |
| 58 | if (!ThreadData::StartTracking(true)) |
| 59 | return; |
| 60 | |
| 61 | // Instigate tracking on a single task, or our thread. |
| 62 | NoopTask task; |
| 63 | |
| 64 | const ThreadData* data = ThreadData::first(); |
| 65 | EXPECT_TRUE(data); |
| 66 | EXPECT_TRUE(!data->next()); |
| 67 | EXPECT_EQ(data, ThreadData::current()); |
| 68 | ThreadData::BirthMap birth_map; |
| 69 | data->SnapshotBirthMap(&birth_map); |
| 70 | EXPECT_EQ(1u, birth_map.size()); // 1 birth location. |
| 71 | EXPECT_EQ(1, birth_map.begin()->second->birth_count()); // 1 birth. |
| 72 | ThreadData::DeathMap death_map; |
| 73 | data->SnapshotDeathMap(&death_map); |
| 74 | EXPECT_EQ(0u, death_map.size()); // No deaths. |
| 75 | |
| 76 | |
| 77 | // Now instigate a birth, and a death. |
| 78 | delete new NoopTask; |
| 79 | |
| 80 | birth_map.clear(); |
| 81 | data->SnapshotBirthMap(&birth_map); |
| 82 | EXPECT_EQ(1u, birth_map.size()); // 1 birth location. |
| 83 | EXPECT_EQ(2, birth_map.begin()->second->birth_count()); // 2 births. |
| 84 | death_map.clear(); |
| 85 | data->SnapshotDeathMap(&death_map); |
| 86 | EXPECT_EQ(1u, death_map.size()); // 1 location. |
| 87 | EXPECT_EQ(1, death_map.begin()->second.count()); // 1 death. |
| 88 | |
| 89 | // The births were at the same location as the one known death. |
| 90 | EXPECT_EQ(birth_map.begin()->second, death_map.begin()->first); |
| 91 | |
| 92 | ThreadData::ShutdownSingleThreadedCleanup(); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | } // namespace tracked_objects |
| 97 | |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 98 | |