1 /*
2  * Copyright (c) 2021 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 #include "completed_callback.h"
18 #define private public
19 #define protected public
20 #include "completed_dispatcher.h"
21 #undef private
22 #undef protected
23 #include "element_name.h"
24 #include "event_handler.h"
25 #include "pending_want.h"
26 #include "base_types.h"
27 #include "want.h"
28 #include "want_params.h"
29 #include "want_receiver_stub.h"
30 #include "bool_wrapper.h"
31 
32 using namespace testing::ext;
33 using namespace OHOS::AAFwk;
34 using namespace OHOS;
35 using OHOS::AppExecFwk::ElementName;
36 using namespace OHOS::AppExecFwk;
37 using vector_str = std::vector<std::string>;
38 namespace {
39 const int32_t SEND_FINISHED_CODE = 100;
40 }
41 
42 namespace OHOS::AbilityRuntime::WantAgent {
43 class CompletedDispatcherTest : public testing::Test {
44 public:
CompletedDispatcherTest()45     CompletedDispatcherTest()
46     {}
~CompletedDispatcherTest()47     ~CompletedDispatcherTest()
48     {}
49     static void SetUpTestCase(void);
50     static void TearDownTestCase(void);
51     void SetUp();
52     void TearDown();
53     Want MakeWant(std::string deviceId, std::string abilityName, std::string bundleName);
54 
55     class CompletedCallbackSon : public CompletedCallback {
56         /**
57          * Called when a Send operation as completed.
58          *
59          * @param want The original Want that was sent.
60          * @param resultCode The final result code determined by the Send.
61          * @param resultData The final data collected by a broadcast.
62          * @param resultExtras The final extras collected by a broadcast.
63          */
64     public:
65         void OnSendFinished(const AAFwk::Want& want, int resultCode, const std::string& resultData,
66             const AAFwk::WantParams& resultExtras) override;
67         static int code;
68     };
69 };
70 
71 int CompletedDispatcherTest::CompletedCallbackSon::code = 0;
72 
OnSendFinished(const AAFwk::Want & want,int resultCode,const std::string & resultData,const AAFwk::WantParams & resultExtras)73 void CompletedDispatcherTest::CompletedCallbackSon::OnSendFinished(
74     const AAFwk::Want& want, int resultCode, const std::string& resultData, const AAFwk::WantParams& resultExtras)
75 {
76     code = SEND_FINISHED_CODE;
77 }
78 
MakeWant(std::string deviceId,std::string abilityName,std::string bundleName)79 Want CompletedDispatcherTest::MakeWant(std::string deviceId, std::string abilityName, std::string bundleName)
80 {
81     ElementName element(deviceId, bundleName, abilityName);
82     Want want;
83     want.SetElement(element);
84     return want;
85 }
86 
SetUpTestCase(void)87 void CompletedDispatcherTest::SetUpTestCase(void)
88 {}
89 
TearDownTestCase(void)90 void CompletedDispatcherTest::TearDownTestCase(void)
91 {}
92 
SetUp(void)93 void CompletedDispatcherTest::SetUp(void)
94 {}
95 
TearDown(void)96 void CompletedDispatcherTest::TearDown(void)
97 {}
98 
99 /*
100  * @tc.number    : CompletedDispatcher_0100
101  * @tc.name      : CompletedDispatcher Constructors
102  * @tc.desc      : 1.The parameter is nullptr
103  */
104 HWTEST_F(CompletedDispatcherTest, CompletedDispatcher_0100, Function | MediumTest | Level1)
105 {
106     CompletedDispatcher completedDispatcher(nullptr, nullptr, nullptr);
107     EXPECT_EQ(completedDispatcher.pendingWant_, nullptr);
108     EXPECT_EQ(completedDispatcher.callback_, nullptr);
109     EXPECT_EQ(completedDispatcher.handler_, nullptr);
110 }
111 
112 /*
113  * @tc.number    : CompletedDispatcher_0200
114  * @tc.name      : CompletedDispatcher Constructors
115  * @tc.desc      : 1.The parameter is not nullptr
116  */
117 HWTEST_F(CompletedDispatcherTest, CompletedDispatcher_0200, Function | MediumTest | Level1)
118 {
119     std::shared_ptr<CompletedDispatcherTest::CompletedCallbackSon> callBack =
120         std::make_shared<CompletedDispatcherTest::CompletedCallbackSon>();
121     CompletedDispatcher completedDispatcher(nullptr, callBack, nullptr);
122     EXPECT_EQ(completedDispatcher.pendingWant_, nullptr);
123     EXPECT_EQ(completedDispatcher.callback_, callBack);
124     EXPECT_EQ(completedDispatcher.handler_, nullptr);
125 }
126 
127 /*
128  * @tc.number    : CompletedDispatcher_0300
129  * @tc.name      : CompletedDispatcher PerformReceive
130  * @tc.desc      : 1.The parameter is not nullptr
131  */
132 HWTEST_F(CompletedDispatcherTest, CompletedDispatcher_0300, Function | MediumTest | Level1)
133 {
134     std::shared_ptr<CompletedDispatcherTest::CompletedCallbackSon> callBack =
135         std::make_shared<CompletedDispatcherTest::CompletedCallbackSon>();
136     CompletedDispatcher completedDispatcher(nullptr, callBack, nullptr);
137 
138     Want want = MakeWant("device", "ability", "bundleName");
139     std::string key = "key";
140     bool value = true;
141     WantParams wParams;
142     wParams.SetParam(key, Boolean::Box(value));
143     completedDispatcher.PerformReceive(want, 10, "test", wParams, 0, 0, 1);
144     EXPECT_EQ(completedDispatcher.want_.GetElement().GetBundleName(), "bundleName");
145     EXPECT_EQ(completedDispatcher.want_.GetElement().GetAbilityName(), "ability");
146     EXPECT_EQ(completedDispatcher.resultCode_, 10);
147     EXPECT_EQ(completedDispatcher.resultData_, "test");
148     EXPECT_EQ(Boolean::Unbox(IBoolean::Query(completedDispatcher.resultExtras_.GetParam(key))), value);
149     EXPECT_EQ(CompletedCallbackSon::code, 100);
150     CompletedCallbackSon::code = 0;
151 }
152 
153 /*
154  * @tc.number    : CompletedDispatcher_0400
155  * @tc.name      : CompletedDispatcher PerformReceive
156  * @tc.desc      : 1.The parameter is not nullptr
157  *                 2.called callBack
158  */
159 HWTEST_F(CompletedDispatcherTest, CompletedDispatcher_0400, Function | MediumTest | Level1)
160 {
161     std::shared_ptr<CompletedDispatcherTest::CompletedCallbackSon> callBack =
162         std::make_shared<CompletedDispatcherTest::CompletedCallbackSon>();
163     std::shared_ptr<EventHandler> handler = std::make_shared<EventHandler>();
164     CompletedDispatcher completedDispatcher(nullptr, callBack, handler);
165 
166     Want want = MakeWant("device", "ability", "bundleName");
167     std::string key = "key";
168     bool value = false;
169     WantParams wParams;
170     wParams.SetParam(key, Boolean::Box(value));
171     completedDispatcher.PerformReceive(want, 10, "test", wParams, 0, 0, 1);
172     EXPECT_EQ(completedDispatcher.want_.GetElement().GetBundleName(), "bundleName");
173     EXPECT_EQ(completedDispatcher.want_.GetElement().GetAbilityName(), "ability");
174     EXPECT_EQ(completedDispatcher.resultCode_, 10);
175     EXPECT_EQ(completedDispatcher.resultData_, "test");
176     EXPECT_EQ(Boolean::Unbox(IBoolean::Query(completedDispatcher.resultExtras_.GetParam(key))), value);
177     EXPECT_EQ(CompletedCallbackSon::code, 0);
178     CompletedCallbackSon::code = 0;
179 }
180 
181 /*
182  * @tc.number    : CompletedDispatcher_0500
183  * @tc.name      : CompletedDispatcher Run
184  * @tc.desc      : 1.The parameter is not nullptr
185  *                 2.called callBack
186  */
187 HWTEST_F(CompletedDispatcherTest, CompletedDispatcher_0500, Function | MediumTest | Level1)
188 {
189     std::shared_ptr<CompletedDispatcherTest::CompletedCallbackSon> callBack =
190         std::make_shared<CompletedDispatcherTest::CompletedCallbackSon>();
191     CompletedDispatcher completedDispatcher(nullptr, callBack, nullptr);
192     EXPECT_EQ(completedDispatcher.callback_, callBack);
193     completedDispatcher.Run();
194     EXPECT_EQ(CompletedCallbackSon::code, 100);
195     CompletedCallbackSon::code = 0;
196 }
197 
198 /*
199  * @tc.number    : CompletedDispatcher_0600
200  * @tc.name      : CompletedDispatcher Run
201  * @tc.desc      : 1.The parameter is nullptr
202  *                 2.no called callBack
203  */
204 HWTEST_F(CompletedDispatcherTest, CompletedDispatcher_0600, Function | MediumTest | Level1)
205 {
206     CompletedDispatcher completedDispatcher(nullptr, nullptr, nullptr);
207     EXPECT_EQ(completedDispatcher.callback_, nullptr);
208     completedDispatcher.Run();
209     EXPECT_EQ(CompletedCallbackSon::code, 0);
210     CompletedCallbackSon::code = 0;
211 }
212 }  // namespace OHOS::AbilityRuntime::WantAgent
213