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 #include "widget_context_callback_impl.h"
16 
17 #include <future>
18 #include <gmock/gmock.h>
19 #include "iam_ptr.h"
20 #include "mock_context.h"
21 #include "context_factory.h"
22 #include "widget_context.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
30 
31 class WidgetContextCallbackImplTest : public testing::Test {
32 public:
33     static void SetUpTestCase();
34 
35     static void TearDownTestCase();
36 
37     void SetUp() ;
38 
39     void TearDown() ;
40 };
41 
SetUpTestCase()42 void WidgetContextCallbackImplTest::SetUpTestCase()
43 {
44 }
45 
TearDownTestCase()46 void WidgetContextCallbackImplTest::TearDownTestCase()
47 {
48 }
49 
SetUp()50 void WidgetContextCallbackImplTest::SetUp()
51 {
52 }
53 
TearDown()54 void WidgetContextCallbackImplTest::TearDown()
55 {
56 }
57 
58 HWTEST_F(WidgetContextCallbackImplTest, WidgetContextCallbackImplOnResult_001, TestSize.Level0)
59 {
60     uint64_t contextId = 1;
61     ContextFactory::AuthWidgetContextPara para;
62     std::shared_ptr<ContextCallback> callback = Common::MakeShared<MockContextCallback>();
63     WidgetContext *widgetContext = new WidgetContext(contextId, para, callback);
64     int32_t authType = 0;
65     auto contextCallback =
66         Common::MakeShared<WidgetContextCallbackImpl>(std::shared_ptr<WidgetContext>(widgetContext), authType);
67     ASSERT_NE(contextCallback, nullptr);
68     int32_t result = 1;
69     Attributes extraInfo;
70     contextCallback->OnResult(result, extraInfo);
71     ASSERT_EQ(para.tokenId, 0);
72 }
73 
74 HWTEST_F(WidgetContextCallbackImplTest, WidgetContextCallbackImplOnResult_002, TestSize.Level0)
75 {
76     std::shared_ptr<WidgetContext> widgetContext = nullptr;
77     int32_t authType = 0;
78     auto contextCallback = Common::MakeShared<WidgetContextCallbackImpl>(widgetContext, authType);
79     ASSERT_NE(contextCallback, nullptr);
80     int32_t result = 1;
81     Attributes extraInfo;
82     contextCallback->OnResult(result, extraInfo);
83 }
84 
85 HWTEST_F(WidgetContextCallbackImplTest, WidgetContextCallbackImplOnAcquireInfo_003, TestSize.Level0)
86 {
87     uint64_t contextId = 1;
88     ContextFactory::AuthWidgetContextPara para;
89     std::shared_ptr<ContextCallback> callback = Common::MakeShared<MockContextCallback>();
90     WidgetContext *widgetContext = new WidgetContext(contextId, para, callback);
91     int32_t authType = 0;
92     auto contextCallback =
93         Common::MakeShared<WidgetContextCallbackImpl>(std::shared_ptr<WidgetContext>(widgetContext), authType);
94     ASSERT_NE(contextCallback, nullptr);
95 
96     int32_t module = 0;
97     int32_t acquireInfo = 0;
98     Attributes extraInfo;
99     contextCallback->OnAcquireInfo(module, acquireInfo, extraInfo);
100 
101     sptr<IRemoteObject> object = contextCallback->AsObject();
102     EXPECT_EQ(object, nullptr);
103 }
104 } // namespace UserAuth
105 } // namespace UserIam
106 } // namespace OHOS