• 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 #include "dummy_controller.h"
21 
22 using namespace OHOS::uitest;
23 using namespace std;
24 
25 class UiControllerTest : public testing::Test {
26 protected:
27     /**
28      * Remove registered controllers at teardown.
29      * */
TearDown()30     void TearDown() override
31     {
32         UiController::RemoveAllControllers();
33     }
34 };
35 
TEST_F(UiControllerTest,testGetControllerImplWithNoneRegistered)36 TEST_F(UiControllerTest, testGetControllerImplWithNoneRegistered)
37 {
38     ASSERT_EQ(nullptr, UiController::GetController());
39 }
40 
TEST_F(UiControllerTest,addAndRemoveController)41 TEST_F(UiControllerTest, addAndRemoveController)
42 {
43     auto c1 = make_unique<DummyController>("controller1");
44     auto c2 = make_unique<DummyController>("controller2");
45     c1->SetWorkable(true);
46     c2->SetWorkable(true);
47     UiController::RegisterController(move(c1), Priority::LOW);
48     UiController::RegisterController(move(c2), Priority::HIGH);
49     auto ctrl0 = UiController::GetController();
50     ASSERT_TRUE(ctrl0 != nullptr);
51     ASSERT_EQ("controller2", ctrl0->GetName()) << "Should get c2 because it has higher priority";
52 
53     UiController::RemoveController("controller2");
54     auto ctrl1 = UiController::GetController();
55     ASSERT_TRUE(ctrl1 != nullptr);
56     ASSERT_EQ("controller1", ctrl1->GetName()) << "Should get c1 because c2 is is removed";
57 
58     UiController::RemoveController("controller1");
59     ASSERT_EQ(nullptr, UiController::GetController()) << "Should get null because all controllers are removed";
60 }
61 
TEST_F(UiControllerTest,controllerPriority)62 TEST_F(UiControllerTest, controllerPriority)
63 {
64     auto c1 = make_unique<DummyController>("controller1");
65     auto c2 = make_unique<DummyController>("controller2");
66     auto c3 = make_unique<DummyController>("controller3");
67     c1->SetWorkable(true);
68     c2->SetWorkable(true);
69     c3->SetWorkable(true);
70     UiController::RegisterController(move(c1), Priority::LOW);
71     UiController::RegisterController(move(c2), Priority::HIGH);
72     UiController::RegisterController(move(c3), Priority::MEDIUM);
73     auto controller = UiController::GetController();
74     ASSERT_TRUE(controller != nullptr);
75     ASSERT_EQ("controller2", controller->GetName()) << "Should get ctrl2 because it has highest priority";
76 }
77 
TEST_F(UiControllerTest,noWorkableController)78 TEST_F(UiControllerTest, noWorkableController)
79 {
80     auto c1 = make_unique<DummyController>("controller1");
81     auto c2 = make_unique<DummyController>("controller2");
82     c1->SetWorkable(false);
83     c2->SetWorkable(false);
84     UiController::RegisterController(move(c1), Priority::LOW);
85     UiController::RegisterController(move(c2), Priority::HIGH);
86     ASSERT_EQ(nullptr, UiController::GetController()) << "No workable controller should get";
87 }
88 
TEST_F(UiControllerTest,testControllerWorkable)89 TEST_F(UiControllerTest, testControllerWorkable)
90 {
91     auto c1 = make_unique<DummyController>("controller1");
92     auto c2 = make_unique<DummyController>("controller2");
93     c1->SetWorkable(true);
94     c2->SetWorkable(false);
95     UiController::RegisterController(move(c1), Priority::LOW);
96     UiController::RegisterController(move(c2), Priority::HIGH);
97     auto controller = UiController::GetController();
98     ASSERT_TRUE(controller != nullptr);
99     ASSERT_EQ("controller1", controller->GetName()) << "Should get ctrl1 because ctrl2 is not workable";
100 }
101 
TEST_F(UiControllerTest,controllerProvider)102 TEST_F(UiControllerTest, controllerProvider)
103 {
104     UiController::RegisterControllerProvider(nullptr);
105     ASSERT_EQ(nullptr, UiController::GetController());
106 
107     auto controllerProvider = [](list<unique_ptr<UiController>> &receiver) {
108         auto controller = make_unique<DummyController>("dummy_controller");
109         controller->SetWorkable(true);
110         receiver.push_back(move(controller));
111     };
112 
113     // register controllerProvider
114     UiController::RegisterControllerProvider(controllerProvider);
115     UiController::InstallFromProvider();
116     ASSERT_NE(nullptr, UiController::GetController());
117     UiController::RemoveController("dummy_controller");
118     ASSERT_EQ(nullptr, UiController::GetController());
119 }