OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "remoting/host/pairing_registry_delegate_linux.h" | 5 #include "remoting/host/pairing_registry_delegate_linux.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/message_loop/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" | 8 #include "base/timer/timer.h" |
9 #include "base/values.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
14 | 11 |
15 namespace remoting { | 12 namespace remoting { |
16 | 13 |
17 using protocol::PairingRegistry; | 14 using protocol::PairingRegistry; |
18 | 15 |
19 class PairingRegistryDelegateLinuxTest : public testing::Test { | 16 class PairingRegistryDelegateLinuxTest : public testing::Test { |
20 public: | 17 public: |
21 void SaveComplete(PairingRegistry::Delegate* delegate, | 18 virtual void SetUp() OVERRIDE { |
22 const std::string& expected_json, | 19 // Create a temporary directory in order to get a unique name and use a |
23 bool success) { | 20 // subdirectory to ensure that the AddPairing method creates the parent |
Jamie
2013/08/01 22:44:04
s/AddPairing/Save/ (or LinuxPairingRegistryDelegat
alexeypa (please no reviews)
2013/08/01 23:07:43
Done.
| |
24 EXPECT_TRUE(success); | 21 // directory if it doesn't exist. |
25 // Load the pairings again to make sure we get what we've just written. | 22 file_util::CreateNewTempDirectory("chromoting-test", &temp_dir_); |
26 delegate->Load( | 23 temp_registry_ = temp_dir_.Append("paired-clients"); |
27 base::Bind(&PairingRegistryDelegateLinuxTest::VerifyLoad, | |
28 base::Unretained(this), | |
29 expected_json)); | |
30 } | 24 } |
31 | 25 |
32 void VerifyLoad(const std::string& expected, | 26 virtual void TearDown() OVERRIDE { |
33 const std::string& actual) { | 27 base::DeleteFile(temp_dir_, true); |
34 EXPECT_EQ(actual, expected); | |
35 base::MessageLoop::current()->Quit(); | |
36 } | 28 } |
29 | |
30 protected: | |
31 base::FilePath temp_dir_; | |
32 base::FilePath temp_registry_; | |
37 }; | 33 }; |
38 | 34 |
35 // Verify that the registry is initially empty. | |
36 TEST_F(PairingRegistryDelegateLinuxTest, Empty) { | |
37 scoped_ptr<PairingRegistryDelegateLinux> delegate( | |
38 new PairingRegistryDelegateLinux()); | |
39 delegate->SetRegistryPathForTesting(temp_registry_); | |
40 EXPECT_TRUE(delegate->LoadAll()->empty()); | |
41 } | |
42 | |
43 // Verify that an entry can deleted. | |
44 TEST_F(PairingRegistryDelegateLinuxTest, Delete) { | |
Jamie
2013/08/01 22:44:04
Can you add a test for DeleteAll as well (since it
alexeypa (please no reviews)
2013/08/01 23:07:43
Done.
| |
45 scoped_ptr<PairingRegistryDelegateLinux> delegate( | |
46 new PairingRegistryDelegateLinux()); | |
47 delegate->SetRegistryPathForTesting(temp_registry_); | |
48 | |
49 EXPECT_TRUE(delegate->LoadAll()->empty()); | |
50 | |
51 PairingRegistry::Pairing pairing(base::Time::Now(), "xxx", "xxx", "xxx"); | |
52 EXPECT_TRUE(delegate->Save(pairing)); | |
53 EXPECT_EQ(delegate->Load(pairing.client_id()), pairing); | |
54 | |
55 EXPECT_TRUE(delegate->Delete(pairing.client_id())); | |
56 EXPECT_TRUE(delegate->LoadAll()->empty()); | |
57 } | |
58 | |
59 // Verify that an entry can be saved and loaded. Different instances of | |
60 // the delegate are used to make sure that the objects are stateless. | |
39 TEST_F(PairingRegistryDelegateLinuxTest, SaveAndLoad) { | 61 TEST_F(PairingRegistryDelegateLinuxTest, SaveAndLoad) { |
40 base::MessageLoop message_loop; | 62 scoped_ptr<PairingRegistryDelegateLinux> save_delegate( |
41 base::RunLoop run_loop; | 63 new PairingRegistryDelegateLinux()); |
64 scoped_ptr<PairingRegistryDelegateLinux> load_delegate( | |
65 new PairingRegistryDelegateLinux()); | |
66 save_delegate->SetRegistryPathForTesting(temp_registry_); | |
67 load_delegate->SetRegistryPathForTesting(temp_registry_); | |
42 | 68 |
43 // Create a temporary directory in order to get a unique name and use a | 69 PairingRegistry::Pairing pairing(base::Time::Now(), "xxx", "xxx", "xxx"); |
44 // subdirectory to ensure that the AddPairing method creates the parent | 70 EXPECT_TRUE(save_delegate->Save(pairing)); |
45 // directory if it doesn't exist. | 71 EXPECT_EQ(load_delegate->Load(pairing.client_id()), pairing); |
46 base::FilePath temp_dir; | 72 } |
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 | 73 |
76 } // namespace remoting | 74 } // namespace remoting |
OLD | NEW |