blob: 95ddf7fd8ee0cd05d81658d8832832901eba6481 [file] [log] [blame]
Himanshu Jaju115ec0b2019-10-30 11:59:391// 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 Chau2badb802019-11-15 13:47:037#include "base/test/scoped_feature_list.h"
Alex Chau328eeb932020-04-22 11:06:358#include "chrome/browser/sharing/fake_device_info.h"
Alex Chau2badb802019-11-15 13:47:039#include "chrome/browser/sharing/features.h"
Alex Chau328eeb932020-04-22 11:06:3510#include "chrome/browser/sharing/proto/sharing_message.pb.h"
11#include "chrome/browser/sharing/sharing_constants.h"
Alex Chau2badb802019-11-15 13:47:0312#include "components/sync/driver/test_sync_service.h"
Victor Hugo Vianna Silvae92e3002021-07-26 14:38:5013#include "components/sync/protocol/device_info_specifics.pb.h"
14#include "components/sync/protocol/sync_enums.pb.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:
34 base::test::ScopedFeatureList scoped_feature_list_;
35 syncer::TestSyncService test_sync_service_;
36};
37
Himanshu Jaju115ec0b2019-10-30 11:59:3938} // namespace
39
Naomi Musgraved9fc0bf2020-04-22 18:46:2640TEST_F(SharingUtilsTest, SyncEnabled_FullySynced) {
41 // Disable transport mode required features.
Alex Chau2badb802019-11-15 13:47:0342 scoped_feature_list_.InitWithFeatures(
Naomi Musgraved9fc0bf2020-04-22 18:46:2643 /*enabled_features=*/{},
44 /*disabled_features=*/{kSharingSendViaSync});
Alex Chau2badb802019-11-15 13:47:0345 test_sync_service_.SetTransportState(
46 syncer::SyncService::TransportState::ACTIVE);
Naomi Musgraved9fc0bf2020-04-22 18:46:2647 // PREFERENCES is actively synced.
48 test_sync_service_.SetActiveDataTypes(
49 {syncer::DEVICE_INFO, syncer::PREFERENCES});
Alex Chau2badb802019-11-15 13:47:0350
51 EXPECT_TRUE(IsSyncEnabledForSharing(&test_sync_service_));
52 EXPECT_FALSE(IsSyncDisabledForSharing(&test_sync_service_));
53}
54
Naomi Musgraved9fc0bf2020-04-22 18:46:2655TEST_F(SharingUtilsTest, SyncDisabled_FullySynced_MissingDataTypes) {
Alex Chau2badb802019-11-15 13:47:0356 // Disable transport mode required features.
57 scoped_feature_list_.InitWithFeatures(
58 /*enabled_features=*/{},
Naomi Musgraved9fc0bf2020-04-22 18:46:2659 /*disabled_features=*/{kSharingSendViaSync});
Alex Chau2badb802019-11-15 13:47:0360 test_sync_service_.SetTransportState(
61 syncer::SyncService::TransportState::ACTIVE);
Naomi Musgraved9fc0bf2020-04-22 18:46:2662 // 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
69TEST_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 Chau2badb802019-11-15 13:47:0377 test_sync_service_.SetActiveDataTypes(
Naomi Musgraved9fc0bf2020-04-22 18:46:2678 {syncer::DEVICE_INFO, syncer::SHARING_MESSAGE});
Alex Chau2badb802019-11-15 13:47:0379
80 EXPECT_TRUE(IsSyncEnabledForSharing(&test_sync_service_));
81 EXPECT_FALSE(IsSyncDisabledForSharing(&test_sync_service_));
82}
83
84TEST_F(SharingUtilsTest, SyncDisabled_SigninOnly_MissingDataTypes) {
85 // Enable transport mode required features.
86 scoped_feature_list_.InitWithFeatures(
Naomi Musgraved9fc0bf2020-04-22 18:46:2687 /*enabled_features=*/{kSharingSendViaSync},
Alex Chau2badb802019-11-15 13:47:0388 /*disabled_features=*/{});
89 test_sync_service_.SetTransportState(
90 syncer::SyncService::TransportState::ACTIVE);
Naomi Musgraved9fc0bf2020-04-22 18:46:2691 // Missing SHARING_MESSAGE.
Alex Chau2badb802019-11-15 13:47:0392 test_sync_service_.SetActiveDataTypes({syncer::DEVICE_INFO});
93
94 EXPECT_FALSE(IsSyncEnabledForSharing(&test_sync_service_));
95 EXPECT_TRUE(IsSyncDisabledForSharing(&test_sync_service_));
96}
97
98TEST_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
108TEST_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 Chau328eeb932020-04-22 11:06:35117
118TEST_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
137TEST_F(SharingUtilsTest, GetDevicePlatform) {
138 EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo(
Anton Bikineev46bbb972021-05-15 17:53:53139 kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt,
Alex Chau328eeb932020-04-22 11:06:35140 sync_pb::SyncEnums_DeviceType_TYPE_CROS)),
141 SharingDevicePlatform::kChromeOS);
142
143 EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo(
Anton Bikineev46bbb972021-05-15 17:53:53144 kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt,
Alex Chau328eeb932020-04-22 11:06:35145 sync_pb::SyncEnums_DeviceType_TYPE_LINUX)),
146 SharingDevicePlatform::kLinux);
147
148 EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo(
Anton Bikineev46bbb972021-05-15 17:53:53149 kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt,
Alex Chau328eeb932020-04-22 11:06:35150 sync_pb::SyncEnums_DeviceType_TYPE_MAC)),
151 SharingDevicePlatform::kMac);
152
153 EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo(
Anton Bikineev46bbb972021-05-15 17:53:53154 kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt,
Alex Chau328eeb932020-04-22 11:06:35155 sync_pb::SyncEnums_DeviceType_TYPE_WIN)),
156 SharingDevicePlatform::kWindows);
157
Alex Chau328eeb932020-04-22 11:06:35158 EXPECT_EQ(
159 GetDevicePlatform(*CreateFakeDeviceInfo(
Anton Bikineev46bbb972021-05-15 17:53:53160 kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt,
Richard Knoll5b608be2020-07-10 08:39:40161 sync_pb::SyncEnums_DeviceType_TYPE_PHONE, "Apple Inc.", "iPhone 50")),
162 SharingDevicePlatform::kIOS);
163 EXPECT_EQ(
164 GetDevicePlatform(*CreateFakeDeviceInfo(
Anton Bikineev46bbb972021-05-15 17:53:53165 kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt,
Richard Knoll5b608be2020-07-10 08:39:40166 sync_pb::SyncEnums_DeviceType_TYPE_TABLET, "Apple Inc.", "iPad 99")),
Alex Chau328eeb932020-04-22 11:06:35167 SharingDevicePlatform::kIOS);
168
169 EXPECT_EQ(
170 GetDevicePlatform(*CreateFakeDeviceInfo(
Anton Bikineev46bbb972021-05-15 17:53:53171 kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt,
Richard Knoll5b608be2020-07-10 08:39:40172 sync_pb::SyncEnums_DeviceType_TYPE_PHONE, "Google", "Pixel 777")),
Alex Chau328eeb932020-04-22 11:06:35173 SharingDevicePlatform::kAndroid);
174 EXPECT_EQ(
175 GetDevicePlatform(*CreateFakeDeviceInfo(
Anton Bikineev46bbb972021-05-15 17:53:53176 kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt,
Richard Knoll5b608be2020-07-10 08:39:40177 sync_pb::SyncEnums_DeviceType_TYPE_TABLET, "Google", "Pixel Z")),
Alex Chau328eeb932020-04-22 11:06:35178 SharingDevicePlatform::kAndroid);
179
180 EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo(
Anton Bikineev46bbb972021-05-15 17:53:53181 kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt,
Alex Chau328eeb932020-04-22 11:06:35182 sync_pb::SyncEnums_DeviceType_TYPE_UNSET)),
183 SharingDevicePlatform::kUnknown);
184 EXPECT_EQ(GetDevicePlatform(*CreateFakeDeviceInfo(
Anton Bikineev46bbb972021-05-15 17:53:53185 kDeviceGuid, kDeviceName, /*sharing_info=*/absl::nullopt,
Alex Chau328eeb932020-04-22 11:06:35186 sync_pb::SyncEnums_DeviceType_TYPE_OTHER)),
187 SharingDevicePlatform::kUnknown);
188}