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 #include "gtest/gtest.h"
16 #include "gtest/hwext/gtest-multithread.h"
17 
18 #include "standby_service_log.h"
19 #include "json_utils.h"
20 #include "standby_config_manager.h"
21 #include "nlohmann/json.hpp"
22 
23 #include "bundle_manager_helper.h"
24 #include "app_mgr_helper.h"
25 #include "ability_manager_helper.h"
26 #ifdef ENABLE_BACKGROUND_TASK_MGR
27 #include "background_task_helper.h"
28 #endif
29 
30 using namespace testing::ext;
31 using namespace testing::mt;
32 
33 namespace OHOS {
34 namespace DevStandbyMgr {
35 namespace {
36     const std::string JSON_KEY = "key";
37     const std::string JSON_ERROR_KEY = "error_key";
38 }
39 class StandbyHelperUnitTest : public testing::Test {
40 public:
SetUpTestCase()41     static void SetUpTestCase() {}
TearDownTestCase()42     static void TearDownTestCase() {}
SetUp()43     void SetUp() override {}
TearDown()44     void TearDown() override {}
45 };
46 
47 /**
48  * @tc.name: StandbyHelperUnitTest_001
49  * @tc.desc: test AppMgrHelper.
50  * @tc.type: FUNC
51  * @tc.require:
52  */
53 HWTEST_F(StandbyHelperUnitTest, StandbyHelperUnitTest_001, TestSize.Level1)
54 {
55     std::vector<AppExecFwk::RunningProcessInfo> allAppProcessInfos;
56     bool isRunning {false};
57     sptr<AppExecFwk::IApplicationStateObserver> observer {nullptr};
58     AppMgrHelper::GetInstance()->Connect();
59     AppMgrHelper::GetInstance()->GetAllRunningProcesses(allAppProcessInfos);
60     AppMgrHelper::GetInstance()->GetAppRunningStateByBundleName("", isRunning);
61     AppMgrHelper::GetInstance()->SubscribeObserver(observer);
62     AppMgrHelper::GetInstance()->UnsubscribeObserver(observer);
63     EXPECT_FALSE(allAppProcessInfos.empty());
64 }
65 
66 /**
67  * @tc.name: StandbyHelperUnitTest_002
68  * @tc.desc: test BundleManagerHelper.
69  * @tc.type: FUNC
70  * @tc.require:
71  */
72 HWTEST_F(StandbyHelperUnitTest, StandbyHelperUnitTest_002, TestSize.Level1)
73 {
74     int32_t uid {0};
75     BundleManagerHelper::GetInstance()->GetClientBundleName(uid);
76     AppExecFwk::ApplicationInfo applicationInfo {};
77     BundleManagerHelper::GetInstance()->GetApplicationInfo("test",
78         AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, 0, applicationInfo);
79     EXPECT_FALSE(applicationInfo.uid > 0);
80 }
81 
82 #ifdef ENABLE_BACKGROUND_TASK_MGR
83 /**
84  * @tc.name: StandbyHelperUnitTest_003
85  * @tc.desc: test BackgroundTaskHelper.
86  * @tc.type: FUNC
87  * @tc.require:
88  */
89 HWTEST_F(StandbyHelperUnitTest, StandbyHelperUnitTest_003, TestSize.Level1)
90 {
91     std::vector<std::shared_ptr<OHOS::BackgroundTaskMgr::ContinuousTaskCallbackInfo>> list;
92     BackgroundTaskHelper::GetInstance()->GetContinuousTaskApps(list);
93     std::vector<std::shared_ptr<OHOS::BackgroundTaskMgr::TransientTaskAppInfo>> appInfoList;
94     BackgroundTaskHelper::GetInstance()->GetTransientTaskApps(appInfoList);
95     EXPECT_TRUE(list.empty());
96 }
97 #endif
98 
99 /**
100  * @tc.name: StandbyHelperUnitTest_004
101  * @tc.desc: test AbilityManagerHelper.
102  * @tc.type: FUNC
103  * @tc.require:
104  */
105 HWTEST_F(StandbyHelperUnitTest, StandbyHelperUnitTest_004, TestSize.Level1)
106 {
107     std::list<SystemProcessInfo> systemProcessInfos {};
108     AbilityManagerHelper::GetInstance()->GetRunningSystemProcess(systemProcessInfos);
109     EXPECT_FALSE(systemProcessInfos.empty());
110 }
111 
112 /**
113  * @tc.name: StandbyHelperUnitTest_005
114  * @tc.desc: test BundleManagerHelper.
115  * @tc.type: FUNC
116  * @tc.require:
117  */
118 HWTEST_F(StandbyHelperUnitTest, StandbyHelperUnitTest_005, TestSize.Level1)
119 {
120     BundleManagerHelper::GetInstance()->bundleMgr_  = nullptr;
121     AppExecFwk::ApplicationInfo appInfo;
122     BundleManagerHelper::GetInstance()->GetApplicationInfo("", AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO,
123         0, appInfo);
124 
125     std::vector<AppExecFwk::ApplicationInfo> appInfos {};
126     BundleManagerHelper::GetInstance()->GetApplicationInfos(AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO,
127         0, appInfos);
128 
129     EXPECT_TRUE(appInfos.empty());
130 }
131 }  // namespace DevStandbyMgr
132 }  // namespace OHOS