OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "mojo/common/data_pipe_utils.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/synchronization/condition_variable.h" |
| 12 #include "base/threading/sequenced_worker_pool.h" |
| 13 #include "base/time/time.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace mojo { |
| 17 namespace common { |
| 18 namespace { |
| 19 |
| 20 void TransferBooleanValueAndExecute(const base::Closure& closure, |
| 21 bool* to_write, |
| 22 bool value) { |
| 23 *to_write = value; |
| 24 if (!closure.is_null()) { |
| 25 closure.Run(); |
| 26 } |
| 27 } |
| 28 |
| 29 TEST(DataPipeUtilsTest, AsyncFileTransfer) { |
| 30 const char kData[] = "Hello world."; |
| 31 base::ScopedTempDir temp_dir; |
| 32 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 33 base::FilePath input; |
| 34 base::FilePath output; |
| 35 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &input)); |
| 36 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &output)); |
| 37 ASSERT_TRUE(base::WriteFile(input, kData, arraysize(kData))); |
| 38 base::MessageLoop loop; |
| 39 scoped_refptr<base::SequencedWorkerPool> blocking_pool = |
| 40 new base::SequencedWorkerPool(2, "blocking_pool"); |
| 41 |
| 42 bool write_succeded = false; |
| 43 bool read_succeded = false; |
| 44 |
| 45 DataPipe pipes; |
| 46 CopyFromFile(input, pipes.producer_handle.Pass(), 0, blocking_pool.get(), |
| 47 base::Bind(&TransferBooleanValueAndExecute, base::Closure(), |
| 48 base::Unretained(&write_succeded))); |
| 49 CopyToFile(pipes.consumer_handle.Pass(), output, blocking_pool.get(), |
| 50 base::Bind(&TransferBooleanValueAndExecute, |
| 51 base::MessageLoop::QuitClosure(), |
| 52 base::Unretained(&read_succeded))); |
| 53 loop.Run(); |
| 54 |
| 55 EXPECT_TRUE(write_succeded); |
| 56 EXPECT_TRUE(read_succeded); |
| 57 EXPECT_TRUE(base::ContentsEqual(input, output)); |
| 58 |
| 59 blocking_pool->Shutdown(); |
| 60 } |
| 61 |
| 62 } // namespace |
| 63 } // namespace common |
| 64 } // namespace mojo |
OLD | NEW |