File: push_notification_manager_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (143 lines) | stat: -rw-r--r-- 4,851 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/optimization_guide/core/push_notification_manager.h"

#include "base/test/metrics/histogram_tester.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::_;
using ::testing::SaveArg;

namespace optimization_guide {

class TestDelegate : public PushNotificationManager::Delegate {
 public:
  using RemoveMultiplePair =
      std::pair<proto::KeyRepresentation, base::flat_set<std::string>>;

  TestDelegate() = default;
  ~TestDelegate() = default;

  const std::vector<RemoveMultiplePair>& removed_entries() const {
    return removed_entries_;
  }

  void RemoveFetchedEntriesByHintKeys(
      base::OnceClosure on_success,
      proto::KeyRepresentation key_representation,
      const base::flat_set<std::string>& hint_keys) override {
    removed_entries_.emplace_back(key_representation, hint_keys);
    std::move(on_success).Run();
  }

 private:
  std::vector<RemoveMultiplePair> removed_entries_;
};

class MockObserver : public PushNotificationManager::Observer {
 public:
  MockObserver() = default;
  ~MockObserver() override = default;
  MOCK_METHOD(void,
              OnNotificationPayload,
              (proto::OptimizationType, const optimization_guide::proto::Any&),
              (override));
};

class AnotherMockObserver : public PushNotificationManager::Observer {
 public:
  AnotherMockObserver() = default;
  ~AnotherMockObserver() override = default;

  MOCK_METHOD(void,
              OnNotificationPayload,
              (proto::OptimizationType, const optimization_guide::proto::Any&),
              (override));
};

class PushNotificationManagerUnitTest : public testing::Test {
 public:
  PushNotificationManagerUnitTest() = default;
  ~PushNotificationManagerUnitTest() override = default;

  void SetUp() override {
    manager_.SetDelegate(&delegate_);
    manager_.AddObserver(&observer_);
  }

  MockObserver* observer() { return &observer_; }

  PushNotificationManager* manager() { return &manager_; }

  TestDelegate* delegate() { return &delegate_; }

  bool manager_has_observer(PushNotificationManager::Observer* observer) {
    return manager()->observers_.HasObserver(observer);
  }

 private:
  MockObserver observer_;
  PushNotificationManager manager_;
  TestDelegate delegate_;
};

TEST_F(PushNotificationManagerUnitTest, TestNewNotificationReceived) {
  base::HistogramTester histogram_tester;

  proto::HintNotificationPayload notification;
  notification.set_hint_key("hintkey");
  notification.set_key_representation(proto::KeyRepresentation::HOST);
  notification.set_optimization_type(
      proto::OptimizationType::PERFORMANCE_HINTS);
  optimization_guide::proto::Any* payload = notification.mutable_payload();
  payload->set_type_url("type_url");
  payload->set_value("value");

  optimization_guide::proto::Any payload_to_observer;
  EXPECT_CALL(*observer(), OnNotificationPayload(
                               proto::OptimizationType::PERFORMANCE_HINTS, _))
      .WillOnce(SaveArg<1>(&payload_to_observer));

  manager()->OnNewPushNotification(notification);

  histogram_tester.ExpectUniqueSample(
      "OptimizationGuide.PushNotifications.ReceivedNotificationType",
      proto::OptimizationType::PERFORMANCE_HINTS, 1);
  EXPECT_EQ(payload_to_observer.type_url(), "type_url");
  EXPECT_EQ(payload_to_observer.value(), "value");
  std::vector<TestDelegate::RemoveMultiplePair> removed_entries =
      delegate()->removed_entries();
  EXPECT_EQ(1U, removed_entries.size());
  EXPECT_EQ(proto::KeyRepresentation::HOST, removed_entries[0].first);
  EXPECT_EQ(base::flat_set<std::string>{"hintkey"}, removed_entries[0].second);
}

TEST_F(PushNotificationManagerUnitTest, TestNewNotificationReceivedNoPayload) {
  base::HistogramTester histogram_tester;

  proto::HintNotificationPayload notification;
  notification.set_hint_key("hintkey");
  notification.set_key_representation(proto::KeyRepresentation::HOST);
  notification.set_optimization_type(
      proto::OptimizationType::PERFORMANCE_HINTS);
  EXPECT_CALL(*observer(), OnNotificationPayload(_, _)).Times(0);

  manager()->OnNewPushNotification(notification);

  histogram_tester.ExpectUniqueSample(
      "OptimizationGuide.PushNotifications.ReceivedNotificationType",
      proto::OptimizationType::PERFORMANCE_HINTS, 0);
  EXPECT_EQ(0U, delegate()->removed_entries().size());
}

TEST_F(PushNotificationManagerUnitTest, TestAddRemoveObserver) {
  AnotherMockObserver another_mock_observer;
  manager()->AddObserver(&another_mock_observer);
  EXPECT_TRUE(manager_has_observer(&another_mock_observer));
  manager()->RemoveObserver(&another_mock_observer);
  EXPECT_FALSE(manager_has_observer(&another_mock_observer));
}

}  // namespace optimization_guide