File: data_sharing_service_android_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (285 lines) | stat: -rw-r--r-- 10,591 bytes parent folder | download | duplicates (3)
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
// Copyright 2024 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/data_sharing/internal/android/data_sharing_service_android.h"

#include "base/android/jni_android.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/callback_forward.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/task_environment.h"
#include "components/data_sharing/internal/data_sharing_service_impl.h"
#include "components/data_sharing/public/data_sharing_service.h"
#include "components/data_sharing/public/group_data.h"
#include "components/data_sharing/test_support/fake_data_sharing_sdk_delegate.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/sync/test/data_type_store_test_util.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/status/status.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "components/data_sharing/internal/test_jni_headers/TestServiceObserver_jni.h"

namespace data_sharing {

using ::base::android::AttachCurrentThread;
using ::base::android::ScopedJavaLocalRef;

// Java observer for testing, counter part of TestServiceObserver.
// On each observation increments a counter and runs the callback.
class TestJavaObserver {
 public:
  TestJavaObserver(DataSharingService* service, base::OnceClosure callback)
      : service_(DataSharingService::GetJavaObject(service)),
        java_obj_(Java_TestServiceObserver_createAndAdd(
            AttachCurrentThread(),
            service_,
            reinterpret_cast<long>(this))),
        callback_(std::move(callback)) {}
  ~TestJavaObserver() {
    Java_TestServiceObserver_destroy(AttachCurrentThread(), java_obj_,
                                     service_);
  }

  int GetGroupChangeCount() {
    return Java_TestServiceObserver_getOnGroupChangeCount(AttachCurrentThread(),
                                                          java_obj_);
  }
  int GetGroupAddedCount() {
    return Java_TestServiceObserver_getOnGroupAddedCount(AttachCurrentThread(),
                                                         java_obj_);
  }
  int GetGroupRemovedCount() {
    return Java_TestServiceObserver_getOnGroupRemovedCount(
        AttachCurrentThread(), java_obj_);
  }

  void ResetCallback(base::OnceClosure callback) {
    callback_ = std::move(callback);
  }

  void OnObserverNotify() { std::move(callback_).Run(); }

 private:
  ScopedJavaLocalRef<jobject> service_;
  ScopedJavaLocalRef<jobject> java_obj_;

  base::OnceClosure callback_;
};

// Implements TestServiceObserver.onObserverNotify static method.
void JNI_TestServiceObserver_OnObserverNotify(JNIEnv* env, jlong observer_ptr) {
  reinterpret_cast<TestJavaObserver*>(observer_ptr)->OnObserverNotify();
}

namespace {

sync_pb::CollaborationGroupSpecifics MakeCollaborationGroupSpecifics(
    const GroupId& id) {
  sync_pb::CollaborationGroupSpecifics result;
  result.set_collaboration_id(id.value());

  base::Time now = base::Time::Now();
  result.set_changed_at_timestamp_millis_since_unix_epoch(
      now.InMillisecondsSinceUnixEpoch());
  result.set_consistency_token(
      base::NumberToString(now.InMillisecondsSinceUnixEpoch()));
  return result;
}

syncer::EntityData EntityDataFromSpecifics(
    const sync_pb::CollaborationGroupSpecifics& specifics) {
  syncer::EntityData entity_data;
  *entity_data.specifics.mutable_collaboration_group() = specifics;
  entity_data.name = specifics.collaboration_id();
  return entity_data;
}

std::unique_ptr<syncer::EntityChange> EntityChangeAddFromSpecifics(
    const sync_pb::CollaborationGroupSpecifics& specifics) {
  return syncer::EntityChange::CreateAdd(specifics.collaboration_id(),
                                         EntityDataFromSpecifics(specifics));
}

std::unique_ptr<syncer::EntityChange> EntityChangeUpdateFromSpecifics(
    const sync_pb::CollaborationGroupSpecifics& specifics) {
  return syncer::EntityChange::CreateUpdate(specifics.collaboration_id(),
                                            EntityDataFromSpecifics(specifics));
}

std::unique_ptr<syncer::EntityChange> EntityChangeDeleteFromSpecifics(
    const sync_pb::CollaborationGroupSpecifics& specifics) {
  return syncer::EntityChange::CreateDelete(specifics.collaboration_id(),
                                            syncer::EntityData());
}

}  // namespace

class DataSharingServiceAndroidTest : public testing::Test {
 public:
  DataSharingServiceAndroidTest() = default;

  ~DataSharingServiceAndroidTest() override = default;

  void SetUp() override {
    Test::SetUp();
    EXPECT_TRUE(profile_dir_.CreateUniqueTempDir());

    scoped_refptr<network::SharedURLLoaderFactory> test_url_loader_factory =
        base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
            &test_url_loader_factory_);

    std::unique_ptr<FakeDataSharingSDKDelegate> sdk_delegate =
        std::make_unique<FakeDataSharingSDKDelegate>();
    not_owned_sdk_delegate_ = sdk_delegate.get();

    data_sharing_service_ = std::make_unique<DataSharingServiceImpl>(
        profile_dir_.GetPath(), std::move(test_url_loader_factory),
        identity_test_env_.identity_manager(),
        syncer::DataTypeStoreTestUtil::FactoryForInMemoryStoreForTest(),
        version_info::Channel::UNKNOWN, std::move(sdk_delegate),
        /*ui_delegate=*/nullptr);
    data_sharing_service_android_ = std::make_unique<DataSharingServiceAndroid>(
        data_sharing_service_.get());
  }

  void TearDown() override {
    data_sharing_service_android_.reset();
    not_owned_sdk_delegate_ = nullptr;
    data_sharing_service_.reset();
  }

