• 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 <gtest/gtest.h>
17 #include "ability_context_impl.h"
18 #include "window.h"
19 #include "mock_window_adapter.h"
20 #include "singleton_mocker.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace Rosen {
27 using Mocker = SingletonMocker<WindowAdapter, MockWindowAdapter>;
28 class WindowTest : public testing::Test {
29 public:
30     static void SetUpTestCase();
31     static void TearDownTestCase();
32     virtual void SetUp() override;
33     virtual void TearDown() override;
34     static inline std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext_;
35 };
SetUpTestCase()36 void WindowTest::SetUpTestCase()
37 {
38     abilityContext_ = std::make_shared<AbilityRuntime::AbilityContextImpl>();
39 }
40 
TearDownTestCase()41 void WindowTest::TearDownTestCase()
42 {
43 }
44 
SetUp()45 void WindowTest::SetUp()
46 {
47 }
48 
TearDown()49 void WindowTest::TearDown()
50 {
51 }
52 
53 namespace {
54 /**
55  * @tc.name: Create01
56  * @tc.desc: Create window with no WindowName and no abilityToken
57  * @tc.type: FUNC
58  */
59 HWTEST_F(WindowTest, Create01, Function | SmallTest | Level2)
60 {
61     sptr<WindowOption> option = new WindowOption();
62     ASSERT_EQ(nullptr, Window::Create("", option));
63 }
64 
65 /**
66  * @tc.name: Create02
67  * @tc.desc: Create window with WindowName and no abilityToken
68  * @tc.type: FUNC
69  */
70 HWTEST_F(WindowTest, Create02, Function | SmallTest | Level2)
71 {
72     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
73     sptr<WindowOption> option = new WindowOption();
74     EXPECT_CALL(m->Mock(), CreateWindow(_, _, _, _, _)).Times(1).WillOnce(Return(WMError::WM_OK));
75     auto window = Window::Create("WindowTest02", option);
76     ASSERT_NE(nullptr, window);
77     EXPECT_CALL(m->Mock(), DestroyWindow(_)).Times(1).WillOnce(Return(WMError::WM_OK));
78     ASSERT_EQ(WMError::WM_OK, window->Destroy());
79 }
80 
81 /**
82  * @tc.name: Create03
83  * @tc.desc: Mock CreateWindow return WM_ERROR_SAMGR, create window with WindowName and no abilityToken
84  * @tc.type: FUNC
85  */
86 HWTEST_F(WindowTest, Create03, Function | SmallTest | Level2)
87 {
88     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
89     sptr<WindowOption> option = new WindowOption();
90     EXPECT_CALL(m->Mock(), CreateWindow(_, _, _, _, _)).Times(1).WillOnce(Return(WMError::WM_ERROR_SAMGR));
91     ASSERT_EQ(nullptr, Window::Create("WindowTest03", option));
92 }
93 
94 /**
95  * @tc.name: Create06
96  * @tc.desc: Create window with WindowName and no option
97  * @tc.type: FUNC
98  */
99 HWTEST_F(WindowTest, Create06, Function | SmallTest | Level2)
100 {
101     sptr<WindowOption> option = nullptr;
102     ASSERT_EQ(nullptr, Window::Create("", option));
103 }
104 
105 /**
106  * @tc.name: Find01
107  * @tc.desc: Find with no name
108  * @tc.type: FUNC
109  */
110 HWTEST_F(WindowTest, Find01, Function | SmallTest | Level2)
111 {
112     ASSERT_EQ(nullptr, Window::Find(""));
113 }
114 
115 /**
116  * @tc.name: Find02
117  * @tc.desc: Find with name
118  * @tc.type: FUNC
119  */
120 HWTEST_F(WindowTest, Find02, Function | SmallTest | Level2)
121 {
122     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
123     sptr<WindowOption> option = new WindowOption();
124     EXPECT_CALL(m->Mock(), CreateWindow(_, _, _, _, _)).Times(1).WillOnce(Return(WMError::WM_OK));
125     auto window = Window::Create("WindowTest03", option);
126     ASSERT_NE(nullptr, window);
127     ASSERT_NE(nullptr, Window::Find("WindowTest03"));
128     EXPECT_CALL(m->Mock(), DestroyWindow(_)).Times(1).WillOnce(Return(WMError::WM_OK));
129     ASSERT_EQ(WMError::WM_OK, window->Destroy());
130 }
131 }
132 } // namespace Rosen
133 } // namespace OHOS
134