canonical_topic_unittest.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2022 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/privacy_sandbox/canonical_topic.h"
  5. #include "base/test/gtest_util.h"
  6. #include "base/test/metrics/histogram_tester.h"
  7. #include "components/strings/grit/components_strings.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. #include "ui/base/l10n/l10n_util.h"
  10. namespace privacy_sandbox {
  11. namespace {
  12. using Topic = browsing_topics::Topic;
  13. // Constraints around the currently checked in topics and taxonomy. Changes to
  14. // the taxononmy version or number of topics will fail these tests unless these
  15. // are also updated.
  16. constexpr int kAvailableTaxononmyVersion = 1;
  17. constexpr Topic kLowestTopicID = Topic(1);
  18. constexpr Topic kHighestTopicID = Topic(349);
  19. constexpr char kInvalidTopicLocalizedHistogramName[] =
  20. "Settings.PrivacySandbox.InvalidTopicIdLocalized";
  21. } // namespace
  22. using CanonicalTopicTest = testing::Test;
  23. TEST_F(CanonicalTopicTest, LocalizedRepresentation) {
  24. // Confirm that topics at the boundaries convert to strings appropriately.
  25. base::HistogramTester histogram_tester;
  26. CanonicalTopic first_topic(
  27. kLowestTopicID, privacy_sandbox::CanonicalTopic::AVAILABLE_TAXONOMY);
  28. CanonicalTopic last_topic(
  29. kHighestTopicID, privacy_sandbox::CanonicalTopic::AVAILABLE_TAXONOMY);
  30. EXPECT_EQ(l10n_util::GetStringUTF16(
  31. IDS_PRIVACY_SANDBOX_TOPICS_TAXONOMY_V1_TOPIC_ID_1),
  32. first_topic.GetLocalizedRepresentation());
  33. EXPECT_EQ(l10n_util::GetStringUTF16(
  34. IDS_PRIVACY_SANDBOX_TOPICS_TAXONOMY_V1_TOPIC_ID_349),
  35. last_topic.GetLocalizedRepresentation());
  36. // Successful localizations should not result in any metrics being recorded.
  37. histogram_tester.ExpectTotalCount(kInvalidTopicLocalizedHistogramName, 0);
  38. }
  39. TEST_F(CanonicalTopicTest, InvalidTopicIdLocalized) {
  40. // Confirm that an attempt to localize an invalid Topic ID returns the correct
  41. // error string and logs to UMA.
  42. base::HistogramTester histogram_tester;
  43. CanonicalTopic too_low_id(Topic(kLowestTopicID.value() - 1),
  44. kAvailableTaxononmyVersion);
  45. CanonicalTopic negative_id(Topic(-1), kAvailableTaxononmyVersion);
  46. CanonicalTopic too_high_id(Topic(kHighestTopicID.value() + 1),
  47. kAvailableTaxononmyVersion);
  48. std::vector<CanonicalTopic> test_bad_topics = {too_low_id, negative_id,
  49. too_high_id};
  50. for (const auto& topic : test_bad_topics) {
  51. EXPECT_EQ(
  52. l10n_util::GetStringUTF16(IDS_PRIVACY_SANDBOX_TOPICS_INVALID_TOPIC),
  53. topic.GetLocalizedRepresentation());
  54. }
  55. histogram_tester.ExpectTotalCount(kInvalidTopicLocalizedHistogramName, 3);
  56. histogram_tester.ExpectBucketCount(kInvalidTopicLocalizedHistogramName,
  57. too_low_id.topic_id().value(), 1);
  58. histogram_tester.ExpectBucketCount(kInvalidTopicLocalizedHistogramName,
  59. negative_id.topic_id().value(), 1);
  60. histogram_tester.ExpectBucketCount(kInvalidTopicLocalizedHistogramName,
  61. too_high_id.topic_id().value(), 1);
  62. }
  63. TEST_F(CanonicalTopicTest, ValueConversion) {
  64. // Confirm that conversion to and from base::Value forms work correctly.
  65. CanonicalTopic test_topic(kLowestTopicID, kAvailableTaxononmyVersion);
  66. auto topic_value = test_topic.ToValue();
  67. auto converted_topic = CanonicalTopic::FromValue(topic_value);
  68. EXPECT_TRUE(converted_topic);
  69. EXPECT_EQ(test_topic, *converted_topic);
  70. base::Value invalid_value(base::Value::Type::DICTIONARY);
  71. invalid_value.SetKey("unrelated", base::Value("unrelated"));
  72. converted_topic = CanonicalTopic::FromValue(invalid_value);
  73. EXPECT_FALSE(converted_topic);
  74. }
  75. using CanonicalTopicDeathTest = testing::Test;
  76. TEST_F(CanonicalTopicDeathTest, OutOfBoundsDeath) {
  77. // Confirm that requesting a topics with invalid Taxononmy results in a
  78. // CHECK failure.
  79. CanonicalTopic too_low_taxonomy(kLowestTopicID,
  80. kAvailableTaxononmyVersion - 1);
  81. CanonicalTopic negative_taxonomy(kLowestTopicID, -1);
  82. CanonicalTopic too_high_taxonomy(kLowestTopicID,
  83. kAvailableTaxononmyVersion + 1);
  84. std::vector<CanonicalTopic> test_bad_topics = {
  85. too_low_taxonomy, negative_taxonomy, too_high_taxonomy};
  86. for (const auto& topic : test_bad_topics)
  87. EXPECT_CHECK_DEATH(topic.GetLocalizedRepresentation());
  88. }
  89. } // namespace privacy_sandbox