| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "apps/moterm/moterm_driver.h" | 5 #include "apps/moterm/moterm_driver.h" |
| 6 | 6 |
| 7 #include <deque> | 7 #include <deque> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 | 63 |
| 64 using MotermDriverTest = mojo::test::ApplicationTestBase; | 64 using MotermDriverTest = mojo::test::ApplicationTestBase; |
| 65 | 65 |
| 66 // This spins the message loop until the callback is run. Note that we can't | 66 // This spins the message loop until the callback is run. Note that we can't |
| 67 // just wait for the response message, since we need to give a chance for the | 67 // just wait for the response message, since we need to give a chance for the |
| 68 // driver to handle the message. | 68 // driver to handle the message. |
| 69 void FileWriteString(mojo::files::File* file, const std::string& s) { | 69 void FileWriteString(mojo::files::File* file, const std::string& s) { |
| 70 std::vector<uint8_t> bytes_to_write; | 70 std::vector<uint8_t> bytes_to_write; |
| 71 for (size_t i = 0; i < s.size(); i++) | 71 for (size_t i = 0; i < s.size(); i++) |
| 72 bytes_to_write.push_back(static_cast<uint8_t>(s[i])); | 72 bytes_to_write.push_back(static_cast<uint8_t>(s[i])); |
| 73 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 73 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 74 uint32_t num_bytes_written = 0; | 74 uint32_t num_bytes_written = 0; |
| 75 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, | 75 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0, |
| 76 mojo::files::WHENCE_FROM_CURRENT, | 76 mojo::files::Whence::FROM_CURRENT, |
| 77 [&error, &num_bytes_written](mojo::files::Error e, uint32_t n) { | 77 [&error, &num_bytes_written](mojo::files::Error e, uint32_t n) { |
| 78 error = e; | 78 error = e; |
| 79 num_bytes_written = n; | 79 num_bytes_written = n; |
| 80 base::MessageLoop::current()->Quit(); | 80 base::MessageLoop::current()->Quit(); |
| 81 }); | 81 }); |
| 82 base::MessageLoop::current()->Run(); | 82 base::MessageLoop::current()->Run(); |
| 83 EXPECT_EQ(mojo::files::ERROR_OK, error); | 83 EXPECT_EQ(mojo::files::Error::OK, error); |
| 84 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); | 84 EXPECT_EQ(bytes_to_write.size(), num_bytes_written); |
| 85 } | 85 } |
| 86 | 86 |
| 87 // (See the comments above |FileWriteString()|.) | 87 // (See the comments above |FileWriteString()|.) |
| 88 void FileClose(mojo::files::File* file) { | 88 void FileClose(mojo::files::File* file) { |
| 89 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 89 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 90 file->Close([&error](mojo::files::Error e) { | 90 file->Close([&error](mojo::files::Error e) { |
| 91 error = e; | 91 error = e; |
| 92 base::MessageLoop::current()->Quit(); | 92 base::MessageLoop::current()->Quit(); |
| 93 }); | 93 }); |
| 94 base::MessageLoop::current()->Run(); | 94 base::MessageLoop::current()->Run(); |
| 95 EXPECT_EQ(mojo::files::ERROR_OK, error); | 95 EXPECT_EQ(mojo::files::Error::OK, error); |
| 96 } | 96 } |
| 97 | 97 |
| 98 // (See the comments above |FileWriteString()|.) | 98 // (See the comments above |FileWriteString()|.) |
| 99 std::string FileReadString(mojo::files::File* file, uint32_t max_bytes) { | 99 std::string FileReadString(mojo::files::File* file, uint32_t max_bytes) { |
| 100 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 100 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 101 mojo::Array<uint8_t> bytes_read; | 101 mojo::Array<uint8_t> bytes_read; |
| 102 file->Read( | 102 file->Read( |
| 103 10, 0, mojo::files::WHENCE_FROM_CURRENT, | 103 10, 0, mojo::files::Whence::FROM_CURRENT, |
| 104 [&error, &bytes_read](mojo::files::Error e, mojo::Array<uint8_t> b) { | 104 [&error, &bytes_read](mojo::files::Error e, mojo::Array<uint8_t> b) { |
| 105 error = e; | 105 error = e; |
| 106 bytes_read = b.Pass(); | 106 bytes_read = b.Pass(); |
| 107 base::MessageLoop::current()->Quit(); | 107 base::MessageLoop::current()->Quit(); |
| 108 }); | 108 }); |
| 109 base::MessageLoop::current()->Run(); | 109 base::MessageLoop::current()->Run(); |
| 110 EXPECT_EQ(mojo::files::ERROR_OK, error); | 110 EXPECT_EQ(mojo::files::Error::OK, error); |
| 111 if (!bytes_read.size()) | 111 if (!bytes_read.size()) |
| 112 return std::string(); | 112 return std::string(); |
| 113 return std::string(reinterpret_cast<const char*>(&bytes_read[0]), | 113 return std::string(reinterpret_cast<const char*>(&bytes_read[0]), |
| 114 bytes_read.size()); | 114 bytes_read.size()); |
| 115 } | 115 } |
| 116 | 116 |
| 117 TEST_F(MotermDriverTest, BasicWritesToTerminal) { | 117 TEST_F(MotermDriverTest, BasicWritesToTerminal) { |
| 118 TestClient client; | 118 TestClient client; |
| 119 mojo::files::FilePtr file; | 119 mojo::files::FilePtr file; |
| 120 base::WeakPtr<MotermDriver> driver = | 120 base::WeakPtr<MotermDriver> driver = |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 TEST_F(MotermDriverTest, BasicReadsFromTerminal) { | 164 TEST_F(MotermDriverTest, BasicReadsFromTerminal) { |
| 165 TestClient client; | 165 TestClient client; |
| 166 mojo::files::FilePtr file; | 166 mojo::files::FilePtr file; |
| 167 base::WeakPtr<MotermDriver> driver = | 167 base::WeakPtr<MotermDriver> driver = |
| 168 MotermDriver::Create(&client, GetProxy(&file)); | 168 MotermDriver::Create(&client, GetProxy(&file)); |
| 169 ASSERT_TRUE(driver); | 169 ASSERT_TRUE(driver); |
| 170 | 170 |
| 171 // Kick off two reads. They should be satisfied in order. (Don't use | 171 // Kick off two reads. They should be satisfied in order. (Don't use |
| 172 // |FileReadString()| here, since we want to queue up the reads before calling | 172 // |FileReadString()| here, since we want to queue up the reads before calling |
| 173 // |SendData()|.) | 173 // |SendData()|.) |
| 174 mojo::files::Error error1 = mojo::files::ERROR_INTERNAL; | 174 mojo::files::Error error1 = mojo::files::Error::INTERNAL; |
| 175 mojo::Array<uint8_t> bytes_read1; | 175 mojo::Array<uint8_t> bytes_read1; |
| 176 file->Read( | 176 file->Read( |
| 177 4, 0, mojo::files::WHENCE_FROM_CURRENT, | 177 4, 0, mojo::files::Whence::FROM_CURRENT, |
| 178 [&error1, &bytes_read1](mojo::files::Error e, mojo::Array<uint8_t> b) { | 178 [&error1, &bytes_read1](mojo::files::Error e, mojo::Array<uint8_t> b) { |
| 179 error1 = e; | 179 error1 = e; |
| 180 bytes_read1 = b.Pass(); | 180 bytes_read1 = b.Pass(); |
| 181 }); | 181 }); |
| 182 // Only quit the message loop on getting the response to the second. | 182 // Only quit the message loop on getting the response to the second. |
| 183 mojo::files::Error error2 = mojo::files::ERROR_INTERNAL; | 183 mojo::files::Error error2 = mojo::files::Error::INTERNAL; |
| 184 mojo::Array<uint8_t> bytes_read2; | 184 mojo::Array<uint8_t> bytes_read2; |
| 185 file->Read( | 185 file->Read( |
| 186 100, 0, mojo::files::WHENCE_FROM_CURRENT, | 186 100, 0, mojo::files::Whence::FROM_CURRENT, |
| 187 [&error2, &bytes_read2](mojo::files::Error e, mojo::Array<uint8_t> b) { | 187 [&error2, &bytes_read2](mojo::files::Error e, mojo::Array<uint8_t> b) { |
| 188 error2 = e; | 188 error2 = e; |
| 189 bytes_read2 = b.Pass(); | 189 bytes_read2 = b.Pass(); |
| 190 base::MessageLoop::current()->Quit(); | 190 base::MessageLoop::current()->Quit(); |
| 191 }); | 191 }); |
| 192 // They'll only be satisfied on input from the terminal. (We need to send a | 192 // They'll only be satisfied on input from the terminal. (We need to send a |
| 193 // \n (or a \r), since by default we start off in canonical mode.) | 193 // \n (or a \r), since by default we start off in canonical mode.) |
| 194 std::string data("hello\nworld"); | 194 std::string data("hello\nworld"); |
| 195 driver->SendData(data.data(), data.size()); | 195 driver->SendData(data.data(), data.size()); |
| 196 base::MessageLoop::current()->Run(); | 196 base::MessageLoop::current()->Run(); |
| 197 EXPECT_EQ(mojo::files::ERROR_OK, error1); | 197 EXPECT_EQ(mojo::files::Error::OK, error1); |
| 198 EXPECT_EQ(4u, bytes_read1.size()); | 198 EXPECT_EQ(4u, bytes_read1.size()); |
| 199 EXPECT_EQ(std::string("hell"), | 199 EXPECT_EQ(std::string("hell"), |
| 200 std::string(reinterpret_cast<const char*>(&bytes_read1[0]), | 200 std::string(reinterpret_cast<const char*>(&bytes_read1[0]), |
| 201 bytes_read1.size())); | 201 bytes_read1.size())); |
| 202 EXPECT_EQ(mojo::files::ERROR_OK, error2); | 202 EXPECT_EQ(mojo::files::Error::OK, error2); |
| 203 EXPECT_EQ(2u, bytes_read2.size()); | 203 EXPECT_EQ(2u, bytes_read2.size()); |
| 204 // (We shouldn't get the "world" yet.) | 204 // (We shouldn't get the "world" yet.) |
| 205 EXPECT_EQ(std::string("o\n"), | 205 EXPECT_EQ(std::string("o\n"), |
| 206 std::string(reinterpret_cast<const char*>(&bytes_read2[0]), | 206 std::string(reinterpret_cast<const char*>(&bytes_read2[0]), |
| 207 bytes_read2.size())); | 207 bytes_read2.size())); |
| 208 | 208 |
| 209 // Now send a \n. | 209 // Now send a \n. |
| 210 driver->SendData("\n", 1); | 210 driver->SendData("\n", 1); |
| 211 // And then do a read. | 211 // And then do a read. |
| 212 std::string result = FileReadString(file.get(), 100); | 212 std::string result = FileReadString(file.get(), 100); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 // Here, detach rather than doing destroying the other end. | 274 // Here, detach rather than doing destroying the other end. |
| 275 // TODO(vtl): Verify that the driver's end of the message pipe is closed | 275 // TODO(vtl): Verify that the driver's end of the message pipe is closed |
| 276 // (e.g., by checking that |file| detects that its peer is closed). | 276 // (e.g., by checking that |file| detects that its peer is closed). |
| 277 driver->Detach(); | 277 driver->Detach(); |
| 278 EXPECT_FALSE(driver); | 278 EXPECT_FALSE(driver); |
| 279 // The client shouldn't have received anything. | 279 // The client shouldn't have received anything. |
| 280 EXPECT_TRUE(client.events().empty()); | 280 EXPECT_TRUE(client.events().empty()); |
| 281 } | 281 } |
| 282 | 282 |
| 283 } // namespace | 283 } // namespace |
| OLD | NEW |