metrics_unittest.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. #include "components/ntp_tiles/metrics.h"
  5. #include <stddef.h>
  6. #include <string>
  7. #include "base/test/metrics/histogram_tester.h"
  8. #include "testing/gmock/include/gmock/gmock.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace ntp_tiles {
  11. namespace metrics {
  12. namespace {
  13. constexpr int kUnknownTitleSource = static_cast<int>(TileTitleSource::UNKNOWN);
  14. constexpr int kManifestTitleSource =
  15. static_cast<int>(TileTitleSource::MANIFEST);
  16. constexpr int kMetaTagTitleSource = static_cast<int>(TileTitleSource::META_TAG);
  17. constexpr int kTitleTagTitleSource =
  18. static_cast<int>(TileTitleSource::TITLE_TAG);
  19. constexpr int kInferredTitleSource =
  20. static_cast<int>(TileTitleSource::INFERRED);
  21. using favicon_base::IconType;
  22. using testing::ElementsAre;
  23. using testing::IsEmpty;
  24. MATCHER_P3(IsBucketBetween, lower_bound, upper_bound, count, "") {
  25. return arg.min >= lower_bound && arg.min <= upper_bound && arg.count == count;
  26. }
  27. // Builder for instances of NTPTileImpression that uses sensible defaults.
  28. class Builder {
  29. public:
  30. Builder() {}
  31. Builder& WithIndex(int index) {
  32. impression_.index = index;
  33. return *this;
  34. }
  35. Builder& WithSource(TileSource source) {
  36. impression_.source = source;
  37. return *this;
  38. }
  39. Builder& WithTitleSource(TileTitleSource title_source) {
  40. impression_.title_source = title_source;
  41. return *this;
  42. }
  43. Builder& WithVisualType(TileVisualType visual_type) {
  44. impression_.visual_type = visual_type;
  45. return *this;
  46. }
  47. Builder& WithIconType(favicon_base::IconType icon_type) {
  48. impression_.icon_type = icon_type;
  49. return *this;
  50. }
  51. Builder& WithUrl(const GURL& url) {
  52. impression_.url_for_rappor = url;
  53. return *this;
  54. }
  55. NTPTileImpression Build() { return impression_; }
  56. private:
  57. NTPTileImpression impression_;
  58. };
  59. TEST(RecordPageImpressionTest, ShouldRecordNumberOfTiles) {
  60. base::HistogramTester histogram_tester;
  61. RecordPageImpression(5);
  62. EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.NumberOfTiles"),
  63. ElementsAre(base::Bucket(/*min=*/5, /*count=*/1)));
  64. }
  65. TEST(RecordTileImpressionTest, ShouldRecordUmaForIcons) {
  66. base::HistogramTester histogram_tester;
  67. RecordTileImpression(Builder()
  68. .WithIndex(0)
  69. .WithSource(TileSource::TOP_SITES)
  70. .WithVisualType(ICON_REAL)
  71. .Build());
  72. RecordTileImpression(Builder()
  73. .WithIndex(1)
  74. .WithSource(TileSource::TOP_SITES)
  75. .WithVisualType(ICON_REAL)
  76. .Build());
  77. RecordTileImpression(Builder()
  78. .WithIndex(2)
  79. .WithSource(TileSource::TOP_SITES)
  80. .WithVisualType(ICON_REAL)
  81. .Build());
  82. RecordTileImpression(Builder()
  83. .WithIndex(3)
  84. .WithSource(TileSource::TOP_SITES)
  85. .WithVisualType(ICON_COLOR)
  86. .Build());
  87. RecordTileImpression(Builder()
  88. .WithIndex(4)
  89. .WithSource(TileSource::TOP_SITES)
  90. .WithVisualType(ICON_COLOR)
  91. .Build());
  92. RecordTileImpression(Builder()
  93. .WithIndex(5)
  94. .WithSource(TileSource::POPULAR)
  95. .WithVisualType(ICON_REAL)
  96. .Build());
  97. RecordTileImpression(Builder()
  98. .WithIndex(6)
  99. .WithSource(TileSource::POPULAR)
  100. .WithVisualType(ICON_DEFAULT)
  101. .Build());
  102. RecordTileImpression(Builder()
  103. .WithIndex(7)
  104. .WithSource(TileSource::POPULAR)
  105. .WithVisualType(ICON_COLOR)
  106. .Build());
  107. EXPECT_THAT(
  108. histogram_tester.GetAllSamples("NewTabPage.SuggestionsImpression"),
  109. ElementsAre(base::Bucket(/*min=*/0, /*count=*/1),
  110. base::Bucket(/*min=*/1, /*count=*/1),
  111. base::Bucket(/*min=*/2, /*count=*/1),
  112. base::Bucket(/*min=*/3, /*count=*/1),
  113. base::Bucket(/*min=*/4, /*count=*/1),
  114. base::Bucket(/*min=*/5, /*count=*/1),
  115. base::Bucket(/*min=*/6, /*count=*/1),
  116. base::Bucket(/*min=*/7, /*count=*/1)));
  117. EXPECT_THAT(
  118. histogram_tester.GetAllSamples("NewTabPage.SuggestionsImpression.client"),
  119. ElementsAre(base::Bucket(/*min=*/0, /*count=*/1),
  120. base::Bucket(/*min=*/1, /*count=*/1),
  121. base::Bucket(/*min=*/2, /*count=*/1),
  122. base::Bucket(/*min=*/3, /*count=*/1),
  123. base::Bucket(/*min=*/4, /*count=*/1)));
  124. EXPECT_THAT(histogram_tester.GetAllSamples(
  125. "NewTabPage.SuggestionsImpression.popular_fetched"),
  126. ElementsAre(base::Bucket(/*min=*/5, /*count=*/1),
  127. base::Bucket(/*min=*/6, /*count=*/1),
  128. base::Bucket(/*min=*/7, /*count=*/1)));
  129. EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.TileType"),
  130. ElementsAre(base::Bucket(/*min=*/ICON_REAL, /*count=*/4),
  131. base::Bucket(/*min=*/ICON_COLOR, /*count=*/3),
  132. base::Bucket(/*min=*/ICON_DEFAULT, /*count=*/1)));
  133. EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.TileType.client"),
  134. ElementsAre(base::Bucket(/*min=*/ICON_REAL, /*count=*/3),
  135. base::Bucket(/*min=*/ICON_COLOR, /*count=*/2)));
  136. EXPECT_THAT(
  137. histogram_tester.GetAllSamples("NewTabPage.TileType.popular_fetched"),
  138. ElementsAre(base::Bucket(/*min=*/ICON_REAL, /*count=*/1),
  139. base::Bucket(/*min=*/ICON_COLOR, /*count=*/1),
  140. base::Bucket(/*min=*/ICON_DEFAULT, /*count=*/1)));
  141. EXPECT_THAT(histogram_tester.GetAllSamples(
  142. "NewTabPage.SuggestionsImpression.IconsReal"),
  143. ElementsAre(base::Bucket(/*min=*/0, /*count=*/1),
  144. base::Bucket(/*min=*/1, /*count=*/1),
  145. base::Bucket(/*min=*/2, /*count=*/1),
  146. base::Bucket(/*min=*/5, /*count=*/1)));
  147. EXPECT_THAT(histogram_tester.GetAllSamples(
  148. "NewTabPage.SuggestionsImpression.IconsColor"),
  149. ElementsAre(base::Bucket(/*min=*/3, /*count=*/1),
  150. base::Bucket(/*min=*/4, /*count=*/1),
  151. base::Bucket(/*min=*/7, /*count=*/1)));
  152. EXPECT_THAT(histogram_tester.GetAllSamples(
  153. "NewTabPage.SuggestionsImpression.IconsGray"),
  154. ElementsAre(base::Bucket(/*min=*/6, /*count=*/1)));
  155. }
  156. TEST(RecordTileImpressionTest, ShouldRecordImpressionsForTileTitleSource) {
  157. base::HistogramTester histogram_tester;
  158. RecordTileImpression(Builder()
  159. .WithSource(TileSource::TOP_SITES)
  160. .WithTitleSource(TileTitleSource::UNKNOWN)
  161. .Build());
  162. RecordTileImpression(Builder()
  163. .WithSource(TileSource::TOP_SITES)
  164. .WithTitleSource(TileTitleSource::INFERRED)
  165. .Build());
  166. RecordTileImpression(Builder()
  167. .WithSource(TileSource::POPULAR)
  168. .WithTitleSource(TileTitleSource::TITLE_TAG)
  169. .Build());
  170. RecordTileImpression(Builder()
  171. .WithSource(TileSource::POPULAR)
  172. .WithTitleSource(TileTitleSource::MANIFEST)
  173. .Build());
  174. RecordTileImpression(Builder()
  175. .WithSource(TileSource::POPULAR_BAKED_IN)
  176. .WithTitleSource(TileTitleSource::TITLE_TAG)
  177. .Build());
  178. RecordTileImpression(Builder()
  179. .WithSource(TileSource::POPULAR_BAKED_IN)
  180. .WithTitleSource(TileTitleSource::META_TAG)
  181. .Build());
  182. EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.TileTitle.client"),
  183. ElementsAre(base::Bucket(kUnknownTitleSource, /*count=*/1),
  184. base::Bucket(kInferredTitleSource, /*count=*/1)));
  185. EXPECT_THAT(
  186. histogram_tester.GetAllSamples("NewTabPage.TileTitle.popular_fetched"),
  187. ElementsAre(base::Bucket(kManifestTitleSource, /*count=*/1),
  188. base::Bucket(kTitleTagTitleSource, /*count=*/1)));
  189. EXPECT_THAT(
  190. histogram_tester.GetAllSamples("NewTabPage.TileTitle.popular_baked_in"),
  191. ElementsAre(base::Bucket(kMetaTagTitleSource, /*count=*/1),
  192. base::Bucket(kTitleTagTitleSource, /*count=*/1)));
  193. EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.TileTitle"),
  194. ElementsAre(base::Bucket(kUnknownTitleSource, /*count=*/1),
  195. base::Bucket(kManifestTitleSource, /*count=*/1),
  196. base::Bucket(kMetaTagTitleSource, /*count=*/1),
  197. base::Bucket(kTitleTagTitleSource, /*count=*/2),
  198. base::Bucket(kInferredTitleSource, /*count=*/1)));
  199. }
  200. TEST(RecordTileImpressionTest, ShouldRecordUmaForIconType) {
  201. base::HistogramTester histogram_tester;
  202. RecordTileImpression(Builder()
  203. .WithVisualType(ICON_COLOR)
  204. .WithIconType(IconType::kTouchIcon)
  205. .Build());
  206. RecordTileImpression(Builder()
  207. .WithVisualType(ICON_REAL)
  208. .WithIconType(IconType::kWebManifestIcon)
  209. .Build());
  210. EXPECT_THAT(
  211. histogram_tester.GetAllSamples("NewTabPage.TileFaviconType.IconsColor"),
  212. ElementsAre(base::Bucket(/*min=*/2, /*count=*/1)));
  213. EXPECT_THAT(
  214. histogram_tester.GetAllSamples("NewTabPage.TileFaviconType.IconsReal"),
  215. ElementsAre(base::Bucket(/*min=*/4, /*count=*/1)));
  216. EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.TileFaviconType"),
  217. ElementsAre(base::Bucket(/*min=*/2, /*count=*/1),
  218. base::Bucket(/*min=*/4, /*count=*/1)));
  219. }
  220. TEST(RecordTileClickTest, ShouldRecordUmaForIcon) {
  221. base::HistogramTester histogram_tester;
  222. RecordTileClick(Builder()
  223. .WithIndex(3)
  224. .WithSource(TileSource::TOP_SITES)
  225. .WithVisualType(ICON_REAL)
  226. .Build());
  227. EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.MostVisited"),
  228. ElementsAre(base::Bucket(/*min=*/3, /*count=*/1)));
  229. EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.MostVisited.client"),
  230. ElementsAre(base::Bucket(/*min=*/3, /*count=*/1)));
  231. EXPECT_THAT(
  232. histogram_tester.GetAllSamples("NewTabPage.MostVisited.popular_fetched"),
  233. IsEmpty());
  234. EXPECT_THAT(
  235. histogram_tester.GetAllSamples("NewTabPage.MostVisited.IconsReal"),
  236. ElementsAre(base::Bucket(/*min=*/3, /*count=*/1)));
  237. EXPECT_THAT(
  238. histogram_tester.GetAllSamples("NewTabPage.MostVisited.IconsColor"),
  239. IsEmpty());
  240. EXPECT_THAT(
  241. histogram_tester.GetAllSamples("NewTabPage.MostVisited.IconsGray"),
  242. IsEmpty());
  243. }
  244. TEST(RecordTileClickTest, ShouldNotRecordUnknownTileType) {
  245. base::HistogramTester histogram_tester;
  246. RecordTileClick(Builder()
  247. .WithIndex(3)
  248. .WithSource(TileSource::TOP_SITES)
  249. .WithVisualType(UNKNOWN_TILE_TYPE)
  250. .Build());
  251. // The click should still get recorded.
  252. EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.MostVisited"),
  253. ElementsAre(base::Bucket(/*min=*/3, /*count=*/1)));
  254. EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.MostVisited.client"),
  255. ElementsAre(base::Bucket(/*min=*/3, /*count=*/1)));
  256. EXPECT_THAT(
  257. histogram_tester.GetAllSamples("NewTabPage.MostVisited.popular_fetched"),
  258. IsEmpty());
  259. // But all of the tile type histograms should be empty.
  260. EXPECT_THAT(
  261. histogram_tester.GetAllSamples("NewTabPage.MostVisited.IconsReal"),
  262. IsEmpty());
  263. EXPECT_THAT(
  264. histogram_tester.GetAllSamples("NewTabPage.MostVisited.IconsColor"),
  265. IsEmpty());
  266. EXPECT_THAT(
  267. histogram_tester.GetAllSamples("NewTabPage.MostVisited.IconsGray"),
  268. IsEmpty());
  269. }
  270. TEST(RecordTileClickTest, ShouldRecordClicksForTileTitleSource) {
  271. base::HistogramTester histogram_tester;
  272. RecordTileClick(Builder()
  273. .WithSource(TileSource::TOP_SITES)
  274. .WithTitleSource(TileTitleSource::UNKNOWN)
  275. .Build());
  276. RecordTileClick(Builder()
  277. .WithSource(TileSource::TOP_SITES)
  278. .WithTitleSource(TileTitleSource::TITLE_TAG)
  279. .Build());
  280. RecordTileClick(Builder()
  281. .WithSource(TileSource::POPULAR)
  282. .WithTitleSource(TileTitleSource::TITLE_TAG)
  283. .Build());
  284. RecordTileClick(Builder()
  285. .WithSource(TileSource::POPULAR)
  286. .WithTitleSource(TileTitleSource::MANIFEST)
  287. .Build());
  288. RecordTileClick(Builder()
  289. .WithSource(TileSource::POPULAR_BAKED_IN)
  290. .WithTitleSource(TileTitleSource::TITLE_TAG)
  291. .Build());
  292. RecordTileClick(Builder()
  293. .WithSource(TileSource::POPULAR_BAKED_IN)
  294. .WithTitleSource(TileTitleSource::META_TAG)
  295. .Build());
  296. EXPECT_THAT(
  297. histogram_tester.GetAllSamples("NewTabPage.TileTitleClicked.client"),
  298. ElementsAre(base::Bucket(kUnknownTitleSource, /*count=*/1),
  299. base::Bucket(kTitleTagTitleSource, /*count=*/1)));
  300. EXPECT_THAT(histogram_tester.GetAllSamples(
  301. "NewTabPage.TileTitleClicked.popular_fetched"),
  302. ElementsAre(base::Bucket(kManifestTitleSource, /*count=*/1),
  303. base::Bucket(kTitleTagTitleSource, /*count=*/1)));
  304. EXPECT_THAT(histogram_tester.GetAllSamples(
  305. "NewTabPage.TileTitleClicked.popular_baked_in"),
  306. ElementsAre(base::Bucket(kMetaTagTitleSource, /*count=*/1),
  307. base::Bucket(kTitleTagTitleSource, /*count=*/1)));
  308. EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.TileTitleClicked"),
  309. ElementsAre(base::Bucket(kUnknownTitleSource, /*count=*/1),
  310. base::Bucket(kManifestTitleSource, /*count=*/1),
  311. base::Bucket(kMetaTagTitleSource, /*count=*/1),
  312. base::Bucket(kTitleTagTitleSource, /*count=*/3)));
  313. }
  314. TEST(RecordTileClickTest, ShouldRecordClicksForIconType) {
  315. base::HistogramTester histogram_tester;
  316. RecordTileClick(Builder()
  317. .WithVisualType(ICON_COLOR)
  318. .WithIconType(IconType::kTouchIcon)
  319. .Build());
  320. RecordTileClick(Builder()
  321. .WithVisualType(ICON_REAL)
  322. .WithIconType(IconType::kWebManifestIcon)
  323. .Build());
  324. EXPECT_THAT(histogram_tester.GetAllSamples(
  325. "NewTabPage.TileFaviconTypeClicked.IconsColor"),
  326. ElementsAre(base::Bucket(/*min=*/2, /*count=*/1)));
  327. EXPECT_THAT(histogram_tester.GetAllSamples(
  328. "NewTabPage.TileFaviconTypeClicked.IconsReal"),
  329. ElementsAre(base::Bucket(/*min=*/4, /*count=*/1)));
  330. EXPECT_THAT(
  331. histogram_tester.GetAllSamples("NewTabPage.TileFaviconTypeClicked"),
  332. ElementsAre(base::Bucket(/*min=*/2, /*count=*/1),
  333. base::Bucket(/*min=*/4, /*count=*/1)));
  334. }
  335. } // namespace
  336. } // namespace metrics
  337. } // namespace ntp_tiles