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/native_messaging/native_messaging_reader.h" | 5 #include "remoting/host/native_messaging/native_messaging_reader.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
| 9 #include <memory> |
9 #include <utility> | 10 #include <utility> |
10 | 11 |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "remoting/host/setup/test_util.h" | 16 #include "remoting/host/setup/test_util.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 namespace remoting { | 19 namespace remoting { |
20 | 20 |
21 class NativeMessagingReaderTest : public testing::Test { | 21 class NativeMessagingReaderTest : public testing::Test { |
22 public: | 22 public: |
23 NativeMessagingReaderTest(); | 23 NativeMessagingReaderTest(); |
24 ~NativeMessagingReaderTest() override; | 24 ~NativeMessagingReaderTest() override; |
25 | 25 |
26 void SetUp() override; | 26 void SetUp() override; |
27 | 27 |
28 // Starts the reader and runs the MessageLoop to completion. | 28 // Starts the reader and runs the MessageLoop to completion. |
29 void Run(); | 29 void Run(); |
30 | 30 |
31 // MessageCallback passed to the Reader. Stores |message| so it can be | 31 // MessageCallback passed to the Reader. Stores |message| so it can be |
32 // verified by tests. | 32 // verified by tests. |
33 void OnMessage(scoped_ptr<base::Value> message); | 33 void OnMessage(std::unique_ptr<base::Value> message); |
34 | 34 |
35 // Writes a message (header+body) to the write-end of the pipe. | 35 // Writes a message (header+body) to the write-end of the pipe. |
36 void WriteMessage(const std::string& message); | 36 void WriteMessage(const std::string& message); |
37 | 37 |
38 // Writes some data to the write-end of the pipe. | 38 // Writes some data to the write-end of the pipe. |
39 void WriteData(const char* data, int length); | 39 void WriteData(const char* data, int length); |
40 | 40 |
41 protected: | 41 protected: |
42 scoped_ptr<NativeMessagingReader> reader_; | 42 std::unique_ptr<NativeMessagingReader> reader_; |
43 base::File read_file_; | 43 base::File read_file_; |
44 base::File write_file_; | 44 base::File write_file_; |
45 scoped_ptr<base::Value> message_; | 45 std::unique_ptr<base::Value> message_; |
46 | 46 |
47 private: | 47 private: |
48 // MessageLoop declared here, since the NativeMessageReader ctor requires a | 48 // MessageLoop declared here, since the NativeMessageReader ctor requires a |
49 // MessageLoop to have been created. | 49 // MessageLoop to have been created. |
50 base::MessageLoopForIO message_loop_; | 50 base::MessageLoopForIO message_loop_; |
51 base::RunLoop run_loop_; | 51 base::RunLoop run_loop_; |
52 }; | 52 }; |
53 | 53 |
54 NativeMessagingReaderTest::NativeMessagingReaderTest() {} | 54 NativeMessagingReaderTest::NativeMessagingReaderTest() {} |
55 NativeMessagingReaderTest::~NativeMessagingReaderTest() {} | 55 NativeMessagingReaderTest::~NativeMessagingReaderTest() {} |
56 | 56 |
57 void NativeMessagingReaderTest::SetUp() { | 57 void NativeMessagingReaderTest::SetUp() { |
58 ASSERT_TRUE(MakePipe(&read_file_, &write_file_)); | 58 ASSERT_TRUE(MakePipe(&read_file_, &write_file_)); |
59 reader_.reset(new NativeMessagingReader(std::move(read_file_))); | 59 reader_.reset(new NativeMessagingReader(std::move(read_file_))); |
60 } | 60 } |
61 | 61 |
62 void NativeMessagingReaderTest::Run() { | 62 void NativeMessagingReaderTest::Run() { |
63 // Close the write-end, so the reader doesn't block waiting for more data. | 63 // Close the write-end, so the reader doesn't block waiting for more data. |
64 write_file_.Close(); | 64 write_file_.Close(); |
65 | 65 |
66 // base::Unretained is safe since no further tasks can run after | 66 // base::Unretained is safe since no further tasks can run after |
67 // RunLoop::Run() returns. | 67 // RunLoop::Run() returns. |
68 reader_->Start( | 68 reader_->Start( |
69 base::Bind(&NativeMessagingReaderTest::OnMessage, base::Unretained(this)), | 69 base::Bind(&NativeMessagingReaderTest::OnMessage, base::Unretained(this)), |
70 run_loop_.QuitClosure()); | 70 run_loop_.QuitClosure()); |
71 run_loop_.Run(); | 71 run_loop_.Run(); |
72 } | 72 } |
73 | 73 |
74 void NativeMessagingReaderTest::OnMessage(scoped_ptr<base::Value> message) { | 74 void NativeMessagingReaderTest::OnMessage( |
| 75 std::unique_ptr<base::Value> message) { |
75 message_ = std::move(message); | 76 message_ = std::move(message); |
76 } | 77 } |
77 | 78 |
78 void NativeMessagingReaderTest::WriteMessage(const std::string& message) { | 79 void NativeMessagingReaderTest::WriteMessage(const std::string& message) { |
79 uint32_t length = message.length(); | 80 uint32_t length = message.length(); |
80 WriteData(reinterpret_cast<char*>(&length), 4); | 81 WriteData(reinterpret_cast<char*>(&length), 4); |
81 WriteData(message.data(), length); | 82 WriteData(message.data(), length); |
82 } | 83 } |
83 | 84 |
84 void NativeMessagingReaderTest::WriteData(const char* data, int length) { | 85 void NativeMessagingReaderTest::WriteData(const char* data, int length) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 Run(); | 147 Run(); |
147 EXPECT_TRUE(message_); | 148 EXPECT_TRUE(message_); |
148 base::DictionaryValue* message_dict; | 149 base::DictionaryValue* message_dict; |
149 EXPECT_TRUE(message_->GetAsDictionary(&message_dict)); | 150 EXPECT_TRUE(message_->GetAsDictionary(&message_dict)); |
150 int result; | 151 int result; |
151 EXPECT_TRUE(message_dict->GetInteger("foo", &result)); | 152 EXPECT_TRUE(message_dict->GetInteger("foo", &result)); |
152 EXPECT_EQ(42, result); | 153 EXPECT_EQ(42, result); |
153 } | 154 } |
154 | 155 |
155 } // namespace remoting | 156 } // namespace remoting |
OLD | NEW |