/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include namespace android { using ::testing::_; using ::testing::ContainerEq; using ::testing::Invoke; struct MockEseInterface : public EseInterface { MOCK_METHOD0(init, void()); MOCK_METHOD0(open, int()); MOCK_METHOD0(close, void()); MOCK_METHOD0(name, const char*()); MOCK_METHOD2(transceive, int(const std::vector&, std::vector&)); MOCK_METHOD0(error, bool()); MOCK_METHOD0(error_message, const char*()); MOCK_METHOD0(error_code, int()); }; inline auto EseReceive(const std::vector& response) { return Invoke([response](const std::vector& /* tx */, std::vector& rx) { const auto end = (rx.size() >= response.size()) ? response.end() : (response.begin() + rx.size()); std::copy(response.begin(), end, rx.begin()); return (int) response.size(); }); } // Returns a mocked device that expects a command, will pass the response to the // callback and finally returns the given value. // Needs to allocate on heap as it can't copy/move the mock. inline std::unique_ptr singleCommandEseMock( const std::vector& command, const std::vector& response) { auto mockEse = std::make_unique(); EXPECT_CALL(*mockEse, transceive(ContainerEq(command), _)).WillOnce(EseReceive(response)); return mockEse; } } // namespace android