OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/signed_in_devices/id_mapping_helper.h" | 5 #include "chrome/browser/extensions/api/signed_in_devices/id_mapping_helper.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/guid.h" | 10 #include "base/guid.h" |
11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "components/sync/device_info/device_info.h" | 13 #include "components/sync/device_info/device_info.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 using sync_driver::DeviceInfo; | 17 using sync_driver::DeviceInfo; |
18 | 18 |
19 namespace extensions { | 19 namespace extensions { |
20 bool VerifyDictionary( | 20 bool VerifyDictionary( |
21 const std::string& path, | 21 const std::string& path, |
22 const std::string& expected_value, | 22 const std::string& expected_value, |
23 const base::DictionaryValue& dictionary) { | 23 const base::DictionaryValue& dictionary) { |
24 std::string out; | 24 std::string out; |
25 if (dictionary.GetString(path, &out)) { | 25 if (dictionary.GetString(path, &out)) { |
26 return (out == expected_value); | 26 return (out == expected_value); |
27 } | 27 } |
28 | 28 |
29 return false; | 29 return false; |
30 } | 30 } |
31 | 31 |
32 TEST(IdMappingHelperTest, SetIdsForDevices) { | 32 TEST(IdMappingHelperTest, SetIdsForDevices) { |
33 ScopedVector<DeviceInfo> devices; | 33 std::vector<std::unique_ptr<DeviceInfo>> devices; |
34 | 34 |
35 devices.push_back(new DeviceInfo(base::GenerateGUID(), | 35 devices.push_back(base::MakeUnique<DeviceInfo>( |
36 "abc Device", | 36 base::GenerateGUID(), "abc Device", "XYZ v1", "XYZ SyncAgent v1", |
37 "XYZ v1", | 37 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, "device_id1")); |
38 "XYZ SyncAgent v1", | |
39 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, | |
40 "device_id1")); | |
41 | 38 |
42 devices.push_back(new DeviceInfo(base::GenerateGUID(), | 39 devices.push_back(base::MakeUnique<DeviceInfo>( |
43 "def Device", | 40 base::GenerateGUID(), "def Device", "XYZ v1", "XYZ SyncAgent v1", |
44 "XYZ v1", | 41 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, "device_id2")); |
45 "XYZ SyncAgent v1", | |
46 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, | |
47 "device_id2")); | |
48 | 42 |
49 base::DictionaryValue dictionary; | 43 base::DictionaryValue dictionary; |
50 | 44 |
51 CreateMappingForUnmappedDevices(&(devices.get()), &dictionary); | 45 CreateMappingForUnmappedDevices(devices, &dictionary); |
52 | 46 |
53 std::string public_id1 = devices[0]->public_id(); | 47 std::string public_id1 = devices[0]->public_id(); |
54 std::string public_id2 = devices[1]->public_id(); | 48 std::string public_id2 = devices[1]->public_id(); |
55 | 49 |
56 EXPECT_FALSE(public_id1.empty()); | 50 EXPECT_FALSE(public_id1.empty()); |
57 EXPECT_FALSE(public_id2.empty()); | 51 EXPECT_FALSE(public_id2.empty()); |
58 | 52 |
59 EXPECT_NE(public_id1, public_id2); | 53 EXPECT_NE(public_id1, public_id2); |
60 | 54 |
61 // Now add a third device. | 55 // Now add a third device. |
62 devices.push_back(new DeviceInfo(base::GenerateGUID(), | 56 devices.push_back(base::MakeUnique<DeviceInfo>( |
63 "ghi Device", | 57 base::GenerateGUID(), "ghi Device", "XYZ v1", "XYZ SyncAgent v1", |
64 "XYZ v1", | 58 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, "device_id3")); |
65 "XYZ SyncAgent v1", | |
66 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, | |
67 "device_id3")); | |
68 | 59 |
69 CreateMappingForUnmappedDevices(&(devices.get()), &dictionary); | 60 CreateMappingForUnmappedDevices(devices, &dictionary); |
70 | 61 |
71 // Now make sure the existing ids are not changed. | 62 // Now make sure the existing ids are not changed. |
72 EXPECT_EQ(public_id1, devices[0]->public_id()); | 63 EXPECT_EQ(public_id1, devices[0]->public_id()); |
73 EXPECT_EQ(public_id2, devices[1]->public_id()); | 64 EXPECT_EQ(public_id2, devices[1]->public_id()); |
74 | 65 |
75 // Now make sure the id for third device is non empty and different. | 66 // Now make sure the id for third device is non empty and different. |
76 std::string public_id3 = devices[2]->public_id(); | 67 std::string public_id3 = devices[2]->public_id(); |
77 EXPECT_FALSE(public_id3.empty()); | 68 EXPECT_FALSE(public_id3.empty()); |
78 EXPECT_NE(public_id3, public_id1); | 69 EXPECT_NE(public_id3, public_id1); |
79 EXPECT_NE(public_id3, public_id2); | 70 EXPECT_NE(public_id3, public_id2); |
80 | 71 |
81 // Verify the dictionary. | 72 // Verify the dictionary. |
82 EXPECT_TRUE(VerifyDictionary(public_id1, devices[0]->guid(), dictionary)); | 73 EXPECT_TRUE(VerifyDictionary(public_id1, devices[0]->guid(), dictionary)); |
83 EXPECT_TRUE(VerifyDictionary(public_id2, devices[1]->guid(), dictionary)); | 74 EXPECT_TRUE(VerifyDictionary(public_id2, devices[1]->guid(), dictionary)); |
84 EXPECT_TRUE(VerifyDictionary(public_id3, devices[2]->guid(), dictionary)); | 75 EXPECT_TRUE(VerifyDictionary(public_id3, devices[2]->guid(), dictionary)); |
85 | 76 |
86 EXPECT_EQ(dictionary.size(), 3U); | 77 EXPECT_EQ(dictionary.size(), 3U); |
87 } | 78 } |
88 } // namespace extensions | 79 } // namespace extensions |
OLD | NEW |