• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 
20 #include <esecpp/EseInterface.h>
21 
22 namespace android {
23 
24 using ::testing::_;
25 using ::testing::ContainerEq;
26 using ::testing::Invoke;
27 
28 struct MockEseInterface : public EseInterface {
29     MOCK_METHOD0(init, void());
30     MOCK_METHOD0(open, int());
31     MOCK_METHOD0(close, void());
32     MOCK_METHOD0(name, const char*());
33     MOCK_METHOD2(transceive, int(const std::vector<uint8_t>&, std::vector<uint8_t>&));
34     MOCK_METHOD0(error, bool());
35     MOCK_METHOD0(error_message, const char*());
36     MOCK_METHOD0(error_code, int());
37 };
38 
EseReceive(const std::vector<uint8_t> & response)39 inline auto EseReceive(const std::vector<uint8_t>& response) {
40     return Invoke([response](const std::vector<uint8_t>& /* tx */, std::vector<uint8_t>& rx) {
41         const auto end = (rx.size() >= response.size())
42                 ? response.end() : (response.begin() + rx.size());
43         std::copy(response.begin(), end, rx.begin());
44         return (int) response.size();
45     });
46 }
47 
48 // Returns a mocked device that expects a command, will pass the response to the
49 // callback and finally returns the given value.
50 // Needs to allocate on heap as it can't copy/move the mock.
singleCommandEseMock(const std::vector<uint8_t> & command,const std::vector<uint8_t> & response)51 inline std::unique_ptr<MockEseInterface> singleCommandEseMock(
52         const std::vector<uint8_t>& command, const std::vector<uint8_t>& response) {
53     auto mockEse = std::make_unique<MockEseInterface>();
54     EXPECT_CALL(*mockEse, transceive(ContainerEq(command), _)).WillOnce(EseReceive(response));
55     return mockEse;
56 }
57 
58 } // namespace android
59