Utkarsh Sanghi | bbef5df | 2015-09-09 19:34:06 -0700 | [diff] [blame] | 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 | // |
Darren Krahn | 80c739e | 2014-12-22 17:05:24 -0800 | [diff] [blame] | 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 | |
Darren Krahn | 295e851 | 2015-03-17 10:20:13 -0700 | [diff] [blame] | 72 | ~BackgroundTransceiverTest() override {} |
Darren Krahn | 80c739e | 2014-12-22 17:05:24 -0800 | [diff] [blame] | 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( |
Darren Krahn | 4dc4629 | 2016-05-25 17:47:09 -0700 | [diff] [blame] | 82 | &next_transceiver_, test_thread_.task_runner()); |
Darren Krahn | 80c739e | 2014-12-22 17:05:24 -0800 | [diff] [blame] | 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 | |
| 93 | TEST_F(BackgroundTransceiverTest, Synchronous) { |
| 94 | trunks::BackgroundCommandTransceiver background_transceiver( |
Darren Krahn | 4dc4629 | 2016-05-25 17:47:09 -0700 | [diff] [blame] | 95 | &next_transceiver_, test_thread_.task_runner()); |
Darren Krahn | 80c739e | 2014-12-22 17:05:24 -0800 | [diff] [blame] | 96 | std::string output = "not_assigned"; |
| 97 | // Post a synchronous call to be run when we start pumping the loop. |
Jay Civelli | a2ea149 | 2017-03-27 09:56:41 -0700 | [diff] [blame] | 98 | message_loop_.task_runner()->PostTask(FROM_HERE, |
Darren Krahn | 80c739e | 2014-12-22 17:05:24 -0800 | [diff] [blame] | 99 | base::Bind(SendCommandAndWaitAndAssign, |
Darren Krahn | 4dc4629 | 2016-05-25 17:47:09 -0700 | [diff] [blame] | 100 | &background_transceiver, &output)); |
Darren Krahn | 80c739e | 2014-12-22 17:05:24 -0800 | [diff] [blame] | 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 |