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