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