blob: f577dc336d5e0f8930b884649647d9196e794993 [file] [log] [blame]
Utkarsh Sanghibbef5df2015-09-09 19:34:06 -07001//
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//
Darren Krahn80c739e2014-12-22 17:05:24 -080016
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
Darren Krahn295e8512015-03-17 10:20:13 -070072 ~BackgroundTransceiverTest() override {}
Darren Krahn80c739e2014-12-22 17:05:24 -080073
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(
Darren Krahn4dc46292016-05-25 17:47:09 -070082 &next_transceiver_, test_thread_.task_runner());
Darren Krahn80c739e2014-12-22 17:05:24 -080083 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(
Darren Krahn4dc46292016-05-25 17:47:09 -070095 &next_transceiver_, test_thread_.task_runner());
Darren Krahn80c739e2014-12-22 17:05:24 -080096 std::string output = "not_assigned";
97 // Post a synchronous call to be run when we start pumping the loop.
Jay Civellia2ea1492017-03-27 09:56:41 -070098 message_loop_.task_runner()->PostTask(FROM_HERE,
Darren Krahn80c739e2014-12-22 17:05:24 -080099 base::Bind(SendCommandAndWaitAndAssign,
Darren Krahn4dc46292016-05-25 17:47:09 -0700100 &background_transceiver, &output));
Darren Krahn80c739e2014-12-22 17:05:24 -0800101 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