1 /*
2  * Copyright (c) 2024 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 #include <gtest/gtest.h>
16 
17 #include "ability_context_impl.h"
18 #include "token_setproc.h"
19 #include "window.h"
20 #include "window_scene.h"
21 #include "wm_common.h"
22 
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace Security {
27 namespace AccessToken {
28 namespace {
29 
30 static const int32_t RET_OK = 0;
31 static inline float g_virtualPixelRatio = 1.0;
32 static inline std::shared_ptr<AbilityRuntime::AbilityContext> g_abilityContext_ = nullptr;
33 }
34 
35 class CreateCameraWindowTest : public testing::Test {
36 public:
37     static void SetUpTestCase();
38 
39     static void TearDownTestCase();
40 
41     void SetUp();
42 
43     void TearDown();
44 };
45 
SetUpTestCase()46 void CreateCameraWindowTest::SetUpTestCase() {}
47 
TearDownTestCase()48 void CreateCameraWindowTest::TearDownTestCase() {}
SetUp()49 void CreateCameraWindowTest::SetUp() {}
TearDown()50 void CreateCameraWindowTest::TearDown() {}
51 
CreateWindowScene()52 static sptr<Rosen::WindowScene> CreateWindowScene()
53 {
54     sptr<Rosen::IWindowLifeCycle> listener = nullptr;
55     g_abilityContext_ = std::make_shared<AbilityRuntime::AbilityContextImpl>();
56 
57     sptr<Rosen::WindowScene> scene = new Rosen::WindowScene();
58     scene->Init(0, g_abilityContext_, listener);
59     return scene;
60 }
61 
CreateAppFloatingWindow(Rosen::WindowType type,Rosen::Rect rect,std::string name="")62 static sptr<Rosen::Window> CreateAppFloatingWindow(Rosen::WindowType type, Rosen::Rect rect, std::string name = "")
63 {
64     sptr<Rosen::WindowOption> option = new Rosen::WindowOption();
65     option->SetWindowType(type);
66     option->SetWindowRect(rect);
67 
68     static int cnt = 0;
69     std::string winName = (name == "") ? "FloatingWindowTest" + std::to_string(cnt++) : name;
70 
71     return Rosen::Window::Create(winName, option, g_abilityContext_);
72 }
73 
GetRectWithVpr(int32_t x,int32_t y,uint32_t w,uint32_t h)74 static inline Rosen::Rect GetRectWithVpr(int32_t x, int32_t y, uint32_t w, uint32_t h)
75 {
76     auto vpr = g_virtualPixelRatio;
77     return {x, y, static_cast<uint32_t>(w * vpr), static_cast<uint32_t>(h * vpr)};
78 }
79 
80 /**
81  * @tc.name: CreateCameraFloatWindowTest
82  * @tc.desc: Create camera float window
83  * @tc.type: FUNC
84  * @tc.require:
85  */
86 HWTEST_F(CreateCameraWindowTest, CreateCameraFloatWindowTest, TestSize.Level1)
87 {
88     uint32_t tokenId = GetSelfTokenID();
89     GTEST_LOG_(INFO) << "CreateCameraFloatWindowTest begin, tokenId: " << tokenId << std::endl;
90 
91     sptr<Rosen::WindowScene> scene = CreateWindowScene();
92     ASSERT_NE(scene, nullptr);
93 
94     Rosen::Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
95     sptr<Rosen::Window> fltWin = CreateAppFloatingWindow(Rosen::WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect);
96     ASSERT_NE(fltWin, nullptr);
97 
98     GTEST_LOG_(INFO) << "1. Create camera float window\n" << std::endl;
99 
100     int32_t ret = static_cast<int32_t>(scene->GoForeground());
101     ASSERT_EQ(ret, RET_OK);
102 
103     ret = static_cast<int32_t>(fltWin->Show());
104     ASSERT_EQ(ret, RET_OK);
105 
106     GTEST_LOG_(INFO) << "2. GoForeground\n" << std::endl;
107 
108     usleep(500000); // 500000us = 0.5s
109 
110     ret = static_cast<int32_t>(fltWin->Hide());
111     ASSERT_EQ(ret, RET_OK);
112 
113     GTEST_LOG_(INFO) << "3. Hide window\n" << std::endl;
114 
115     usleep(500000); // 500000us = 0.5s
116 
117     ret = static_cast<int32_t>(fltWin->Destroy());
118     ASSERT_EQ(ret, RET_OK);
119 
120     GTEST_LOG_(INFO) << "4. Destroy window\n" << std::endl;
121 
122     ret = static_cast<int32_t>(scene->GoDestroy());
123     ASSERT_EQ(ret, RET_OK);
124 }
125 } // namespace AccessToken
126 } // namespace Security
127 } // namespace OHOS
128