  // Creates group and returns ID.
  // Mimics initial sync for collaboration group datatype, this should trigger
  // OnGroupAdded() notification.
  GroupId CreateGroup() {
    const std::string display_name = "display_name";
    const GroupId group_id =
        not_owned_sdk_delegate_->AddGroupAndReturnId(display_name);

    auto* collaboration_group_bridge =
        data_sharing_service_->GetCollaborationGroupSyncBridgeForTesting();

    syncer::EntityChangeList entity_changes;
    entity_changes.push_back(EntityChangeAddFromSpecifics(
        MakeCollaborationGroupSpecifics(group_id)));

    collaboration_group_bridge->MergeFullSyncData(
        collaboration_group_bridge->CreateMetadataChangeList(),
        std::move(entity_changes));
    return group_id;
  }

  // Removes the group with `group_id`, which would trigger the OnGroupRemoved()
  // notification.
  void RemoveGroup(const GroupId& group_id) {
    auto* collaboration_group_bridge =
        data_sharing_service_->GetCollaborationGroupSyncBridgeForTesting();
    not_owned_sdk_delegate_->RemoveGroup(group_id);
    syncer::EntityChangeList entity_changes;
    entity_changes.push_back(EntityChangeDeleteFromSpecifics(
        MakeCollaborationGroupSpecifics(group_id)));

    collaboration_group_bridge->ApplyIncrementalSyncChanges(
        collaboration_group_bridge->CreateMetadataChangeList(),
        std::move(entity_changes));
  }

  // Updates the group with `group_id` with a different name, which wuld trigger
  // the OnGroupUpdated() notification.
  void UpdateGroup(const GroupId& group_id) {
    const std::string new_display_name = "new_display_name";
    auto* collaboration_group_bridge =
        data_sharing_service_->GetCollaborationGroupSyncBridgeForTesting();
    not_owned_sdk_delegate_->UpdateGroup(group_id, new_display_name);
    syncer::EntityChangeList entity_changes;
    entity_changes.push_back(EntityChangeUpdateFromSpecifics(
        MakeCollaborationGroupSpecifics(group_id)));

    collaboration_group_bridge->ApplyIncrementalSyncChanges(
        collaboration_group_bridge->CreateMetadataChangeList(),
        std::move(entity_changes));
  }

 protected:
  base::test::TaskEnvironment task_environment_;
  base::ScopedTempDir profile_dir_;
  signin::IdentityTestEnvironment identity_test_env_;
  network::TestURLLoaderFactory test_url_loader_factory_;
  std::unique_ptr<DataSharingServiceImpl> data_sharing_service_;
  std::unique_ptr<DataSharingServiceAndroid> data_sharing_service_android_;
  raw_ptr<FakeDataSharingSDKDelegate> not_owned_sdk_delegate_;
};

TEST_F(DataSharingServiceAndroidTest, GroupAddedObservation) {
  base::RunLoop run_loop;
  TestJavaObserver observer(data_sharing_service_.get(),
                            run_loop.QuitClosure());

  EXPECT_EQ(observer.GetGroupChangeCount(), 0);
  EXPECT_EQ(observer.GetGroupAddedCount(), 0);
  EXPECT_EQ(observer.GetGroupRemovedCount(), 0);

  CreateGroup();

  run_loop.Run();

  EXPECT_EQ(observer.GetGroupChangeCount(), 0);
  EXPECT_EQ(observer.GetGroupAddedCount(), 1);
  EXPECT_EQ(observer.GetGroupRemovedCount(), 0);
}

TEST_F(DataSharingServiceAndroidTest, GroupRemovedObservation) {
  base::RunLoop run_loop;
  TestJavaObserver observer(data_sharing_service_.get(),
                            run_loop.QuitClosure());
  EXPECT_EQ(observer.GetGroupChangeCount(), 0);
  EXPECT_EQ(observer.GetGroupAddedCount(), 0);
  EXPECT_EQ(observer.GetGroupRemovedCount(), 0);

  GroupId group_id = CreateGroup();

  run_loop.Run();
  EXPECT_EQ(observer.GetGroupChangeCount(), 0);
  EXPECT_EQ(observer.GetGroupAddedCount(), 1);
  EXPECT_EQ(observer.GetGroupRemovedCount(), 0);

  base::RunLoop wait_for_remove;
  observer.ResetCallback(wait_for_remove.QuitClosure());

  RemoveGroup(group_id);

  wait_for_remove.Run();
  EXPECT_EQ(observer.GetGroupChangeCount(), 0);
  EXPECT_EQ(observer.GetGroupAddedCount(), 1);
  EXPECT_EQ(observer.GetGroupRemovedCount(), 1);
}

TEST_F(DataSharingServiceAndroidTest, GroupChangeObservation) {
  base::RunLoop run_loop;
  TestJavaObserver observer(data_sharing_service_.get(),
                            run_loop.QuitClosure());
  EXPECT_EQ(observer.GetGroupChangeCount(), 0);
  EXPECT_EQ(observer.GetGroupAddedCount(), 0);
  EXPECT_EQ(observer.GetGroupRemovedCount(), 0);

  GroupId group_id = CreateGroup();

  run_loop.Run();
  EXPECT_EQ(observer.GetGroupChangeCount(), 0);
  EXPECT_EQ(observer.GetGroupAddedCount(), 1);
  EXPECT_EQ(observer.GetGroupRemovedCount(), 0);

  base::RunLoop wait_for_update;
  observer.ResetCallback(wait_for_update.QuitClosure());

  UpdateGroup(group_id);

  wait_for_update.Run();
}

}  // namespace data_sharing