forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrive_notification_manager_unittest.cc
225 lines (177 loc) · 8.45 KB
/
drive_notification_manager_unittest.cc
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include "base/strings/strcat.h"
#include "base/test/test_mock_time_task_runner.h"
#include "components/drive/drive_notification_manager.h"
#include "components/drive/drive_notification_observer.h"
#include "components/invalidation/impl/fake_invalidation_service.h"
#include "components/invalidation/public/topic_invalidation_map.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace drive {
namespace {
const syncer::Topic kDefaultCorpusTopic = "Drive";
struct ShutdownHelper {
template <typename T>
void operator()(T* object) const {
if (object) {
object->Shutdown();
delete object;
}
}
};
class FakeDriveNotificationObserver : public DriveNotificationObserver {
public:
~FakeDriveNotificationObserver() override {}
// DriveNotificationObserver overrides
void OnNotificationReceived(
const std::map<std::string, int64_t>& ids) override {
notification_ids_ = ids;
}
void OnNotificationTimerFired() override {}
void OnPushNotificationEnabled(bool enabled) override {}
const std::map<std::string, int64_t> GetNotificationIds() const {
return notification_ids_;
}
void ClearNotificationIds() { notification_ids_.clear(); }
private:
std::map<std::string, int64_t> notification_ids_;
};
syncer::Topic CreateTeamDriveInvalidationTopic(
const std::string& team_drive_id) {
return base::StrCat({"team-drive-", team_drive_id});
}
} // namespace
class DriveNotificationManagerTest : public testing::Test {
protected:
void SetUp() override {
task_runner_ = base::MakeRefCounted<base::TestMockTimeTaskRunner>(
base::TestMockTimeTaskRunner::Type::kBoundToThread);
fake_invalidation_service_ =
std::make_unique<invalidation::FakeInvalidationService>();
drive_notification_observer_ =
std::make_unique<FakeDriveNotificationObserver>();
// Can't use make_unique with a custom deleter.
drive_notification_manager_.reset(
new DriveNotificationManager(fake_invalidation_service_.get()));
drive_notification_manager_->AddObserver(
drive_notification_observer_.get());
}
void TearDown() override {
drive_notification_manager_->RemoveObserver(
drive_notification_observer_.get());
}
scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
std::unique_ptr<invalidation::FakeInvalidationService>
fake_invalidation_service_;
std::unique_ptr<FakeDriveNotificationObserver> drive_notification_observer_;
std::unique_ptr<DriveNotificationManager, ShutdownHelper>
drive_notification_manager_;
};
TEST_F(DriveNotificationManagerTest, RegisterTeamDrives) {
// By default, we should have registered for default corpus notifications on
// initialization.
auto subscribed_topics = fake_invalidation_service_->invalidator_registrar()
.GetAllSubscribedTopics();
// TODO(crbug.com/1029698): replace syncer::Topics with syncer::TopicSet once
// |is_public| become the part of dedicated syncer::Topic struct. This should
// simplify this test.
syncer::Topics expected_topics;
expected_topics.emplace(kDefaultCorpusTopic,
syncer::TopicMetadata{/*is_public=*/false});
EXPECT_EQ(expected_topics, subscribed_topics);
const std::string team_drive_id_1 = "td_id_1";
const auto team_drive_1_topic =
CreateTeamDriveInvalidationTopic(team_drive_id_1);
// Add the team drive
drive_notification_manager_->UpdateTeamDriveIds({team_drive_id_1}, {});
subscribed_topics = fake_invalidation_service_->invalidator_registrar()
.GetAllSubscribedTopics();
expected_topics.emplace(team_drive_1_topic,
syncer::TopicMetadata{/*is_public=*/true});
EXPECT_EQ(expected_topics, subscribed_topics);
// Remove the team drive.
drive_notification_manager_->UpdateTeamDriveIds({}, {team_drive_id_1});
subscribed_topics = fake_invalidation_service_->invalidator_registrar()
.GetAllSubscribedTopics();
expected_topics.erase(team_drive_1_topic);
EXPECT_EQ(expected_topics, subscribed_topics);
const std::string team_drive_id_2 = "td_id_2";
const auto team_drive_2_topic =
CreateTeamDriveInvalidationTopic(team_drive_id_2);
// Add two team drives.
drive_notification_manager_->UpdateTeamDriveIds(
{team_drive_id_1, team_drive_id_2}, {});
subscribed_topics = fake_invalidation_service_->invalidator_registrar()
.GetAllSubscribedTopics();
expected_topics.emplace(team_drive_1_topic,
syncer::TopicMetadata{/*is_public=*/true});
expected_topics.emplace(team_drive_2_topic,
syncer::TopicMetadata{/*is_public=*/true});
EXPECT_EQ(expected_topics, subscribed_topics);
// Remove the first team drive.
drive_notification_manager_->UpdateTeamDriveIds({}, {team_drive_id_1});
subscribed_topics = fake_invalidation_service_->invalidator_registrar()
.GetAllSubscribedTopics();
expected_topics.erase(team_drive_1_topic);
EXPECT_EQ(expected_topics, subscribed_topics);
// Remove a team drive that doesn't exists with no changes.
const std::string team_drive_id_3 = "td_id_3";
drive_notification_manager_->UpdateTeamDriveIds({}, {team_drive_id_3});
subscribed_topics = fake_invalidation_service_->invalidator_registrar()
.GetAllSubscribedTopics();
EXPECT_EQ(expected_topics, subscribed_topics);
}
TEST_F(DriveNotificationManagerTest, TestBatchInvalidation) {
// By default we'll be registered for the default change notification.
// Emitting an invalidation should not call our observer until the timer
// expires.
fake_invalidation_service_->EmitInvalidationForTest(
syncer::Invalidation::InitUnknownVersion(kDefaultCorpusTopic));
EXPECT_TRUE(drive_notification_observer_->GetNotificationIds().empty());
task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(30));
// Default corpus is has the id "" when sent to observers.
std::map<std::string, int64_t> expected_ids = {{"", -1}};
EXPECT_EQ(expected_ids, drive_notification_observer_->GetNotificationIds());
drive_notification_observer_->ClearNotificationIds();
// Register a team drive for notifications
const std::string team_drive_id_1 = "td_id_1";
const auto team_drive_1_topic =
CreateTeamDriveInvalidationTopic(team_drive_id_1);
drive_notification_manager_->UpdateTeamDriveIds({team_drive_id_1}, {});
// Emit invalidation for default corpus, should not emit a team drive
// invalidation.
fake_invalidation_service_->EmitInvalidationForTest(
syncer::Invalidation::Init(kDefaultCorpusTopic, 1, ""));
EXPECT_TRUE(drive_notification_observer_->GetNotificationIds().empty());
task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(30));
// Default corpus is has the id "" when sent to observers.
expected_ids = {{"", 2}};
EXPECT_EQ(expected_ids, drive_notification_observer_->GetNotificationIds());
drive_notification_observer_->ClearNotificationIds();
// Emit team drive invalidation
fake_invalidation_service_->EmitInvalidationForTest(
syncer::Invalidation::Init(team_drive_1_topic, 2, ""));
EXPECT_TRUE(drive_notification_observer_->GetNotificationIds().empty());
task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(30));
expected_ids = {{team_drive_id_1, 2}};
EXPECT_EQ(expected_ids, drive_notification_observer_->GetNotificationIds());
drive_notification_observer_->ClearNotificationIds();
// Emit both default corpus and team drive.
fake_invalidation_service_->EmitInvalidationForTest(
syncer::Invalidation::Init(kDefaultCorpusTopic, 1, ""));
fake_invalidation_service_->EmitInvalidationForTest(
syncer::Invalidation::Init(team_drive_1_topic, 2, ""));
// Emit with an earlier version. This should be ignored.
fake_invalidation_service_->EmitInvalidationForTest(
syncer::Invalidation::Init(kDefaultCorpusTopic, 0, ""));
// Emit without a version. This should be ignored too.
fake_invalidation_service_->EmitInvalidationForTest(
syncer::Invalidation::InitUnknownVersion(kDefaultCorpusTopic));
EXPECT_TRUE(drive_notification_observer_->GetNotificationIds().empty());
task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(30));
expected_ids = {{"", 2}, {team_drive_id_1, 2}};
EXPECT_EQ(expected_ids, drive_notification_observer_->GetNotificationIds());
}
} // namespace drive