Himanshu Jaju | 115ec0b | 2019-10-30 11:59:39 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/sharing/sharing_utils.h" |
| 6 | |
Alex Chau | 2badb80 | 2019-11-15 13:47:03 | [diff] [blame] | 7 | #include "base/test/scoped_feature_list.h" |
Alex Chau | 328eeb93 | 2020-04-22 11:06:35 | [diff] [blame] | 8 | #include "chrome/browser/sharing/fake_device_info.h" |
Alex Chau | 2badb80 | 2019-11-15 13:47:03 | [diff] [blame] | 9 | #include "chrome/browser/sharing/features.h" |
Alex Chau | 328eeb93 | 2020-04-22 11:06:35 | [diff] [blame] | 10 | #include "chrome/browser/sharing/proto/sharing_message.pb.h" |
| 11 | #include "chrome/browser/sharing/sharing_constants.h" |
Alex Chau | 2badb80 | 2019-11-15 13:47:03 | [diff] [blame] | 12 | #include "components/sync/driver/test_sync_service.h" |
Victor Hugo Vianna Silva | e92e300 | 2021-07-26 14:38:50 | [diff] [blame] | 13 | #include "components/sync/protocol/device_info_specifics.pb.h" |
| 14 | #include "components/sync/protocol/sync_enums.pb.h" |
Himanshu Jaju | 115ec0b | 2019-10-30 11:59:39 | [diff] [blame] | 15 | #include "components/sync_device_info/device_info.h" |
| 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
| 18 | namespace { |
Alex Chau | 2badb80 | 2019-11-15 13:47:03 | [diff] [blame] | 19 | |
Alex Chau | 328eeb93 | 2020-04-22 11:06:35 | [diff] [blame] | 20 | const char kDeviceGuid[] = "test_guid"; |
| 21 | const char kDeviceName[] = "test_name"; |
| 22 | const char kVapidFCMToken[] = "test_vapid_fcm_token"; |
| 23 | const char kVapidP256dh[] = "test_vapid_p256_dh"; |
| 24 | const char kVapidAuthSecret[] = "test_vapid_auth_secret"; |
| 25 | const char kSenderIdFCMToken[] = "test_sender_id_fcm_token"; |
| 26 | const char kSenderIdP256dh[] = "test_sender_id_p256_dh"; |
| 27 | const char kSenderIdAuthSecret[] = "test_sender_id_auth_secret"; |
| 28 | |
Alex Chau | 2badb80 | 2019-11-15 13:47:03 | [diff] [blame] | 29 | class SharingUtilsTest : public testing::Test { |
| 30 | public: |
| 31 | SharingUtilsTest() = default; |
| 32 | |
| 33 | protected: |
| 34 | base::test::ScopedFeatureList scoped_feature_list_; |
| 35 | syncer::TestSyncService test_sync_service_; |
| 36 | }; |
| 37 | |
Himanshu Jaju | 115ec0b | 2019-10-30 11:59:39 | [diff] [blame] | 38 | } // namespace |
| 39 | |
Naomi Musgrave | d9fc0bf | 2020-04-22 18:46:26 | [diff] [blame] | 40 | TEST_F(SharingUtilsTest, SyncEnabled_FullySynced) { |
| 41 | // Disable transport mode required features. |
Alex Chau | 2badb80 | 2019-11-15 13:47:03 | [diff] [blame] | 42 | scoped_feature_list_.InitWithFeatures( |
Naomi Musgrave | d9fc0bf | 2020-04-22 18:46:26 | [diff] [blame] | 43 | /*enabled_features=*/{}, |
| 44 | /*disabled_features=*/{kSharingSendViaSync}); |
Alex Chau | 2badb80 | 2019-11-15 13:47:03 | [diff] [blame] | 45 | test_sync_service_.SetTransportState( |
| 46 | syncer::SyncService::TransportState::ACTIVE); |
Naomi Musgrave | d9fc0bf | 2020-04-22 18:46:26 | [diff] [blame] | 47 | // PREFERENCES is actively synced. |
| 48 | test_sync_service_.SetActiveDataTypes( |
| 49 | {syncer::DEVICE_INFO, syncer::PREFERENCES}); |
Alex Chau | 2badb80 | 2019-11-15 13:47:03 | [diff] [blame] | 50 | |
| 51 | EXPECT_TRUE(IsSyncEnabledForSharing(&test_sync_service_)); |
| 52 | EXPECT_FALSE(IsSyncDisabledForSharing(&test_sync_service_)); |
| 53 | } |
| 54 | |
Naomi Musgrave | d9fc0bf | 2020-04-22 18:46:26 | [diff] [blame] | 55 | TEST_F(SharingUtilsTest, SyncDisabled_FullySynced_MissingDataTypes) { |
Alex Chau | 2badb80 | 2019-11-15 13:47:03 | [diff] [blame] | 56 | // Disable transport mode required features. |
| 57 | scoped_feature_list_.InitWithFeatures( |
| 58 | /*enabled_features=*/{}, |
Naomi Musgrave | d9fc0bf | 2020-04-22 18:46:26 | [diff] [blame] | 59 | /*disabled_features=*/{kSharingSendViaSync}); |
Alex Chau | 2badb80 | 2019-11-15 13:47:03 | [diff] [blame] | 60 | test_sync_service_.SetTransportState( |
| 61 | syncer::SyncService::TransportState::ACTIVE); |
Naomi Musgrave | d9fc0bf | 2020-04-22 18:46:26 | [diff] [blame] | 62 | // Missing PREFERENCES. |
| 63 | test_sync_service_.SetActiveDataTypes({syncer::DEVICE_INFO}); |
| 64 | |
| 65 | EXPECT_FALSE(IsSyncEnabledForSharing(&test_sync_service_)); |
| 66 | EXPECT_TRUE(IsSyncDisabledForSharing(&test_sync_service_)); |
| 67 | } |
| 68 | |
| 69 | TEST_F(SharingUtilsTest, SyncEnabled_SigninOnly) { |
| 70 | // Enable transport mode required features. |
| 71 | scoped_feature_list_.InitWithFeatures( |
| 72 | /*enabled_features=*/{kSharingSendViaSync}, |
| 73 | /*disabled_features=*/{}); |
| 74 | test_sync_service_.SetTransportState( |
| 75 | syncer::SyncService::TransportState::ACTIVE); |
| 76 | // SHARING_MESSAGE is actively synced. |
Alex Chau | 2badb80 | 2019-11-15 13:47:03 | [diff] [blame] | 77 | test_sync_service_.SetActiveDataTypes( |
Naomi Musgrave | d9fc0bf | 2020-04-22 18:46:26 | [diff] [blame] | 78 | {syncer::DEVICE_INFO, syncer::SHARING_MESSAGE}); |
Alex Chau | 2badb80 | 2019-11-15 13:47:03 | [diff] [blame] | 79 | |
| 80 | EXPECT_TRUE(IsSyncEnabledForSharing(&test_sync_service_)); |
| 81 | EXPECT_FALSE(IsSyncDisabledForSharing(&test_sync_service_)); |
| 82 | } |
| 83 | |
| 84 | TEST_F(SharingUtilsTest, SyncDisabled_SigninOnly_MissingDataTypes) { |
| 85 | // Enable transport mode required features. |
| 86 | scoped_feature_list_.InitWithFeatures( |
Naomi Musgrave | d9fc0bf | 2020-04-22 18:46:26 | [diff] [blame] | 87 | /*enabled_features=*/{kSharingSendViaSync}, |
Alex Chau | 2badb80 | 2019-11-15 13:47:03 | [diff] [blame] | 88 | /*disabled_features=*/{}); |
| 89 | test_sync_service_.SetTransportState( |
| 90 | syncer::SyncService::TransportState::ACTIVE); |
Naomi Musgrave | d9fc0bf | 2020-04-22 18:46:26 | [diff] [blame] | 91 | // Missing SHARING_MESSAGE. |
Alex Chau | 2badb80 | 2019-11-15 13:47:03 | [diff] [blame] | 92 | test_sync_service_.SetActiveDataTypes({syncer::DEVICE_INFO}); |
| 93 | |
| 94 | EXPECT_FALSE(IsSyncEnabledForSharing(&test_sync_service_)); |
| 95 | EXPECT_TRUE(IsSyncDisabledForSharing(&test_sync_service_)); |
| 96 | } |
| 97 | |
| 98 | TEST_F(SharingUtilsTest, SyncDisabled_Disabled) { |
| 99 | test_sync_service_.SetTransportState( |
| 100 | syncer::SyncService::TransportState::DISABLED); |
| 101 | test_sync_service_.SetActiveDataTypes( |
| 102 | {syncer::DEVICE_INFO, syncer::PREFERENCES}); |
| 103 | |
| 104 | EXPECT_FALSE(IsSyncEnabledForSharing(&test_sync_service_)); |
| 105 | EXPECT_TRUE(IsSyncDisabledForSharing(&test_sync_service_)); |
| 106 | } |
| 107 | |
| 108 | TEST_F(SharingUtilsTest, SyncDisabled_Configuring) { |
| 109 | test_sync_service_.SetTransportState( |
| 110 | syncer::SyncService::TransportState::CONFIGURING); |
| 111 | test_sync_service_.SetActiveDataTypes( |
| 112 | {syncer::DEVICE_INFO, syncer::PREFERENCES}); |
| 113 | |
| 114 | EXPECT_FALSE(IsSyncEnabledForSharing(&test_sync_service_)); |
| 115 | EXPECT_FALSE(IsSyncDisabledForSharing(&test_sync_service_)); |
| 116 | } |
Alex Chau | 328eeb93 | 2020-04-22 11:06:35 | [diff] [blame] | 117 | |
| 118 | TEST_F(SharingUtilsTest, GetFCMChannel) { |
| 119 | std::unique_ptr<syncer::DeviceInfo> device_info = CreateFakeDeviceInfo( |
| 120 | kDeviceGuid, kDeviceName, |
| 121 | syncer::DeviceInfo::SharingInfo( |
| 122 | {kVapidFCMToken, kVapidP256dh, kVapidAuthSecret}, |
| 123 | {kSenderIdFCMToken, kSenderIdP256dh, kSenderIdAuthSecret}, |
| 124 | std::set<sync_pb::SharingSpecificFields::EnabledFeatures>())); |
| 125 | |
| 126 | auto fcm_channel = GetFCMChannel(*device_info); |
| 127 | |
| 128 | ASSERT_TRUE(fcm_channel); |
| 129 | EXPECT_EQ(fcm_channel->vapid_fcm_token(), kVapidFCMToken); |
| 130 | EXPECT_EQ(fcm_channel->vapid_p256dh(), kVapidP256dh); |
| 131 | EXPECT_EQ(fcm_channel->vapid_auth_secret(), kVapidAuthSecret); |
| 132 | EXPECT_EQ(fcm_channel->sender_id_fcm_token(), kSenderIdFCMToken); |
| 133 | EXPECT_EQ(fcm_channel->sender_id_p256dh(), kSenderIdP256dh); |
| 134 | EXPECT_EQ(fcm_channel->sender_id_auth_secret(), kSenderIdAuthSecret); |
| 135 | } |
| 136 | |
| 137 | TEST_F(SharingUtilsTest, GetDevicePlatform) { |
| 138 | EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo( |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame] | 139 | kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt, |
Alex Chau | 328eeb93 | 2020-04-22 11:06:35 | [diff] [blame] | 140 | sync_pb::SyncEnums_DeviceType_TYPE_CROS)), |
| 141 | SharingDevicePlatform::kChromeOS); |
| 142 | |
| 143 | EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo( |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame] | 144 | kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt, |
Alex Chau | 328eeb93 | 2020-04-22 11:06:35 | [diff] [blame] | 145 | sync_pb::SyncEnums_DeviceType_TYPE_LINUX)), |
| 146 | SharingDevicePlatform::kLinux); |
| 147 | |
| 148 | EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo( |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame] | 149 | kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt, |
Alex Chau | 328eeb93 | 2020-04-22 11:06:35 | [diff] [blame] | 150 | sync_pb::SyncEnums_DeviceType_TYPE_MAC)), |
| 151 | SharingDevicePlatform::kMac); |
| 152 | |
| 153 | EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo( |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame] | 154 | kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt, |
Alex Chau | 328eeb93 | 2020-04-22 11:06:35 | [diff] [blame] | 155 | sync_pb::SyncEnums_DeviceType_TYPE_WIN)), |
| 156 | SharingDevicePlatform::kWindows); |
| 157 | |
Alex Chau | 328eeb93 | 2020-04-22 11:06:35 | [diff] [blame] | 158 | EXPECT_EQ( |
| 159 | GetDevicePlatform(*CreateFakeDeviceInfo( |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame] | 160 | kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt, |
Richard Knoll | 5b608be | 2020-07-10 08:39:40 | [diff] [blame] | 161 | sync_pb::SyncEnums_DeviceType_TYPE_PHONE, "Apple Inc.", "iPhone 50")), |
| 162 | SharingDevicePlatform::kIOS); |
| 163 | EXPECT_EQ( |
| 164 | GetDevicePlatform(*CreateFakeDeviceInfo( |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame] | 165 | kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt, |
Richard Knoll | 5b608be | 2020-07-10 08:39:40 | [diff] [blame] | 166 | sync_pb::SyncEnums_DeviceType_TYPE_TABLET, "Apple Inc.", "iPad 99")), |
Alex Chau | 328eeb93 | 2020-04-22 11:06:35 | [diff] [blame] | 167 | SharingDevicePlatform::kIOS); |
| 168 | |
| 169 | EXPECT_EQ( |
| 170 | GetDevicePlatform(*CreateFakeDeviceInfo( |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame] | 171 | kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt, |
Richard Knoll | 5b608be | 2020-07-10 08:39:40 | [diff] [blame] | 172 | sync_pb::SyncEnums_DeviceType_TYPE_PHONE, "Google", "Pixel 777")), |
Alex Chau | 328eeb93 | 2020-04-22 11:06:35 | [diff] [blame] | 173 | SharingDevicePlatform::kAndroid); |
| 174 | EXPECT_EQ( |
| 175 | GetDevicePlatform(*CreateFakeDeviceInfo( |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame] | 176 | kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt, |
Richard Knoll | 5b608be | 2020-07-10 08:39:40 | [diff] [blame] | 177 | sync_pb::SyncEnums_DeviceType_TYPE_TABLET, "Google", "Pixel Z")), |
Alex Chau | 328eeb93 | 2020-04-22 11:06:35 | [diff] [blame] | 178 | SharingDevicePlatform::kAndroid); |
| 179 | |
| 180 | EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo( |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame] | 181 | kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt, |
Alex Chau | 328eeb93 | 2020-04-22 11:06:35 | [diff] [blame] | 182 | sync_pb::SyncEnums_DeviceType_TYPE_UNSET)), |
| 183 | SharingDevicePlatform::kUnknown); |
| 184 | EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo( |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame] | 185 | kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt, |
Alex Chau | 328eeb93 | 2020-04-22 11:06:35 | [diff] [blame] | 186 | sync_pb::SyncEnums_DeviceType_TYPE_OTHER)), |
| 187 | SharingDevicePlatform::kUnknown); |
| 188 | } |