blob: f0302dbdf88fcea013ff77a6a1350d0445390add [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2019 The Chromium Authors
Himanshu Jaju115ec0b2019-10-30 11:59:392// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Hira Mahmood0f3c35a82024-07-31 15:43:265#include "components/sharing_message/sharing_utils.h"
Himanshu Jaju115ec0b2019-10-30 11:59:396
Alex Chau2badb802019-11-15 13:47:037#include "base/test/scoped_feature_list.h"
Hira Mahmood0f3c35a82024-07-31 15:43:268#include "components/sharing_message/fake_device_info.h"
Hira Mahmood538940952024-06-12 08:31:159#include "components/sharing_message/features.h"
Hira Mahmoode16b75f02024-07-18 17:46:4310#include "components/sharing_message/proto/sharing_message.pb.h"
Hira Mahmood0f3c35a82024-07-31 15:43:2611#include "components/sharing_message/sharing_constants.h"
Victor Hugo Vianna Silvae92e3002021-07-26 14:38:5012#include "components/sync/protocol/device_info_specifics.pb.h"
13#include "components/sync/protocol/sync_enums.pb.h"
Victor Hugo Vianna Silvae5009002022-08-31 13:36:3914#include "components/sync/test/test_sync_service.h"
Himanshu Jaju115ec0b2019-10-30 11:59:3915#include "components/sync_device_info/device_info.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18namespace {
Alex Chau2badb802019-11-15 13:47:0319
Alex Chau328eeb932020-04-22 11:06:3520const char kDeviceGuid[] = "test_guid";
21const char kDeviceName[] = "test_name";
Alex Chau328eeb932020-04-22 11:06:3522const char kSenderIdFCMToken[] = "test_sender_id_fcm_token";
23const char kSenderIdP256dh[] = "test_sender_id_p256_dh";
24const char kSenderIdAuthSecret[] = "test_sender_id_auth_secret";
25
Alex Chau2badb802019-11-15 13:47:0326class SharingUtilsTest : public testing::Test {
27 public:
28 SharingUtilsTest() = default;
29
30 protected:
Alex Chau2badb802019-11-15 13:47:0331 syncer::TestSyncService test_sync_service_;
32};
33
Himanshu Jaju115ec0b2019-10-30 11:59:3934} // namespace
35
Naomi Musgraved9fc0bf2020-04-22 18:46:2636TEST_F(SharingUtilsTest, SyncEnabled_FullySynced) {
Naomi Musgraved9fc0bf2020-04-22 18:46:2637 // PREFERENCES is actively synced.
Jood Hajeer6fef74c9f2022-10-12 12:41:0938 test_sync_service_.GetUserSettings()->SetSelectedTypes(
39 /*sync_everything=*/false,
Maksim Moskvitinc306ea8e2023-05-12 18:05:2740 /*types=*/{syncer::UserSelectableType::kPreferences});
Alex Chau2badb802019-11-15 13:47:0341
42 EXPECT_TRUE(IsSyncEnabledForSharing(&test_sync_service_));
43 EXPECT_FALSE(IsSyncDisabledForSharing(&test_sync_service_));
44}
45
Naomi Musgraved9fc0bf2020-04-22 18:46:2646TEST_F(SharingUtilsTest, SyncDisabled_FullySynced_MissingDataTypes) {
Naomi Musgraved9fc0bf2020-04-22 18:46:2647 // Missing PREFERENCES.
Jood Hajeer6fef74c9f2022-10-12 12:41:0948 test_sync_service_.GetUserSettings()->SetSelectedTypes(
49 /*sync_everything=*/false,
50 /*types=*/syncer::UserSelectableTypeSet());
Jens Mueller92781872023-01-19 17:31:0651 // Not able to sync SHARING_MESSAGE.
52 test_sync_service_.SetFailedDataTypes({syncer::SHARING_MESSAGE});
Naomi Musgraved9fc0bf2020-04-22 18:46:2653
54 EXPECT_FALSE(IsSyncEnabledForSharing(&test_sync_service_));
55 EXPECT_TRUE(IsSyncDisabledForSharing(&test_sync_service_));
56}
57
58TEST_F(SharingUtilsTest, SyncEnabled_SigninOnly) {
Naomi Musgraved9fc0bf2020-04-22 18:46:2659 // SHARING_MESSAGE is actively synced.
Alex Chau2badb802019-11-15 13:47:0360 EXPECT_TRUE(IsSyncEnabledForSharing(&test_sync_service_));
61 EXPECT_FALSE(IsSyncDisabledForSharing(&test_sync_service_));
62}
63
64TEST_F(SharingUtilsTest, SyncDisabled_SigninOnly_MissingDataTypes) {
Naomi Musgraved9fc0bf2020-04-22 18:46:2665 // Missing SHARING_MESSAGE.
Jood Hajeer6fef74c9f2022-10-12 12:41:0966 test_sync_service_.GetUserSettings()->SetSelectedTypes(
67 /*sync_everything=*/false,
68 /*types=*/syncer::UserSelectableTypeSet());
69 test_sync_service_.SetFailedDataTypes({syncer::SHARING_MESSAGE});
Alex Chau2badb802019-11-15 13:47:0370
71 EXPECT_FALSE(IsSyncEnabledForSharing(&test_sync_service_));
72 EXPECT_TRUE(IsSyncDisabledForSharing(&test_sync_service_));
73}
74
75TEST_F(SharingUtilsTest, SyncDisabled_Disabled) {
Victor Hugo Vianna Silva26387dd2024-07-02 00:18:3376 test_sync_service_.SetSignedOut();
Jood Hajeer6fef74c9f2022-10-12 12:41:0977 test_sync_service_.GetUserSettings()->SetSelectedTypes(
78 /*sync_everything=*/false,
Maksim Moskvitinc306ea8e2023-05-12 18:05:2779 /*types=*/{syncer::UserSelectableType::kPreferences});
Alex Chau2badb802019-11-15 13:47:0380
81 EXPECT_FALSE(IsSyncEnabledForSharing(&test_sync_service_));
82 EXPECT_TRUE(IsSyncDisabledForSharing(&test_sync_service_));
83}
84
85TEST_F(SharingUtilsTest, SyncDisabled_Configuring) {
Victor Hugo Vianna Silvac527daa2024-07-02 01:35:4486 test_sync_service_.SetMaxTransportState(
Alex Chau2badb802019-11-15 13:47:0387 syncer::SyncService::TransportState::CONFIGURING);
Jood Hajeer6fef74c9f2022-10-12 12:41:0988 test_sync_service_.GetUserSettings()->SetSelectedTypes(
89 /*sync_everything=*/false,
Maksim Moskvitinc306ea8e2023-05-12 18:05:2790 /*types=*/{syncer::UserSelectableType::kPreferences});
Alex Chau2badb802019-11-15 13:47:0391
92 EXPECT_FALSE(IsSyncEnabledForSharing(&test_sync_service_));
93 EXPECT_FALSE(IsSyncDisabledForSharing(&test_sync_service_));
94}
Alex Chau328eeb932020-04-22 11:06:3595
96TEST_F(SharingUtilsTest, GetFCMChannel) {
97 std::unique_ptr<syncer::DeviceInfo> device_info = CreateFakeDeviceInfo(
98 kDeviceGuid, kDeviceName,
99 syncer::DeviceInfo::SharingInfo(
Alex Chau328eeb932020-04-22 11:06:35100 {kSenderIdFCMToken, kSenderIdP256dh, kSenderIdAuthSecret},
Hira Mahmoodb56e95f2024-07-15 19:46:03101 /*chime_representative_target_id=*/std::string(),
Alex Chau328eeb932020-04-22 11:06:35102 std::set<sync_pb::SharingSpecificFields::EnabledFeatures>()));
103
104 auto fcm_channel = GetFCMChannel(*device_info);
105
106 ASSERT_TRUE(fcm_channel);
Alex Chau328eeb932020-04-22 11:06:35107 EXPECT_EQ(fcm_channel->sender_id_fcm_token(), kSenderIdFCMToken);
108 EXPECT_EQ(fcm_channel->sender_id_p256dh(), kSenderIdP256dh);
109 EXPECT_EQ(fcm_channel->sender_id_auth_secret(), kSenderIdAuthSecret);
110}
111
112TEST_F(SharingUtilsTest, GetDevicePlatform) {
113 EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo(
Arthur Sonzognife132ee2024-01-15 11:01:04114 kDeviceGuid, kDeviceName, /*sharing_info=*/std::nullopt,
Jood Hajeer1c515ea2022-09-27 12:05:45115 sync_pb::SyncEnums_DeviceType_TYPE_CROS,
116 syncer::DeviceInfo::OsType::kChromeOsAsh,
117 syncer::DeviceInfo::FormFactor::kDesktop)),
Alex Chau328eeb932020-04-22 11:06:35118 SharingDevicePlatform::kChromeOS);
119
120 EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo(
Arthur Sonzognife132ee2024-01-15 11:01:04121 kDeviceGuid, kDeviceName, /*sharing_info=*/std::nullopt,
Jood Hajeer1c515ea2022-09-27 12:05:45122 sync_pb::SyncEnums_DeviceType_TYPE_LINUX,
123 syncer::DeviceInfo::OsType::kLinux,
124 syncer::DeviceInfo::FormFactor::kDesktop)),
Alex Chau328eeb932020-04-22 11:06:35125 SharingDevicePlatform::kLinux);
126
127 EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo(
Arthur Sonzognife132ee2024-01-15 11:01:04128 kDeviceGuid, kDeviceName, /*sharing_info=*/std::nullopt,
Jood Hajeer1c515ea2022-09-27 12:05:45129 sync_pb::SyncEnums_DeviceType_TYPE_MAC,
130 syncer::DeviceInfo::OsType::kMac,
131 syncer::DeviceInfo::FormFactor::kDesktop)),
Alex Chau328eeb932020-04-22 11:06:35132 SharingDevicePlatform::kMac);
133
134 EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo(
Arthur Sonzognife132ee2024-01-15 11:01:04135 kDeviceGuid, kDeviceName, /*sharing_info=*/std::nullopt,
Jood Hajeer1c515ea2022-09-27 12:05:45136 sync_pb::SyncEnums_DeviceType_TYPE_WIN,
137 syncer::DeviceInfo::OsType::kWindows,
138 syncer::DeviceInfo::FormFactor::kDesktop)),
Alex Chau328eeb932020-04-22 11:06:35139 SharingDevicePlatform::kWindows);
140
Alex Chau328eeb932020-04-22 11:06:35141 EXPECT_EQ(
142 GetDevicePlatform(*CreateFakeDeviceInfo(
Arthur Sonzognife132ee2024-01-15 11:01:04143 kDeviceGuid, kDeviceName, /*sharing_info=*/std::nullopt,
Jood Hajeer1c515ea2022-09-27 12:05:45144 sync_pb::SyncEnums_DeviceType_TYPE_PHONE,
145 syncer::DeviceInfo::OsType::kIOS,
146 syncer::DeviceInfo::FormFactor::kPhone, "Apple Inc.", "iPhone 50")),
Richard Knoll5b608be2020-07-10 08:39:40147 SharingDevicePlatform::kIOS);
148 EXPECT_EQ(
149 GetDevicePlatform(*CreateFakeDeviceInfo(
Arthur Sonzognife132ee2024-01-15 11:01:04150 kDeviceGuid, kDeviceName, /*sharing_info=*/std::nullopt,
Jood Hajeer1c515ea2022-09-27 12:05:45151 sync_pb::SyncEnums_DeviceType_TYPE_TABLET,
152 syncer::DeviceInfo::OsType::kIOS,
153 syncer::DeviceInfo::FormFactor::kTablet, "Apple Inc.", "iPad 99")),
Alex Chau328eeb932020-04-22 11:06:35154 SharingDevicePlatform::kIOS);
155
Alex Chau328eeb932020-04-22 11:06:35156 EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo(
Arthur Sonzognife132ee2024-01-15 11:01:04157 kDeviceGuid, kDeviceName, /*sharing_info=*/std::nullopt,
Jood Hajeer1c515ea2022-09-27 12:05:45158 sync_pb::SyncEnums_DeviceType_TYPE_PHONE,
159 syncer::DeviceInfo::OsType::kAndroid,
160 syncer::DeviceInfo::FormFactor::kPhone, "Google", "Pixel 777")),
161 SharingDevicePlatform::kAndroid);
162 EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo(
Arthur Sonzognife132ee2024-01-15 11:01:04163 kDeviceGuid, kDeviceName, /*sharing_info=*/std::nullopt,
Jood Hajeer1c515ea2022-09-27 12:05:45164 sync_pb::SyncEnums_DeviceType_TYPE_TABLET,
165 syncer::DeviceInfo::OsType::kAndroid,
166 syncer::DeviceInfo::FormFactor::kTablet, "Google", "Pixel Z")),
167 SharingDevicePlatform::kAndroid);
168
169 EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo(
Arthur Sonzognife132ee2024-01-15 11:01:04170 kDeviceGuid, kDeviceName, /*sharing_info=*/std::nullopt,
Jood Hajeer1c515ea2022-09-27 12:05:45171 sync_pb::SyncEnums_DeviceType_TYPE_UNSET,
172 syncer::DeviceInfo::OsType::kUnknown,
173 syncer::DeviceInfo::FormFactor::kUnknown)),
Alex Chau328eeb932020-04-22 11:06:35174 SharingDevicePlatform::kUnknown);
175 EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo(
Arthur Sonzognife132ee2024-01-15 11:01:04176 kDeviceGuid, kDeviceName, /*sharing_info=*/std::nullopt,
Jood Hajeer1c515ea2022-09-27 12:05:45177 sync_pb::SyncEnums_DeviceType_TYPE_OTHER,
178 syncer::DeviceInfo::OsType::kUnknown,
179 syncer::DeviceInfo::FormFactor::kUnknown)),
Alex Chau328eeb932020-04-22 11:06:35180 SharingDevicePlatform::kUnknown);
181}