Chromium Code Reviews| Index: components/ntp_tiles/metrics_unittest.cc |
| diff --git a/components/ntp_tiles/metrics_unittest.cc b/components/ntp_tiles/metrics_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..430008df41a2285a8c2f5e73e2243d12c3845234 |
| --- /dev/null |
| +++ b/components/ntp_tiles/metrics_unittest.cc |
| @@ -0,0 +1,162 @@ |
| +// 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/ntp_tiles/metrics.h" |
| + |
| +#include <stddef.h> |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/macros.h" |
| +#include "base/test/histogram_tester.h" |
| +#include "components/rappor/test_rappor_service.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace ntp_tiles { |
| +namespace metrics { |
| +namespace { |
| + |
| +using testing::ElementsAre; |
| +using testing::IsEmpty; |
| + |
| +TEST(RecordPageImpressionTest, ShouldRecordUmaForIcons) { |
| + base::HistogramTester histogram_tester; |
| + RecordPageImpression({{NTPTileSource::TOP_SITES, ICON_REAL}, |
| + {NTPTileSource::TOP_SITES, ICON_REAL}, |
| + {NTPTileSource::TOP_SITES, ICON_REAL}, |
| + {NTPTileSource::TOP_SITES, ICON_COLOR}, |
| + {NTPTileSource::TOP_SITES, ICON_COLOR}, |
| + {NTPTileSource::SUGGESTIONS_SERVICE, ICON_REAL}, |
| + {NTPTileSource::SUGGESTIONS_SERVICE, ICON_DEFAULT}, |
| + {NTPTileSource::POPULAR, ICON_COLOR}}, |
| + /*rappor_service=*/nullptr); |
| + EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.NumberOfTiles"), |
| + ElementsAre(base::Bucket(/*min=*/8, /*count=*/1))); |
| + EXPECT_THAT( |
| + histogram_tester.GetAllSamples("NewTabPage.SuggestionsImpression"), |
| + ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), |
| + base::Bucket(/*min=*/1, /*count=*/1), |
| + base::Bucket(/*min=*/2, /*count=*/1), |
| + base::Bucket(/*min=*/3, /*count=*/1), |
| + base::Bucket(/*min=*/4, /*count=*/1), |
| + base::Bucket(/*min=*/5, /*count=*/1), |
| + base::Bucket(/*min=*/6, /*count=*/1), |
| + base::Bucket(/*min=*/7, /*count=*/1))); |
| + EXPECT_THAT( |
| + histogram_tester.GetAllSamples("NewTabPage.SuggestionsImpression.server"), |
| + ElementsAre(base::Bucket(/*min=*/5, /*count=*/1), |
| + base::Bucket(/*min=*/6, /*count=*/1))); |
| + EXPECT_THAT( |
| + histogram_tester.GetAllSamples("NewTabPage.SuggestionsImpression.client"), |
| + ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), |
| + base::Bucket(/*min=*/1, /*count=*/1), |
| + base::Bucket(/*min=*/2, /*count=*/1), |
| + base::Bucket(/*min=*/3, /*count=*/1), |
| + base::Bucket(/*min=*/4, /*count=*/1))); |
| + EXPECT_THAT(histogram_tester.GetAllSamples( |
| + "NewTabPage.SuggestionsImpression.popular"), |
| + ElementsAre(base::Bucket(/*min=*/7, /*count=*/1))); |
| + EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.TileType"), |
| + ElementsAre(base::Bucket(/*min=*/1, /*count=*/4), |
|
Marc Treib
2016/12/08 12:58:22
Can we use the enum entries for "min" here, instea
mastiz
2016/12/08 13:47:07
Done.
|
| + base::Bucket(/*min=*/2, /*count=*/3), |
| + base::Bucket(/*min=*/3, /*count=*/1))); |
| + EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.TileType.server"), |
| + ElementsAre(base::Bucket(/*min=*/1, /*count=*/1), |
| + base::Bucket(/*min=*/3, /*count=*/1))); |
| + EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.TileType.client"), |
| + ElementsAre(base::Bucket(/*min=*/1, /*count=*/3), |
| + base::Bucket(/*min=*/2, /*count=*/2))); |
| + EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.TileType.popular"), |
| + ElementsAre(base::Bucket(/*min=*/2, /*count=*/1))); |
| + EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.IconsReal"), |
| + ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); |
| + EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.IconsColor"), |
| + ElementsAre(base::Bucket(/*min=*/3, /*count=*/1))); |
| + EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.IconsGray"), |
| + ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); |
| +} |
| + |
| +TEST(RecordPageImpressionTest, ShouldRecordUmaForThumbnails) { |
| + base::HistogramTester histogram_tester; |
| + RecordPageImpression({{NTPTileSource::TOP_SITES, THUMBNAIL}, |
| + {NTPTileSource::SUGGESTIONS_SERVICE, THUMBNAIL}, |
| + {NTPTileSource::POPULAR, THUMBNAIL}}, |
| + /*rappor_service=*/nullptr); |
| + EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.NumberOfTiles"), |
| + ElementsAre(base::Bucket(/*min=*/3, /*count=*/1))); |
| + EXPECT_THAT( |
| + histogram_tester.GetAllSamples("NewTabPage.SuggestionsImpression"), |
| + ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), |
| + base::Bucket(/*min=*/1, /*count=*/1), |
| + base::Bucket(/*min=*/2, /*count=*/1))); |
| + EXPECT_THAT( |
| + histogram_tester.GetAllSamples("NewTabPage.SuggestionsImpression.server"), |
| + ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); |
| + EXPECT_THAT( |
| + histogram_tester.GetAllSamples("NewTabPage.SuggestionsImpression.client"), |
| + ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); |
| + EXPECT_THAT(histogram_tester.GetAllSamples( |
| + "NewTabPage.SuggestionsImpression.popular"), |
| + ElementsAre(base::Bucket(/*min=*/2, /*count=*/1))); |
| + EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.TileType"), IsEmpty()); |
| + EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.TileType.server"), |
| + IsEmpty()); |
| + EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.TileType.client"), |
| + IsEmpty()); |
| + EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.TileType.popular"), |
| + IsEmpty()); |
| + EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.IconsReal"), |
| + IsEmpty()); |
| + EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.IconsColor"), |
| + IsEmpty()); |
| + EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.IconsGray"), |
| + IsEmpty()); |
| +} |
| + |
| +TEST(RecordPageImpressionTest, ShouldRecordRappor) { |
| + rappor::TestRapporServiceImpl rappor_service; |
| + |
| + RecordPageImpression( |
| + {{NTPTileSource::TOP_SITES, ICON_REAL, GURL("http://www.site1.com/")}, |
| + {NTPTileSource::TOP_SITES, ICON_COLOR, GURL("http://www.site2.com/")}, |
| + {NTPTileSource::TOP_SITES, ICON_DEFAULT, GURL("http://www.site3.com/")}, |
| + {NTPTileSource::TOP_SITES, THUMBNAIL, GURL("http://www.site4.com/")}}, |
| + &rappor_service); |
| + |
| + // Thumbnail shouldn't get reported. |
| + EXPECT_EQ(3, rappor_service.GetReportsCount()); |
| + |
| + { |
| + std::string sample; |
| + rappor::RapporType type; |
| + EXPECT_TRUE(rappor_service.GetRecordedSampleForMetric( |
| + "NTP.SuggestionsImpressions.IconsReal", &sample, &type)); |
| + EXPECT_EQ("site1.com", sample); |
| + EXPECT_EQ(rappor::ETLD_PLUS_ONE_RAPPOR_TYPE, type); |
| + } |
| + |
| + { |
| + std::string sample; |
| + rappor::RapporType type; |
| + EXPECT_TRUE(rappor_service.GetRecordedSampleForMetric( |
| + "NTP.SuggestionsImpressions.IconsColor", &sample, &type)); |
| + EXPECT_EQ("site2.com", sample); |
| + EXPECT_EQ(rappor::ETLD_PLUS_ONE_RAPPOR_TYPE, type); |
| + } |
| + |
| + { |
| + std::string sample; |
| + rappor::RapporType type; |
| + EXPECT_TRUE(rappor_service.GetRecordedSampleForMetric( |
| + "NTP.SuggestionsImpressions.IconsGray", &sample, &type)); |
| + EXPECT_EQ("site3.com", sample); |
| + EXPECT_EQ(rappor::ETLD_PLUS_ONE_RAPPOR_TYPE, type); |
| + } |
| +} |
| + |
| +} // namespace |
| +} // namespace metrics |
| +} // namespace ntp_tiles |