2 * Copyright (C) 2016, The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <android-base/logging.h>
22 #include <android-base/unique_fd.h>
23 #include <gtest/gtest.h>
24 #include <utils/Errors.h>
25 #include <utils/StopWatch.h>
27 #include <looper_backed_event_loop.h>
31 const int kTimingToleranceMs = 25;
33 // Adapt from libutils/tests/TestHelpers.h
36 android::base::unique_fd send_fd;
37 android::base::unique_fd receive_fd;
43 receive_fd = android::base::unique_fd(fds[0]);
44 send_fd = android::base::unique_fd(fds[1]);
48 ssize_t n_written = ::write(send_fd, "*", 1);
50 LOG(ERROR) << "Failed to write signal to pipe: " << strerror(errno);
58 ssize_t n_read = ::read(receive_fd, buf, 1);
61 LOG(ERROR) << "No data from pipe";
63 LOG(ERROR) << "Failed to read signal from pipe: " << strerror(errno);
76 class WificondLooperBackedEventLoopTest : public ::testing::Test {
78 std::unique_ptr<LooperBackedEventLoop> event_loop_;
80 virtual void SetUp() {
81 event_loop_.reset(new LooperBackedEventLoop());
85 TEST_F(WificondLooperBackedEventLoopTest, LooperBackedEventLoopPostTaskTest) {
86 bool task_executed = false;
87 event_loop_->PostTask([this, &task_executed]() mutable {
88 task_executed = true; event_loop_->TriggerExit();});
89 EXPECT_FALSE(task_executed);
91 EXPECT_TRUE(task_executed);
94 TEST_F(WificondLooperBackedEventLoopTest,
95 LooperBackedEventLoopPostDelayedTaskTest) {
96 bool task_executed = false;
97 event_loop_->PostDelayedTask([this, &task_executed]() mutable {
98 task_executed = true; event_loop_->TriggerExit();}, 500);
99 EXPECT_FALSE(task_executed);
100 StopWatch stopWatch("DelayedTask");
102 int32_t elapsedMillis = ns2ms(stopWatch.elapsedTime());
103 EXPECT_NEAR(500, elapsedMillis, kTimingToleranceMs);
104 EXPECT_TRUE(task_executed);
107 TEST_F(WificondLooperBackedEventLoopTest, LooperBackedEventLoopWatchFdInputReadyTest) {
109 android::status_t read_result;
110 android::status_t write_result;
111 event_loop_->PostTask([&write_result, &pipe]() {write_result = pipe.writeSignal();});
112 // Read data from pipe when fd is ready for input.
113 EXPECT_TRUE(event_loop_->WatchFileDescriptor(
115 EventLoop::kModeInput,
116 [&read_result, &pipe, this](int fd) {
117 read_result = pipe.readSignal();
118 event_loop_->TriggerExit();}));
120 EXPECT_EQ(true, read_result);
121 EXPECT_EQ(true, write_result);
124 TEST_F(WificondLooperBackedEventLoopTest, LooperBackedEventLoopWatchFdOutputReadyTest) {
126 android::status_t write_result;
127 // Write data to pipe when fd is ready for output.
128 EXPECT_TRUE(event_loop_->WatchFileDescriptor(
130 EventLoop::kModeOutput,
131 [&write_result, &pipe, this](int fd) {
132 write_result = pipe.writeSignal();
133 event_loop_->TriggerExit();}));
135 EXPECT_EQ(true, write_result);
136 EXPECT_EQ(true, pipe.readSignal());
139 } // namespace wificond
140 } // namespace android