pairing_registry_delegate_linux_unittest.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
1// Copyright 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 "remoting/host/pairing_registry_delegate_linux.h" 6 7#include "base/file_util.h" 8#include "base/message_loop.h" 9#include "base/run_loop.h" 10#include "base/task_runner.h" 11#include "base/thread_task_runner_handle.h" 12#include "base/timer/timer.h" 13#include "testing/gtest/include/gtest/gtest.h" 14 15namespace remoting { 16 17using protocol::PairingRegistry; 18 19class PairingRegistryDelegateLinuxTest : public testing::Test { 20 public: 21 void SaveComplete(PairingRegistry::Delegate* delegate, 22 const std::string& expected_json, 23 bool success) { 24 EXPECT_TRUE(success); 25 // Load the pairings again to make sure we get what we've just written. 26 delegate->Load( 27 base::Bind(&PairingRegistryDelegateLinuxTest::VerifyLoad, 28 base::Unretained(this), 29 expected_json)); 30 } 31 32 void VerifyLoad(const std::string& expected, 33 const std::string& actual) { 34 EXPECT_EQ(actual, expected); 35 base::MessageLoop::current()->Quit(); 36 } 37}; 38 39TEST_F(PairingRegistryDelegateLinuxTest, SaveAndLoad) { 40 base::MessageLoop message_loop; 41 base::RunLoop run_loop; 42 43 // Create a temporary directory in order to get a unique name and use a 44 // subdirectory to ensure that the AddPairing method creates the parent 45 // directory if it doesn't exist. 46 base::FilePath temp_dir; 47 file_util::CreateNewTempDirectory("chromoting-test", &temp_dir); 48 base::FilePath temp_file = temp_dir.Append("dir").Append("registry.json"); 49 50 scoped_refptr<base::TaskRunner> task_runner = 51 base::ThreadTaskRunnerHandle::Get(); 52 scoped_ptr<PairingRegistryDelegateLinux> save_delegate( 53 new PairingRegistryDelegateLinux(task_runner)); 54 scoped_ptr<PairingRegistryDelegateLinux> load_delegate( 55 new PairingRegistryDelegateLinux(task_runner)); 56 save_delegate->SetFilenameForTesting(temp_file); 57 load_delegate->SetFilenameForTesting(temp_file); 58 59 // Save the pairings, then load them using a different delegate to ensure 60 // that the test isn't passing due to cached values. Note that the delegate 61 // doesn't require that the strings it loads and saves are valid JSON, so 62 // we can simplify the test a bit. 63 std::string test_data = "test data"; 64 save_delegate->Save( 65 test_data, 66 base::Bind(&PairingRegistryDelegateLinuxTest::SaveComplete, 67 base::Unretained(this), 68 load_delegate.get(), 69 test_data)); 70 71 run_loop.Run(); 72 73 base::DeleteFile(temp_dir, true); 74}; 75 76} // namespace remoting 77