blob: 53438777ee560b72fdfd6210b9b186f482013f4b [file] [log] [blame]
// 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, GURL()},
{NTPTileSource::TOP_SITES, ICON_REAL, GURL()},
{NTPTileSource::TOP_SITES, ICON_REAL, GURL()},
{NTPTileSource::TOP_SITES, ICON_COLOR, GURL()},
{NTPTileSource::TOP_SITES, ICON_COLOR, GURL()},
{NTPTileSource::SUGGESTIONS_SERVICE, ICON_REAL, GURL()},
{NTPTileSource::SUGGESTIONS_SERVICE, ICON_DEFAULT, GURL()},
{NTPTileSource::POPULAR, ICON_COLOR, GURL()}},
/*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=*/ICON_REAL, /*count=*/4),
base::Bucket(/*min=*/ICON_COLOR, /*count=*/3),
base::Bucket(/*min=*/ICON_DEFAULT, /*count=*/1)));
EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.TileType.server"),
ElementsAre(base::Bucket(/*min=*/ICON_REAL, /*count=*/1),
base::Bucket(/*min=*/ICON_DEFAULT, /*count=*/1)));
EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.TileType.client"),
ElementsAre(base::Bucket(/*min=*/ICON_REAL, /*count=*/3),
base::Bucket(/*min=*/ICON_COLOR, /*count=*/2)));
EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.TileType.popular"),
ElementsAre(base::Bucket(/*min=*/ICON_COLOR, /*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, GURL()},
{NTPTileSource::SUGGESTIONS_SERVICE, THUMBNAIL, GURL()},
{NTPTileSource::POPULAR, THUMBNAIL, GURL()}},
/*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