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/signed_in_devices_api.
h" |
| 6 |
| 7 #include <memory> |
5 #include <string> | 8 #include <string> |
6 | |
7 #include <vector> | 9 #include <vector> |
8 | 10 |
9 #include "base/guid.h" | 11 #include "base/guid.h" |
10 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/ptr_util.h" |
11 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
12 #include "base/values.h" | 14 #include "base/values.h" |
13 #include "chrome/browser/extensions/api/signed_in_devices/signed_in_devices_api.
h" | |
14 #include "chrome/browser/extensions/extension_api_unittest.h" | 15 #include "chrome/browser/extensions/extension_api_unittest.h" |
15 #include "chrome/browser/extensions/test_extension_prefs.h" | 16 #include "chrome/browser/extensions/test_extension_prefs.h" |
16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/sync/profile_sync_service_factory.h" | 18 #include "chrome/browser/sync/profile_sync_service_factory.h" |
18 #include "chrome/browser/sync/profile_sync_test_util.h" | 19 #include "chrome/browser/sync/profile_sync_test_util.h" |
19 #include "components/browser_sync/browser/profile_sync_service_mock.h" | 20 #include "components/browser_sync/browser/profile_sync_service_mock.h" |
20 #include "components/prefs/pref_service.h" | 21 #include "components/prefs/pref_service.h" |
21 #include "components/sync_driver/device_info.h" | 22 #include "components/sync_driver/device_info.h" |
22 #include "components/sync_driver/device_info_tracker.h" | 23 #include "components/sync_driver/device_info_tracker.h" |
23 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
24 #include "extensions/common/extension.h" | 25 #include "extensions/common/extension.h" |
25 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
27 | 28 |
28 using sync_driver::DeviceInfo; | 29 using sync_driver::DeviceInfo; |
29 using sync_driver::DeviceInfoTracker; | 30 using sync_driver::DeviceInfoTracker; |
30 using testing::Return; | 31 using testing::Return; |
31 | 32 |
32 namespace extensions { | 33 namespace extensions { |
33 | 34 |
34 class MockDeviceInfoTracker : public DeviceInfoTracker { | 35 class MockDeviceInfoTracker : public DeviceInfoTracker { |
35 public: | 36 public: |
36 ~MockDeviceInfoTracker() override {} | 37 ~MockDeviceInfoTracker() override {} |
37 | 38 |
38 bool IsSyncing() const override { return !devices_.empty(); } | 39 bool IsSyncing() const override { return !devices_.empty(); } |
39 | 40 |
40 scoped_ptr<DeviceInfo> GetDeviceInfo( | 41 std::unique_ptr<DeviceInfo> GetDeviceInfo( |
41 const std::string& client_id) const override { | 42 const std::string& client_id) const override { |
42 NOTREACHED(); | 43 NOTREACHED(); |
43 return scoped_ptr<DeviceInfo>(); | 44 return std::unique_ptr<DeviceInfo>(); |
44 } | 45 } |
45 | 46 |
46 static DeviceInfo* CloneDeviceInfo(const DeviceInfo* device_info) { | 47 static DeviceInfo* CloneDeviceInfo(const DeviceInfo* device_info) { |
47 return new DeviceInfo(device_info->guid(), | 48 return new DeviceInfo(device_info->guid(), |
48 device_info->client_name(), | 49 device_info->client_name(), |
49 device_info->chrome_version(), | 50 device_info->chrome_version(), |
50 device_info->sync_user_agent(), | 51 device_info->sync_user_agent(), |
51 device_info->device_type(), | 52 device_info->device_type(), |
52 device_info->signin_scoped_device_id()); | 53 device_info->signin_scoped_device_id()); |
53 } | 54 } |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 public ProfileSyncServiceMock { | 146 public ProfileSyncServiceMock { |
146 public: | 147 public: |
147 explicit ProfileSyncServiceMockForExtensionTests(Profile* p) | 148 explicit ProfileSyncServiceMockForExtensionTests(Profile* p) |
148 : ProfileSyncServiceMock(CreateProfileSyncServiceParamsForTest(p)) {} | 149 : ProfileSyncServiceMock(CreateProfileSyncServiceParamsForTest(p)) {} |
149 ~ProfileSyncServiceMockForExtensionTests() {} | 150 ~ProfileSyncServiceMockForExtensionTests() {} |
150 | 151 |
151 MOCK_METHOD0(Shutdown, void()); | 152 MOCK_METHOD0(Shutdown, void()); |
152 MOCK_CONST_METHOD0(GetDeviceInfoTracker, DeviceInfoTracker*()); | 153 MOCK_CONST_METHOD0(GetDeviceInfoTracker, DeviceInfoTracker*()); |
153 }; | 154 }; |
154 | 155 |
155 scoped_ptr<KeyedService> CreateProfileSyncServiceMock( | 156 std::unique_ptr<KeyedService> CreateProfileSyncServiceMock( |
156 content::BrowserContext* context) { | 157 content::BrowserContext* context) { |
157 return make_scoped_ptr(new ProfileSyncServiceMockForExtensionTests( | 158 return base::WrapUnique(new ProfileSyncServiceMockForExtensionTests( |
158 Profile::FromBrowserContext(context))); | 159 Profile::FromBrowserContext(context))); |
159 } | 160 } |
160 | 161 |
161 class ExtensionSignedInDevicesTest : public ExtensionApiUnittest { | 162 class ExtensionSignedInDevicesTest : public ExtensionApiUnittest { |
162 public: | 163 public: |
163 void SetUp() override { | 164 void SetUp() override { |
164 ExtensionApiUnittest::SetUp(); | 165 ExtensionApiUnittest::SetUp(); |
165 | 166 |
166 ProfileSyncServiceFactory::GetInstance()->SetTestingFactory( | 167 ProfileSyncServiceFactory::GetInstance()->SetTestingFactory( |
167 profile(), CreateProfileSyncServiceMock); | 168 profile(), CreateProfileSyncServiceMock); |
168 } | 169 } |
169 }; | 170 }; |
170 | 171 |
171 std::string GetPublicId(const base::DictionaryValue* dictionary) { | 172 std::string GetPublicId(const base::DictionaryValue* dictionary) { |
172 std::string public_id; | 173 std::string public_id; |
173 if (!dictionary->GetString("id", &public_id)) { | 174 if (!dictionary->GetString("id", &public_id)) { |
174 ADD_FAILURE() << "Not able to find public id in the dictionary"; | 175 ADD_FAILURE() << "Not able to find public id in the dictionary"; |
175 } | 176 } |
176 | 177 |
177 return public_id; | 178 return public_id; |
178 } | 179 } |
179 | 180 |
180 void VerifyDictionaryWithDeviceInfo(const base::DictionaryValue* actual_value, | 181 void VerifyDictionaryWithDeviceInfo(const base::DictionaryValue* actual_value, |
181 DeviceInfo* device_info) { | 182 DeviceInfo* device_info) { |
182 std::string public_id = GetPublicId(actual_value); | 183 std::string public_id = GetPublicId(actual_value); |
183 device_info->set_public_id(public_id); | 184 device_info->set_public_id(public_id); |
184 | 185 |
185 scoped_ptr<base::DictionaryValue> expected_value(device_info->ToValue()); | 186 std::unique_ptr<base::DictionaryValue> expected_value(device_info->ToValue()); |
186 EXPECT_TRUE(expected_value->Equals(actual_value)); | 187 EXPECT_TRUE(expected_value->Equals(actual_value)); |
187 } | 188 } |
188 | 189 |
189 base::DictionaryValue* GetDictionaryFromList(int index, | 190 base::DictionaryValue* GetDictionaryFromList(int index, |
190 base::ListValue* value) { | 191 base::ListValue* value) { |
191 base::DictionaryValue* dictionary; | 192 base::DictionaryValue* dictionary; |
192 if (!value->GetDictionary(index, &dictionary)) { | 193 if (!value->GetDictionary(index, &dictionary)) { |
193 ADD_FAILURE() << "Expected a list of dictionaries"; | 194 ADD_FAILURE() << "Expected a list of dictionaries"; |
194 return NULL; | 195 return NULL; |
195 } | 196 } |
(...skipping 21 matching lines...) Expand all Loading... |
217 "device_id"); | 218 "device_id"); |
218 | 219 |
219 device_tracker.Add(&device_info1); | 220 device_tracker.Add(&device_info1); |
220 device_tracker.Add(&device_info2); | 221 device_tracker.Add(&device_info2); |
221 | 222 |
222 EXPECT_CALL(*pss_mock, GetDeviceInfoTracker()) | 223 EXPECT_CALL(*pss_mock, GetDeviceInfoTracker()) |
223 .WillOnce(Return(&device_tracker)); | 224 .WillOnce(Return(&device_tracker)); |
224 | 225 |
225 EXPECT_CALL(*pss_mock, Shutdown()); | 226 EXPECT_CALL(*pss_mock, Shutdown()); |
226 | 227 |
227 scoped_ptr<base::ListValue> result(RunFunctionAndReturnList( | 228 std::unique_ptr<base::ListValue> result( |
228 new SignedInDevicesGetFunction(), "[null]")); | 229 RunFunctionAndReturnList(new SignedInDevicesGetFunction(), "[null]")); |
229 | 230 |
230 // Ensure dictionary matches device info. | 231 // Ensure dictionary matches device info. |
231 VerifyDictionaryWithDeviceInfo(GetDictionaryFromList(0, result.get()), | 232 VerifyDictionaryWithDeviceInfo(GetDictionaryFromList(0, result.get()), |
232 &device_info1); | 233 &device_info1); |
233 VerifyDictionaryWithDeviceInfo(GetDictionaryFromList(1, result.get()), | 234 VerifyDictionaryWithDeviceInfo(GetDictionaryFromList(1, result.get()), |
234 &device_info2); | 235 &device_info2); |
235 | 236 |
236 // Ensure public ids are set and unique. | 237 // Ensure public ids are set and unique. |
237 std::string public_id1 = GetPublicId(GetDictionaryFromList(0, result.get())); | 238 std::string public_id1 = GetPublicId(GetDictionaryFromList(0, result.get())); |
238 std::string public_id2 = GetPublicId(GetDictionaryFromList(1, result.get())); | 239 std::string public_id2 = GetPublicId(GetDictionaryFromList(1, result.get())); |
(...skipping 14 matching lines...) Expand all Loading... |
253 .WillOnce(Return(&device_tracker)); | 254 .WillOnce(Return(&device_tracker)); |
254 EXPECT_CALL(*pss_mock, Shutdown()); | 255 EXPECT_CALL(*pss_mock, Shutdown()); |
255 | 256 |
256 ScopedVector<DeviceInfo> output = GetAllSignedInDevices( | 257 ScopedVector<DeviceInfo> output = GetAllSignedInDevices( |
257 extension()->id(), profile()); | 258 extension()->id(), profile()); |
258 | 259 |
259 EXPECT_TRUE(output.empty()); | 260 EXPECT_TRUE(output.empty()); |
260 } | 261 } |
261 | 262 |
262 } // namespace extensions | 263 } // namespace extensions |
OLD | NEW |