| 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 "files/public/cpp/output_stream_file.h" | 5 #include "files/public/cpp/output_stream_file.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 std::string data_; | 72 std::string data_; |
| 73 bool got_on_closed_; | 73 bool got_on_closed_; |
| 74 | 74 |
| 75 MOJO_DISALLOW_COPY_AND_ASSIGN(TestClient); | 75 MOJO_DISALLOW_COPY_AND_ASSIGN(TestClient); |
| 76 }; | 76 }; |
| 77 | 77 |
| 78 void TestWrite(mojo::files::File* file, | 78 void TestWrite(mojo::files::File* file, |
| 79 TestClient* client, | 79 TestClient* client, |
| 80 const std::string& s) { | 80 const std::string& s) { |
| 81 bool write_cb_called = false; | 81 bool write_cb_called = false; |
| 82 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 82 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 83 uint32_t num_bytes_written = 0; | 83 uint32_t num_bytes_written = 0; |
| 84 file->Write(StringToArray(s), 0, mojo::files::WHENCE_FROM_CURRENT, | 84 file->Write(StringToArray(s), 0, mojo::files::Whence::FROM_CURRENT, |
| 85 [&write_cb_called, &error, &num_bytes_written]( | 85 [&write_cb_called, &error, &num_bytes_written]( |
| 86 mojo::files::Error e, uint32_t n) { | 86 mojo::files::Error e, uint32_t n) { |
| 87 write_cb_called = true; | 87 write_cb_called = true; |
| 88 error = e; | 88 error = e; |
| 89 num_bytes_written = n; | 89 num_bytes_written = n; |
| 90 QuitMessageLoop(); | 90 QuitMessageLoop(); |
| 91 }); | 91 }); |
| 92 if (client) { | 92 if (client) { |
| 93 // If there's a client, since we're running everything on one thread, the | 93 // If there's a client, since we're running everything on one thread, the |
| 94 // impl (which will call the client, which will quit the message loop) will | 94 // impl (which will call the client, which will quit the message loop) will |
| 95 // get called before the callback. | 95 // get called before the callback. |
| 96 client->Reset(); | 96 client->Reset(); |
| 97 RunMessageLoop(); | 97 RunMessageLoop(); |
| 98 EXPECT_TRUE(client->got_on_data_received()); | 98 EXPECT_TRUE(client->got_on_data_received()); |
| 99 EXPECT_EQ(s, client->data()); | 99 EXPECT_EQ(s, client->data()); |
| 100 EXPECT_FALSE(client->got_on_closed()); | 100 EXPECT_FALSE(client->got_on_closed()); |
| 101 EXPECT_FALSE(write_cb_called); | 101 EXPECT_FALSE(write_cb_called); |
| 102 // Spin the message loop again to get the callback. | 102 // Spin the message loop again to get the callback. |
| 103 client->Reset(); | 103 client->Reset(); |
| 104 RunMessageLoop(); | 104 RunMessageLoop(); |
| 105 EXPECT_FALSE(client->got_on_data_received()); | 105 EXPECT_FALSE(client->got_on_data_received()); |
| 106 EXPECT_FALSE(client->got_on_closed()); | 106 EXPECT_FALSE(client->got_on_closed()); |
| 107 } else { | 107 } else { |
| 108 // Otherwise, only the write callback will be called and quit the message | 108 // Otherwise, only the write callback will be called and quit the message |
| 109 // loop. | 109 // loop. |
| 110 RunMessageLoop(); | 110 RunMessageLoop(); |
| 111 } | 111 } |
| 112 EXPECT_TRUE(write_cb_called); | 112 EXPECT_TRUE(write_cb_called); |
| 113 EXPECT_EQ(mojo::files::ERROR_OK, error); | 113 EXPECT_EQ(mojo::files::Error::OK, error); |
| 114 EXPECT_EQ(s.size(), num_bytes_written); | 114 EXPECT_EQ(s.size(), num_bytes_written); |
| 115 } | 115 } |
| 116 | 116 |
| 117 void TestClose(mojo::files::File* file, TestClient* client) { | 117 void TestClose(mojo::files::File* file, TestClient* client) { |
| 118 bool close_cb_called = false; | 118 bool close_cb_called = false; |
| 119 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 119 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 120 file->Close([&close_cb_called, &error](mojo::files::Error e) { | 120 file->Close([&close_cb_called, &error](mojo::files::Error e) { |
| 121 close_cb_called = true; | 121 close_cb_called = true; |
| 122 error = e; | 122 error = e; |
| 123 QuitMessageLoop(); | 123 QuitMessageLoop(); |
| 124 }); | 124 }); |
| 125 // (This is analogous to |TestWrite()|.) | 125 // (This is analogous to |TestWrite()|.) |
| 126 if (client) { | 126 if (client) { |
| 127 client->Reset(); | 127 client->Reset(); |
| 128 RunMessageLoop(); | 128 RunMessageLoop(); |
| 129 EXPECT_FALSE(client->got_on_data_received()); | 129 EXPECT_FALSE(client->got_on_data_received()); |
| 130 EXPECT_TRUE(client->got_on_closed()); | 130 EXPECT_TRUE(client->got_on_closed()); |
| 131 EXPECT_FALSE(close_cb_called); | 131 EXPECT_FALSE(close_cb_called); |
| 132 client->Reset(); | 132 client->Reset(); |
| 133 RunMessageLoop(); | 133 RunMessageLoop(); |
| 134 EXPECT_FALSE(client->got_on_data_received()); | 134 EXPECT_FALSE(client->got_on_data_received()); |
| 135 EXPECT_FALSE(client->got_on_closed()); | 135 EXPECT_FALSE(client->got_on_closed()); |
| 136 } else { | 136 } else { |
| 137 RunMessageLoop(); | 137 RunMessageLoop(); |
| 138 } | 138 } |
| 139 EXPECT_TRUE(close_cb_called); | 139 EXPECT_TRUE(close_cb_called); |
| 140 EXPECT_EQ(mojo::files::ERROR_OK, error); | 140 EXPECT_EQ(mojo::files::Error::OK, error); |
| 141 } | 141 } |
| 142 | 142 |
| 143 TEST_F(OutputStreamFileTest, Basic) { | 143 TEST_F(OutputStreamFileTest, Basic) { |
| 144 mojo::files::FilePtr file; | 144 mojo::files::FilePtr file; |
| 145 TestClient client; | 145 TestClient client; |
| 146 std::unique_ptr<OutputStreamFile> file_impl = | 146 std::unique_ptr<OutputStreamFile> file_impl = |
| 147 OutputStreamFile::Create(&client, GetProxy(&file)); | 147 OutputStreamFile::Create(&client, GetProxy(&file)); |
| 148 | 148 |
| 149 TestWrite(file.get(), &client, "hello"); | 149 TestWrite(file.get(), &client, "hello"); |
| 150 TestWrite(file.get(), &client, "world"); | 150 TestWrite(file.get(), &client, "world"); |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 }); | 278 }); |
| 279 TestClose(file.get(), nullptr); | 279 TestClose(file.get(), nullptr); |
| 280 if (!got_connection_error) | 280 if (!got_connection_error) |
| 281 RunMessageLoop(); | 281 RunMessageLoop(); |
| 282 EXPECT_TRUE(got_connection_error); | 282 EXPECT_TRUE(got_connection_error); |
| 283 } | 283 } |
| 284 } | 284 } |
| 285 | 285 |
| 286 } // namespace | 286 } // namespace |
| 287 } // namespace files_impl | 287 } // namespace files_impl |
| OLD | NEW |