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 // gtest
17 #include <gtest/gtest.h>
18 
19 #include <atomic>
20 #include <chrono>
21 #include <condition_variable>
22 #include <mutex>
23 
24 #include <transaction/rs_transaction.h>
25 #include "display_manager.h"
26 #include "display_manager_proxy.h"
27 #include "surface_draw.h"
28 #include "window_test_utils.h"
29 #include "window_manager.h"
30 #include "wm_common.h"
31 
32 using namespace testing;
33 using namespace testing::ext;
34 
35 namespace OHOS {
36 namespace Rosen {
37 namespace {
38 constexpr uint32_t COLOR_RED = 0xffff0000;
39 constexpr uint8_t ALPHA = 255;
40 constexpr int NORMAL_SLEEP_TIME = 3; // 1s
41 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowWaterMarkTest"};
42 }
43 
44 using Utils = WindowTestUtils;
45 
46 class TestIWaterMarkFlagChangedListener : public IWaterMarkFlagChangedListener {
47 public:
48     void OnWaterMarkFlagUpdate(bool showWaterMark) override;
49 
50     bool isShowing_ = false;
51     bool isCallbackCalled_ = false;
52 };
53 
OnWaterMarkFlagUpdate(bool showWaterMark)54 void TestIWaterMarkFlagChangedListener::OnWaterMarkFlagUpdate(bool showWaterMark)
55 {
56     WLOGFI("water mark flag update result:%{public}d", showWaterMark);
57     isShowing_ = showWaterMark;
58     isCallbackCalled_ = true;
59 }
60 
61 class WaterMarkTest : public testing::Test {
62 public:
63     static void SetUpTestCase();
64     static void TearDownTestCase();
65     void SetUp() override;
66     void TearDown() override;
67 
68     bool FillColor(sptr<Window> window);
69 
70     static sptr<TestIWaterMarkFlagChangedListener> lisenter_;
71     Utils::TestWindowInfo appInfo_;
72     sptr<Window> CreateWindow(const Utils::TestWindowInfo& appinfo);
73     static inline DisplayId displayId_;
74 };
75 
76 sptr<TestIWaterMarkFlagChangedListener> WaterMarkTest::lisenter_ = nullptr;
SetUpTestCase()77 void WaterMarkTest::SetUpTestCase()
78 {
79     lisenter_= new TestIWaterMarkFlagChangedListener();
80     WindowManager::GetInstance().RegisterWaterMarkFlagChangedListener(lisenter_);
81     displayId_ = DisplayManager::GetInstance().GetDefaultDisplayId();
82 }
83 
TearDownTestCase()84 void WaterMarkTest::TearDownTestCase()
85 {
86     WindowManager::GetInstance().UnregisterWaterMarkFlagChangedListener(lisenter_);
87 }
88 
SetUp()89 void WaterMarkTest::SetUp()
90 {
91     appInfo_ = {
92         .name = "testWindow",
93         .rect = Utils::customAppRect_,
94         .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
95         .mode = WindowMode::WINDOW_MODE_FLOATING,
96         .needAvoid = false,
97         .parentLimit = false,
98         .showWhenLocked = true,
99         .parentId = INVALID_WINDOW_ID,
100     };
101 }
102 
TearDown()103 void WaterMarkTest::TearDown()
104 {
105 }
106 
CreateWindow(const Utils::TestWindowInfo & appinfo)107 sptr<Window> WaterMarkTest::CreateWindow(const Utils::TestWindowInfo& appinfo)
108 {
109     sptr<WindowOption> option = new WindowOption();
110     option->SetDisplayId(displayId_);
111     option->SetWindowRect(appinfo.rect);
112     option->SetWindowType(appinfo.type);
113     option->SetWindowMode(appinfo.mode);
114     option->AddWindowFlag(WindowFlag::WINDOW_FLAG_SHOW_WHEN_LOCKED);
115     sptr<Window> window = Window::Create(appinfo.name, option);
116     return window;
117 }
118 
FillColor(sptr<Window> window)119 bool WaterMarkTest::FillColor(sptr<Window> window)
120 {
121     if (window == nullptr) {
122         return false;
123     }
124     auto surfaceNode = window->GetSurfaceNode();
125     if (surfaceNode == nullptr) {
126         return false;
127     }
128     Rect rect = window->GetRect();
129     bool isDrawSuccess = SurfaceDraw::DrawColor(surfaceNode, rect.width_, rect.height_, COLOR_RED);
130     surfaceNode->SetAbilityBGAlpha(ALPHA);
131     RSTransaction::FlushImplicitTransaction();
132     return isDrawSuccess;
133 }
134 
135 namespace {
136 /**
137 * @tc.name: WindowVisibilityInfoTest01
138 * @tc.desc: window show or hide
139 * @tc.type: FUNC
140 * @tc.require: issueI5FSQW
141 */
142 HWTEST_F(WaterMarkTest, SetWaterMarkFlag01, Function | MediumTest | Level1)
143 {
144     appInfo_.name = "window1";
145     appInfo_.rect = {200, 200, 300, 300};
146     sptr<Window> window = CreateWindow(appInfo_);
147     if (window == nullptr) {
148         return;
149     }
150     ASSERT_NE(window, nullptr);
151     window->Show();
152     sleep(NORMAL_SLEEP_TIME);
153     auto drawSuccess = FillColor(window);
154     sleep(NORMAL_SLEEP_TIME);
155     ASSERT_TRUE(drawSuccess);
156 
157     window->AddWindowFlag(WindowFlag::WINDOW_FLAG_WATER_MARK);
158     sleep(NORMAL_SLEEP_TIME);
159     ASSERT_EQ(lisenter_->isShowing_, true);
160 
161     window->RemoveWindowFlag(WindowFlag::WINDOW_FLAG_WATER_MARK);
162     sleep(NORMAL_SLEEP_TIME);
163     ASSERT_EQ(lisenter_->isShowing_, false);
164 
165     window->Destroy();
166     sleep(NORMAL_SLEEP_TIME);
167 }
168 }
169 } // namespace Rosen
170 } // namespace OHOS
171 
172