xref: /aosp_15_r20/hardware/libhardware/tests/input/evdev/TestHelpers.h (revision e01b6f769022e40d0923dee176e8dc7cd1d52984)
1*e01b6f76SAndroid Build Coastguard Worker /*
2*e01b6f76SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*e01b6f76SAndroid Build Coastguard Worker  *
4*e01b6f76SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*e01b6f76SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*e01b6f76SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*e01b6f76SAndroid Build Coastguard Worker  *
8*e01b6f76SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*e01b6f76SAndroid Build Coastguard Worker  *
10*e01b6f76SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*e01b6f76SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*e01b6f76SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e01b6f76SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*e01b6f76SAndroid Build Coastguard Worker  * limitations under the License.
15*e01b6f76SAndroid Build Coastguard Worker  */
16*e01b6f76SAndroid Build Coastguard Worker 
17*e01b6f76SAndroid Build Coastguard Worker #ifndef ANDROID_TEST_HELPERS_H_
18*e01b6f76SAndroid Build Coastguard Worker #define ANDROID_TEST_HELPERS_H_
19*e01b6f76SAndroid Build Coastguard Worker 
20*e01b6f76SAndroid Build Coastguard Worker #include <future>
21*e01b6f76SAndroid Build Coastguard Worker #include <thread>
22*e01b6f76SAndroid Build Coastguard Worker 
23*e01b6f76SAndroid Build Coastguard Worker namespace android {
24*e01b6f76SAndroid Build Coastguard Worker 
25*e01b6f76SAndroid Build Coastguard Worker /**
26*e01b6f76SAndroid Build Coastguard Worker  * Runs the given function after the specified delay.
27*e01b6f76SAndroid Build Coastguard Worker  * NOTE: if the std::future returned from std::async is not bound, this function
28*e01b6f76SAndroid Build Coastguard Worker  * will block until the task completes. This is almost certainly NOT what you
29*e01b6f76SAndroid Build Coastguard Worker  * want, so save the return value from delay_async into a variable:
30*e01b6f76SAndroid Build Coastguard Worker  *
31*e01b6f76SAndroid Build Coastguard Worker  * auto f = delay_async(100ms, []{ ALOGD("Hello world"); });
32*e01b6f76SAndroid Build Coastguard Worker  */
33*e01b6f76SAndroid Build Coastguard Worker template<class Function, class Duration>
decltype(auto)34*e01b6f76SAndroid Build Coastguard Worker decltype(auto) delay_async(Duration&& delay, Function&& task)
35*e01b6f76SAndroid Build Coastguard Worker {
36*e01b6f76SAndroid Build Coastguard Worker     return std::async(std::launch::async, [=]{ std::this_thread::sleep_for(delay); task(); });
37*e01b6f76SAndroid Build Coastguard Worker }
38*e01b6f76SAndroid Build Coastguard Worker 
39*e01b6f76SAndroid Build Coastguard Worker /**
40*e01b6f76SAndroid Build Coastguard Worker  * Creates and opens a temporary file at the given path. The file is unlinked
41*e01b6f76SAndroid Build Coastguard Worker  * and closed in the destructor.
42*e01b6f76SAndroid Build Coastguard Worker  */
43*e01b6f76SAndroid Build Coastguard Worker class TempFile {
44*e01b6f76SAndroid Build Coastguard Worker public:
45*e01b6f76SAndroid Build Coastguard Worker     explicit TempFile(const char* path);
46*e01b6f76SAndroid Build Coastguard Worker     ~TempFile();
47*e01b6f76SAndroid Build Coastguard Worker 
48*e01b6f76SAndroid Build Coastguard Worker     // No copy or assign
49*e01b6f76SAndroid Build Coastguard Worker     TempFile(const TempFile&) = delete;
50*e01b6f76SAndroid Build Coastguard Worker     TempFile& operator=(const TempFile&) = delete;
51*e01b6f76SAndroid Build Coastguard Worker 
getName()52*e01b6f76SAndroid Build Coastguard Worker     const char* getName() const { return mName; }
getFd()53*e01b6f76SAndroid Build Coastguard Worker     int getFd() const { return mFd; }
54*e01b6f76SAndroid Build Coastguard Worker 
55*e01b6f76SAndroid Build Coastguard Worker private:
56*e01b6f76SAndroid Build Coastguard Worker     char* mName;
57*e01b6f76SAndroid Build Coastguard Worker     int mFd;
58*e01b6f76SAndroid Build Coastguard Worker };
59*e01b6f76SAndroid Build Coastguard Worker 
60*e01b6f76SAndroid Build Coastguard Worker /**
61*e01b6f76SAndroid Build Coastguard Worker  * Creates a temporary directory that can create temporary files. The directory
62*e01b6f76SAndroid Build Coastguard Worker  * is emptied and deleted in the destructor.
63*e01b6f76SAndroid Build Coastguard Worker  */
64*e01b6f76SAndroid Build Coastguard Worker class TempDir {
65*e01b6f76SAndroid Build Coastguard Worker public:
66*e01b6f76SAndroid Build Coastguard Worker     TempDir();
67*e01b6f76SAndroid Build Coastguard Worker     ~TempDir();
68*e01b6f76SAndroid Build Coastguard Worker 
69*e01b6f76SAndroid Build Coastguard Worker     // No copy or assign
70*e01b6f76SAndroid Build Coastguard Worker     TempDir(const TempDir&) = delete;
71*e01b6f76SAndroid Build Coastguard Worker     TempDir& operator=(const TempDir&) = delete;
72*e01b6f76SAndroid Build Coastguard Worker 
getName()73*e01b6f76SAndroid Build Coastguard Worker     const char* getName() const { return mName; }
74*e01b6f76SAndroid Build Coastguard Worker 
75*e01b6f76SAndroid Build Coastguard Worker     TempFile* newTempFile();
76*e01b6f76SAndroid Build Coastguard Worker 
77*e01b6f76SAndroid Build Coastguard Worker private:
78*e01b6f76SAndroid Build Coastguard Worker     char* mName;
79*e01b6f76SAndroid Build Coastguard Worker };
80*e01b6f76SAndroid Build Coastguard Worker 
81*e01b6f76SAndroid Build Coastguard Worker }  // namespace android
82*e01b6f76SAndroid Build Coastguard Worker 
83*e01b6f76SAndroid Build Coastguard Worker #endif  // ANDROID_TEST_HELPERS_H_
84