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