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
31using testing::_;
32using testing::Invoke;
33using testing::InvokeWithoutArgs;
34using testing::WithArgs;
35
36namespace {
37
38const char kTestThreadName[] = "test_thread";
39
40std::string GetThreadName() {
41  return std::string(base::PlatformThread::GetName());
42}
43
44void GetThreadNameAndCall(
45    const trunks::CommandTransceiver::ResponseCallback& callback) {
46  callback.Run(GetThreadName());
47}
48
49void Assign(std::string* to, const std::string& from) {
50  *to = from;
51}
52
53void SendCommandAndWaitAndAssign(trunks::CommandTransceiver* transceiver,
54                                 std::string* output) {
55  *output = transceiver->SendCommandAndWait("test");
56}
57
58}  // namespace
59
60namespace trunks {
61
62class 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
80TEST_F(BackgroundTransceiverTest, Asynchronous) {
81  trunks::BackgroundCommandTransceiver background_transceiver(
82      &next_transceiver_, test_thread_.task_runner());
83  std::string output = "not_assigned";
84  background_transceiver.SendCommand("test", base::Bind(Assign, &output));
85  do {
86    base::RunLoop run_loop;
87    run_loop.RunUntilIdle();
88  } while (output == "not_assigned");
89  // The call to our mock should have happened on the background thread.
90  EXPECT_EQ(std::string(kTestThreadName), output);
91}
92
93TEST_F(BackgroundTransceiverTest, Synchronous) {
94  trunks::BackgroundCommandTransceiver background_transceiver(
95      &next_transceiver_, test_thread_.task_runner());
96  std::string output = "not_assigned";
97  // Post a synchronous call to be run when we start pumping the loop.
98  message_loop_.PostTask(FROM_HERE,
99                         base::Bind(SendCommandAndWaitAndAssign,
100                                    &background_transceiver, &output));
101  base::RunLoop run_loop;
102  run_loop.RunUntilIdle();
103  // The call to our mock should have happened on the background thread.
104  EXPECT_EQ(std::string("test_thread"), output);
105}
106
107}  // namespace trunks
108