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 <functional>
17 #include <gtest/gtest.h>
18 #include <ctime>
19 
20 #include "bg_transient_task_mgr.h"
21 #include "background_task_subscriber.h"
22 
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace BackgroundTaskMgr {
27 class TestBackgroundTaskSubscriber : public BackgroundTaskSubscriber {
28 public:
OnTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo> & info)29     void OnTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo>& info) override {}
OnTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo> & info)30     void OnTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo>& info) override {}
31 };
32 
33 class BgTransientTaskMgrTest : public testing::Test {
34 public:
SetUpTestCase()35     static void SetUpTestCase() {}
TearDownTestCase()36     static void TearDownTestCase() {}
SetUp()37     void SetUp() {}
TearDown()38     void TearDown() {}
39 };
40 
41 /**
42  * @tc.name: SubscribeTransientTask_001
43  * @tc.desc: subscribe transient task event.
44  * @tc.type: FUNC
45  * @tc.require: SR000GGTET AR000GH86P AR000GH86O
46  */
47 HWTEST_F(BgTransientTaskMgrTest, SubscribeTransientTask_001, TestSize.Level1)
48 {
49     auto subscriber = std::make_shared<TestBackgroundTaskSubscriber>();
50     auto ret = DelayedSingleton<BgTransientTaskMgr>::GetInstance()->SubscribeBackgroundTask(subscriber->GetImpl());
51     EXPECT_TRUE(ret == ERR_OK);
52 }
53 
54 /**
55  * @tc.name: SubscribeTransientTask_002
56  * @tc.desc: subscribe transient task event.
57  * @tc.type: FUNC
58  * @tc.require: SR000GGTET AR000GH86P AR000GH86O
59  */
60 HWTEST_F(BgTransientTaskMgrTest, SubscribeTransientTask_002, TestSize.Level1)
61 {
62     auto ret = DelayedSingleton<BgTransientTaskMgr>::GetInstance()->SubscribeBackgroundTask(nullptr);
63     EXPECT_FALSE(ret == ERR_OK);
64 }
65 
66 /**
67  * @tc.name: SubscribeTransientTask_003
68  * @tc.desc: subscribe transient task event.
69  * @tc.type: FUNC
70  * @tc.require: SR000GGTET AR000GH86P AR000GH86O
71  */
72 HWTEST_F(BgTransientTaskMgrTest, SubscribeTransientTask_003, TestSize.Level1)
73 {
74     auto subscriber = std::make_shared<TestBackgroundTaskSubscriber>();
75     auto ret = DelayedSingleton<BgTransientTaskMgr>::GetInstance()->SubscribeBackgroundTask(subscriber->GetImpl());
76     EXPECT_TRUE(ret == ERR_OK);
77 
78     ret = DelayedSingleton<BgTransientTaskMgr>::GetInstance()->SubscribeBackgroundTask(subscriber->GetImpl());
79     EXPECT_TRUE(ret == ERR_OK);
80 }
81 
82 /**
83  * @tc.name: UnsubscribeTransientTask_001
84  * @tc.desc: unsubscribe transient task event.
85  * @tc.type: FUNC
86  * @tc.require: SR000GGTET AR000GH86P AR000GH86O
87  */
88 HWTEST_F(BgTransientTaskMgrTest, UnsubscribeTransientTask_001, TestSize.Level1)
89 {
90     auto subscriber = std::make_shared<TestBackgroundTaskSubscriber>();
91     auto ret = DelayedSingleton<BgTransientTaskMgr>::GetInstance()->SubscribeBackgroundTask(subscriber->GetImpl());
92     EXPECT_TRUE(ret == ERR_OK);
93 
94     usleep(1000);
95 
96     ret = DelayedSingleton<BgTransientTaskMgr>::GetInstance()->UnsubscribeBackgroundTask(subscriber->GetImpl());
97     EXPECT_TRUE(ret == ERR_OK);
98 }
99 
100 /**
101  * @tc.name: UnsubscribeTransientTask_002
102  * @tc.desc: unsubscribe transient task event.
103  * @tc.type: FUNC
104  * @tc.require: SR000GGTET AR000GH86P AR000GH86O
105  */
106 HWTEST_F(BgTransientTaskMgrTest, UnsubscribeTransientTask_002, TestSize.Level1)
107 {
108     auto subscriber = std::make_shared<TestBackgroundTaskSubscriber>();
109     auto ret = DelayedSingleton<BgTransientTaskMgr>::GetInstance()->UnsubscribeBackgroundTask(subscriber->GetImpl());
110     EXPECT_TRUE(ret == ERR_OK);
111 }
112 
113 /**
114  * @tc.name: UnsubscribeTransientTask_003
115  * @tc.desc: unsubscribe transient task event.
116  * @tc.type: FUNC
117  * @tc.require: SR000GGTET AR000GH86P AR000GH86O
118  */
119 HWTEST_F(BgTransientTaskMgrTest, UnsubscribeTransientTask_003, TestSize.Level1)
120 {
121     auto ret = DelayedSingleton<BgTransientTaskMgr>::GetInstance()->UnsubscribeBackgroundTask(nullptr);
122     EXPECT_FALSE(ret == ERR_OK);
123 }
124 
125 /**
126  * @tc.name: Marshalling_001
127  * @tc.desc: marshalling transient task app info.
128  * @tc.type: FUNC
129  * @tc.require: SR000GGTET AR000GH86Q
130  */
131 HWTEST_F(BgTransientTaskMgrTest, Marshalling_001, TestSize.Level1)
132 {
133     auto appInfo = std::make_shared<TransientTaskAppInfo>();
134     MessageParcel data;
135     EXPECT_TRUE(appInfo->Marshalling(data));
136 
137     std::shared_ptr<TransientTaskAppInfo> transientTaskAppInfo (TransientTaskAppInfo::Unmarshalling(data));
138     EXPECT_TRUE(transientTaskAppInfo != nullptr);
139 }
140 
141 /**
142  * @tc.name: Unmarshalling_001
143  * @tc.desc: unmarshalling transient task app info.
144  * @tc.type: FUNC
145  * @tc.require: SR000GGTET AR000GH86Q
146  */
147 HWTEST_F(BgTransientTaskMgrTest, Unmarshalling_001, TestSize.Level1)
148 {
149     MessageParcel data;
150     std::shared_ptr<TransientTaskAppInfo> transientTaskAppInfo (TransientTaskAppInfo::Unmarshalling(data));
151     EXPECT_TRUE(transientTaskAppInfo == nullptr);
152 }
153 }  // namespace BackgroundTaskMgr
154 }  // namespace OHOS
155