Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / signed_in_devices / id_mapping_helper_unittest.cc
blob7a4bb6f73eeb2f7e3db80f9d4740a0c8f4f8d5ed
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
3 // found in the LICENSE file.
5 #include <string>
7 #include "base/guid.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/values.h"
11 #include "chrome/browser/extensions/api/signed_in_devices/id_mapping_helper.h"
12 #include "components/sync_driver/device_info.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using sync_driver::DeviceInfo;
18 namespace extensions {
19 bool VerifyDictionary(
20 const std::string& path,
21 const std::string& expected_value,
22 const base::DictionaryValue& dictionary) {
23 std::string out;
24 if (dictionary.GetString(path, &out)) {
25 return (out == expected_value);
28 return false;
31 TEST(IdMappingHelperTest, SetIdsForDevices) {
32 ScopedVector<DeviceInfo> devices;
34 devices.push_back(new DeviceInfo(base::GenerateGUID(),
35 "abc Device",
36 "XYZ v1",
37 "XYZ SyncAgent v1",
38 sync_pb::SyncEnums_DeviceType_TYPE_LINUX,
39 "device_id1"));
41 devices.push_back(new DeviceInfo(base::GenerateGUID(),
42 "def Device",
43 "XYZ v1",
44 "XYZ SyncAgent v1",
45 sync_pb::SyncEnums_DeviceType_TYPE_LINUX,
46 "device_id2"));
48 base::DictionaryValue dictionary;
50 CreateMappingForUnmappedDevices(&(devices.get()), &dictionary);
52 std::string public_id1 = devices[0]->public_id();
53 std::string public_id2 = devices[1]->public_id();
55 EXPECT_FALSE(public_id1.empty());
56 EXPECT_FALSE(public_id2.empty());
58 EXPECT_NE(public_id1, public_id2);
60 // Now add a third device.
61 devices.push_back(new DeviceInfo(base::GenerateGUID(),
62 "ghi Device",
63 "XYZ v1",
64 "XYZ SyncAgent v1",
65 sync_pb::SyncEnums_DeviceType_TYPE_LINUX,
66 "device_id3"));
68 CreateMappingForUnmappedDevices(&(devices.get()), &dictionary);
70 // Now make sure the existing ids are not changed.
71 EXPECT_EQ(public_id1, devices[0]->public_id());
72 EXPECT_EQ(public_id2, devices[1]->public_id());
74 // Now make sure the id for third device is non empty and different.
75 std::string public_id3 = devices[2]->public_id();
76 EXPECT_FALSE(public_id3.empty());
77 EXPECT_NE(public_id3, public_id1);
78 EXPECT_NE(public_id3, public_id2);
80 // Verify the dictionary.
81 EXPECT_TRUE(VerifyDictionary(public_id1, devices[0]->guid(), dictionary));
82 EXPECT_TRUE(VerifyDictionary(public_id2, devices[1]->guid(), dictionary));
83 EXPECT_TRUE(VerifyDictionary(public_id3, devices[2]->guid(), dictionary));
85 EXPECT_EQ(dictionary.size(), 3U);
87 } // namespace extensions