Index: components/metrics/leak_detector/leak_detector_unittest.cc |
diff --git a/components/metrics/leak_detector/leak_detector_unittest.cc b/components/metrics/leak_detector/leak_detector_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f85d8db079987d84a596f704c12ae92c5125cfc9 |
--- /dev/null |
+++ b/components/metrics/leak_detector/leak_detector_unittest.cc |
@@ -0,0 +1,122 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/metrics/leak_detector/leak_detector.h" |
+ |
+#include <set> |
+ |
+#include "base/macros.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace metrics { |
+ |
+namespace { |
+ |
+// Observer class that receives leak reports and stores them in |reports_|. |
+// Only one copy of each unique report will be stored. |
+class TestObserver : public LeakDetector::LeakDetector::Observer { |
+ public: |
+ TestObserver() {} |
+ |
+ void OnLeakFound(const LeakDetector::LeakReport& report) override { |
+ reports_.insert(report); |
+ } |
+ |
+ const std::set<LeakDetector::LeakReport>& reports() const { return reports_; } |
+ |
+ private: |
+ std::set<LeakDetector::LeakReport> reports_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TestObserver); |
+}; |
+ |
+} // namespace |
+ |
+class LeakDetectorTest : public ::testing::Test { |
+ public: |
+ LeakDetectorTest() {} |
+ |
+ void SetUp() override { detector_.reset(new LeakDetector); } |
Alexei Svitkine (slow)
2016/02/10 20:16:58
You can just put this in the ctor and avoid having
Simon Que
2016/02/11 01:45:38
I actually call "detector_.reset()" in the second
|
+ |
+ void TearDown() override { detector_.reset(); } |
+ |
+ protected: |
+ scoped_ptr<LeakDetector> detector_; |
+ |
+ protected: |
Alexei Svitkine (slow)
2016/02/10 20:16:58
private:
Simon Que
2016/02/11 01:45:38
Done.
|
+ DISALLOW_COPY_AND_ASSIGN(LeakDetectorTest); |
+}; |
+ |
+TEST_F(LeakDetectorTest, AddAndRemoveObservers) { |
+ TestObserver obs1, obs2, obs3; |
+ |
+ // There are no registered observers at first. Make sure RemoveObserver() |
+ // returns false in this case. |
+ EXPECT_FALSE(detector_->RemoveObserver(&obs1)); |
+ EXPECT_FALSE(detector_->RemoveObserver(&obs2)); |
+ EXPECT_FALSE(detector_->RemoveObserver(&obs3)); |
+ |
+ // Add observer and then remove it. |
+ EXPECT_TRUE(detector_->AddObserver(&obs1)); |
+ EXPECT_TRUE(detector_->RemoveObserver(&obs1)); |
+ EXPECT_FALSE(detector_->RemoveObserver(&obs1)); |
+ |
+ // Add all three observers at once, then remove them. |
+ EXPECT_TRUE(detector_->AddObserver(&obs1)); |
+ EXPECT_TRUE(detector_->AddObserver(&obs2)); |
+ EXPECT_TRUE(detector_->AddObserver(&obs3)); |
+ |
+ EXPECT_TRUE(detector_->RemoveObserver(&obs1)); |
+ EXPECT_FALSE(detector_->RemoveObserver(&obs1)); |
+ EXPECT_TRUE(detector_->RemoveObserver(&obs2)); |
+ EXPECT_FALSE(detector_->RemoveObserver(&obs2)); |
+ EXPECT_TRUE(detector_->RemoveObserver(&obs3)); |
+ EXPECT_FALSE(detector_->RemoveObserver(&obs3)); |
+} |
+ |
+TEST_F(LeakDetectorTest, NotifyObservers) { |
+ // Generate two sets of leak reports. |
+ std::vector<LeakDetector::LeakReport> reports1(3); |
+ reports1[0].alloc_size_bytes = 8; |
+ reports1[0].call_stack = {1, 2, 3, 4}; |
+ reports1[1].alloc_size_bytes = 16; |
+ reports1[1].call_stack = {5, 6, 7, 8}; |
+ reports1[2].alloc_size_bytes = 24; |
+ reports1[2].call_stack = {9, 10, 11, 12}; |
+ |
+ std::vector<LeakDetector::LeakReport> reports2(3); |
+ reports2[0].alloc_size_bytes = 32; |
+ reports2[0].call_stack = {1, 2, 4, 8}; |
+ reports2[1].alloc_size_bytes = 40; |
+ reports2[1].call_stack = {16, 32, 64, 128}; |
+ reports2[2].alloc_size_bytes = 48; |
+ reports2[2].call_stack = {256, 512, 1024, 2048}; |
+ |
+ // Register three observers; |
+ TestObserver obs1, obs2, obs3; |
+ ASSERT_TRUE(detector_->AddObserver(&obs1)); |
+ ASSERT_TRUE(detector_->AddObserver(&obs2)); |
+ ASSERT_TRUE(detector_->AddObserver(&obs3)); |
+ |
+ // Pass both sets of reports to the leak detector. |
+ detector_->NotifyObservers(reports1); |
+ detector_->NotifyObservers(reports2); |
+ |
+ // Shut down the leak detector before checking the reports, so that the |
+ // stored reports can be examined without new reports being generated. |
+ detector_.reset(); |
+ |
+ // Check that all three observers got both sets of reports, passed in |
+ // separately. |
+ for (const TestObserver* obs : {&obs1, &obs2, &obs3}) { |
+ EXPECT_EQ(6U, obs->reports().size()); |
+ for (const auto& report : {reports1[0], reports1[1], reports1[2], |
+ reports2[0], reports2[1], reports2[2]}) { |
+ EXPECT_TRUE(obs->reports().find(report) != obs->reports().end()); |
+ } |
+ } |
+} |
+ |
+} // namespace metrics |