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 "components/send_tab_to_self/target_device_info.h"
6 
7 #include "base/test/scoped_feature_list.h"
8 #include "components/send_tab_to_self/features.h"
9 #include "components/sync/driver/test_sync_service.h"
10 #include "components/sync_device_info/device_info.h"
11 #include "components/sync_device_info/device_info_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 
14 namespace send_tab_to_self {
15 
16 namespace {
17 
18 class SharingUtilsTest : public testing::Test {
19  public:
20   SharingUtilsTest() = default;
21 
22  protected:
23   base::test::ScopedFeatureList scoped_feature_list_;
24   syncer::TestSyncService test_sync_service_;
25 };
26 
CreateFakeDeviceInfo(const std::string & id,const std::string & name,sync_pb::SyncEnums_DeviceType device_type,const std::string & manufacturer_name,const std::string & model_name)27 static std::unique_ptr<syncer::DeviceInfo> CreateFakeDeviceInfo(
28     const std::string& id,
29     const std::string& name,
30     sync_pb::SyncEnums_DeviceType device_type,
31     const std::string& manufacturer_name,
32     const std::string& model_name) {
33   return std::make_unique<syncer::DeviceInfo>(
34       id, name, "chrome_version", "user_agent", device_type, "device_id",
35       manufacturer_name, model_name,
36       /*last_updated_timestamp=*/base::Time::Now(),
37       syncer::DeviceInfoUtil::GetPulseInterval(),
38       /*send_tab_to_self_receiving_enabled=*/false,
39       syncer::DeviceInfo::SharingInfo(
40           {"vapid_fcm_token", "vapid_p256dh", "vapid_auth_secret"},
41           {"sender_id_fcm_token", "sender_id_p256dh", "sender_id_auth_secret"},
42           std::set<sync_pb::SharingSpecificFields::EnabledFeatures>{
43               sync_pb::SharingSpecificFields::CLICK_TO_CALL_V2}),
44       /*fcm_registration_token=*/std::string(),
45       /*interested_data_types=*/syncer::ModelTypeSet());
46 }
47 
48 }  // namespace
49 
TEST_F(SharingUtilsTest,GetSharingDeviceNames_AppleDevices_SigninOnly)50 TEST_F(SharingUtilsTest, GetSharingDeviceNames_AppleDevices_SigninOnly) {
51   std::unique_ptr<syncer::DeviceInfo> device = CreateFakeDeviceInfo(
52       "guid", "MacbookPro1,1", sync_pb::SyncEnums_DeviceType_TYPE_MAC,
53       "Apple Inc.", "MacbookPro1,1");
54   SharingDeviceNames names = GetSharingDeviceNames(device.get());
55 
56   EXPECT_EQ("MacbookPro1,1", names.full_name);
57   EXPECT_EQ("MacbookPro", names.short_name);
58 }
59 
TEST_F(SharingUtilsTest,GetSharingDeviceNames_AppleDevices_FullySynced)60 TEST_F(SharingUtilsTest, GetSharingDeviceNames_AppleDevices_FullySynced) {
61   std::unique_ptr<syncer::DeviceInfo> device = CreateFakeDeviceInfo(
62       "guid", "Bobs-iMac", sync_pb::SyncEnums_DeviceType_TYPE_MAC, "Apple Inc.",
63       "MacbookPro1,1");
64   SharingDeviceNames names = GetSharingDeviceNames(device.get());
65 
66   EXPECT_EQ("Bobs-iMac", names.full_name);
67   EXPECT_EQ("Bobs-iMac", names.short_name);
68 }
69 
TEST_F(SharingUtilsTest,GetSharingDeviceNames_ChromeOSDevices)70 TEST_F(SharingUtilsTest, GetSharingDeviceNames_ChromeOSDevices) {
71   std::unique_ptr<syncer::DeviceInfo> device = CreateFakeDeviceInfo(
72       "guid", "Chromebook", sync_pb::SyncEnums_DeviceType_TYPE_CROS, "Google",
73       "Chromebook");
74   SharingDeviceNames names = GetSharingDeviceNames(device.get());
75 
76   EXPECT_EQ("Google Chromebook", names.full_name);
77   EXPECT_EQ("Google Chromebook", names.short_name);
78 }
79 
TEST_F(SharingUtilsTest,GetSharingDeviceNames_AndroidPhones)80 TEST_F(SharingUtilsTest, GetSharingDeviceNames_AndroidPhones) {
81   std::unique_ptr<syncer::DeviceInfo> device = CreateFakeDeviceInfo(
82       "guid", "Pixel 2", sync_pb::SyncEnums_DeviceType_TYPE_PHONE, "Google",
83       "Pixel 2");
84   SharingDeviceNames names = GetSharingDeviceNames(device.get());
85 
86   EXPECT_EQ("Google Phone Pixel 2", names.full_name);
87   EXPECT_EQ("Google Phone", names.short_name);
88 }
89 
TEST_F(SharingUtilsTest,GetSharingDeviceNames_AndroidTablets)90 TEST_F(SharingUtilsTest, GetSharingDeviceNames_AndroidTablets) {
91   std::unique_ptr<syncer::DeviceInfo> device = CreateFakeDeviceInfo(
92       "guid", "Pixel C", sync_pb::SyncEnums_DeviceType_TYPE_TABLET, "Google",
93       "Pixel C");
94   SharingDeviceNames names = GetSharingDeviceNames(device.get());
95 
96   EXPECT_EQ("Google Tablet Pixel C", names.full_name);
97   EXPECT_EQ("Google Tablet", names.short_name);
98 }
99 
TEST_F(SharingUtilsTest,GetSharingDeviceNames_Windows_SigninOnly)100 TEST_F(SharingUtilsTest, GetSharingDeviceNames_Windows_SigninOnly) {
101   std::unique_ptr<syncer::DeviceInfo> device = CreateFakeDeviceInfo(
102       "guid", "BX123", sync_pb::SyncEnums_DeviceType_TYPE_WIN, "Dell", "BX123");
103   SharingDeviceNames names = GetSharingDeviceNames(device.get());
104 
105   EXPECT_EQ("Dell Computer BX123", names.full_name);
106   EXPECT_EQ("Dell Computer", names.short_name);
107 }
108 
TEST_F(SharingUtilsTest,GetSharingDeviceNames_Windows_FullySynced)109 TEST_F(SharingUtilsTest, GetSharingDeviceNames_Windows_FullySynced) {
110   std::unique_ptr<syncer::DeviceInfo> device = CreateFakeDeviceInfo(
111       "guid", "BOBS-WINDOWS-1", sync_pb::SyncEnums_DeviceType_TYPE_WIN, "Dell",
112       "BX123");
113   SharingDeviceNames names = GetSharingDeviceNames(device.get());
114 
115   EXPECT_EQ("BOBS-WINDOWS-1", names.full_name);
116   EXPECT_EQ("BOBS-WINDOWS-1", names.short_name);
117 }
118 
TEST_F(SharingUtilsTest,GetSharingDeviceNames_Linux_SigninOnly)119 TEST_F(SharingUtilsTest, GetSharingDeviceNames_Linux_SigninOnly) {
120   std::unique_ptr<syncer::DeviceInfo> device = CreateFakeDeviceInfo(
121       "guid", "30BDS0RA0G", sync_pb::SyncEnums_DeviceType_TYPE_LINUX, "LENOVO",
122       "30BDS0RA0G");
123   SharingDeviceNames names = GetSharingDeviceNames(device.get());
124 
125   EXPECT_EQ("LENOVO Computer 30BDS0RA0G", names.full_name);
126   EXPECT_EQ("LENOVO Computer", names.short_name);
127 }
128 
TEST_F(SharingUtilsTest,GetSharingDeviceNames_Linux_FullySynced)129 TEST_F(SharingUtilsTest, GetSharingDeviceNames_Linux_FullySynced) {
130   std::unique_ptr<syncer::DeviceInfo> device = CreateFakeDeviceInfo(
131       "guid", "bob.chromium.org", sync_pb::SyncEnums_DeviceType_TYPE_LINUX,
132       "LENOVO", "30BDS0RA0G");
133   SharingDeviceNames names = GetSharingDeviceNames(device.get());
134 
135   EXPECT_EQ("bob.chromium.org", names.full_name);
136   EXPECT_EQ("bob.chromium.org", names.short_name);
137 }
138 
TEST_F(SharingUtilsTest,CheckManufacturerNameCapitalization)139 TEST_F(SharingUtilsTest, CheckManufacturerNameCapitalization) {
140   std::unique_ptr<syncer::DeviceInfo> device = CreateFakeDeviceInfo(
141       "guid", "model", sync_pb::SyncEnums_DeviceType_TYPE_WIN, "foo bar",
142       "model");
143   SharingDeviceNames names = GetSharingDeviceNames(device.get());
144 
145   EXPECT_EQ("Foo Bar Computer model", names.full_name);
146   EXPECT_EQ("Foo Bar Computer", names.short_name);
147 
148   device = CreateFakeDeviceInfo("guid", "model",
149                                 sync_pb::SyncEnums_DeviceType_TYPE_WIN,
150                                 "foo1bar", "model");
151   names = GetSharingDeviceNames(device.get());
152 
153   EXPECT_EQ("Foo1Bar Computer model", names.full_name);
154   EXPECT_EQ("Foo1Bar Computer", names.short_name);
155 
156   device = CreateFakeDeviceInfo("guid", "model",
157                                 sync_pb::SyncEnums_DeviceType_TYPE_WIN,
158                                 "foo_bar-FOO", "model");
159   names = GetSharingDeviceNames(device.get());
160 
161   EXPECT_EQ("Foo_Bar-FOO Computer model", names.full_name);
162   EXPECT_EQ("Foo_Bar-FOO Computer", names.short_name);
163 
164   device = CreateFakeDeviceInfo("guid", "model",
165                                 sync_pb::SyncEnums_DeviceType_TYPE_WIN,
166                                 "foo&bar foo", "model");
167   names = GetSharingDeviceNames(device.get());
168 
169   EXPECT_EQ("Foo&Bar Foo Computer model", names.full_name);
170   EXPECT_EQ("Foo&Bar Foo Computer", names.short_name);
171 }
172 
173 }  // namespace send_tab_to_self
174