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