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 "future.h"
20 #include "window_manager.h"
21 #include "wm_common.h"
22 #include "scene_board_judgement.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace Rosen {
29 namespace {
30 constexpr int WAIT_FUTURE_RESULT = 20000; // 20s
31 constexpr int WAIT_SLEEP_TIME = 1; // 1s
32 }
33 
34 class TestGestureNavigationEnabledChangedListener : public IGestureNavigationEnabledChangedListener {
35 public:
36     void OnGestureNavigationEnabledUpdate(bool enable) override;
37 
38     RunnableFuture<bool> future_;
39 };
40 
OnGestureNavigationEnabledUpdate(bool enable)41 void TestGestureNavigationEnabledChangedListener::OnGestureNavigationEnabledUpdate(bool enable)
42 {
43     future_.SetValue(enable);
44 }
45 
46 class GestureNavigationEnabledTest : public testing::Test {
47 public:
48     static void SetUpTestCase();
49     static void TearDownTestCase();
50     void SetUp() override;
51     void TearDown() override;
52 
53     static sptr<TestGestureNavigationEnabledChangedListener> lisenter_;
54 };
55 
56 sptr<TestGestureNavigationEnabledChangedListener> GestureNavigationEnabledTest::lisenter_ = nullptr;
SetUpTestCase()57 void GestureNavigationEnabledTest::SetUpTestCase()
58 {
59     lisenter_= new (std::nothrow)TestGestureNavigationEnabledChangedListener();
60     if (lisenter_ == nullptr) {
61         return;
62     }
63 }
64 
TearDownTestCase()65 void GestureNavigationEnabledTest::TearDownTestCase()
66 {
67 }
68 
SetUp()69 void GestureNavigationEnabledTest::SetUp()
70 {
71 }
72 
TearDown()73 void GestureNavigationEnabledTest::TearDown()
74 {
75 }
76 
77 namespace {
78 /**
79 * @tc.name: SetGestureNavigationEnabled
80 * @tc.desc: Check gesture navigation enabled
81 * @tc.type: FUNC
82 */
83 HWTEST_F(GestureNavigationEnabledTest, SetGestureNavigationEnabled, Function | MediumTest | Level1)
84 {
85     ASSERT_NE(lisenter_, nullptr);
86 
87     auto& windowManager =  WindowManager::GetInstance();
88     windowManager.SetGestureNavigaionEnabled(false);
89     sleep(WAIT_SLEEP_TIME);
90 
91     windowManager.RegisterGestureNavigationEnabledChangedListener(lisenter_);
92     sleep(WAIT_SLEEP_TIME);
93     windowManager.SetGestureNavigaionEnabled(true);
94     auto result = lisenter_->future_.GetResult(WAIT_FUTURE_RESULT);
95 
96     if (!SceneBoardJudgement::IsSceneBoardEnabled()) {
97         ASSERT_EQ(result, true);
98     }
99 
100     lisenter_->future_.Reset(true);
101     windowManager.SetGestureNavigaionEnabled(false);
102     result = lisenter_->future_.GetResult(WAIT_FUTURE_RESULT);
103     if (!SceneBoardJudgement::IsSceneBoardEnabled()) {
104         ASSERT_EQ(result, false);
105     }
106 
107     lisenter_->future_.Reset(false);
108     windowManager.UnregisterGestureNavigationEnabledChangedListener(lisenter_);
109     sleep(WAIT_SLEEP_TIME);
110 }
111 }
112 } // namespace Rosen
113 } // namespace OHOS
114 
115