1 /*
2 * Copyright 2020 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 #undef LOG_TAG
18 #define LOG_TAG "LibSurfaceFlingerUnittests"
19
20 #include <chrono>
21 #include <thread>
22
23 #include "DisplayTransactionTestHelpers.h"
24 #include "FakeDisplayInjector.h"
25
26 #include <aidl/android/hardware/power/Boost.h>
27
28 namespace android {
29 namespace {
30
31 using aidl::android::hardware::power::Boost;
32
TEST_F(DisplayTransactionTest,notifyPowerBoostNotifiesTouchEvent)33 TEST_F(DisplayTransactionTest, notifyPowerBoostNotifiesTouchEvent) {
34 using namespace std::chrono_literals;
35
36 std::mutex timerMutex;
37 std::condition_variable cv;
38
39 injectDefaultInternalDisplay([](FakeDisplayDeviceInjector&) {});
40
41 std::unique_lock lock(timerMutex);
42 bool didReset = false; // keeps track of what the most recent call was
43
44 auto waitForTimerReset = [&] { cv.wait_for(lock, 100ms, [&] { return didReset; }); };
45 auto waitForTimerExpired = [&] { cv.wait_for(lock, 100ms, [&] { return !didReset; }); };
46
47 // Add extra logic to unblock the test when the timer callbacks get called
48 mFlinger.scheduler()->replaceTouchTimer(10, [&](bool isReset) {
49 {
50 std::unique_lock lock(timerMutex); // guarantee we're waiting on the cv
51 didReset = isReset;
52 }
53 cv.notify_one(); // wake the cv
54 std::unique_lock lock(timerMutex); // guarantee we finished the cv logic
55 });
56
57 waitForTimerReset();
58 EXPECT_TRUE(mFlinger.scheduler()->isTouchActive()); // Starting timer activates touch
59
60 waitForTimerExpired();
61 EXPECT_FALSE(mFlinger.scheduler()->isTouchActive()); // Stopping timer deactivates touch
62
63 EXPECT_EQ(NO_ERROR, mFlinger.notifyPowerBoost(static_cast<int32_t>(Boost::CAMERA_SHOT)));
64
65 EXPECT_FALSE(mFlinger.scheduler()->isTouchActive());
66 // Wait for the timer to start just in case
67 waitForTimerReset();
68 EXPECT_FALSE(mFlinger.scheduler()->isTouchActive());
69 // Wait for the timer to stop, again just in case
70 waitForTimerExpired();
71 EXPECT_FALSE(mFlinger.scheduler()->isTouchActive());
72
73 EXPECT_EQ(NO_ERROR, mFlinger.notifyPowerBoost(static_cast<int32_t>(Boost::INTERACTION)));
74 waitForTimerReset();
75 EXPECT_TRUE(mFlinger.scheduler()->isTouchActive());
76 }
77
78 } // namespace
79 } // namespace android
80