• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2025 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 #define private   public
17 #define protected public
18 #include "ime_lifecycle_manager.h"
19 #undef private
20 #include <gtest/gtest.h>
21 #include <sys/time.h>
22 
23 #include "event_handler.h"
24 #include "global.h"
25 using namespace testing::ext;
26 namespace OHOS {
27 namespace MiscServices {
28 constexpr int32_t TEST_STOP_DELAY_TIME = 100; // 100ms
29 constexpr int32_t WAIT_FOR_OTHERS = 50; // 50ms
30 constexpr int32_t MS_TO_US = 1000;
31 class ImeLifecycleManagerTest : public testing::Test {
32 public:
SetUpTestCase()33     static void SetUpTestCase()
34     {
35         imeLifecycleManager_ = std::make_shared<ImeLifecycleManager>(-1, StopImeCb, TEST_STOP_DELAY_TIME);
36         auto runner = AppExecFwk::EventRunner::Create("test_imeLifecycleManager");
37         eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
38     }
TearDownTestCase()39     static void TearDownTestCase()
40     {
41         imeLifecycleManager_->SetEventHandler(nullptr);
42         eventHandler_ = nullptr;
43     }
44 
SetUp()45     void SetUp()
46     {
47         IMSA_HILOGI("ImeLifecycleManagerTest::SetUp");
48         imeLifecycleManager_->SetEventHandler(eventHandler_);
49         isStopImeCalled_ = false;
50     }
TearDown()51     void TearDown()
52     {
53         IMSA_HILOGI("ImeLifecycleManagerTest::TearDown");
54     }
StopImeCb()55     static void StopImeCb()
56     {
57         IMSA_HILOGI("StopImeCb");
58         isStopImeCalled_ = true;
59     }
60     static std::shared_ptr<ImeLifecycleManager> imeLifecycleManager_;
61     static std::shared_ptr<AppExecFwk::EventHandler> eventHandler_;
62     static std::atomic_bool isStopImeCalled_;
63 };
64 
65 std::shared_ptr<ImeLifecycleManager> ImeLifecycleManagerTest::imeLifecycleManager_ = nullptr;
66 std::shared_ptr<AppExecFwk::EventHandler> ImeLifecycleManagerTest::eventHandler_ = nullptr;
67 std::atomic_bool ImeLifecycleManagerTest::isStopImeCalled_ = false;
68 
69 /**
70  * @tc.name: ControlIme_EventHandlerNull
71  * @tc.desc: test ime lifecycle manager
72  * @tc.type: FUNC
73  */
74 HWTEST_F(ImeLifecycleManagerTest, ControlIme_EventHandlerNull, TestSize.Level1)
75 {
76     ASSERT_NE(ImeLifecycleManagerTest::imeLifecycleManager_, nullptr);
77     ImeLifecycleManagerTest::imeLifecycleManager_->SetEventHandler(nullptr);
78     ImeLifecycleManagerTest::imeLifecycleManager_->ControlIme(true);
79     EXPECT_FALSE(ImeLifecycleManagerTest::isStopImeCalled_);
80 }
81 
82 /**
83  * @tc.name: ControlIme_shouldStopIme
84  * @tc.desc: test ime lifecycle manager
85  * @tc.type: FUNC
86  */
87 HWTEST_F(ImeLifecycleManagerTest, ControlIme_shouldStopIme, TestSize.Level1)
88 {
89     auto manager = std::make_shared<ImeLifecycleManager>(-1, ImeLifecycleManagerTest::StopImeCb, TEST_STOP_DELAY_TIME);
90     manager->ControlIme(true);
91     usleep((TEST_STOP_DELAY_TIME + WAIT_FOR_OTHERS) * MS_TO_US);
92     EXPECT_TRUE(ImeLifecycleManagerTest::isStopImeCalled_);
93 }
94 
95 /**
96  * @tc.name: ControlIme_shouldNotStopIme
97  * @tc.desc: test ime lifecycle manager
98  * @tc.type: FUNC
99  */
100 HWTEST_F(ImeLifecycleManagerTest, ControlIme_shouldNotStopIme, TestSize.Level1)
101 {
102     auto manager = std::make_shared<ImeLifecycleManager>(-1, ImeLifecycleManagerTest::StopImeCb, TEST_STOP_DELAY_TIME);
103     manager->ControlIme(false);
104     usleep((TEST_STOP_DELAY_TIME + WAIT_FOR_OTHERS) * MS_TO_US);
105     EXPECT_FALSE(ImeLifecycleManagerTest::isStopImeCalled_);
106 }
107 
108 /**
109  * @tc.name: ControlIme_stopImeFuncIsNull
110  * @tc.desc: test ime lifecycle manager
111  * @tc.type: FUNC
112  */
113 HWTEST_F(ImeLifecycleManagerTest, ControlIme_stopImeFuncIsNull, TestSize.Level1)
114 {
115     auto manager = std::make_shared<ImeLifecycleManager>(-1, nullptr, TEST_STOP_DELAY_TIME);
116     manager->ControlIme(true);
117     usleep((TEST_STOP_DELAY_TIME + WAIT_FOR_OTHERS) * MS_TO_US);
118     EXPECT_FALSE(ImeLifecycleManagerTest::isStopImeCalled_);
119 }
120 
121 /**
122  * @tc.name: ControlIme_weakPtrIsExpired
123  * @tc.desc: test ime lifecycle manager
124  * @tc.type: FUNC
125  */
126 HWTEST_F(ImeLifecycleManagerTest, ControlIme_weakPtrIsExpired, TestSize.Level1)
127 {
128     auto manager = std::make_shared<ImeLifecycleManager>(-1, ImeLifecycleManagerTest::StopImeCb, TEST_STOP_DELAY_TIME);
129     manager->ControlIme(true);
130     manager = nullptr;
131     usleep((TEST_STOP_DELAY_TIME + WAIT_FOR_OTHERS) * MS_TO_US);
132     EXPECT_FALSE(ImeLifecycleManagerTest::isStopImeCalled_);
133 }
134 } // namespace MiscServices
135 } // namespace OHOS