1e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland/*
2e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland * Copyright (C) 2016 The Android Open Source Project
3e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland *
4e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland * Licensed under the Apache License, Version 2.0 (the "License");
5e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland * you may not use this file except in compliance with the License.
6e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland * You may obtain a copy of the License at
7e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland *
8e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland *      http://www.apache.org/licenses/LICENSE-2.0
9e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland *
10e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland * Unless required by applicable law or agreed to in writing, software
11e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland * distributed under the License is distributed on an "AS IS" BASIS,
12e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland * See the License for the specific language governing permissions and
14e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland * limitations under the License.
15e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland */
16e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
17e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland#define LOG_TAG "VtsFwkDisplayServiceV1_0TargetTest"
18e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
19e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland#include <android/frameworks/displayservice/1.0/IDisplayEventReceiver.h>
20e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland#include <android/frameworks/displayservice/1.0/IDisplayService.h>
21e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland#include <android/frameworks/displayservice/1.0/IEventCallback.h>
22e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland#include <log/log.h>
23e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland#include <VtsHalHidlTargetTestBase.h>
24e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
25e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland#include <atomic>
26e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland#include <chrono>
27e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland#include <cmath>
28e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland#include <inttypes.h>
29e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland#include <thread>
30e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
31e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Morelandusing ::android::frameworks::displayservice::V1_0::IDisplayEventReceiver;
32e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Morelandusing ::android::frameworks::displayservice::V1_0::IDisplayService;
33e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Morelandusing ::android::frameworks::displayservice::V1_0::IEventCallback;
34e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Morelandusing ::android::frameworks::displayservice::V1_0::Status;
35e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Morelandusing ::android::hardware::Return;
36e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Morelandusing ::android::hardware::Void;
37e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Morelandusing ::android::sp;
38e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Morelandusing namespace ::std::chrono_literals;
39e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
40e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland#define ASSERT_OK(ret) ASSERT_TRUE((ret).isOk())
41e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland#define EXPECT_SUCCESS(retExpr) do { \
42e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        Return<Status> retVal = (retExpr); \
43e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        ASSERT_OK(retVal); \
44e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        EXPECT_EQ(Status::SUCCESS, static_cast<Status>(retVal)); \
45e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    } while(false)
46e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland#define EXPECT_BAD_VALUE(retExpr) do { \
47e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        Return<Status> retVal = (retExpr); \
48e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        ASSERT_OK(retVal); \
49e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        EXPECT_EQ(Status::BAD_VALUE, static_cast<Status>(retVal)); \
50e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    } while(false)
51e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
52e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland#define MAX_INACCURACY 3
53e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
54e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Morelandclass TestCallback : public IEventCallback {
55e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Morelandpublic:
56e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    Return<void> onVsync(uint64_t timestamp, uint32_t count) override {
57e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        ALOGE("onVsync: timestamp=%" PRIu64 " count=%d", timestamp, count);
58e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
59e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        vsyncs++;
60e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        return Void();
61e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    }
62e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    Return<void> onHotplug(uint64_t timestamp, bool connected) override {
63e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        ALOGE("onVsync: timestamp=%" PRIu64 " connected=%s", timestamp, connected ? "true" : "false");
64e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
65e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        hotplugs++;
66e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        return Void();
67e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    }
68e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
69e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    std::atomic<int> vsyncs{0};
70e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    std::atomic<int> hotplugs{0};
71e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland};
72e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
73e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Morelandclass DisplayServiceTest : public ::testing::VtsHalHidlTargetTestBase {
74e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Morelandpublic:
75e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    ~DisplayServiceTest() {}
76e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
77e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    virtual void SetUp() override {
78e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        sp<IDisplayService> service = ::testing::VtsHalHidlTargetTestBase::getService<IDisplayService>();
79e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
80e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        ASSERT_NE(service, nullptr);
81e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
82e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        Return<sp<IDisplayEventReceiver>> ret = service->getEventReceiver();
83e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        ASSERT_OK(ret);
84e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
85e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        receiver = ret;
86e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        ASSERT_NE(receiver, nullptr);
87e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
88e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
89e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        cb = new TestCallback();
90e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        EXPECT_SUCCESS(receiver->init(cb));
91e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    }
92e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
93e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    virtual void TearDown() override {
94e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland        EXPECT_SUCCESS(receiver->close());
95e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    }
96e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
97e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    sp<TestCallback> cb;
98e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    sp<IDisplayEventReceiver> receiver;
99e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland};
100e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
101e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland/**
102e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland * No vsync events should happen unless you explicitly request one.
103e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland */
104e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven MorelandTEST_F(DisplayServiceTest, TestAttachRequestVsync) {
105e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_EQ(0, cb->vsyncs);
106e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
107e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_SUCCESS(receiver->requestNextVsync());
108e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
109e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    std::this_thread::sleep_for(100ms); // framerate is not fixed on Android devices
110e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_EQ(1, cb->vsyncs);
111e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland}
112e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
113e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland/**
114e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland * Vsync rate respects count.
115e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland */
116e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven MorelandTEST_F(DisplayServiceTest, TestSetVsyncRate) {
117e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    ASSERT_EQ(0, cb->vsyncs);
118e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
119e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_SUCCESS(receiver->setVsyncRate(1));
120e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    std::this_thread::sleep_for(250ms);
121e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    int at1 = cb->vsyncs;
122e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
123e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    cb->vsyncs = 0;
124e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_SUCCESS(receiver->setVsyncRate(2));
125e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    std::this_thread::sleep_for(250ms);
126e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    int at2 = cb->vsyncs;
127e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
128e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    cb->vsyncs = 0;
129e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_SUCCESS(receiver->setVsyncRate(4));
130e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    std::this_thread::sleep_for(250ms);
131e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    int at4 = cb->vsyncs;
132e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
133e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_NE(0, at1);
134e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_NE(0, at2);
135e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_NE(0, at4);
136e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
137e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_LE(std::abs(at1 - 2 * at2), 2 * MAX_INACCURACY);
138e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_LE(std::abs(at1 - 4 * at4), 4 * MAX_INACCURACY);
139e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_LE(std::abs(at2 - 2 * at4), 2 * MAX_INACCURACY);
140e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
141e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    ALOGE("Vsync counts: %d %d %d", at1, at2, at4);
142e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland}
143e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
144e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland/**
145e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland * Open/close should return proper error results.
146e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland */
147e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven MorelandTEST_F(DisplayServiceTest, TestOpenClose) {
148e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_BAD_VALUE(receiver->init(cb)); // already opened in SetUp
149e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_SUCCESS(receiver->close()); // can close what was originally opened
150e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_BAD_VALUE(receiver->close()); // can't close again
151e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_SUCCESS(receiver->init(cb)); // open so can close again in SetUp
152e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland}
153e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
154e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland/**
155e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland * Vsync must be given a value that is >= 0.
156e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland */
157e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven MorelandTEST_F(DisplayServiceTest, TestVsync) {
158e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_SUCCESS(receiver->setVsyncRate(0));
159e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_SUCCESS(receiver->setVsyncRate(5));
160e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_SUCCESS(receiver->setVsyncRate(0));
161e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_BAD_VALUE(receiver->setVsyncRate(-1));
162e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    EXPECT_BAD_VALUE(receiver->setVsyncRate(-1000));
163e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland}
164e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland
165e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Morelandint main(int argc, char **argv) {
166e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    ::testing::InitGoogleTest(&argc, argv);
167e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    int status = RUN_ALL_TESTS();
168e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    ALOGE("Test status = %d", status);
169e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland    return status;
170e7fc8b65b0efa7015afb9c1e59069a081f192fcfSteven Moreland}
171