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. 4 5 #include <string> 6 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/signedin_devices/id_mapping_helper.h" 12 #include "chrome/browser/sync/glue/device_info.h" 13 #include "testing/gmock/include/gmock/gmock.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 using browser_sync::DeviceInfo; 17 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); 26 } 27 28 return false; 29 } 30 31 TEST(IdMappingHelperTest, SetIdsForDevices) { 32 ScopedVector<DeviceInfo> devices; 33 34 devices.push_back(new DeviceInfo( 35 base::GenerateGUID(), "abc Device", "XYZ v1", "XYZ SyncAgent v1", 36 sync_pb::SyncEnums_DeviceType_TYPE_LINUX)); 37 38 devices.push_back(new DeviceInfo( 39 base::GenerateGUID(), "def Device", "XYZ v1", "XYZ SyncAgent v1", 40 sync_pb::SyncEnums_DeviceType_TYPE_LINUX)); 41 42 base::DictionaryValue dictionary; 43 44 CreateMappingForUnmappedDevices(&(devices.get()), &dictionary); 45 46 std::string public_id1 = devices[0]->public_id(); 47 std::string public_id2 = devices[1]->public_id(); 48 49 EXPECT_FALSE(public_id1.empty()); 50 EXPECT_FALSE(public_id2.empty()); 51 52 EXPECT_NE(public_id1, public_id2); 53 54 // Now add a third device. 55 devices.push_back(new DeviceInfo( 56 base::GenerateGUID(), "ghi Device", "XYZ v1", "XYZ SyncAgent v1", 57 sync_pb::SyncEnums_DeviceType_TYPE_LINUX)); 58 59 CreateMappingForUnmappedDevices(&(devices.get()), &dictionary); 60 61 // Now make sure the existing ids are not changed. 62 EXPECT_EQ(public_id1, devices[0]->public_id()); 63 EXPECT_EQ(public_id2, devices[1]->public_id()); 64 65 // Now make sure the id for third device is non empty and different. 66 std::string public_id3 = devices[2]->public_id(); 67 EXPECT_FALSE(public_id3.empty()); 68 EXPECT_NE(public_id3, public_id1); 69 EXPECT_NE(public_id3, public_id2); 70 71 // Verify the dictionary. 72 EXPECT_TRUE(VerifyDictionary(public_id1, devices[0]->guid(), dictionary)); 73 EXPECT_TRUE(VerifyDictionary(public_id2, devices[1]->guid(), dictionary)); 74 EXPECT_TRUE(VerifyDictionary(public_id3, devices[2]->guid(), dictionary)); 75 76 EXPECT_EQ(dictionary.size(), 3U); 77 } 78 } // namespace extensions 79