blob: bbd8eb34aea7d1366866f9be2a1b1446910a4c18 [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";
22const char kVapidFCMToken[] = "test_vapid_fcm_token";
23const char kVapidP256dh[] = "test_vapid_p256_dh";
24const char kVapidAuthSecret[] = "test_vapid_auth_secret";
25const char kSenderIdFCMToken[] = "test_sender_id_fcm_token";
26const char kSenderIdP256dh[] = "test_sender_id_p256_dh";
27const char kSenderIdAuthSecret[] = "test_sender_id_auth_secret";
28
Alex Chau2badb802019-11-15 13:47:0329class SharingUtilsTest : public testing::Test {
30 public:
31 SharingUtilsTest() = default;
32
33 protected:
Alex Chau2badb802019-11-15 13:47:0334 syncer::TestSyncService test_sync_service_;
35};
36
Himanshu Jaju115ec0b2019-10-30 11:59:3937} // namespace
38
Naomi Musgraved9fc0bf2020-04-22 18:46:2639TEST_F(SharingUtilsTest, SyncEnabled_FullySynced) {
Naomi Musgraved9fc0bf2020-04-22 18:46:2640 // PREFERENCES is actively synced.
Jood Hajeer6fef74c9f2022-10-12 12:41:0941 test_sync_service_.GetUserSettings()->SetSelectedTypes(
42 /*sync_everything=*/false,
Maksim Moskvitinc306ea8e2023-05-12 18:05:2743 /*types=*/{syncer::UserSelectableType::kPreferences});
Alex Chau2badb802019-11-15 13:47:0344
45 EXPECT_TRUE(IsSyncEnabledForSharing(&test_sync_service_));
46 EXPECT_FALSE(IsSyncDisabledForSharing(&test_sync_service_));
47}
48
Naomi Musgraved9fc0bf2020-04-22 18:46:2649TEST_F(SharingUtilsTest, SyncDisabled_FullySynced_MissingDataTypes) {
Naomi Musgraved9fc0bf2020-04-22 18:46:2650 // Missing PREFERENCES.
Jood Hajeer6fef74c9f2022-10-12 12:41:0951 test_sync_service_.GetUserSettings()->SetSelectedTypes(
52 /*sync_everything=*/false,
53 /*types=*/syncer::UserSelectableTypeSet());
Jens Mueller92781872023-01-19 17:31:0654 // Not able to sync SHARING_MESSAGE.
55 test_sync_service_.SetFailedDataTypes({syncer::SHARING_MESSAGE});
Naomi Musgraved9fc0bf2020-04-22 18:46:2656
57 EXPECT_FALSE(IsSyncEnabledForSharing(&test_sync_service_));
58 EXPECT_TRUE(IsSyncDisabledForSharing(&test_sync_service_));
59}
60
61TEST_F(SharingUtilsTest, SyncEnabled_SigninOnly) {
Naomi Musgraved9fc0bf2020-04-22 18:46:2662 // SHARING_MESSAGE is actively synced.
Alex Chau2badb802019-11-15 13:47:0363 EXPECT_TRUE(IsSyncEnabledForSharing(&test_sync_service_));
64 EXPECT_FALSE(IsSyncDisabledForSharing(&test_sync_service_));
65}
66
67TEST_F(SharingUtilsTest, SyncDisabled_SigninOnly_MissingDataTypes) {
Naomi Musgraved9fc0bf2020-04-22 18:46:2668 // Missing SHARING_MESSAGE.
Jood Hajeer6fef74c9f2022-10-12 12:41:0969 test_sync_service_.GetUserSettings()->SetSelectedTypes(
70 /*sync_everything=*/false,
71 /*types=*/syncer::UserSelectableTypeSet());
72 test_sync_service_.SetFailedDataTypes({syncer::SHARING_MESSAGE});
Alex Chau2badb802019-11-15 13:47:0373
74 EXPECT_FALSE(IsSyncEnabledForSharing(&test_sync_service_));
75 EXPECT_TRUE(IsSyncDisabledForSharing(&test_sync_service_));
76}
77
78TEST_F(SharingUtilsTest, SyncDisabled_Disabled) {
Victor Hugo Vianna Silva26387dd2024-07-02 00:18:3379 test_sync_service_.SetSignedOut();
Jood Hajeer6fef74c9f2022-10-12 12:41:0980 test_sync_service_.GetUserSettings()->SetSelectedTypes(
81 /*sync_everything=*/false,
Maksim Moskvitinc306ea8e2023-05-12 18:05:2782 /*types=*/{syncer::UserSelectableType::kPreferences});
Alex Chau2badb802019-11-15 13:47:0383
84 EXPECT_FALSE(IsSyncEnabledForSharing(&test_sync_service_));
85 EXPECT_TRUE(IsSyncDisabledForSharing(&test_sync_service_));
86}
87
88TEST_F(SharingUtilsTest, SyncDisabled_Configuring) {
Victor Hugo Vianna Silvac527daa2024-07-02 01:35:4489 test_sync_service_.SetMaxTransportState(
Alex Chau2badb802019-11-15 13:47:0390 syncer::SyncService::TransportState::CONFIGURING);
Jood Hajeer6fef74c9f2022-10-12 12:41:0991 test_sync_service_.GetUserSettings()->SetSelectedTypes(
92 /*sync_everything=*/false,
Maksim Moskvitinc306ea8e2023-05-12 18:05:2793 /*types=*/{syncer::UserSelectableType::kPreferences});
Alex Chau2badb802019-11-15 13:47:0394
95 EXPECT_FALSE(IsSyncEnabledForSharing(&test_sync_service_));
96 EXPECT_FALSE(IsSyncDisabledForSharing(&test_sync_service_));
97}
Alex Chau328eeb932020-04-22 11:06:3598
99TEST_F(SharingUtilsTest, GetFCMChannel) {
100 std::unique_ptr<syncer::DeviceInfo> device_info = CreateFakeDeviceInfo(
101 kDeviceGuid, kDeviceName,
102 syncer::DeviceInfo::SharingInfo(
103 {kVapidFCMToken, kVapidP256dh, kVapidAuthSecret},
104 {kSenderIdFCMToken, kSenderIdP256dh, kSenderIdAuthSecret},
Hira Mahmoodb56e95f2024-07-15 19:46:03105 /*chime_representative_target_id=*/std::string(),
Alex Chau328eeb932020-04-22 11:06:35106 std::set<sync_pb::SharingSpecificFields::EnabledFeatures>()));
107
108 auto fcm_channel = GetFCMChannel(*device_info);
109
110 ASSERT_TRUE(fcm_channel);
Rushan Suleymanov9dced9d2024-11-26 15:56:57111 EXPECT_EQ(fcm_channel->vapid_fcm_token(), "");
112 EXPECT_EQ(fcm_channel->vapid_p256dh(), "");
113 EXPECT_EQ(fcm_channel->vapid_auth_secret(), "");
Alex Chau328eeb932020-04-22 11:06:35114 EXPECT_EQ(fcm_channel->sender_id_fcm_token(), kSenderIdFCMToken);
115 EXPECT_EQ(fcm_channel->sender_id_p256dh(), kSenderIdP256dh);
116 EXPECT_EQ(fcm_channel->sender_id_auth_secret(), kSenderIdAuthSecret);
117}
118
119TEST_F(SharingUtilsTest, GetDevicePlatform) {
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_CROS,
123 syncer::DeviceInfo::OsType::kChromeOsAsh,
124 syncer::DeviceInfo::FormFactor::kDesktop)),
Alex Chau328eeb932020-04-22 11:06:35125 SharingDevicePlatform::kChromeOS);
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_LINUX,
130 syncer::DeviceInfo::OsType::kLinux,
131 syncer::DeviceInfo::FormFactor::kDesktop)),
Alex Chau328eeb932020-04-22 11:06:35132 SharingDevicePlatform::kLinux);
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_MAC,
137 syncer::DeviceInfo::OsType::kMac,
138 syncer::DeviceInfo::FormFactor::kDesktop)),
Alex Chau328eeb932020-04-22 11:06:35139 SharingDevicePlatform::kMac);
140
141 EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo(
Arthur Sonzognife132ee2024-01-15 11:01:04142 kDeviceGuid, kDeviceName, /*sharing_info=*/std::nullopt,
Jood Hajeer1c515ea2022-09-27 12:05:45143 sync_pb::SyncEnums_DeviceType_TYPE_WIN,
144 syncer::DeviceInfo::OsType::kWindows,
145 syncer::DeviceInfo::FormFactor::kDesktop)),
Alex Chau328eeb932020-04-22 11:06:35146 SharingDevicePlatform::kWindows);
147
Alex Chau328eeb932020-04-22 11:06:35148 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_PHONE,
152 syncer::DeviceInfo::OsType::kIOS,
153 syncer::DeviceInfo::FormFactor::kPhone, "Apple Inc.", "iPhone 50")),
Richard Knoll5b608be2020-07-10 08:39:40154 SharingDevicePlatform::kIOS);
155 EXPECT_EQ(
156 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_TABLET,
159 syncer::DeviceInfo::OsType::kIOS,
160 syncer::DeviceInfo::FormFactor::kTablet, "Apple Inc.", "iPad 99")),
Alex Chau328eeb932020-04-22 11:06:35161 SharingDevicePlatform::kIOS);
162
Alex Chau328eeb932020-04-22 11:06:35163 EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo(
Arthur Sonzognife132ee2024-01-15 11:01:04164 kDeviceGuid, kDeviceName, /*sharing_info=*/std::nullopt,
Jood Hajeer1c515ea2022-09-27 12:05:45165 sync_pb::SyncEnums_DeviceType_TYPE_PHONE,
166 syncer::DeviceInfo::OsType::kAndroid,
167 syncer::DeviceInfo::FormFactor::kPhone, "Google", "Pixel 777")),
168 SharingDevicePlatform::kAndroid);
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_TABLET,
172 syncer::DeviceInfo::OsType::kAndroid,
173 syncer::DeviceInfo::FormFactor::kTablet, "Google", "Pixel Z")),
174 SharingDevicePlatform::kAndroid);
175
176 EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo(
Arthur Sonzognife132ee2024-01-15 11:01:04177 kDeviceGuid, kDeviceName, /*sharing_info=*/std::nullopt,
Jood Hajeer1c515ea2022-09-27 12:05:45178 sync_pb::SyncEnums_DeviceType_TYPE_UNSET,
179 syncer::DeviceInfo::OsType::kUnknown,
180 syncer::DeviceInfo::FormFactor::kUnknown)),
Alex Chau328eeb932020-04-22 11:06:35181 SharingDevicePlatform::kUnknown);
182 EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo(
Arthur Sonzognife132ee2024-01-15 11:01:04183 kDeviceGuid, kDeviceName, /*sharing_info=*/std::nullopt,
Jood Hajeer1c515ea2022-09-27 12:05:45184 sync_pb::SyncEnums_DeviceType_TYPE_OTHER,
185 syncer::DeviceInfo::OsType::kUnknown,
186 syncer::DeviceInfo::FormFactor::kUnknown)),
Alex Chau328eeb932020-04-22 11:06:35187 SharingDevicePlatform::kUnknown);
188}