• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 #ifndef IAM_MOCK_CONTEXT_H
16 #define IAM_MOCK_CONTEXT_H
17 
18 #include <memory>
19 
20 #include <gmock/gmock.h>
21 
22 #include "app_mgr_interface.h"
23 #include "context.h"
24 #include "context_callback.h"
25 #include "iam_ptr.h"
26 #include "mock_schedule_node.h"
27 
28 namespace OHOS {
29 namespace UserIam {
30 namespace UserAuth {
31 using namespace OHOS::AppExecFwk;
32 class MockContextCallback : public ContextCallback {
33 public:
34     virtual ~MockContextCallback() = default;
35     MOCK_METHOD2(NewInstance, std::shared_ptr<ContextCallback>(sptr<IamCallbackInterface> iamCallback,
36         OperationType operationType));
37     MOCK_METHOD2(OnResult, void(int32_t resultCode, const Attributes &finalResult));
38     MOCK_METHOD3(
39         OnAcquireInfo, void(ExecutorRole src, int32_t moduleType, const std::vector<uint8_t> &acquireMsg));
40     MOCK_METHOD1(SetTraceCallerName, void(const std::string &callerName));
41     MOCK_METHOD1(SetTraceRequestContextId, void(uint64_t requestContextId));
42     MOCK_METHOD1(SetTraceAuthContextId, void(uint64_t authContextId));
43     MOCK_METHOD1(SetTraceUserId, void(int32_t userId));
44     MOCK_METHOD1(SetTraceRemainTime, void(int32_t remainTime));
45     MOCK_METHOD1(SetTraceFreezingTime, void(int32_t freezingTime));
46     MOCK_METHOD1(SetTraceSdkVersion, void(int32_t version));
47     MOCK_METHOD1(SetTraceAuthType, void(int32_t authType));
48     MOCK_METHOD1(SetTraceAuthWidgetType, void(uint32_t authWidgetType));
49     MOCK_METHOD1(SetTraceAuthTrustLevel, void(AuthTrustLevel atl));
50     MOCK_METHOD1(SetTraceReuseUnlockResultMode, void(uint32_t reuseUnlockResultMode));
51     MOCK_METHOD1(SetTraceReuseUnlockResultDuration, void(uint64_t reuseUnlockResultDuration));
52     MOCK_METHOD1(SetCleaner, void(Context::ContextStopCallback callback));
53     MOCK_METHOD2(ProcessAuthResult, void(int32_t tip, const std::vector<uint8_t> &extraInfo));
54     MOCK_METHOD0(GetIamCallback, sptr<IamCallbackInterface>());
55     MOCK_METHOD0(GetCallerName, std::string());
56     MOCK_METHOD1(SetTraceCallerType, void(int32_t callerType));
57     MOCK_METHOD1(SetTraceIsRemoteAuth, void(bool isRemoteAuth));
58     MOCK_METHOD1(SetTraceRemoteUdid, void(const std::string &remoteUdid));
59     MOCK_METHOD1(SetTraceLocalUdid, void(const std::string &LocalUdid));
60     MOCK_METHOD1(SetTraceConnectionName, void(const std::string &connectionName));
61     MOCK_METHOD1(SetTraceAuthFinishReason, void(const std::string &authFinishReason));
62     MOCK_METHOD1(SetTraceIsBackgroundApplication, void(const bool isBackgroundApplication));
63 };
64 
65 class MockContext final : public Context {
66 public:
67     MOCK_METHOD0(Start, bool());
68     MOCK_METHOD0(Stop, bool());
69     MOCK_CONST_METHOD0(GetContextId, uint64_t());
70     MOCK_CONST_METHOD0(GetContextType, ContextType());
71     MOCK_CONST_METHOD1(GetScheduleNode, std::shared_ptr<ScheduleNode>(uint64_t scheduleId));
72     MOCK_CONST_METHOD0(GetScheduleNodes, std::vector<std::shared_ptr<ScheduleNode>> ());
73     MOCK_CONST_METHOD0(GetLatestError, int32_t());
74     MOCK_CONST_METHOD0(GetTokenId, uint32_t());
75     MOCK_CONST_METHOD0(GetUserId, int32_t());
76     MOCK_CONST_METHOD0(GetAuthType, int32_t());
77     MOCK_CONST_METHOD0(GetCallerName, std::string());
78 
CreateWithContextId(uint64_t contextId)79     static std::shared_ptr<Context> CreateWithContextId(uint64_t contextId)
80     {
81         using namespace testing;
82         auto context = Common::MakeShared<MockContext>();
83         if (context == nullptr) {
84             EXPECT_NE(context, nullptr);
85             return nullptr;
86         };
87         EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(contextId));
88         return context;
89     }
90 
CreateContextWithScheduleNode(uint64_t contextId,std::set<uint64_t> scheduleIdList)91     static std::shared_ptr<Context> CreateContextWithScheduleNode(uint64_t contextId, std::set<uint64_t> scheduleIdList)
92     {
93         using namespace testing;
94         auto context = Common::MakeShared<MockContext>();
95         if (context == nullptr) {
96             EXPECT_NE(context, nullptr);
97             return nullptr;
98         };
99         EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(contextId));
100         EXPECT_CALL(*context, GetScheduleNode(_)).Times(AnyNumber());
101 
102         ON_CALL(*context, GetScheduleNode)
103             .WillByDefault([scheduleIdList](uint64_t id) -> std::shared_ptr<ScheduleNode> {
104                 auto iter = scheduleIdList.find(id);
105                 if (iter != scheduleIdList.end()) {
106                     return MockScheduleNode::CreateWithScheduleId(id);
107                 }
108                 return nullptr;
109             });
110         return context;
111     }
112 
CreateContextWithScheduleNode(uint64_t contextId,const std::set<std::shared_ptr<ScheduleNode>> & scheduleIdList)113     static std::shared_ptr<Context> CreateContextWithScheduleNode(
114         uint64_t contextId, const std::set<std::shared_ptr<ScheduleNode>> &scheduleIdList)
115     {
116         using namespace testing;
117         auto context = Common::MakeShared<MockContext>();
118         if (context == nullptr) {
119             EXPECT_NE(context, nullptr);
120             return nullptr;
121         };
122         EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(contextId));
123         EXPECT_CALL(*context, GetScheduleNode(_)).Times(AnyNumber());
124 
125         ON_CALL(*context, GetScheduleNode)
126             .WillByDefault([scheduleIdList](uint64_t id) -> std::shared_ptr<ScheduleNode> {
127                 for (auto const &node : scheduleIdList) {
128                     if (node->GetScheduleId() == id) {
129                         return node;
130                     }
131                 }
132                 return nullptr;
133             });
134         return context;
135     }
136 
137 protected:
138     MOCK_METHOD1(SetLatestError, void(int32_t error));
139 };
140 } // namespace UserAuth
141 } // namespace UserIam
142 } // namespace OHOS
143 #endif // IAM_MOCK_CONTEXT_H