Chromium Code Reviews| Index: chrome/browser/extensions/api/signedin_devices/signedin_devices_api_unittest.cc |
| diff --git a/chrome/browser/extensions/api/signedin_devices/signedin_devices_api_unittest.cc b/chrome/browser/extensions/api/signedin_devices/signedin_devices_api_unittest.cc |
| index ab21083c9325732f7427b211a22664af52bfd1e6..b5a71d2b03a95d39e806040e77e5191bbaedf40c 100644 |
| --- a/chrome/browser/extensions/api/signedin_devices/signedin_devices_api_unittest.cc |
| +++ b/chrome/browser/extensions/api/signedin_devices/signedin_devices_api_unittest.cc |
| @@ -8,12 +8,16 @@ |
| #include "base/guid.h" |
| #include "base/message_loop/message_loop.h" |
| +#include "base/values.h" |
| #include "chrome/browser/extensions/api/signedin_devices/signedin_devices_api.h" |
| +#include "chrome/browser/extensions/extension_function_test_utils.h" |
| #include "chrome/browser/extensions/test_extension_prefs.h" |
| #include "chrome/browser/sync/glue/device_info.h" |
| +#include "chrome/browser/sync/profile_sync_service_factory.h" |
| #include "chrome/browser/sync/profile_sync_service_mock.h" |
| #include "chrome/common/extensions/extension.h" |
| #include "chrome/common/extensions/extension_manifest_constants.h" |
| +#include "chrome/test/base/browser_with_test_window_test.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -24,6 +28,8 @@ using testing::Return; |
| namespace extensions { |
| +namespace utils = extension_function_test_utils; |
| + |
| TEST(SignedinDevicesAPITest, GetSignedInDevices) { |
| ProfileSyncServiceMock pss_mock; |
| base::MessageLoop message_loop_; |
| @@ -98,4 +104,119 @@ TEST(SignedinDevicesAPITest, GetSignedInDevices) { |
| output2.weak_clear(); |
| } |
| +BrowserContextKeyedService* CreateProfileSyncServiceMock( |
| + content::BrowserContext* profile) { |
| + return new ProfileSyncServiceMock(); |
| +} |
| + |
| +class ExtensionSignedinDevicesTest : public BrowserWithTestWindowTest { |
| + public: |
| + virtual void SetUp() { |
| + BrowserWithTestWindowTest::SetUp(); |
| + |
| + ProfileSyncServiceFactory::GetInstance()->SetTestingFactory( |
| + profile(), CreateProfileSyncServiceMock); |
| + |
| + extension_ = utils::CreateEmptyExtensionWithLocation( |
| + extensions::Manifest::UNPACKED); |
| + |
| + } |
| + |
| + base::Value* RunFunctionWithExtension( |
| + UIThreadExtensionFunction* function, |
| + const std::string& args) { |
| + scoped_refptr<UIThreadExtensionFunction> delete_function(function); |
| + function->set_extension(extension_.get()); |
| + return utils::RunFunctionAndReturnSingleResult(function, args, browser()); |
| + } |
| + |
| + base::ListValue* RunFunctionAndReturnList( |
| + UIThreadExtensionFunction* function, |
| + const std::string& args) { |
| + base::Value* result = RunFunctionWithExtension(function, args); |
| + return result ? utils::ToList(result) : NULL; |
| + } |
| + |
| + protected: |
| + scoped_refptr<extensions::Extension> extension_; |
| +}; |
| + |
| +DeviceInfo* CreateDeviceInfo(const DeviceInfo& device_info) { |
| + return new DeviceInfo(device_info.guid(), |
| + device_info.client_name(), |
| + device_info.chrome_version(), |
| + device_info.sync_user_agent(), |
| + device_info.device_type()); |
| +} |
| + |
| +std::string GetPublicId(const base::DictionaryValue* dictionary) { |
| + std::string public_id; |
| + if (!dictionary->GetString("id", &public_id)) { |
| + ADD_FAILURE() << "Not able to find public id in the dictionary"; |
| + } |
| + |
| + return public_id; |
| +} |
| + |
| +void VerifyDictionaryWithDeviceInfo(const base::DictionaryValue* actual_value, |
| + DeviceInfo* device_info) { |
| + std::string public_id = GetPublicId(actual_value); |
| + device_info->set_public_id(public_id); |
| + |
| + scoped_ptr<base::DictionaryValue> expected_value(device_info->ToValue()); |
| + EXPECT_TRUE(expected_value->Equals(actual_value)); |
| +} |
| + |
| +base::DictionaryValue* GetDictionaryFromList(int index, |
| + base::ListValue* value) { |
| + base::DictionaryValue* dictionary; |
| + if (!value->GetDictionary(index, &dictionary)) { |
| + ADD_FAILURE() << "Expected a list of dictionaries"; |
| + return NULL; |
| + } |
| + return dictionary; |
| +} |
| + |
| +TEST_F(ExtensionSignedinDevicesTest, GetAll) { |
| + ProfileSyncServiceMock* pss_mock = |
| + (ProfileSyncServiceMock*) |
|
Matt Perry
2013/08/12 22:21:01
don't use C-style casts
lipalani1
2013/08/13 22:42:37
Done.
|
| + ProfileSyncServiceFactory::GetForProfile(profile()); |
| + |
| + DeviceInfo device_info1( |
| + base::GenerateGUID(), |
| + "abc Device", "XYZ v1", "XYZ SyncAgent v1", |
| + sync_pb::SyncEnums_DeviceType_TYPE_LINUX); |
| + |
| + DeviceInfo device_info2( |
| + base::GenerateGUID(), |
| + "def Device", "XYZ v2", "XYZ SyncAgent v2", |
| + sync_pb::SyncEnums_DeviceType_TYPE_LINUX); |
| + |
| + std::vector<DeviceInfo*> devices; |
| + devices.push_back(CreateDeviceInfo(device_info1)); |
| + devices.push_back(CreateDeviceInfo(device_info2)); |
| + |
| + EXPECT_CALL(*pss_mock, GetAllSignedInDevicesMock()). |
| + WillOnce(Return(&devices)); |
| + |
| + EXPECT_CALL(*pss_mock, Shutdown()); |
| + |
| + scoped_ptr<base::ListValue> result(RunFunctionAndReturnList( |
| + new SignedinDevicesGetFunction(), "[null]")); |
| + |
| + // Ensure dictionary matches device info. |
| + VerifyDictionaryWithDeviceInfo(GetDictionaryFromList(0, result.get()), |
| + &device_info1); |
| + VerifyDictionaryWithDeviceInfo(GetDictionaryFromList(1, result.get()), |
| + &device_info2); |
| + |
| + // Ensure public ids are set and unique. |
| + std::string public_id1 = GetPublicId(GetDictionaryFromList(0, result.get())); |
| + std::string public_id2 = GetPublicId(GetDictionaryFromList(1, result.get())); |
| + |
| + EXPECT_FALSE(public_id1.empty()); |
| + EXPECT_FALSE(public_id2.empty()); |
| + EXPECT_NE(public_id1, public_id2); |
| +} |
| + |
| } // namespace extensions |