1 // Copyright 2014 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 "base/bind.h" 6 #include "base/message_loop/message_loop.h" 7 #include "base/run_loop.h" 8 #include "chrome/browser/sync/glue/local_device_info_provider_impl.h" 9 #include "chrome/common/chrome_version_info.h" 10 #include "sync/util/get_session_name.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 13 using sync_driver::DeviceInfo; 14 using sync_driver::LocalDeviceInfoProvider; 15 16 namespace browser_sync { 17 18 const char kLocalDeviceGuid[] = "foo"; 19 const char kSigninScopedDeviceId[] = "device_id"; 20 21 class LocalDeviceInfoProviderTest : public testing::Test { 22 public: 23 LocalDeviceInfoProviderTest() 24 : called_back_(false) {} 25 virtual ~LocalDeviceInfoProviderTest() {} 26 27 virtual void SetUp() OVERRIDE { 28 provider_.reset(new LocalDeviceInfoProviderImpl()); 29 } 30 31 virtual void TearDown() OVERRIDE { 32 provider_.reset(); 33 called_back_ = false; 34 } 35 36 protected: 37 void InitializeProvider() { 38 // Start initialization. 39 provider_->Initialize(kLocalDeviceGuid, kSigninScopedDeviceId); 40 41 // Subscribe to the notification and wait until the callback 42 // is called. The callback will quit the loop. 43 base::RunLoop run_loop; 44 scoped_ptr<LocalDeviceInfoProvider::Subscription> subscription( 45 provider_->RegisterOnInitializedCallback( 46 base::Bind(&LocalDeviceInfoProviderTest::QuitLoopOnInitialized, 47 base::Unretained(this), &run_loop))); 48 run_loop.Run(); 49 } 50 51 void QuitLoopOnInitialized(base::RunLoop* loop) { 52 called_back_ = true; 53 loop->Quit(); 54 } 55 56 scoped_ptr<LocalDeviceInfoProviderImpl> provider_; 57 58 bool called_back_; 59 60 private: 61 base::MessageLoop message_loop_; 62 }; 63 64 TEST_F(LocalDeviceInfoProviderTest, OnInitializedCallback) { 65 ASSERT_FALSE(called_back_); 66 67 InitializeProvider(); 68 EXPECT_TRUE(called_back_); 69 } 70 71 TEST_F(LocalDeviceInfoProviderTest, GetLocalDeviceInfo) { 72 ASSERT_EQ(NULL, provider_->GetLocalDeviceInfo()); 73 74 InitializeProvider(); 75 76 const DeviceInfo* local_device_info = provider_->GetLocalDeviceInfo(); 77 EXPECT_TRUE(!!local_device_info); 78 EXPECT_EQ(std::string(kLocalDeviceGuid), local_device_info->guid()); 79 EXPECT_EQ(std::string(kSigninScopedDeviceId), 80 local_device_info->signin_scoped_device_id()); 81 EXPECT_EQ(syncer::GetSessionNameSynchronouslyForTesting(), 82 local_device_info->client_name()); 83 84 chrome::VersionInfo version_info; 85 EXPECT_EQ(browser_sync::LocalDeviceInfoProviderImpl::MakeUserAgentForSyncApi( 86 version_info), 87 local_device_info->sync_user_agent()); 88 } 89 90 TEST_F(LocalDeviceInfoProviderTest, GetLocalSyncCacheGUID) { 91 ASSERT_EQ(std::string(), provider_->GetLocalSyncCacheGUID()); 92 93 InitializeProvider(); 94 95 EXPECT_EQ(std::string(kLocalDeviceGuid), provider_->GetLocalSyncCacheGUID()); 96 } 97 98 } // namespace browser_sync 99