blob: 05858bf8394ed356682293b780900eb39989e12c [file] [log] [blame]
Ady Abraham4a474e82020-10-02 15:47:55 -07001/*
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#include <thread>
18
19#include <gtest/gtest.h>
20
21#include <gui/SurfaceComposerClient.h>
22#include <private/gui/ComposerService.h>
23
24static constexpr int kRefreshRateOverlayCode = 1034;
25static constexpr int kRefreshRateOverlayEnable = 1;
26static constexpr int kRefreshRateOverlayDisable = 0;
27static constexpr int kRefreshRateOverlayQuery = 2;
28
29// These values must match the ones we used for developer options in
30// com.android.settings.development.ShowRefreshRatePreferenceController
31static_assert(kRefreshRateOverlayCode == 1034);
32static_assert(kRefreshRateOverlayEnable == 1);
33static_assert(kRefreshRateOverlayDisable == 0);
34static_assert(kRefreshRateOverlayQuery == 2);
35
36namespace android {
37
38namespace {
39void sendCommandToSf(int command, Parcel& reply) {
40 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
41 Parcel request;
42 request.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
43 request.writeInt32(command);
44 ASSERT_EQ(NO_ERROR,
45 IInterface::asBinder(sf)->transact(kRefreshRateOverlayCode, request, &reply));
46}
47
48bool isOverlayEnabled() {
49 Parcel reply;
50 sendCommandToSf(kRefreshRateOverlayQuery, reply);
51 return reply.readBool();
52}
53
54void waitForOverlay(bool enabled) {
55 static constexpr auto kTimeout = std::chrono::nanoseconds(1s);
56 static constexpr auto kIterations = 10;
57 for (int i = 0; i < kIterations; i++) {
58 if (enabled == isOverlayEnabled()) {
59 return;
60 }
61 std::this_thread::sleep_for(kTimeout / kIterations);
62 }
63}
64
65void toggleOverlay(bool enabled) {
66 if (enabled == isOverlayEnabled()) {
67 return;
68 }
69
70 Parcel reply;
71 const auto command = enabled ? kRefreshRateOverlayEnable : kRefreshRateOverlayDisable;
72 sendCommandToSf(command, reply);
73 waitForOverlay(enabled);
74 ASSERT_EQ(enabled, isOverlayEnabled());
75}
76
77} // namespace
78
79TEST(RefreshRateOverlayTest, enableOverlay) {
80 toggleOverlay(true);
81}
82
83TEST(RefreshRateOverlayTest, disableOverlay) {
84 toggleOverlay(false);
85}
86
87TEST(RefreshRateOverlayTest, enableAndDisableOverlay) {
88 toggleOverlay(true);
89 toggleOverlay(false);
90
91 toggleOverlay(true);
92 toggleOverlay(false);
93}
94
95} // namespace android