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 
16 #include <gtest/gtest.h>
17 
18 #include "app_state_observer.h"
19 #include "mmi_log.h"
20 
21 #undef MMI_LOG_TAG
22 #define MMI_LOG_TAG "ApplicationStateObserverTest"
23 namespace OHOS {
24 namespace MMI {
25 namespace {
26 using namespace testing::ext;
27 } // namespace
28 
29 class ApplicationStateObserverTest : public testing::Test {
30 public:
SetUpTestCase(void)31     static void SetUpTestCase(void) {}
TearDownTestCase(void)32     static void TearDownTestCase(void) {}
33 
SetUp()34     void SetUp() {}
TearDown()35     void TearDown() {}
36 };
37 
38 /**
39  * @tc.name: ApplicationStateObserverTest_ForegroundAppData_001
40  * @tc.desc: Verify the SetForegroundAppData and GetForegroundAppData functions
41  * @tc.type: FUNC
42  * @tc.require:
43  */
44 HWTEST_F(ApplicationStateObserverTest, ApplicationStateObserverTest_ForegroundAppData_001, TestSize.Level1)
45 {
46     CALL_DEBUG_ENTER;
47     std::vector<AppExecFwk::AppStateData> appDatas {};
48     APP_OBSERVER_MGR->SetForegroundAppData(appDatas);
49     auto result = APP_OBSERVER_MGR->GetForegroundAppData();
50     EXPECT_EQ(appDatas.size(), result.size());
51 }
52 
53 /**
54  * @tc.name: ApplicationStateObserverTest_InitAppStateObserver_001
55  * @tc.desc: Verify the action of the InitAppStateObserver function when hasInit_ is true and false respectively
56  * @tc.type: FUNC
57  * @tc.require:
58  */
59 HWTEST_F(ApplicationStateObserverTest, ApplicationStateObserverTest_InitAppStateObserver_001, TestSize.Level1)
60 {
61     CALL_DEBUG_ENTER;
62     APP_OBSERVER_MGR->hasInit_ = true;
63     ASSERT_NO_FATAL_FAILURE(APP_OBSERVER_MGR->InitAppStateObserver());
64     APP_OBSERVER_MGR->hasInit_ = false;
65     ASSERT_NO_FATAL_FAILURE(APP_OBSERVER_MGR->InitAppStateObserver());
66 }
67 
68 /**
69  * @tc.name: ApplicationStateObserverTest_GetAppMgr_001
70  * @tc.desc: Verify the first and non-first entry into the GetAppMgr function
71  * @tc.type: FUNC
72  * @tc.require:
73  */
74 HWTEST_F(ApplicationStateObserverTest, ApplicationStateObserverTest_GetAppMgr_001, TestSize.Level1)
75 {
76     CALL_DEBUG_ENTER;
77     ApplicationStateObserver obsever;
78     auto appManager = obsever.GetAppMgr();
79     EXPECT_NE(appManager, nullptr);
80     appManager = obsever.GetAppMgr();
81     EXPECT_NE(appManager, nullptr);
82 }
83 
84 /**
85  * @tc.name: ApplicationStateObserverTest_GetForegroundApplicationInfo_001
86  * @tc.desc: Verify the results of obtaining foreground application information
87  * @tc.type: FUNC
88  * @tc.require:
89  */
90 HWTEST_F(ApplicationStateObserverTest, ApplicationStateObserverTest_GetForegroundApplicationInfo_001, TestSize.Level1)
91 {
92     CALL_DEBUG_ENTER;
93     ApplicationStateObserver obsever;
94     std::vector<AppExecFwk::AppStateData> appDatas {};
95     int32_t ret = obsever.GetForegroundApplicationInfo(appDatas);
96     EXPECT_EQ(ret, RET_OK);
97     EXPECT_FALSE(appDatas.empty());
98     auto appManager = obsever.GetAppMgr();
99     EXPECT_NE(appManager, nullptr);
100     std::vector<AppExecFwk::AppStateData> appStateDatas {};
101     ret = obsever.GetForegroundApplicationInfo(appStateDatas);
102     EXPECT_EQ(ret, RET_OK);
103 }
104 } // namespace MMI
105 } // namespace OHOS