• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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/public/cpp/system/data_pipe_drainer.h"
6 #include "base/callback.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/values.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 namespace mojo {
13 namespace test {
14 
15 template <typename Functor>
BindLambda(Functor callable)16 base::RepeatingClosure BindLambda(Functor callable) {
17   return base::BindRepeating([](Functor callable) { callable(); }, callable);
18 }
19 
20 class DataPipeDrainerTest : public testing::Test,
21                             public DataPipeDrainer::Client {
22  protected:
DataPipeDrainerTest()23   DataPipeDrainerTest() {
24     DataPipe pipe;
25     drainer_ = std::make_unique<DataPipeDrainer>(
26         this, std::move(pipe.consumer_handle));
27     producer_handle_ = std::move(pipe.producer_handle);
28   }
29 
30   ScopedDataPipeProducerHandle producer_handle_;
31   base::RepeatingClosure completion_callback_;
32 
OnDataAvailable(const void * data,size_t num_bytes)33   void OnDataAvailable(const void* data, size_t num_bytes) override {
34     data_.append(static_cast<const char*>(data), num_bytes);
35   }
36 
OnDataComplete()37   void OnDataComplete() override { completion_callback_.Run(); }
38 
39   base::MessageLoop message_loop_;
40   std::string data_;
41   std::unique_ptr<DataPipeDrainer> drainer_;
42 
43   DISALLOW_COPY_AND_ASSIGN(DataPipeDrainerTest);
44 };
45 
TEST_F(DataPipeDrainerTest,TestCompleteIsCalledOnce)46 TEST_F(DataPipeDrainerTest, TestCompleteIsCalledOnce) {
47   bool had_data_complete = false;
48 
49   completion_callback_ = BindLambda([&had_data_complete]() {
50     EXPECT_FALSE(had_data_complete);
51     had_data_complete = true;
52   });
53   uint32_t size = 5;
54   EXPECT_EQ(MOJO_RESULT_OK, producer_handle_->WriteData(
55                                 "hello", &size, MOJO_WRITE_DATA_FLAG_NONE));
56   base::RunLoop().RunUntilIdle();
57   producer_handle_.reset();
58   base::RunLoop().RunUntilIdle();
59 }
60 
61 }  // namespace test
62 }  // namespace mojo
63