xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/tests/utils/WindowInfosListenerUtils.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright 2023 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 #include <android-base/properties.h>
18 #include <gtest/gtest.h>
19 #include <gui/SurfaceComposerClient.h>
20 #include <private/android_filesystem_config.h>
21 #include <cstdint>
22 #include <future>
23 
24 namespace android {
25 
26 using base::HwTimeoutMultiplier;
27 using gui::DisplayInfo;
28 using gui::WindowInfo;
29 
30 using WindowInfosPredicate = std::function<bool(const std::vector<WindowInfo>&)>;
31 
32 class WindowInfosListenerUtils {
33 public:
WindowInfosListenerUtils()34     WindowInfosListenerUtils() { mClient = sp<SurfaceComposerClient>::make(); }
35 
waitForWindowInfosPredicate(const WindowInfosPredicate & predicate)36     bool waitForWindowInfosPredicate(const WindowInfosPredicate& predicate) {
37         std::promise<void> promise;
38         auto listener = sp<WindowInfosListener>::make(std::move(predicate), promise);
39         mClient->addWindowInfosListener(listener);
40         auto future = promise.get_future();
41         bool satisfied = future.wait_for(std::chrono::seconds{5 * HwTimeoutMultiplier()}) ==
42                 std::future_status::ready;
43         mClient->removeWindowInfosListener(listener);
44         return satisfied;
45     }
46 
findMatchingWindowInfo(const WindowInfo & targetWindowInfo,const std::vector<WindowInfo> & windowInfos)47     static const WindowInfo* findMatchingWindowInfo(const WindowInfo& targetWindowInfo,
48                                                     const std::vector<WindowInfo>& windowInfos) {
49         for (const WindowInfo& windowInfo : windowInfos) {
50             if (windowInfo.token == targetWindowInfo.token) {
51                 return &windowInfo;
52             }
53         }
54         return nullptr;
55     }
56 
57 private:
58     struct WindowInfosListener : public gui::WindowInfosListener {
59     public:
WindowInfosListenerWindowInfosListener60         WindowInfosListener(WindowInfosPredicate predicate, std::promise<void>& promise)
61               : mPredicate(std::move(predicate)), mPromise(promise) {}
62 
onWindowInfosChangedWindowInfosListener63         void onWindowInfosChanged(const gui::WindowInfosUpdate& update) override {
64             if (mPredicate(update.windowInfos)) {
65                 mPromise.set_value();
66             }
67         }
68 
69     private:
70         WindowInfosPredicate mPredicate;
71         std::promise<void>& mPromise;
72     };
73 
74     sp<SurfaceComposerClient> mClient;
75 };
76 
77 } // namespace android
78