Home | History | Annotate | Download | only in unittests
      1 /*
      2  * Copyright (C) 2018 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 <gmock/gmock.h>
     21 #include <gtest/gtest.h>
     22 
     23 #include <log/log.h>
     24 
     25 #include "AsyncCallRecorder.h"
     26 #include "EventControlThread.h"
     27 
     28 namespace android {
     29 namespace {
     30 
     31 using namespace std::chrono_literals;
     32 using testing::_;
     33 
     34 class EventControlThreadTest : public testing::Test {
     35 protected:
     36     EventControlThreadTest();
     37     ~EventControlThreadTest() override;
     38 
     39     void createThread();
     40 
     41     void expectVSyncEnableCallbackCalled(bool enable);
     42 
     43     AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
     44 
     45     std::unique_ptr<EventControlThread> mThread;
     46 };
     47 
     48 EventControlThreadTest::EventControlThreadTest() {
     49     const ::testing::TestInfo* const test_info =
     50             ::testing::UnitTest::GetInstance()->current_test_info();
     51     ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
     52 }
     53 
     54 EventControlThreadTest::~EventControlThreadTest() {
     55     const ::testing::TestInfo* const test_info =
     56             ::testing::UnitTest::GetInstance()->current_test_info();
     57     ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
     58 }
     59 
     60 void EventControlThreadTest::createThread() {
     61     mThread = std::make_unique<android::impl::EventControlThread>(
     62             mVSyncSetEnabledCallRecorder.getInvocable());
     63 }
     64 
     65 void EventControlThreadTest::expectVSyncEnableCallbackCalled(bool expectedEnabled) {
     66     auto args = mVSyncSetEnabledCallRecorder.waitForCall();
     67     ASSERT_TRUE(args.has_value());
     68     EXPECT_EQ(std::get<0>(args.value()), expectedEnabled);
     69 }
     70 
     71 /* ------------------------------------------------------------------------
     72  * Test cases
     73  */
     74 
     75 TEST_F(EventControlThreadTest, signalsVSyncDisabledOnStartup) {
     76     createThread();
     77 
     78     // On thread start, there should be an automatic explicit call to disable
     79     // vsyncs
     80     expectVSyncEnableCallbackCalled(false);
     81 }
     82 
     83 TEST_F(EventControlThreadTest, signalsVSyncDisabledOnce) {
     84     createThread();
     85     expectVSyncEnableCallbackCalled(false);
     86 
     87     mThread->setVsyncEnabled(false);
     88 
     89     EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
     90 }
     91 
     92 TEST_F(EventControlThreadTest, signalsVSyncEnabledThenDisabled) {
     93     createThread();
     94     expectVSyncEnableCallbackCalled(false);
     95 
     96     mThread->setVsyncEnabled(true);
     97 
     98     expectVSyncEnableCallbackCalled(true);
     99 
    100     mThread->setVsyncEnabled(false);
    101 
    102     expectVSyncEnableCallbackCalled(false);
    103 }
    104 
    105 TEST_F(EventControlThreadTest, signalsVSyncEnabledOnce) {
    106     createThread();
    107     expectVSyncEnableCallbackCalled(false);
    108 
    109     mThread->setVsyncEnabled(true);
    110 
    111     expectVSyncEnableCallbackCalled(true);
    112 
    113     mThread->setVsyncEnabled(true);
    114 
    115     EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
    116 }
    117 
    118 } // namespace
    119 } // namespace android
    120