• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ui_controller.h"
17 
18 #include <utility>
19 #include "gtest/gtest.h"
20 
21 using namespace OHOS::uitest;
22 using namespace std;
23 
24 class UiControllerTest : public testing::Test {
25 protected:
26     /**
27      * Remove registered controllers at teardown.
28      * */
TearDown()29     void TearDown() override
30     {
31         UiController::RemoveAllControllers();
32     }
33 };
34 
TEST_F(UiControllerTest,testGetControllerImplWithNoneRegistered)35 TEST_F(UiControllerTest, testGetControllerImplWithNoneRegistered)
36 {
37     ASSERT_EQ(nullptr, UiController::GetController(""));
38 }
39 
40 class DummyController : public UiController {
41 public:
DummyController(string_view name,string_view device)42     explicit DummyController(string_view name, string_view device) : UiController(name, device) {}
43 
~DummyController()44     ~DummyController() {}
45 
GetCurrentUiDom(nlohmann::json & out) const46     void GetCurrentUiDom(nlohmann::json& out) const override {}
47 
IsWorkable() const48     bool IsWorkable() const override
49     {
50         return workable_;
51     }
52 
SetWorkable(bool wb)53     void SetWorkable(bool wb)
54     {
55         this->workable_ = wb;
56     }
57 
58 private:
59     bool workable_ = false;
60 };
61 
TEST_F(UiControllerTest,addAndRemoveController)62 TEST_F(UiControllerTest, addAndRemoveController)
63 {
64     auto c1 = make_unique<DummyController>("controller1", "");
65     auto c2 = make_unique<DummyController>("controller2", "");
66     c1->SetWorkable(true);
67     c2->SetWorkable(true);
68     UiController::RegisterController(move(c1), Priority::LOW);
69     UiController::RegisterController(move(c2), Priority::HIGH);
70     auto ctrl0 = UiController::GetController("");
71     ASSERT_TRUE(ctrl0 != nullptr);
72     ASSERT_EQ("controller2", ctrl0->GetName()) << "Should get c2 because it has higher priority";
73 
74     UiController::RemoveController("controller2");
75     auto ctrl1 = UiController::GetController("");
76     ASSERT_TRUE(ctrl1 != nullptr);
77     ASSERT_EQ("controller1", ctrl1->GetName()) << "Should get c1 because c2 is is removed";
78 
79     UiController::RemoveController("controller1");
80     ASSERT_EQ(nullptr, UiController::GetController("")) << "Should get null because all controllers are removed";
81 }
82 
TEST_F(UiControllerTest,controllerPriority)83 TEST_F(UiControllerTest, controllerPriority)
84 {
85     auto c1 = make_unique<DummyController>("controller1", "");
86     auto c2 = make_unique<DummyController>("controller2", "");
87     auto c3 = make_unique<DummyController>("controller3", "");
88     c1->SetWorkable(true);
89     c2->SetWorkable(true);
90     c3->SetWorkable(true);
91     UiController::RegisterController(move(c1), Priority::LOW);
92     UiController::RegisterController(move(c2), Priority::HIGH);
93     UiController::RegisterController(move(c3), Priority::MEDIUM);
94     auto controller = UiController::GetController("");
95     ASSERT_TRUE(controller != nullptr);
96     ASSERT_EQ("controller2", controller->GetName()) << "Should get ctrl2 because it has highest priority";
97 }
98 
TEST_F(UiControllerTest,noWorkableController)99 TEST_F(UiControllerTest, noWorkableController)
100 {
101     auto c1 = make_unique<DummyController>("controller1", "");
102     auto c2 = make_unique<DummyController>("controller2", "");
103     c1->SetWorkable(false);
104     c2->SetWorkable(false);
105     UiController::RegisterController(move(c1), Priority::LOW);
106     UiController::RegisterController(move(c2), Priority::HIGH);
107     ASSERT_EQ(nullptr, UiController::GetController("")) << "No workable controller should get";
108 }
109 
TEST_F(UiControllerTest,testControllerWorkable)110 TEST_F(UiControllerTest, testControllerWorkable)
111 {
112     auto c1 = make_unique<DummyController>("controller1", "");
113     auto c2 = make_unique<DummyController>("controller2", "");
114     c1->SetWorkable(true);
115     c2->SetWorkable(false);
116     UiController::RegisterController(move(c1), Priority::LOW);
117     UiController::RegisterController(move(c2), Priority::HIGH);
118     auto controller = UiController::GetController("");
119     ASSERT_TRUE(controller != nullptr);
120     ASSERT_EQ("controller1", controller->GetName()) << "Should get ctrl1 because ctrl2 is not workable";
121 }
122 
TEST_F(UiControllerTest,getControllerForDevice)123 TEST_F(UiControllerTest, getControllerForDevice)
124 {
125     auto c1 = make_unique<DummyController>("controller1", "device1");
126     auto c2 = make_unique<DummyController>("controller2", "device2");
127     c1->SetWorkable(true);
128     c2->SetWorkable(true);
129     UiController::RegisterController(move(c1), Priority::MEDIUM);
130     UiController::RegisterController(move(c2), Priority::MEDIUM);
131     auto ctrlA = UiController::GetController("device1");
132     ASSERT_NE(nullptr, ctrlA);
133     ASSERT_EQ("controller1", ctrlA->GetName()) << "Should get controller1 for device1";
134 
135     UiController::RemoveController("controller1");
136     auto ctrlB = UiController::GetController("device1");
137     ASSERT_EQ(nullptr, ctrlB) << "Should get null since no available controller for device1";
138 
139     auto ctrlC = UiController::GetController("device2");
140     ASSERT_NE(nullptr, ctrlC);
141     ASSERT_EQ("controller2", ctrlC->GetName()) << "Should get controller2 for device2";
142 }
143 
TEST_F(UiControllerTest,controllerProvider)144 TEST_F(UiControllerTest, controllerProvider)
145 {
146     UiController::RegisterControllerProvider(nullptr);
147     ASSERT_EQ(nullptr, UiController::GetController("dummy_device"));
148 
149     auto controllerProvider = [](string_view device, list<unique_ptr<UiController>> &receiver) {
150         if (device == "dummy_device") {
151             auto controller = make_unique<DummyController>("dummy_controller", "dummy_device");
152             controller->SetWorkable(true);
153             receiver.push_back(move(controller));
154         }
155     };
156 
157     // register controllerProvider for 'dummy_device' only
158     UiController::RegisterControllerProvider(controllerProvider);
159     UiController::InstallForDevice("dummy_device");
160     ASSERT_NE(nullptr, UiController::GetController("dummy_device"));
161     ASSERT_EQ(nullptr, UiController::GetController("dummy_device_2"));
162     UiController::RemoveController("dummy_controller");
163     ASSERT_EQ(nullptr, UiController::GetController("dummy_device"));
164 }