• 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 
16 #include <gtest/gtest.h>
17 #define private public
18 #define protected public
19 #include "ability_bundle_event_callback.h"
20 #include "ability_event_util.h"
21 #undef private
22 #undef protected
23 
24 using namespace testing::ext;
25 using namespace testing;
26 using namespace OHOS::AppExecFwk;
27 using namespace OHOS::AbilityRuntime;
28 namespace OHOS {
29 namespace AAFwk {
30 class AbilityBundleEventCallbackTest : public testing::Test {
31 public:
32     static void SetUpTestCase(void);
33     static void TearDownTestCase(void);
34     void SetUp();
35     void TearDown();
36 };
SetUpTestCase(void)37 void AbilityBundleEventCallbackTest::SetUpTestCase(void) {}
TearDownTestCase(void)38 void AbilityBundleEventCallbackTest::TearDownTestCase(void) {}
TearDown()39 void AbilityBundleEventCallbackTest::TearDown() {}
SetUp()40 void AbilityBundleEventCallbackTest::SetUp() {}
41 
42 class MockTaskHandlerWrap : public AAFwk::TaskHandlerWrap {
43 public:
MockTaskHandlerWrap(const std::string & queueName="")44     explicit MockTaskHandlerWrap(const std::string &queueName = "") : TaskHandlerWrap(queueName) {};
45 
~MockTaskHandlerWrap()46     virtual ~MockTaskHandlerWrap() {};
47 
SubmitTaskInner(std::function<void ()> && task,const AAFwk::TaskAttribute & taskAttr)48     std::shared_ptr<AAFwk::InnerTaskHandle> SubmitTaskInner(
49         std::function<void()>&& task, const AAFwk::TaskAttribute& taskAttr) override
50     {
51         taskCount++;
52         task();
53         return nullptr;
54     }
55 
CancelTaskInner(const std::shared_ptr<AAFwk::InnerTaskHandle> & taskHandle)56     bool CancelTaskInner(const std::shared_ptr<AAFwk::InnerTaskHandle>& taskHandle) override
57     {
58         return false;
59     }
60 
WaitTaskInner(const std::shared_ptr<AAFwk::InnerTaskHandle> & taskHandle)61     void WaitTaskInner(const std::shared_ptr<AAFwk::InnerTaskHandle>& taskHandle) override
62     {
63         return;
64     }
65 
GetTaskCount()66     uint64_t GetTaskCount() override
67     {
68         return tasks_.size();
69     }
70 
71     int32_t taskCount = 0;
72 };
73 
74 /**
75  * @tc.name: AbilityBundleEventCallbackTest_OnReceiveEvent_0100
76  * @tc.desc: Test the state of OnReceiveEvent
77  * @tc.type: FUNC
78  */
79 HWTEST_F(AbilityBundleEventCallbackTest, OnReceiveEvent_0100, TestSize.Level1)
80 {
81     sptr<AbilityBundleEventCallback> abilityBundleEventCallback_ =
82         new (std::nothrow) AbilityBundleEventCallback(nullptr, nullptr);
83     EXPECT_NE(abilityBundleEventCallback_, nullptr);
84     EventFwk::CommonEventData eventData;
85     abilityBundleEventCallback_->OnReceiveEvent(eventData);
86     EXPECT_EQ(abilityBundleEventCallback_->taskHandler_, nullptr);
87 }
88 
89 /**
90  * @tc.name: AbilityBundleEventCallbackTest_OnReceiveEvent_0200
91  * @tc.desc: Test the state of OnReceiveEvent
92  * @tc.type: FUNC
93  */
94 HWTEST_F(AbilityBundleEventCallbackTest, OnReceiveEvent_0200, TestSize.Level1)
95 {
96     sptr<AbilityBundleEventCallback> abilityBundleEventCallback_ =
97         new (std::nothrow) AbilityBundleEventCallback(nullptr, nullptr);
98     EXPECT_NE(abilityBundleEventCallback_, nullptr);
99     abilityBundleEventCallback_->taskHandler_ = TaskHandlerWrap::CreateQueueHandler("AbilityBundleEventCallbackTest");
100     EventFwk::CommonEventData eventData;
101     abilityBundleEventCallback_->OnReceiveEvent(eventData);
102     EXPECT_NE(abilityBundleEventCallback_->taskHandler_, nullptr);
103 }
104 
105 /**
106  * @tc.name: AbilityBundleEventCallbackTest_OnReceiveEvent_0300
107  * @tc.desc: Test the state of OnReceiveEvent
108  * @tc.type: FUNC
109  */
110 HWTEST_F(AbilityBundleEventCallbackTest, OnReceiveEvent_0300, TestSize.Level1)
111 {
112     GTEST_LOG_(INFO) << "OnReceiveEvent_0300 start";
113 
114     sptr<AbilityBundleEventCallback> abilityBundleEventCallback_ =
115         new (std::nothrow) AbilityBundleEventCallback(nullptr, nullptr);
116     EXPECT_NE(abilityBundleEventCallback_, nullptr);
117 
118     auto mockHandler = std::make_shared<MockTaskHandlerWrap>();
119     abilityBundleEventCallback_->taskHandler_ = mockHandler;
120     EventFwk::CommonEventData eventData;
121     Want want;
122     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
123     want.SetBundle("com.test.demo");
124     want.SetParam("isRecover", false);
125     eventData.SetWant(want);
126 
127     abilityBundleEventCallback_->OnReceiveEvent(eventData);
128     EXPECT_TRUE(mockHandler->taskCount >= 0);
129 
130     GTEST_LOG_(INFO) << "OnReceiveEvent_0300 end";
131 }
132 
133 /**
134  * @tc.name: AbilityBundleEventCallbackTest_OnReceiveEvent_0400
135  * @tc.desc: Test the state of OnReceiveEvent
136  * @tc.type: FUNC
137  */
138 HWTEST_F(AbilityBundleEventCallbackTest, OnReceiveEvent_0400, TestSize.Level1)
139 {
140     GTEST_LOG_(INFO) << "OnReceiveEvent_0400 start";
141 
142     sptr<AbilityBundleEventCallback> abilityBundleEventCallback_ =
143         new (std::nothrow) AbilityBundleEventCallback(nullptr, nullptr);
144     EXPECT_NE(abilityBundleEventCallback_, nullptr);
145 
146     auto mockHandler = std::make_shared<MockTaskHandlerWrap>();
147     abilityBundleEventCallback_->taskHandler_ = mockHandler;
148     EventFwk::CommonEventData eventData;
149     Want want;
150     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
151     want.SetBundle("com.test.demo");
152     want.SetParam("isRecover", true);
153     eventData.SetWant(want);
154 
155     abilityBundleEventCallback_->OnReceiveEvent(eventData);
156     EXPECT_TRUE(mockHandler->taskCount >= 1);
157 
158     GTEST_LOG_(INFO) << "OnReceiveEvent_0400 end";
159 }
160 } // namespace AAFwk
161 } // namespace OHOS
162