Chromium Code Reviews| 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/guid.h" | 9 #include "base/guid.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/values.h" | |
| 11 #include "chrome/browser/extensions/api/signedin_devices/signedin_devices_api.h" | 12 #include "chrome/browser/extensions/api/signedin_devices/signedin_devices_api.h" |
| 13 #include "chrome/browser/extensions/extension_function_test_utils.h" | |
| 12 #include "chrome/browser/extensions/test_extension_prefs.h" | 14 #include "chrome/browser/extensions/test_extension_prefs.h" |
| 13 #include "chrome/browser/sync/glue/device_info.h" | 15 #include "chrome/browser/sync/glue/device_info.h" |
| 16 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
| 14 #include "chrome/browser/sync/profile_sync_service_mock.h" | 17 #include "chrome/browser/sync/profile_sync_service_mock.h" |
| 15 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
| 16 #include "chrome/common/extensions/extension_manifest_constants.h" | 19 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 20 #include "chrome/test/base/browser_with_test_window_test.h" | |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 23 |
| 20 using extensions::Extension; | 24 using extensions::Extension; |
| 21 using extensions::TestExtensionPrefs; | 25 using extensions::TestExtensionPrefs; |
| 22 using browser_sync::DeviceInfo; | 26 using browser_sync::DeviceInfo; |
| 23 using testing::Return; | 27 using testing::Return; |
| 24 | 28 |
| 25 namespace extensions { | 29 namespace extensions { |
| 26 | 30 |
| 31 namespace utils = extension_function_test_utils; | |
| 32 | |
| 27 TEST(SignedinDevicesAPITest, GetSignedInDevices) { | 33 TEST(SignedinDevicesAPITest, GetSignedInDevices) { |
| 28 ProfileSyncServiceMock pss_mock; | 34 ProfileSyncServiceMock pss_mock; |
| 29 base::MessageLoop message_loop_; | 35 base::MessageLoop message_loop_; |
| 30 TestExtensionPrefs extension_prefs( | 36 TestExtensionPrefs extension_prefs( |
| 31 message_loop_.message_loop_proxy().get()); | 37 message_loop_.message_loop_proxy().get()); |
| 32 | 38 |
| 33 // Add a couple of devices and make sure we get back public ids for them. | 39 // Add a couple of devices and make sure we get back public ids for them. |
| 34 std::string extension_name = "test"; | 40 std::string extension_name = "test"; |
| 35 scoped_refptr<Extension> extension_test = | 41 scoped_refptr<Extension> extension_test = |
| 36 extension_prefs.AddExtension(extension_name); | 42 extension_prefs.AddExtension(extension_name); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 std::string public_id3 = device_info3.public_id(); | 97 std::string public_id3 = device_info3.public_id(); |
| 92 EXPECT_FALSE(public_id3.empty()); | 98 EXPECT_FALSE(public_id3.empty()); |
| 93 EXPECT_NE(public_id3, public_id1); | 99 EXPECT_NE(public_id3, public_id1); |
| 94 EXPECT_NE(public_id3, public_id2); | 100 EXPECT_NE(public_id3, public_id2); |
| 95 | 101 |
| 96 // Now clear output2 so that its destructor does not destroy the | 102 // Now clear output2 so that its destructor does not destroy the |
| 97 // |DeviceInfo| pointers. | 103 // |DeviceInfo| pointers. |
| 98 output2.weak_clear(); | 104 output2.weak_clear(); |
| 99 } | 105 } |
| 100 | 106 |
| 107 BrowserContextKeyedService* CreateProfileSyncServiceMock( | |
| 108 content::BrowserContext* profile) { | |
| 109 return new ProfileSyncServiceMock(); | |
| 110 } | |
| 111 | |
| 112 class ExtensionSignedinDevicesTest : public BrowserWithTestWindowTest { | |
| 113 public: | |
| 114 virtual void SetUp() { | |
| 115 BrowserWithTestWindowTest::SetUp(); | |
| 116 | |
| 117 ProfileSyncServiceFactory::GetInstance()->SetTestingFactory( | |
| 118 profile(), CreateProfileSyncServiceMock); | |
| 119 | |
| 120 extension_ = utils::CreateEmptyExtensionWithLocation( | |
| 121 extensions::Manifest::UNPACKED); | |
| 122 | |
| 123 } | |
| 124 | |
| 125 base::Value* RunFunctionWithExtension( | |
| 126 UIThreadExtensionFunction* function, | |
| 127 const std::string& args) { | |
| 128 scoped_refptr<UIThreadExtensionFunction> delete_function(function); | |
| 129 function->set_extension(extension_.get()); | |
| 130 return utils::RunFunctionAndReturnSingleResult(function, args, browser()); | |
| 131 } | |
| 132 | |
| 133 base::ListValue* RunFunctionAndReturnList( | |
| 134 UIThreadExtensionFunction* function, | |
| 135 const std::string& args) { | |
| 136 base::Value* result = RunFunctionWithExtension(function, args); | |
| 137 return result ? utils::ToList(result) : NULL; | |
| 138 } | |
| 139 | |
| 140 protected: | |
| 141 scoped_refptr<extensions::Extension> extension_; | |
| 142 }; | |
| 143 | |
| 144 DeviceInfo* CreateDeviceInfo(const DeviceInfo& device_info) { | |
| 145 return new DeviceInfo(device_info.guid(), | |
| 146 device_info.client_name(), | |
| 147 device_info.chrome_version(), | |
| 148 device_info.sync_user_agent(), | |
| 149 device_info.device_type()); | |
| 150 } | |
| 151 | |
| 152 std::string GetPublicId(const base::DictionaryValue* dictionary) { | |
| 153 std::string public_id; | |
| 154 if (!dictionary->GetString("id", &public_id)) { | |
| 155 ADD_FAILURE() << "Not able to find public id in the dictionary"; | |
| 156 } | |
| 157 | |
| 158 return public_id; | |
| 159 } | |
| 160 | |
| 161 void VerifyDictionaryWithDeviceInfo(const base::DictionaryValue* actual_value, | |
| 162 DeviceInfo* device_info) { | |
| 163 std::string public_id = GetPublicId(actual_value); | |
| 164 device_info->set_public_id(public_id); | |
| 165 | |
| 166 scoped_ptr<base::DictionaryValue> expected_value(device_info->ToValue()); | |
| 167 EXPECT_TRUE(expected_value->Equals(actual_value)); | |
| 168 } | |
| 169 | |
| 170 base::DictionaryValue* GetDictionaryFromList(int index, | |
| 171 base::ListValue* value) { | |
| 172 base::DictionaryValue* dictionary; | |
| 173 if (!value->GetDictionary(index, &dictionary)) { | |
| 174 ADD_FAILURE() << "Expected a list of dictionaries"; | |
| 175 return NULL; | |
| 176 } | |
| 177 return dictionary; | |
| 178 } | |
| 179 | |
| 180 TEST_F(ExtensionSignedinDevicesTest, GetAll) { | |
| 181 ProfileSyncServiceMock* pss_mock = | |
| 182 reinterpret_cast<ProfileSyncServiceMock*>( | |
|
Matt Perry
2013/08/13 23:01:38
use static_cast for casting between members of a c
lipalani1
2013/08/13 23:36:16
Done.
| |
| 183 ProfileSyncServiceFactory::GetForProfile(profile())); | |
| 184 | |
| 185 DeviceInfo device_info1( | |
| 186 base::GenerateGUID(), | |
| 187 "abc Device", "XYZ v1", "XYZ SyncAgent v1", | |
| 188 sync_pb::SyncEnums_DeviceType_TYPE_LINUX); | |
| 189 | |
| 190 DeviceInfo device_info2( | |
| 191 base::GenerateGUID(), | |
| 192 "def Device", "XYZ v2", "XYZ SyncAgent v2", | |
| 193 sync_pb::SyncEnums_DeviceType_TYPE_LINUX); | |
| 194 | |
| 195 std::vector<DeviceInfo*> devices; | |
| 196 devices.push_back(CreateDeviceInfo(device_info1)); | |
| 197 devices.push_back(CreateDeviceInfo(device_info2)); | |
| 198 | |
| 199 EXPECT_CALL(*pss_mock, GetAllSignedInDevicesMock()). | |
| 200 WillOnce(Return(&devices)); | |
| 201 | |
| 202 EXPECT_CALL(*pss_mock, Shutdown()); | |
| 203 | |
| 204 scoped_ptr<base::ListValue> result(RunFunctionAndReturnList( | |
| 205 new SignedinDevicesGetFunction(), "[null]")); | |
| 206 | |
| 207 // Ensure dictionary matches device info. | |
| 208 VerifyDictionaryWithDeviceInfo(GetDictionaryFromList(0, result.get()), | |
| 209 &device_info1); | |
| 210 VerifyDictionaryWithDeviceInfo(GetDictionaryFromList(1, result.get()), | |
| 211 &device_info2); | |
| 212 | |
| 213 // Ensure public ids are set and unique. | |
| 214 std::string public_id1 = GetPublicId(GetDictionaryFromList(0, result.get())); | |
| 215 std::string public_id2 = GetPublicId(GetDictionaryFromList(1, result.get())); | |
| 216 | |
| 217 EXPECT_FALSE(public_id1.empty()); | |
| 218 EXPECT_FALSE(public_id2.empty()); | |
| 219 EXPECT_NE(public_id1, public_id2); | |
| 220 } | |
| 221 | |
| 101 } // namespace extensions | 222 } // namespace extensions |
| OLD | NEW |