1 /*
2  * Copyright (C) 2021 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 "Enumerator.h"
18 #include "MockPermissionsChecker.h"
19 #include "MockServiceFactory.h"
20 #include "MockStatsCollector.h"
21 #include "ServiceFactory.h"
22 
23 #include <android/hardware/automotive/evs/1.1/IEvsEnumerator.h>
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26 
27 using ::android::automotive::evs::V1_1::implementation::Enumerator;
28 using ::android::automotive::evs::V1_1::implementation::NiceMockPermissionsChecker;
29 using ::android::automotive::evs::V1_1::implementation::NiceMockServiceFactory;
30 using ::android::automotive::evs::V1_1::implementation::NiceMockStatsCollector;
31 using ::android::hardware::automotive::evs::V1_1::IEvsDisplay;
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 // Construction Tests.
35 ////////////////////////////////////////////////////////////////////////////////
TEST(Enumerator,BuildsNullObjectWithoutServiceNameProvided)36 TEST(Enumerator, BuildsNullObjectWithoutServiceNameProvided) {
37     EXPECT_EQ(Enumerator::build(nullptr), nullptr);
38 }
39 
TEST(Enumerator,ReturnsNullWhenNullNamePassed)40 TEST(Enumerator, ReturnsNullWhenNullNamePassed) {
41     EXPECT_EQ(Enumerator::build(static_cast<const char*>(nullptr)), nullptr);
42 }
43 
TEST(Enumerator,ReturnsNullWhenServiceNotAvailable)44 TEST(Enumerator, ReturnsNullWhenServiceNotAvailable) {
45     auto mockServiceFactory = std::make_unique<NiceMockServiceFactory>();
46     ON_CALL(*mockServiceFactory, getService).WillByDefault(::testing::Invoke([&]() {
47         return nullptr;
48     }));
49     EXPECT_EQ(Enumerator::build(std::move(mockServiceFactory),
50                                 std::make_unique<NiceMockStatsCollector>(),
51                                 std::make_unique<NiceMockPermissionsChecker>()),
52               nullptr);
53 }
54 
TEST(Enumerator,ConstructsAndDestroys)55 TEST(Enumerator, ConstructsAndDestroys) {
56     EXPECT_NE(Enumerator::build(std::make_unique<NiceMockServiceFactory>(),
57                                 std::make_unique<NiceMockStatsCollector>(),
58                                 std::make_unique<NiceMockPermissionsChecker>()),
59               nullptr);
60 }
61 
62 ////////////////////////////////////////////////////////////////////////////////
63 // Behavioural Tests.
64 ////////////////////////////////////////////////////////////////////////////////
TEST(Enumerator,PreventsGettingDisplayWithNoPermissions)65 TEST(Enumerator, PreventsGettingDisplayWithNoPermissions) {
66     auto mockPermissionsChecker = std::make_unique<NiceMockPermissionsChecker>();
67     ON_CALL(*mockPermissionsChecker, processHasPermissionsForEvs)
68             .WillByDefault(::testing::Return(false));
69 
70     std::unique_ptr<Enumerator> enumerator =
71             Enumerator::build(std::make_unique<NiceMockServiceFactory>(),
72                               std::make_unique<NiceMockStatsCollector>(),
73                               std::move(mockPermissionsChecker));
74 
75     android::sp<IEvsDisplay> evs_display{enumerator->openDisplay_1_1(-1)};
76 }
77