Ady Abraham | 4a474e8 | 2020-10-02 15:47:55 -0700 | [diff] [blame] | 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 | #include <thread> |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | #include <gui/SurfaceComposerClient.h> |
| 22 | #include <private/gui/ComposerService.h> |
| 23 | |
| 24 | static constexpr int kRefreshRateOverlayCode = 1034; |
| 25 | static constexpr int kRefreshRateOverlayEnable = 1; |
| 26 | static constexpr int kRefreshRateOverlayDisable = 0; |
| 27 | static constexpr int kRefreshRateOverlayQuery = 2; |
| 28 | |
| 29 | // These values must match the ones we used for developer options in |
| 30 | // com.android.settings.development.ShowRefreshRatePreferenceController |
| 31 | static_assert(kRefreshRateOverlayCode == 1034); |
| 32 | static_assert(kRefreshRateOverlayEnable == 1); |
| 33 | static_assert(kRefreshRateOverlayDisable == 0); |
| 34 | static_assert(kRefreshRateOverlayQuery == 2); |
| 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | namespace { |
| 39 | void 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 | |
| 48 | bool isOverlayEnabled() { |
| 49 | Parcel reply; |
| 50 | sendCommandToSf(kRefreshRateOverlayQuery, reply); |
| 51 | return reply.readBool(); |
| 52 | } |
| 53 | |
| 54 | void 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 | |
| 65 | void 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 | |
| 79 | TEST(RefreshRateOverlayTest, enableOverlay) { |
| 80 | toggleOverlay(true); |
| 81 | } |
| 82 | |
| 83 | TEST(RefreshRateOverlayTest, disableOverlay) { |
| 84 | toggleOverlay(false); |
| 85 | } |
| 86 | |
| 87 | TEST(RefreshRateOverlayTest, enableAndDisableOverlay) { |
| 88 | toggleOverlay(true); |
| 89 | toggleOverlay(false); |
| 90 | |
| 91 | toggleOverlay(true); |
| 92 | toggleOverlay(false); |
| 93 | } |
| 94 | |
| 95 | } // namespace android |