| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/metrics/histogram.h" | 6 #include "base/metrics/histogram.h" |
| 7 #include "base/metrics/statistics_recorder.h" | 7 #include "base/metrics/statistics_recorder.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "chrome/browser/search/most_visited_iframe_source.h" | 9 #include "chrome/browser/search/most_visited_iframe_source.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 class MostVisitedIframeSourceTest : public testing::Test { | 12 class MostVisitedIframeSourceTest : public testing::Test { |
| 13 public: | 13 public: |
| 14 void ExpectNullData(base::RefCountedMemory* data) { | 14 void ExpectNullData(base::RefCountedMemory* data) { |
| 15 EXPECT_EQ(NULL, data); | 15 EXPECT_EQ(NULL, data); |
| 16 } | 16 } |
| 17 | 17 |
| 18 protected: | 18 protected: |
| 19 MostVisitedIframeSource* source() { return source_.get(); } | 19 MostVisitedIframeSource* source() { return source_.get(); } |
| 20 | 20 |
| 21 private: | 21 private: |
| 22 virtual void SetUp() { source_.reset(new MostVisitedIframeSource()); } | 22 virtual void SetUp() { source_.reset(new MostVisitedIframeSource()); } |
| 23 | 23 |
| 24 scoped_ptr<MostVisitedIframeSource> source_; | 24 scoped_ptr<MostVisitedIframeSource> source_; |
| 25 }; | 25 }; |
| 26 | |
| 27 TEST_F(MostVisitedIframeSourceTest, LogEndpointIsValidNoProvider) { | |
| 28 content::URLDataSource::GotDataCallback callback = base::Bind( | |
| 29 &MostVisitedIframeSourceTest::ExpectNullData, base::Unretained(this)); | |
| 30 | |
| 31 base::StatisticsRecorder::Initialize(); | |
| 32 // Making sure the histogram is created. | |
| 33 UMA_HISTOGRAM_ENUMERATION(MostVisitedIframeSource::kMostVisitedHistogramName, | |
| 34 0, MostVisitedIframeSource::kNumMostVisited); | |
| 35 | |
| 36 base::HistogramBase* histogram = base::StatisticsRecorder::FindHistogram( | |
| 37 MostVisitedIframeSource::kMostVisitedHistogramName); | |
| 38 | |
| 39 scoped_ptr<base::HistogramSamples> samples1(histogram->SnapshotSamples()); | |
| 40 // The dummy value got logged. | |
| 41 EXPECT_EQ(1, samples1->TotalCount()); | |
| 42 EXPECT_EQ(1, samples1->GetCount(0)); | |
| 43 | |
| 44 // Test the method. | |
| 45 source()->StartDataRequest("log.html?pos=1", 0, 0, callback); | |
| 46 | |
| 47 scoped_ptr<base::HistogramSamples> samples2(histogram->SnapshotSamples()); | |
| 48 EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount()); | |
| 49 EXPECT_EQ(1, samples2->GetCount(1)); | |
| 50 | |
| 51 // Counts accumulate and behave as expected. | |
| 52 source()->StartDataRequest("log.html?pos=5", 0, 0, callback); | |
| 53 source()->StartDataRequest("log.html?pos=1", 0, 0, callback); | |
| 54 | |
| 55 scoped_ptr<base::HistogramSamples> samples3(histogram->SnapshotSamples()); | |
| 56 EXPECT_EQ(samples2->TotalCount() + 2, samples3->TotalCount()); | |
| 57 EXPECT_EQ(1, samples3->GetCount(5)); | |
| 58 EXPECT_EQ(2, samples3->GetCount(1)); | |
| 59 | |
| 60 // If 'pos' is unspecified or invalid, callback still gets called but values | |
| 61 // are not incremented. | |
| 62 source()->StartDataRequest("log.html?pos=", 0, 0, callback); | |
| 63 source()->StartDataRequest("log.html", 0, 0, callback); | |
| 64 // Total count hasn't changed. | |
| 65 EXPECT_EQ(samples2->TotalCount() + 2, samples3->TotalCount()); | |
| 66 } | |
| 67 | |
| 68 TEST_F(MostVisitedIframeSourceTest, LogEndpointIsValidWithProvider) { | |
| 69 content::URLDataSource::GotDataCallback callback = base::Bind( | |
| 70 &MostVisitedIframeSourceTest::ExpectNullData, base::Unretained(this)); | |
| 71 | |
| 72 base::StatisticsRecorder::Initialize(); | |
| 73 // Making sure a test histogram with provider is created by logging a dummy | |
| 74 // value. | |
| 75 const std::string histogram_name = | |
| 76 MostVisitedIframeSource::GetHistogramNameForProvider("foobar"); | |
| 77 UMA_HISTOGRAM_ENUMERATION(histogram_name, 0, | |
| 78 MostVisitedIframeSource::kNumMostVisited); | |
| 79 | |
| 80 base::HistogramBase* histogram = base::StatisticsRecorder::FindHistogram( | |
| 81 histogram_name); | |
| 82 scoped_ptr<base::HistogramSamples> samples1(histogram->SnapshotSamples()); | |
| 83 // The dummy value got logged. | |
| 84 EXPECT_EQ(1, samples1->TotalCount()); | |
| 85 EXPECT_EQ(1, samples1->GetCount(0)); | |
| 86 | |
| 87 // Test the method. | |
| 88 source()->StartDataRequest("log.html?pos=1&pr=foobar", 0, 0, callback); | |
| 89 | |
| 90 scoped_ptr<base::HistogramSamples> samples2(histogram->SnapshotSamples()); | |
| 91 EXPECT_EQ(samples1->TotalCount() + 1, samples2->TotalCount()); | |
| 92 EXPECT_EQ(1, samples2->GetCount(1)); | |
| 93 | |
| 94 // Counts accumulate and behave as expected. | |
| 95 source()->StartDataRequest("log.html?pos=5&pr=foobar", 0, 0, callback); | |
| 96 source()->StartDataRequest("log.html?pos=1&pr=foobar", 0, 0, callback); | |
| 97 // This should have no effect for the "foobar" histogram. | |
| 98 source()->StartDataRequest("log.html?pos=1&pr=nofoo", 0, 0, callback); | |
| 99 | |
| 100 scoped_ptr<base::HistogramSamples> samples3(histogram->SnapshotSamples()); | |
| 101 EXPECT_EQ(samples2->TotalCount() + 2, samples3->TotalCount()); | |
| 102 EXPECT_EQ(1, samples3->GetCount(5)); | |
| 103 EXPECT_EQ(2, samples3->GetCount(1)); | |
| 104 } | |
| OLD | NEW |