OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #include "components/metrics/leak_detector/leak_detector.h" |
| 6 |
| 7 #include <set> |
| 8 |
| 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace metrics { |
| 15 |
| 16 namespace { |
| 17 |
| 18 using LeakReport = LeakDetector::LeakReport; |
| 19 |
| 20 // Test class for LeakDetector that exposes protected methods. |
| 21 class TestLeakDetector : public LeakDetector { |
| 22 public: |
| 23 using LeakDetector::NotifyObservers; |
| 24 |
| 25 TestLeakDetector() {} |
| 26 |
| 27 private: |
| 28 DISALLOW_COPY_AND_ASSIGN(TestLeakDetector); |
| 29 }; |
| 30 |
| 31 // Observer class that receives leak reports and stores them in |reports_|. |
| 32 // Only one copy of each unique report will be stored. |
| 33 class TestObserver : public LeakDetector::Observer { |
| 34 public: |
| 35 // Contains a comparator function used to compare LeakReports for uniqueness. |
| 36 struct Comparator { |
| 37 bool operator()(const LeakReport& a, const LeakReport& b) const { |
| 38 if (a.alloc_size_bytes != b.alloc_size_bytes) |
| 39 return a.alloc_size_bytes < b.alloc_size_bytes; |
| 40 |
| 41 return a.call_stack < b.call_stack; |
| 42 } |
| 43 }; |
| 44 |
| 45 TestObserver() {} |
| 46 |
| 47 void OnLeakFound(const LeakReport& report) override { |
| 48 reports_.insert(report); |
| 49 } |
| 50 |
| 51 const std::set<LeakReport, Comparator>& reports() const { return reports_; } |
| 52 |
| 53 private: |
| 54 // Container for all leak reports received through OnLeakFound(). Stores only |
| 55 // one copy of each unique report. |
| 56 std::set<LeakReport, Comparator> reports_; |
| 57 |
| 58 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 59 }; |
| 60 |
| 61 } // namespace |
| 62 |
| 63 class LeakDetectorTest : public ::testing::Test { |
| 64 public: |
| 65 LeakDetectorTest() : detector_(new TestLeakDetector) {} |
| 66 |
| 67 protected: |
| 68 scoped_ptr<TestLeakDetector> detector_; |
| 69 |
| 70 private: |
| 71 // For supporting content::BrowserThread operations. |
| 72 content::TestBrowserThreadBundle thread_bundle_; |
| 73 |
| 74 DISALLOW_COPY_AND_ASSIGN(LeakDetectorTest); |
| 75 }; |
| 76 |
| 77 TEST_F(LeakDetectorTest, NotifyObservers) { |
| 78 // Generate two sets of leak reports. |
| 79 std::vector<LeakReport> reports1(3); |
| 80 reports1[0].alloc_size_bytes = 8; |
| 81 reports1[0].call_stack = {1, 2, 3, 4}; |
| 82 reports1[1].alloc_size_bytes = 16; |
| 83 reports1[1].call_stack = {5, 6, 7, 8}; |
| 84 reports1[2].alloc_size_bytes = 24; |
| 85 reports1[2].call_stack = {9, 10, 11, 12}; |
| 86 |
| 87 std::vector<LeakReport> reports2(3); |
| 88 reports2[0].alloc_size_bytes = 32; |
| 89 reports2[0].call_stack = {1, 2, 4, 8}; |
| 90 reports2[1].alloc_size_bytes = 40; |
| 91 reports2[1].call_stack = {16, 32, 64, 128}; |
| 92 reports2[2].alloc_size_bytes = 48; |
| 93 reports2[2].call_stack = {256, 512, 1024, 2048}; |
| 94 |
| 95 // Register three observers; |
| 96 TestObserver obs1, obs2, obs3; |
| 97 detector_->AddObserver(&obs1); |
| 98 detector_->AddObserver(&obs2); |
| 99 detector_->AddObserver(&obs3); |
| 100 |
| 101 // Pass both sets of reports to the leak detector. |
| 102 detector_->NotifyObservers(reports1); |
| 103 detector_->NotifyObservers(reports2); |
| 104 |
| 105 // Shut down the leak detector before checking the reports, so that the |
| 106 // stored reports can be examined without new reports being generated. |
| 107 detector_.reset(); |
| 108 |
| 109 // Check that all three observers got both sets of reports, passed in |
| 110 // separately. |
| 111 for (const TestObserver* obs : {&obs1, &obs2, &obs3}) { |
| 112 EXPECT_EQ(6U, obs->reports().size()); |
| 113 for (const auto& report : {reports1[0], reports1[1], reports1[2], |
| 114 reports2[0], reports2[1], reports2[2]}) { |
| 115 EXPECT_TRUE(obs->reports().find(report) != obs->reports().end()); |
| 116 } |
| 117 } |
| 118 } |
| 119 |
| 120 } // namespace metrics |
OLD | NEW |