summaryrefslogtreecommitdiff
path: root/displayservice/1.0/vts/functional/VtsFwkDisplayServiceV1_0TargetTest.cpp
blob: e99388284ab8365db7e30093e1dbf079612ddfef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "VtsFwkDisplayServiceV1_0TargetTest"

#include <android/frameworks/displayservice/1.0/IDisplayEventReceiver.h>
#include <android/frameworks/displayservice/1.0/IDisplayService.h>
#include <android/frameworks/displayservice/1.0/IEventCallback.h>
#include <gtest/gtest.h>
#include <hidl/GtestPrinter.h>
#include <hidl/ServiceManagement.h>
#include <log/log.h>

#include <atomic>
#include <chrono>
#include <cmath>
#include <inttypes.h>
#include <thread>

using ::android::frameworks::displayservice::V1_0::IDisplayEventReceiver;
using ::android::frameworks::displayservice::V1_0::IDisplayService;
using ::android::frameworks::displayservice::V1_0::IEventCallback;
using ::android::frameworks::displayservice::V1_0::Status;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::sp;
using namespace ::std::chrono_literals;

#define ASSERT_OK(ret) ASSERT_TRUE((ret).isOk())
#define EXPECT_SUCCESS(retExpr) do { \
        Return<Status> retVal = (retExpr); \
        ASSERT_OK(retVal); \
        EXPECT_EQ(Status::SUCCESS, static_cast<Status>(retVal)); \
    } while(false)
#define EXPECT_BAD_VALUE(retExpr) do { \
        Return<Status> retVal = (retExpr); \
        ASSERT_OK(retVal); \
        EXPECT_EQ(Status::BAD_VALUE, static_cast<Status>(retVal)); \
    } while(false)

#define MAX_INACCURACY 3

class TestCallback : public IEventCallback {
public:
    Return<void> onVsync(uint64_t timestamp, uint32_t count) override {
        ALOGE("onVsync: timestamp=%" PRIu64 " count=%d", timestamp, count);

        vsyncs++;
        return Void();
    }
    Return<void> onHotplug(uint64_t timestamp, bool connected) override {
        ALOGE("onVsync: timestamp=%" PRIu64 " connected=%s", timestamp, connected ? "true" : "false");

        hotplugs++;
        return Void();
    }

    std::atomic<int> vsyncs{0};
    std::atomic<int> hotplugs{0};
};

class DisplayServiceTest : public ::testing::TestWithParam<std::string> {
public:
    virtual void SetUp() override {
        sp<IDisplayService> service = IDisplayService::getService(GetParam());

        ASSERT_NE(service, nullptr);

        Return<sp<IDisplayEventReceiver>> ret = service->getEventReceiver();
        ASSERT_OK(ret);

        receiver = ret;
        ASSERT_NE(receiver, nullptr);


        cb = new TestCallback();
        EXPECT_SUCCESS(receiver->init(cb));
    }

    virtual void TearDown() override {
        EXPECT_SUCCESS(receiver->close());
    }

    sp<TestCallback> cb;
    sp<IDisplayEventReceiver> receiver;
};

/**
 * No vsync events should happen unless you explicitly request one.
 */
TEST_P(DisplayServiceTest, TestAttachRequestVsync) {
    EXPECT_EQ(0, cb->vsyncs);

    EXPECT_SUCCESS(receiver->requestNextVsync());

    std::this_thread::sleep_for(100ms); // framerate is not fixed on Android devices
    EXPECT_EQ(1, cb->vsyncs);
}

/**
 * Vsync rate respects count.
 */
TEST_P(DisplayServiceTest, TestSetVsyncRate) {
    ASSERT_EQ(0, cb->vsyncs);

    EXPECT_SUCCESS(receiver->setVsyncRate(1));
    std::this_thread::sleep_for(250ms);
    int at1 = cb->vsyncs;

    cb->vsyncs = 0;
    EXPECT_SUCCESS(receiver->setVsyncRate(2));
    std::this_thread::sleep_for(250ms);
    int at2 = cb->vsyncs;

    cb->vsyncs = 0;
    EXPECT_SUCCESS(receiver->setVsyncRate(4));
    std::this_thread::sleep_for(250ms);
    int at4 = cb->vsyncs;

    EXPECT_NE(0, at1);
    EXPECT_NE(0, at2);
    EXPECT_NE(0, at4);

    EXPECT_LE(std::abs(at1 - 2 * at2), 2 * MAX_INACCURACY);
    EXPECT_LE(std::abs(at1 - 4 * at4), 4 * MAX_INACCURACY);
    EXPECT_LE(std::abs(at2 - 2 * at4), 2 * MAX_INACCURACY);

    ALOGE("Vsync counts: %d %d %d", at1, at2, at4);
}

/**
 * Open/close should return proper error results.
 */
TEST_P(DisplayServiceTest, TestOpenClose) {
    EXPECT_BAD_VALUE(receiver->init(cb)); // already opened in SetUp
    EXPECT_SUCCESS(receiver->close()); // can close what was originally opened
    EXPECT_BAD_VALUE(receiver->close()); // can't close again
    EXPECT_SUCCESS(receiver->init(cb)); // open so can close again in SetUp
}

/**
 * Vsync must be given a value that is >= 0.
 */
TEST_P(DisplayServiceTest, TestVsync) {
    EXPECT_SUCCESS(receiver->setVsyncRate(0));
    EXPECT_SUCCESS(receiver->setVsyncRate(5));
    EXPECT_SUCCESS(receiver->setVsyncRate(0));
    EXPECT_BAD_VALUE(receiver->setVsyncRate(-1));
    EXPECT_BAD_VALUE(receiver->setVsyncRate(-1000));
}

INSTANTIATE_TEST_SUITE_P(
    PerInstance, DisplayServiceTest,
    testing::ValuesIn(android::hardware::getAllHalInstanceNames(IDisplayService::descriptor)),
    android::hardware::PrintInstanceNameToString);