summaryrefslogtreecommitdiff
path: root/libs/input/tests/TestInputChannel.cpp
blob: 26a0ca2106bc7378fc3614d3281f84b8069c5fd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
 * 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.
 */

#define LOG_TAG "TestInputChannel"
#define ATRACE_TAG ATRACE_TAG_INPUT

#include <TestInputChannel.h>

#include <sys/socket.h>
#include <unistd.h>

#include <array>

#include <android-base/logging.h>
#include <android-base/unique_fd.h>
#include <binder/IBinder.h>
#include <utils/StrongPointer.h>

namespace android {

namespace {

/**
 * Returns a stub file descriptor by opening a socket pair and closing one of the fds. The returned
 * fd can be used to construct an InputChannel.
 */
base::unique_fd generateFileDescriptor() {
    std::array<int, 2> kFileDescriptors;
    LOG_IF(FATAL, ::socketpair(AF_UNIX, SOCK_SEQPACKET, 0, kFileDescriptors.data()) != 0)
            << "TestInputChannel. Failed to create socket pair.";
    LOG_IF(FATAL, ::close(kFileDescriptors[1]) != 0)
            << "TestInputChannel. Failed to close file descriptor.";
    return base::unique_fd{kFileDescriptors[0]};
}
} // namespace

// --- TestInputChannel ---

TestInputChannel::TestInputChannel(const std::string& name)
      : InputChannel{name, generateFileDescriptor(), sp<BBinder>::make()} {}

void TestInputChannel::enqueueMessage(const InputMessage& message) {
    mReceivedMessages.push(message);
}

status_t TestInputChannel::sendMessage(const InputMessage* message) {
    LOG_IF(FATAL, message == nullptr)
            << "TestInputChannel " << getName() << ". No message was passed to sendMessage.";

    mSentMessages.push(*message);
    return OK;
}

base::Result<InputMessage> TestInputChannel::receiveMessage() {
    if (mReceivedMessages.empty()) {
        return base::Error(WOULD_BLOCK);
    }
    InputMessage message = mReceivedMessages.front();
    mReceivedMessages.pop();
    return message;
}

bool TestInputChannel::probablyHasInput() const {
    return !mReceivedMessages.empty();
}

void TestInputChannel::assertFinishMessage(uint32_t seq, bool handled) {
    ASSERT_FALSE(mSentMessages.empty())
            << "TestInputChannel " << getName() << ". Cannot assert. mSentMessages is empty.";

    const InputMessage& finishMessage = mSentMessages.front();

    EXPECT_EQ(finishMessage.header.seq, seq)
            << "TestInputChannel " << getName()
            << ". Sequence mismatch. Message seq: " << finishMessage.header.seq
            << " Expected seq: " << seq;

    EXPECT_EQ(finishMessage.body.finished.handled, handled)
            << "TestInputChannel " << getName()
            << ". Handled value mismatch. Message val: " << std::boolalpha
            << finishMessage.body.finished.handled << "Expected val: " << handled
            << std::noboolalpha;
    mSentMessages.pop();
}

void TestInputChannel::assertNoSentMessages() const {
    ASSERT_TRUE(mSentMessages.empty());
}
} // namespace android