blob: 5abf924d2ee3e2b8a082149e0f5a783d201849b0 [file]
/**
* Copyright 2024 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.
*/
#pragma once
#include <expected>
#include <queue>
#include <string>
#include <android-base/logging.h>
#include <gtest/gtest.h>
#include <input/InputTransport.h>
#include <utils/Errors.h>
namespace android {
class TestInputChannel {
public:
explicit TestInputChannel(const std::string& name);
/**
* Enqueues a message in mReceivedMessages.
*/
void enqueueMessage(const InputMessage& message);
/**
* Pushes message to mSentMessages.
*/
status_t sendMessage(const InputMessage* message);
/**
* Returns an InputMessage from mReceivedMessages.
*/
std::expected<InputMessage, status_t> receiveMessage();
/**
* Returns if mReceivedMessages is not empty.
*/
bool probablyHasInput() const;
void assertFinishMessage(uint32_t seq, bool handled);
void assertTimelineMessage(int32_t inputEventId, nsecs_t gpuCompletedTime, nsecs_t presentTime);
void assertNoSentMessages() const;
void setLooperFd(int fd) { mLooperFd = fd; }
int getFd() const { return mLooperFd; }
std::string getName() const { return mName; }
private:
// InputMessages received by the endpoint.
std::queue<InputMessage> mReceivedMessages;
// InputMessages sent by the endpoint.
std::queue<InputMessage> mSentMessages;
int mLooperFd = -1;
const std::string mName;
};
/**
* A proxy InputChannel that delegates all calls to a target TestInputChannel.
* This is used to allow InputConsumer to uniquely own an InputChannel while
* the test fixture keeps the underlying mock alive via a shared_ptr.
*/
class DelegatedInputChannel final : public InputChannel {
public:
explicit DelegatedInputChannel(std::shared_ptr<TestInputChannel> target);
status_t sendMessage(const InputMessage* message) override {
return mTarget->sendMessage(message);
}
std::expected<InputMessage, status_t> receiveMessage() override {
return mTarget->receiveMessage();
}
bool probablyHasInput() const override { return mTarget->probablyHasInput(); }
private:
const std::shared_ptr<TestInputChannel> mTarget;
};
} // namespace android