File: invalidator_registrar_with_memory_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 (374 lines) | stat: -rw-r--r-- 15,330 bytes parent folder | download | duplicates (5)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright 2019 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/invalidation/impl/invalidator_registrar_with_memory.h"

#include <memory>

#include "base/json/json_reader.h"
#include "base/test/gmock_expected_support.h"
#include "base/test/scoped_feature_list.h"
#include "components/invalidation/impl/fake_invalidation_handler.h"
#include "components/invalidation/public/invalidation.h"
#include "components/invalidation/public/invalidation_util.h"
#include "components/invalidation/public/invalidator_state.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::IsEmpty;
using testing::Pair;
using testing::UnorderedElementsAre;

namespace invalidation {

namespace {

template <class... Inv>
void Dispatch(InvalidatorRegistrarWithMemory& registrar, Inv... inv) {
  (registrar.DispatchInvalidationToHandlers(inv), ...);
}

template <class... Topic>
void DispatchSuccessfullySubscribed(InvalidatorRegistrarWithMemory& registrar,
                                    Topic... topics) {
  (registrar.DispatchSuccessfullySubscribedToHandlers(topics), ...);
}

template <class... Inv>
std::map<Topic, Invalidation> ExpectedInvalidations(Inv... inv) {
  std::map<Topic, Invalidation> expected_invalidations;
  (expected_invalidations.emplace(inv.topic(), inv), ...);
  return expected_invalidations;
}

constexpr char kTopicsToHandler[] = "invalidation.per_sender_topics_to_handler";

class InvalidatorRegistrarWithMemoryTest : public testing::Test {
 protected:
  const Topic kTopicName1 = "topic_1";
  const Topic kTopicName2 = "topic_2";
  const Topic kTopicName3 = "topic_3";
  const Topic kTopicName4 = "topic_4";
  const std::pair<Topic, TopicMetadata> kTopic1 = {
      kTopicName1, TopicMetadata{.is_public = false}};
  const std::pair<Topic, TopicMetadata> kTopic2 = {
      kTopicName2, TopicMetadata{.is_public = false}};
  const std::pair<Topic, TopicMetadata> kTopic3 = {
      kTopicName3, TopicMetadata{.is_public = false}};
  const std::pair<Topic, TopicMetadata> kTopic4 = {
      kTopicName4, TopicMetadata{.is_public = false}};
  const Invalidation kInv1 = Invalidation(kTopicName1, 1, "1");
  const Invalidation kInv2 = Invalidation(kTopicName2, 2, "2");
  const Invalidation kInv3 = Invalidation(kTopicName3, 3, "3");
  const Invalidation kInv4 = Invalidation(kTopicName4, 4, "4");
};

// Initialize the invalidator, register a handler, register some topics for that
// handler, and then unregister the handler, dispatching invalidations in
// between. The handler should only see invalidations when it's registered and
// its topics are registered.
TEST_F(InvalidatorRegistrarWithMemoryTest, Basic) {
  TestingPrefServiceSimple pref_service;
  InvalidatorRegistrarWithMemory::RegisterProfilePrefs(pref_service.registry());

  auto invalidator = std::make_unique<InvalidatorRegistrarWithMemory>(
      &pref_service, "sender_id");

  FakeInvalidationHandler handler("owner");
  invalidator->AddObserver(&handler);
  EXPECT_TRUE(invalidator->HasObserver(&handler));

  // Should be ignored since no topics are registered to |handler|.
  DispatchSuccessfullySubscribed(*invalidator, kTopicName1, kTopicName2,
                                 kTopicName3);
  EXPECT_THAT(handler.GetSuccessfullySubscribed(), IsEmpty());

  Dispatch(*invalidator, kInv1, kInv2, kInv3);
  EXPECT_EQ(0, handler.GetInvalidationCount());

  EXPECT_TRUE(
      invalidator->UpdateRegisteredTopics(&handler, {kTopic1, kTopic2}));

  invalidator->UpdateInvalidatorState(InvalidatorState::kEnabled);
  EXPECT_EQ(InvalidatorState::kEnabled, handler.GetInvalidatorState());

  DispatchSuccessfullySubscribed(*invalidator, kTopicName1, kTopicName2,
                                 kTopicName3);
  EXPECT_THAT(handler.GetSuccessfullySubscribed(),
              UnorderedElementsAre(kTopicName1, kTopicName2));

  Dispatch(*invalidator, kInv1, kInv2, kInv3);
  EXPECT_EQ(2, handler.GetInvalidationCount());
  EXPECT_EQ(ExpectedInvalidations(kInv1, kInv2),
            handler.GetReceivedInvalidations());
  handler.Clear();

  // Remove kTopic1, add kTopic3.
  EXPECT_TRUE(
      invalidator->UpdateRegisteredTopics(&handler, {kTopic2, kTopic3}));

  // Removed topic should not be notified, newly-added ones should.
  DispatchSuccessfullySubscribed(*invalidator, kTopicName1, kTopicName2,
                                 kTopicName3);
  EXPECT_THAT(handler.GetSuccessfullySubscribed(),
              UnorderedElementsAre(kTopicName2, kTopicName3));

  Dispatch(*invalidator, kInv1, kInv2, kInv3);
  EXPECT_EQ(2, handler.GetInvalidationCount());
  EXPECT_EQ(ExpectedInvalidations(kInv2, kInv3),
            handler.GetReceivedInvalidations());
  handler.Clear();

  invalidator->UpdateInvalidatorState(InvalidatorState::kDisabled);
  EXPECT_EQ(InvalidatorState::kDisabled, handler.GetInvalidatorState());

  invalidator->RemoveObserver(&handler);
  EXPECT_FALSE(invalidator->HasObserver(&handler));

  // Should be ignored since |handler| isn't registered anymore.
  DispatchSuccessfullySubscribed(*invalidator, kTopicName1, kTopicName2,
                                 kTopicName3);
  EXPECT_THAT(handler.GetSuccessfullySubscribed(), IsEmpty());

  Dispatch(*invalidator, kInv1, kInv2, kInv3);
  EXPECT_EQ(0, handler.GetInvalidationCount());
}

// Register handlers and some topics for those handlers, register a handler with
// no topics, and register a handler with some topics but unregister it. Then,
// dispatch some invalidations. Handlers that are not registered should not get
// invalidations, and the ones that have registered topics should receive
// invalidations for those topics.
TEST_F(InvalidatorRegistrarWithMemoryTest, MultipleHandlers) {
  TestingPrefServiceSimple pref_service;
  InvalidatorRegistrarWithMemory::RegisterProfilePrefs(pref_service.registry());

  auto invalidator = std::make_unique<InvalidatorRegistrarWithMemory>(
      &pref_service, "sender_id");

  FakeInvalidationHandler handler1("owner_1");
  FakeInvalidationHandler handler2("owner_2");
  FakeInvalidationHandler handler3("owner_3");
  FakeInvalidationHandler handler4("owner_4");

  invalidator->AddObserver(&handler1);
  invalidator->AddObserver(&handler2);
  invalidator->AddObserver(&handler3);
  invalidator->AddObserver(&handler4);

  EXPECT_TRUE(
      invalidator->UpdateRegisteredTopics(&handler1, {kTopic1, kTopic2}));
  EXPECT_TRUE(invalidator->UpdateRegisteredTopics(&handler2, {kTopic3}));
  // Don't register any IDs for handler3.
  EXPECT_TRUE(invalidator->UpdateRegisteredTopics(&handler4, {kTopic4}));

  invalidator->RemoveObserver(&handler4);

  invalidator->UpdateInvalidatorState(InvalidatorState::kEnabled);
  EXPECT_EQ(InvalidatorState::kEnabled, handler1.GetInvalidatorState());
  EXPECT_EQ(InvalidatorState::kEnabled, handler2.GetInvalidatorState());
  EXPECT_EQ(InvalidatorState::kEnabled, handler3.GetInvalidatorState());
  EXPECT_EQ(InvalidatorState::kDisabled, handler4.GetInvalidatorState());

  Dispatch(*invalidator, kInv1, kInv2, kInv3, kInv4);

  EXPECT_EQ(2, handler1.GetInvalidationCount());
  EXPECT_EQ(ExpectedInvalidations(kInv1, kInv2),
            handler1.GetReceivedInvalidations());

  std::map<Topic, Invalidation> expected_invalidations2;
  expected_invalidations2.emplace(kInv3.topic(), kInv3);

  EXPECT_EQ(1, handler2.GetInvalidationCount());
  EXPECT_EQ(ExpectedInvalidations(kInv3), handler2.GetReceivedInvalidations());

  EXPECT_EQ(0, handler3.GetInvalidationCount());
  EXPECT_EQ(0, handler4.GetInvalidationCount());

  invalidator->UpdateInvalidatorState(InvalidatorState::kDisabled);
  EXPECT_EQ(InvalidatorState::kDisabled, handler1.GetInvalidatorState());
  EXPECT_EQ(InvalidatorState::kDisabled, handler2.GetInvalidatorState());
  EXPECT_EQ(InvalidatorState::kDisabled, handler3.GetInvalidatorState());
  EXPECT_EQ(InvalidatorState::kDisabled, handler4.GetInvalidatorState());

  invalidator->RemoveObserver(&handler3);
  invalidator->RemoveObserver(&handler2);
  invalidator->RemoveObserver(&handler1);
}

// Multiple registrations by different handlers on the same topic should
// return false.
TEST_F(InvalidatorRegistrarWithMemoryTest, MultipleRegistrations) {
  TestingPrefServiceSimple pref_service;
  InvalidatorRegistrarWithMemory::RegisterProfilePrefs(pref_service.registry());

  auto invalidator = std::make_unique<InvalidatorRegistrarWithMemory>(
      &pref_service, "sender_id");

  FakeInvalidationHandler handler1("owner1");
  FakeInvalidationHandler handler2("owner2");

  invalidator->AddObserver(&handler1);
  invalidator->AddObserver(&handler2);

  // Registering both handlers for the same topic. First call should succeed,
  // second should fail.
  EXPECT_TRUE(invalidator->UpdateRegisteredTopics(&handler1, {kTopic1}));
  EXPECT_FALSE(invalidator->UpdateRegisteredTopics(&handler2, {kTopic1}));

  // |handler1| should still own subscription to the topic and deregistration
  // of its topics should update subscriptions.
  EXPECT_TRUE(invalidator->UpdateRegisteredTopics(&handler1, /*topics=*/{}));
  EXPECT_TRUE(invalidator->GetAllSubscribedTopics().empty());

  invalidator->RemoveObserver(&handler2);
  invalidator->RemoveObserver(&handler1);
}

// Make sure that passing an empty set to UpdateRegisteredTopics clears the
// corresponding entries for the handler.
TEST_F(InvalidatorRegistrarWithMemoryTest, EmptySetUnregisters) {
  TestingPrefServiceSimple pref_service;
  InvalidatorRegistrarWithMemory::RegisterProfilePrefs(pref_service.registry());

  auto invalidator = std::make_unique<InvalidatorRegistrarWithMemory>(
      &pref_service, "sender_id");

  FakeInvalidationHandler handler1("owner_1");

  // Control observer.
  FakeInvalidationHandler handler2("owner_2");

  invalidator->AddObserver(&handler1);
  invalidator->AddObserver(&handler2);

  EXPECT_TRUE(
      invalidator->UpdateRegisteredTopics(&handler1, {kTopic1, kTopic2}));
  EXPECT_TRUE(invalidator->UpdateRegisteredTopics(&handler2, {kTopic3}));

  // Unregister the topics for the first observer. It should not receive any
  // further invalidations.
  EXPECT_TRUE(invalidator->UpdateRegisteredTopics(&handler1, {}));

  invalidator->UpdateInvalidatorState(InvalidatorState::kEnabled);
  EXPECT_EQ(InvalidatorState::kEnabled, handler1.GetInvalidatorState());
  EXPECT_EQ(InvalidatorState::kEnabled, handler2.GetInvalidatorState());

  {
    Dispatch(*invalidator, kInv1, kInv2, kInv3);

    EXPECT_EQ(0, handler1.GetInvalidationCount());
    EXPECT_EQ(1, handler2.GetInvalidationCount());
  }

  invalidator->UpdateInvalidatorState(InvalidatorState::kDisabled);
  EXPECT_EQ(InvalidatorState::kDisabled, handler1.GetInvalidatorState());
  EXPECT_EQ(InvalidatorState::kDisabled, handler2.GetInvalidatorState());

  invalidator->RemoveObserver(&handler2);
  invalidator->RemoveObserver(&handler1);
}

TEST_F(InvalidatorRegistrarWithMemoryTest, RestoresInterestingTopics) {
  TestingPrefServiceSimple pref_service;
  InvalidatorRegistrarWithMemory::RegisterProfilePrefs(pref_service.registry());

  // Set up some previously-registered topics in the pref.
  constexpr char kStoredTopicsJson[] =
      R"({"sender_id": {
            "topic_1": {"handler": "handler_1", "is_public": true},
            "topic_2": {"handler": "handler_2", "is_public": true},
            "topic_3": "handler_3",
            "topic_4_1": {"handler": "handler_4", "is_public": false},
            "topic_4_2": {"handler": "handler_4", "is_public": false},
            "topic_4_3": {"handler": "handler_4", "is_public": false}
      }})";

  ASSERT_OK_AND_ASSIGN(
      auto stored_topics,
      base::JSONReader::ReadAndReturnValueWithError(kStoredTopicsJson));
  pref_service.Set(kTopicsToHandler, std::move(stored_topics));

  // Create an invalidator and make sure it correctly restored state from the
  // pref.
  auto invalidator = std::make_unique<InvalidatorRegistrarWithMemory>(
      &pref_service, "sender_id");

  std::map<std::string, TopicMetadata> expected_subscribed_topics{
      {"topic_1", TopicMetadata{.is_public = true}},
      {"topic_2", TopicMetadata{.is_public = true}},
      {"topic_3", TopicMetadata{.is_public = false}},
      {"topic_4_1", TopicMetadata{.is_public = false}},
      {"topic_4_2", TopicMetadata{.is_public = false}},
      {"topic_4_3", TopicMetadata{.is_public = false}},
  };

  EXPECT_EQ(expected_subscribed_topics, invalidator->GetAllSubscribedTopics());
}

