1 /*
2  * Copyright (c) 2023 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 <transaction/rs_transaction.h>
18 
19 #include "display_manager.h"
20 #include "iremote_object_mocker.h"
21 #include "window_helper.h"
22 #include "window_system_effect.h"
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace Rosen {
28 class WindowSystemEffectTest : public testing::Test {
29 public:
30     static void SetUpTestCase();
31     static void TearDownTestCase();
32     void SetUp() override;
33     void TearDown() override;
34 
35     sptr<WindowProperty> CreateWindowProperty();
36 private:
37     RSSurfaceNode::SharedPtr CreateRSSurfaceNode(std::string name);
38     sptr<WindowNode> node_ = nullptr;
39     AppWindowEffectConfig effectConfig_;
40     sptr<WindowRoot> windowRoot_;
41 };
42 
SetUpTestCase()43 void WindowSystemEffectTest::SetUpTestCase()
44 {
45 }
46 
TearDownTestCase()47 void WindowSystemEffectTest::TearDownTestCase()
48 {
49 }
50 
SetUp()51 void WindowSystemEffectTest::SetUp()
52 {
53     auto display = DisplayManager::GetInstance().GetDefaultDisplay();
54     ASSERT_TRUE((display != nullptr));
55     sptr<DisplayInfo> displayInfo = display->GetDisplayInfo();
56     ASSERT_TRUE((displayInfo != nullptr));
57     node_ = new WindowNode(CreateWindowProperty()); // 101 is windowId
58     node_->SetWindowRect({0, 0, 100, 100}); // 100 test data
59     node_->leashWinSurfaceNode_ = CreateRSSurfaceNode("leashSurfaceNodeTest");
60     node_->surfaceNode_ = CreateRSSurfaceNode("SurfaceNodeTest");
61     effectConfig_.fullScreenCornerRadius_ = 16.0f; // 16.f test data
62     effectConfig_.splitCornerRadius_ = 16.0f; // 16.f test data
63     effectConfig_.floatCornerRadius_ = 16.0f; // 16.f test data
64     effectConfig_.focusedShadow_ = {80, "#000000", 0, 5, 0.45};
65     effectConfig_.unfocusedShadow_ = {55, "#000000", 0, 10, 0.25};
66     WindowSystemEffect::SetWindowSystemEffectConfig(effectConfig_);
67     windowRoot_ = new WindowRoot([](Event event, const sptr<IRemoteObject>& remoteObject) {});
68     ASSERT_NE(nullptr, windowRoot_);
69     WindowSystemEffect::SetWindowRoot(windowRoot_);
70 }
71 
TearDown()72 void WindowSystemEffectTest::TearDown()
73 {
74     node_ = nullptr;
75     AppWindowEffectConfig config;
76     effectConfig_ = config;
77 }
78 
CreateRSSurfaceNode(std::string name)79 RSSurfaceNode::SharedPtr WindowSystemEffectTest::CreateRSSurfaceNode(std::string name)
80 {
81     struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
82     rsSurfaceNodeConfig.SurfaceNodeName = name;
83     auto surfaceNode = RSSurfaceNode::Create(rsSurfaceNodeConfig);
84     return surfaceNode;
85 }
86 
CreateWindowProperty()87 sptr<WindowProperty> WindowSystemEffectTest::CreateWindowProperty()
88 {
89     sptr<WindowProperty> property = new WindowProperty();
90     return property;
91 }
92 namespace {
93 /**
94  * @tc.name: SetWindowEffectAndCornerRadius01
95  * @tc.desc: set window corner radius with different parameter
96  * @tc.type: FUNC
97  */
98 HWTEST_F(WindowSystemEffectTest, SetWindowEffectAndCornerRadius01, Function | SmallTest | Level2)
99 {
100     sptr<WindowNode> windowNode = nullptr;
101     ASSERT_EQ(WMError::WM_ERROR_NULLPTR, WindowSystemEffect::SetWindowEffect(windowNode));
102     ASSERT_EQ(WMError::WM_OK, WindowSystemEffect::SetWindowEffect(node_));
103     // fullscreen
104     node_->SetWindowMode(WindowMode::WINDOW_MODE_FULLSCREEN);
105     ASSERT_EQ(WMError::WM_OK, WindowSystemEffect::SetCornerRadius(node_));
106     WindowSystemEffect::windowSystemEffectConfig_.fullScreenCornerRadius_ = 0.0f;
107     ASSERT_EQ(WMError::WM_OK, WindowSystemEffect::SetCornerRadius(node_));
108 
109     // splitmode
110     node_->SetWindowMode(WindowMode::WINDOW_MODE_SPLIT_PRIMARY);
111     ASSERT_EQ(WMError::WM_OK, WindowSystemEffect::SetCornerRadius(node_));
112     WindowSystemEffect::windowSystemEffectConfig_.splitCornerRadius_ = 0.0f;
113     ASSERT_EQ(WMError::WM_OK, WindowSystemEffect::SetCornerRadius(node_));
114 
115     // float mode
116     node_->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
117     ASSERT_EQ(WMError::WM_OK, WindowSystemEffect::SetCornerRadius(node_));
118     WindowSystemEffect::windowSystemEffectConfig_.floatCornerRadius_ = 0.0f;
119     ASSERT_EQ(WMError::WM_DO_NOTHING, WindowSystemEffect::SetCornerRadius(node_));
120 
121     node_->leashWinSurfaceNode_ = nullptr;
122     WindowSystemEffect::windowSystemEffectConfig_.floatCornerRadius_ = 16.0f;
123     ASSERT_EQ(WMError::WM_OK, WindowSystemEffect::SetCornerRadius(node_));
124 
125     node_->leashWinSurfaceNode_ = CreateRSSurfaceNode("leashSurfaceNodeTest");
126     node_->surfaceNode_ = nullptr;
127     ASSERT_EQ(WMError::WM_OK, WindowSystemEffect::SetCornerRadius(node_));
128 
129     node_->leashWinSurfaceNode_ = nullptr;
130     ASSERT_EQ(WMError::WM_ERROR_NULLPTR, WindowSystemEffect::SetCornerRadius(node_));
131 
132     WindowSystemEffect::SetWindowRoot(nullptr);
133     ASSERT_EQ(WMError::WM_ERROR_NULLPTR, WindowSystemEffect::SetCornerRadius(node_));
134 }
135 
136 /**
137  * @tc.name: SetWindowShadow
138  * @tc.desc: set window shadow with different parameter
139  * @tc.type: FUNC
140  */
141 HWTEST_F(WindowSystemEffectTest, SetWindowShadow01, Function | SmallTest | Level2)
142 {
143     sptr<WindowNode> windowNode = nullptr;
144     ASSERT_EQ(WMError::WM_ERROR_NULLPTR, WindowSystemEffect::SetWindowShadow(windowNode));
145 
146     // fullscreen
147     node_->SetWindowMode(WindowMode::WINDOW_MODE_FULLSCREEN);
148     ASSERT_EQ(WMError::WM_OK, WindowSystemEffect::SetWindowShadow(node_));
149     // float
150     node_->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
151     ASSERT_EQ(WMError::WM_OK, WindowSystemEffect::SetWindowShadow(node_));
152     node_->isFocused_ = true;
153     ASSERT_EQ(WMError::WM_OK, WindowSystemEffect::SetWindowShadow(node_));
154 
155     WindowSystemEffect::windowSystemEffectConfig_.focusedShadow_.color_ = "";
156     ASSERT_EQ(WMError::WM_ERROR_INVALID_PARAM, WindowSystemEffect::SetWindowShadow(node_));
157 
158     WindowSystemEffect::windowSystemEffectConfig_.focusedShadow_.elevation_ = 0.0001f;
159     ASSERT_EQ(WMError::WM_ERROR_INVALID_PARAM, WindowSystemEffect::SetWindowShadow(node_));
160 
161     WindowSystemEffect::windowSystemEffectConfig_ = effectConfig_;
162     node_->GetWindowProperty()->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
163     ASSERT_EQ(WMError::WM_DO_NOTHING, WindowSystemEffect::SetWindowShadow(node_));
164 
165     sptr<WindowNode> testNode = new WindowNode(CreateWindowProperty());
166     sptr<IRemoteObject> token = new IRemoteObjectMocker();
167     testNode->abilityToken_ = token;
168     windowRoot_->windowNodeMap_.insert({testNode->GetWindowId(), testNode});
169     node_->abilityToken_ = token;
170     ASSERT_EQ(WMError::WM_OK, WindowSystemEffect::SetWindowShadow(node_));
171 
172     WindowSystemEffect::SetWindowRoot(nullptr);
173     ASSERT_EQ(WMError::WM_ERROR_NULLPTR, WindowSystemEffect::SetWindowShadow(node_));
174 }
175 }
176 }
177 }
178