blob: 43253ec0ef2a1086216562c065e49af730ec2c8b [file] [log] [blame]
Paul Ramireze2bb1872024-08-12 20:21:13 +00001/**
2 * Copyright 2024 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#pragma once
18
19#include <queue>
20#include <string>
21
22#include <android-base/result.h>
23#include <gtest/gtest.h>
24#include <input/InputTransport.h>
25#include <utils/Errors.h>
26
27namespace android {
28
29class TestInputChannel final : public InputChannel {
30public:
31 explicit TestInputChannel(const std::string& name);
32
33 /**
34 * Enqueues a message in mReceivedMessages.
35 */
36 void enqueueMessage(const InputMessage& message);
37
38 /**
39 * Pushes message to mSentMessages. In the default implementation, InputChannel sends messages
40 * through a file descriptor. TestInputChannel, on the contrary, stores sent messages in
41 * mSentMessages for assertion reasons.
42 */
43 status_t sendMessage(const InputMessage* message) override;
44
45 /**
46 * Returns an InputMessage from mReceivedMessages. This is done instead of retrieving data
47 * directly from fd.
48 */
49 base::Result<InputMessage> receiveMessage() override;
50
51 /**
52 * Returns if mReceivedMessages is not empty.
53 */
54 bool probablyHasInput() const override;
55
56 void assertFinishMessage(uint32_t seq, bool handled);
57
58 void assertNoSentMessages() const;
59
60private:
61 // InputMessages received by the endpoint.
62 std::queue<InputMessage> mReceivedMessages;
63 // InputMessages sent by the endpoint.
64 std::queue<InputMessage> mSentMessages;
65};
66} // namespace android