Home | History | Annotate | Download | only in base
      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.
      4 
      5 // Test of classes in the tracked_objects.h classes.
      6 
      7 #include "base/tracked_objects.h"
      8 
      9 #include "base/message_loop.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 namespace tracked_objects {
     13 
     14 class TrackedObjectsTest : public testing::Test {
     15  public:
     16   MessageLoop message_loop_;
     17 };
     18 
     19 TEST_F(TrackedObjectsTest, MinimalStartupShutdown) {
     20   // Minimal test doesn't even create any tasks.
     21   if (!ThreadData::StartTracking(true))
     22     return;
     23 
     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.
     27   EXPECT_TRUE(data);
     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();
     37 
     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.
     43   EXPECT_TRUE(data);
     44   EXPECT_TRUE(!data->next());
     45   EXPECT_EQ(data, ThreadData::current());
     46   birth_map.clear();
     47   data->SnapshotBirthMap(&birth_map);
     48   EXPECT_EQ(0u, birth_map.size());
     49   death_map.clear();
     50   data->SnapshotDeathMap(&death_map);
     51   EXPECT_EQ(0u, death_map.size());
     52   ThreadData::ShutdownSingleThreadedCleanup();
     53 }
     54 
     55 class NoopTracked : public tracked_objects::Tracked {
     56 };
     57 
     58 TEST_F(TrackedObjectsTest, TinyStartupShutdown) {
     59   if (!ThreadData::StartTracking(true))
     60     return;
     61 
     62   // Instigate tracking on a single tracked object, or our thread.
     63   NoopTracked tracked;
     64 
     65   const ThreadData* data = ThreadData::first();
     66   EXPECT_TRUE(data);
     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.
     76 
     77 
     78   // Now instigate a birth, and a death.
     79   delete new NoopTracked;
     80 
     81   birth_map.clear();
     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.
     85   death_map.clear();
     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.
     89 
     90   // The births were at the same location as the one known death.
     91   EXPECT_EQ(birth_map.begin()->second, death_map.begin()->first);
     92 
     93   ThreadData::ShutdownSingleThreadedCleanup();
     94 }
     95 
     96 }  // namespace tracked_objects
     97