1 /*
2 * Copyright (c) 2022 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 #define private public
18 #define protected public
19 #include "background_task_observer.h"
20 #undef private
21 #undef protected
22
23 using namespace testing::ext;
24 using namespace OHOS::BackgroundTaskMgr;
25
26 namespace OHOS {
27 namespace AAFwk {
28 class BackgroundTaskObserverTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 void SetUp();
33 void TearDown();
34 std::shared_ptr<BackgroundTaskObserver> observer_ {nullptr};
35 };
36
SetUpTestCase(void)37 void BackgroundTaskObserverTest::SetUpTestCase(void)
38 {}
TearDownTestCase(void)39 void BackgroundTaskObserverTest::TearDownTestCase(void)
40 {}
TearDown(void)41 void BackgroundTaskObserverTest::TearDown(void)
42 {}
SetUp()43 void BackgroundTaskObserverTest::SetUp()
44 {
45 observer_ = std::make_shared<BackgroundTaskObserver>();
46 }
47
48 /*
49 * Feature: BackgroundTaskObserver
50 * Function: OnContinuousTaskStart
51 * SubFunction: NA
52 * FunctionPoints: BackgroundTaskObserver OnContinuousTaskStart
53 * EnvConditions: NA
54 * CaseDescription: Verify OnContinuousTaskStart
55 */
56 HWTEST_F(BackgroundTaskObserverTest, OnContinuousTaskStart_001, TestSize.Level1)
57 {
58 std::shared_ptr<ContinuousTaskCallbackInfo> info = std::make_shared<ContinuousTaskCallbackInfo>();
59 EXPECT_NE(info, nullptr);
60 observer_->OnContinuousTaskStart(info);
61 }
62
63 /*
64 * Feature: BackgroundTaskObserver
65 * Function: OnContinuousTaskStart
66 * SubFunction: NA
67 * FunctionPoints: BackgroundTaskObserver OnContinuousTaskStart
68 * EnvConditions: NA
69 * CaseDescription: Verify OnContinuousTaskStart
70 */
71 HWTEST_F(BackgroundTaskObserverTest, OnContinuousTaskStart_002, TestSize.Level1)
72 {
73 std::shared_ptr<ContinuousTaskCallbackInfo> info = std::make_shared<ContinuousTaskCallbackInfo>();
74 EXPECT_NE(info, nullptr);
75 observer_->GetAppManager();
76 observer_->OnContinuousTaskStart(info);
77 }
78
79 /*
80 * Feature: BackgroundTaskObserver
81 * Function: OnContinuousTaskStop
82 * SubFunction: NA
83 * FunctionPoints: BackgroundTaskObserver OnContinuousTaskStop
84 * EnvConditions: NA
85 * CaseDescription: Verify OnContinuousTaskStop
86 */
87 HWTEST_F(BackgroundTaskObserverTest, OnContinuousTaskStop_001, TestSize.Level1)
88 {
89 std::shared_ptr<ContinuousTaskCallbackInfo> info = std::make_shared<ContinuousTaskCallbackInfo>();
90 EXPECT_NE(info, nullptr);
91 observer_->OnContinuousTaskStop(info);
92 }
93
94 /*
95 * Feature: BackgroundTaskObserver
96 * Function: OnContinuousTaskStop
97 * SubFunction: NA
98 * FunctionPoints: BackgroundTaskObserver OnContinuousTaskStop
99 * EnvConditions: NA
100 * CaseDescription: Verify OnContinuousTaskStop
101 */
102 HWTEST_F(BackgroundTaskObserverTest, OnContinuousTaskStop_002, TestSize.Level1)
103 {
104 std::shared_ptr<ContinuousTaskCallbackInfo> info = std::make_shared<ContinuousTaskCallbackInfo>();
105 EXPECT_NE(info, nullptr);
106 observer_->GetAppManager();
107 observer_->OnContinuousTaskStop(info);
108 }
109
110 /*
111 * Feature: BackgroundTaskObserver
112 * Function: GetContinuousTaskApps
113 * SubFunction: NA
114 * FunctionPoints: BackgroundTaskObserver GetContinuousTaskApps
115 * EnvConditions: NA
116 * CaseDescription: Verify GetContinuousTaskApps
117 */
118 HWTEST_F(BackgroundTaskObserverTest, GetContinuousTaskApps_001, TestSize.Level1)
119 {
120 ASSERT_NE(observer_, nullptr);
121 observer_->GetContinuousTaskApps();
122 }
123
124 /*
125 * Feature: BackgroundTaskObserver
126 * Function: IsBackgroundTaskUid
127 * SubFunction: NA
128 * FunctionPoints: BackgroundTaskObserver IsBackgroundTaskUid
129 * EnvConditions: NA
130 * CaseDescription: Verify IsBackgroundTaskUid
131 */
132 HWTEST_F(BackgroundTaskObserverTest, IsBackgroundTaskUid_001, TestSize.Level1)
133 {
134 int uid = 0;
135 bool res = observer_->IsBackgroundTaskUid(uid);
136 EXPECT_FALSE(res);
137 }
138
139 /*
140 * Feature: BackgroundTaskObserver
141 * Function: IsBackgroundTaskUid
142 * SubFunction: NA
143 * FunctionPoints: BackgroundTaskObserver IsBackgroundTaskUid
144 * EnvConditions: NA
145 * CaseDescription: Verify IsBackgroundTaskUid
146 */
147 HWTEST_F(BackgroundTaskObserverTest, IsBackgroundTaskUid_002, TestSize.Level1)
148 {
149 int uid = 0;
150 observer_->bgTaskUids_.push_front(uid);
151 bool res = observer_->IsBackgroundTaskUid(uid);
152 EXPECT_TRUE(res);
153 }
154 } // namespace AAFwk
155 } // namespace OHOS
156