Home | History | Annotate | Download | only in trunks
      1 //
      2 // Copyright (C) 2015 The Android Open Source Project
      3 //
      4 // Licensed under the Apache License, Version 2.0 (the "License");
      5 // you may not use this file except in compliance with the License.
      6 // You may obtain a copy of the License at
      7 //
      8 //      http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 // Unless required by applicable law or agreed to in writing, software
     11 // distributed under the License is distributed on an "AS IS" BASIS,
     12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 // See the License for the specific language governing permissions and
     14 // limitations under the License.
     15 //
     16 
     17 #include "trunks/background_command_transceiver.h"
     18 
     19 #include <base/bind.h>
     20 #include <base/logging.h>
     21 #include <base/message_loop/message_loop.h>
     22 #include <base/run_loop.h>
     23 #include <base/threading/platform_thread.h>
     24 #include <base/threading/thread.h>
     25 #include <gmock/gmock.h>
     26 #include <gtest/gtest.h>
     27 
     28 #include "trunks/command_transceiver.h"
     29 #include "trunks/mock_command_transceiver.h"
     30 
     31 using testing::_;
     32 using testing::Invoke;
     33 using testing::InvokeWithoutArgs;
     34 using testing::WithArgs;
     35 
     36 namespace {
     37 
     38 const char kTestThreadName[] = "test_thread";
     39 
     40 std::string GetThreadName() {
     41   return std::string(base::PlatformThread::GetName());
     42 }
     43 
     44 void GetThreadNameAndCall(
     45     const trunks::CommandTransceiver::ResponseCallback& callback) {
     46   callback.Run(GetThreadName());
     47 }
     48 
     49 void Assign(std::string* to, const std::string& from) {
     50   *to = from;
     51 }
     52 
     53 void SendCommandAndWaitAndAssign(trunks::CommandTransceiver* transceiver,
     54                                  std::string* output) {
     55   *output = transceiver->SendCommandAndWait("test");
     56 }
     57 
     58 }  // namespace
     59 
     60 namespace trunks {
     61 
     62 class BackgroundTransceiverTest : public testing::Test {
     63  public:
     64   BackgroundTransceiverTest() : test_thread_(kTestThreadName) {
     65     EXPECT_CALL(next_transceiver_, SendCommand(_, _))
     66         .WillRepeatedly(WithArgs<1>(Invoke(GetThreadNameAndCall)));
     67     EXPECT_CALL(next_transceiver_, SendCommandAndWait(_))
     68         .WillRepeatedly(InvokeWithoutArgs(GetThreadName));
     69     CHECK(test_thread_.Start());
     70   }
     71 
     72   ~BackgroundTransceiverTest() override {}
     73 
     74  protected:
     75   base::MessageLoopForIO message_loop_;
     76   base::Thread test_thread_;
     77   MockCommandTransceiver next_transceiver_;
     78 };
     79 
     80 TEST_F(BackgroundTransceiverTest, Asynchronous) {
     81   trunks::BackgroundCommandTransceiver background_transceiver(
     82       &next_transceiver_,
     83       test_thread_.task_runner());
     84   std::string output = "not_assigned";
     85   background_transceiver.SendCommand("test", base::Bind(Assign, &output));
     86   do {
     87     base::RunLoop run_loop;
     88     run_loop.RunUntilIdle();
     89   } while (output == "not_assigned");
     90   // The call to our mock should have happened on the background thread.
     91   EXPECT_EQ(std::string(kTestThreadName), output);
     92 }
     93 
     94 TEST_F(BackgroundTransceiverTest, Synchronous) {
     95   trunks::BackgroundCommandTransceiver background_transceiver(
     96       &next_transceiver_,
     97       test_thread_.task_runner());
     98   std::string output = "not_assigned";
     99   // Post a synchronous call to be run when we start pumping the loop.
    100   message_loop_.PostTask(FROM_HERE,
    101                          base::Bind(SendCommandAndWaitAndAssign,
    102                                     &background_transceiver,
    103                                     &output));
    104   base::RunLoop run_loop;
    105   run_loop.RunUntilIdle();
    106   // The call to our mock should have happened on the background thread.
    107   EXPECT_EQ(std::string("test_thread"), output);
    108 }
    109 
    110 }  // namespace trunks
    111