1 /*
2  * Copyright (c) 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 <iostream>
17 #include <refbase.h>
18 
19 #include "window.h"
20 #include "wm_common.h"
21 #include "window_option.h"
22 #include "window_manager.h"
23 
24 #include "future.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 class SystemSubWindowFuture : public IVisibilityChangedListener {
29 using VisibleInfos = std::vector<sptr<WindowVisibilityInfo>>;
30 public:
OnWindowVisibilityChanged(const std::vector<sptr<WindowVisibilityInfo>> & windowVisibilityInfo)31     void OnWindowVisibilityChanged(const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfo) override
32     {
33         future_.SetValue(1);
34     };
35     RunnableFuture<int32_t> future_;
36     static constexpr long WAIT_TIME = 20000;
37 };
38 }
39 }
40 
41 using namespace OHOS;
42 using namespace OHOS::Rosen;
43 
OutputWindowInfos(const std::vector<sptr<AccessibilityWindowInfo>> & infos)44 void OutputWindowInfos(const std::vector<sptr<AccessibilityWindowInfo>>& infos)
45 {
46     std::cout << "window tree infos length :" << infos.size() << std::endl;
47     std::cout << "windowId -- windowType -- displayId" << std::endl;
48     for (auto info: infos) {
49         std::cout << "  " << info->wid_;
50         std::cout << " -- " << static_cast<int32_t>(info->type_);
51         std::cout << " -- " << std::to_string(info->displayId_);
52         std::cout << std::endl;
53     }
54 }
55 
main(int argc,char * argv[])56 int main(int argc, char* argv[])
57 {
58     std::cout << "===========================Start===========================" << std::endl;
59     std::cout << "Wait 20s, The Windows will close itself" << std::endl;
60 
61     std::vector<sptr<AccessibilityWindowInfo>> infos;
62     WindowManager::GetInstance().GetAccessibilityWindowInfo(infos);
63     std::cout << "before add window " << std::endl;
64     OutputWindowInfos(infos);
65 
66     Rect baseWindowRect = { 150, 150, 400, 600 };
67     sptr<WindowOption> baseOp = new WindowOption();
68     baseOp->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
69     baseOp->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
70     baseOp->SetWindowRect(baseWindowRect);
71 
72     sptr<Window> window = Window::Create("Demo_SSW_BaseWindow", baseOp, nullptr);
73     window->Show();
74     std::cout << "base window id = " << window->GetWindowId() << std::endl;
75 
76     Rect subWindowRect = { 200, 200, 150, 150 };
77     sptr<WindowOption> subWindowOp = new WindowOption();
78     subWindowOp->SetWindowType(WindowType::WINDOW_TYPE_SYSTEM_SUB_WINDOW);
79     subWindowOp->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
80     subWindowOp->SetWindowRect(subWindowRect);
81     subWindowOp->SetParentId(window->GetWindowId());
82 
83     sptr<Window> subWindow = Window::Create("Demo_SSW_SubWindow", subWindowOp, nullptr);
84     subWindow->Show();
85     std::cout << "sub window id = " << subWindow->GetWindowId() << std::endl;
86 
87     infos.clear();
88     WindowManager::GetInstance().GetAccessibilityWindowInfo(infos);
89     std::cout << "after add window:" << std::endl;
90     OutputWindowInfos(infos);
91 
92     std::cout << std::endl;
93     std::cout << "please check hidump to makesure the sub window node is on window tree" << std::endl;
94     sptr<SystemSubWindowFuture> listener = new SystemSubWindowFuture();
95     listener->future_.Reset(0);
96     listener->future_.GetResult(SystemSubWindowFuture::WAIT_TIME);
97 
98     subWindow->Hide();
99     window->Hide();
100 
101     subWindow->Destroy();
102     window->Destroy();
103 
104     infos.clear();
105     WindowManager::GetInstance().GetAccessibilityWindowInfo(infos);
106     std::cout << "after destroy window:" << std::endl;
107     OutputWindowInfos(infos);
108 
109     std::cout << "============================End============================" << std::endl;
110     return 0;
111 }