// This test verifies that topics are not unsubscribed after browser restart
// even if it's not registered using UpdateRegisteredTopics() method. This is
// useful for Sync invalidations to prevent unsubscribing from some data types
// which require more time to initialize and which are added later in following
// UpdateRegisteredTopics() calls.
//
// TODO(crbug.com/40674001): make the unsubscription behaviour consistent
// regardless of browser restart in between.
TEST_F(InvalidatorRegistrarWithMemoryTest,
       ShouldKeepSubscriptionsAfterRestart) {
  const std::pair kTopic1 = {std::string("topic_1"),
                             TopicMetadata{.is_public = true}};
  const std::pair kTopic2 = {std::string("topic_2"),
                             TopicMetadata{.is_public = true}};

  TestingPrefServiceSimple pref_service;
  InvalidatorRegistrarWithMemory::RegisterProfilePrefs(pref_service.registry());

  // Set up some previously-registered topics in the pref.
  constexpr char kStoredTopicsJson[] =
      R"({"sender_id": {
            "topic_1": {"handler": "handler", "is_public": true},
            "topic_2": {"handler": "handler", "is_public": true}
      }})";

  ASSERT_OK_AND_ASSIGN(
      auto stored_topics,
      base::JSONReader::ReadAndReturnValueWithError(kStoredTopicsJson));
  pref_service.Set(kTopicsToHandler, std::move(stored_topics));

  auto invalidator = std::make_unique<InvalidatorRegistrarWithMemory>(
      &pref_service, "sender_id");
  FakeInvalidationHandler handler("handler");
  invalidator->AddObserver(&handler);

  // Verify that all topics are successfully subscribed but not registered by
  // the |handler|.
  ASSERT_THAT(invalidator->GetRegisteredTopics(&handler), IsEmpty());
  ASSERT_THAT(invalidator->GetAllSubscribedTopics(),
              UnorderedElementsAre(kTopic1, kTopic2));

  // Register fo only one topic, but the previous subscriptions to other topics
  // should be kept.
  ASSERT_TRUE(invalidator->UpdateRegisteredTopics(&handler, {kTopic1}));
  EXPECT_THAT(invalidator->GetRegisteredTopics(&handler),
              UnorderedElementsAre(kTopic1));
  EXPECT_THAT(invalidator->GetAllSubscribedTopics(),
              UnorderedElementsAre(kTopic1, kTopic2));

  // To unsubscribe from the topics which were added before browser restart, the
  // handler needs to explicitly register this topic, then unregister again.
  ASSERT_TRUE(
      invalidator->UpdateRegisteredTopics(&handler, {kTopic1, kTopic2}));
  ASSERT_TRUE(invalidator->UpdateRegisteredTopics(&handler, {kTopic1}));
  EXPECT_THAT(invalidator->GetRegisteredTopics(&handler),
              UnorderedElementsAre(kTopic1));
  EXPECT_THAT(invalidator->GetAllSubscribedTopics(),
              UnorderedElementsAre(kTopic1));

  invalidator->RemoveObserver(&handler);
}

}  // namespace

}  // namespace invalidation