push_notification_manager_unittest.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/optimization_guide/core/push_notification_manager.h"
  5. #include "base/test/metrics/histogram_tester.h"
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. using ::testing::_;
  8. using ::testing::SaveArg;
  9. namespace optimization_guide {
  10. class TestDelegate : public PushNotificationManager::Delegate {
  11. public:
  12. using RemoveMultiplePair =
  13. std::pair<proto::KeyRepresentation, base::flat_set<std::string>>;
  14. TestDelegate() = default;
  15. ~TestDelegate() = default;
  16. const std::vector<RemoveMultiplePair>& removed_entries() const {
  17. return removed_entries_;
  18. }
  19. void RemoveFetchedEntriesByHintKeys(
  20. base::OnceClosure on_success,
  21. proto::KeyRepresentation key_representation,
  22. const base::flat_set<std::string>& hint_keys) override {
  23. removed_entries_.emplace_back(key_representation, hint_keys);
  24. std::move(on_success).Run();
  25. }
  26. private:
  27. std::vector<RemoveMultiplePair> removed_entries_;
  28. };
  29. class MockObserver : public PushNotificationManager::Observer {
  30. public:
  31. MockObserver() = default;
  32. ~MockObserver() override = default;
  33. MOCK_METHOD(void,
  34. OnNotificationPayload,
  35. (proto::OptimizationType, const optimization_guide::proto::Any&),
  36. (override));
  37. };
  38. class AnotherMockObserver : public PushNotificationManager::Observer {
  39. public:
  40. AnotherMockObserver() = default;
  41. ~AnotherMockObserver() override = default;
  42. MOCK_METHOD(void,
  43. OnNotificationPayload,
  44. (proto::OptimizationType, const optimization_guide::proto::Any&),
  45. (override));
  46. };
  47. class PushNotificationManagerUnitTest : public testing::Test {
  48. public:
  49. PushNotificationManagerUnitTest() = default;
  50. ~PushNotificationManagerUnitTest() override = default;
  51. void SetUp() override {
  52. manager_.SetDelegate(&delegate_);
  53. manager_.AddObserver(&observer_);
  54. }
  55. MockObserver* observer() { return &observer_; }
  56. PushNotificationManager* manager() { return &manager_; }
  57. TestDelegate* delegate() { return &delegate_; }
  58. bool manager_has_observer(PushNotificationManager::Observer* observer) {
  59. return manager()->observers_.HasObserver(observer);
  60. }
  61. private:
  62. MockObserver observer_;
  63. PushNotificationManager manager_;
  64. TestDelegate delegate_;
  65. };
  66. TEST_F(PushNotificationManagerUnitTest, TestNewNotificationReceived) {
  67. base::HistogramTester histogram_tester;
  68. proto::HintNotificationPayload notification;
  69. notification.set_hint_key("hintkey");
  70. notification.set_key_representation(proto::KeyRepresentation::HOST);
  71. notification.set_optimization_type(
  72. proto::OptimizationType::PERFORMANCE_HINTS);
  73. optimization_guide::proto::Any* payload = notification.mutable_payload();
  74. payload->set_type_url("type_url");
  75. payload->set_value("value");
  76. optimization_guide::proto::Any payload_to_observer;
  77. EXPECT_CALL(*observer(), OnNotificationPayload(
  78. proto::OptimizationType::PERFORMANCE_HINTS, _))
  79. .WillOnce(SaveArg<1>(&payload_to_observer));
  80. manager()->OnNewPushNotification(notification);
  81. histogram_tester.ExpectUniqueSample(
  82. "OptimizationGuide.PushNotifications.ReceivedNotificationType",
  83. proto::OptimizationType::PERFORMANCE_HINTS, 1);
  84. EXPECT_EQ(payload_to_observer.type_url(), "type_url");
  85. EXPECT_EQ(payload_to_observer.value(), "value");
  86. std::vector<TestDelegate::RemoveMultiplePair> removed_entries =
  87. delegate()->removed_entries();
  88. EXPECT_EQ(1U, removed_entries.size());
  89. EXPECT_EQ(proto::KeyRepresentation::HOST, removed_entries[0].first);
  90. EXPECT_EQ(base::flat_set<std::string>{"hintkey"}, removed_entries[0].second);
  91. }
  92. TEST_F(PushNotificationManagerUnitTest, TestNewNotificationReceivedNoPayload) {
  93. base::HistogramTester histogram_tester;
  94. proto::HintNotificationPayload notification;
  95. notification.set_hint_key("hintkey");
  96. notification.set_key_representation(proto::KeyRepresentation::HOST);
  97. notification.set_optimization_type(
  98. proto::OptimizationType::PERFORMANCE_HINTS);
  99. EXPECT_CALL(*observer(), OnNotificationPayload(_, _)).Times(0);
  100. manager()->OnNewPushNotification(notification);
  101. histogram_tester.ExpectUniqueSample(
  102. "OptimizationGuide.PushNotifications.ReceivedNotificationType",
  103. proto::OptimizationType::PERFORMANCE_HINTS, 0);
  104. EXPECT_EQ(0U, delegate()->removed_entries().size());
  105. }
  106. TEST_F(PushNotificationManagerUnitTest, TestAddRemoveObserver) {
  107. AnotherMockObserver another_mock_observer;
  108. manager()->AddObserver(&another_mock_observer);
  109. EXPECT_TRUE(manager_has_observer(&another_mock_observer));
  110. manager()->RemoveObserver(&another_mock_observer);
  111. EXPECT_FALSE(manager_has_observer(&another_mock_observer));
  112. }
  113. } // namespace optimization_guide