• 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 "context.h"
23 #include "context_callback.h"
24 #include "iam_ptr.h"
25 #include "mock_schedule_node.h"
26 
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
30 class MockContextCallback : public ContextCallback {
31 public:
32     virtual ~MockContextCallback() = default;
33     MOCK_METHOD2(NewInstance, std::shared_ptr<ContextCallback>(sptr<IamCallbackInterface> iamCallback,
34         OperationType operationType));
35     MOCK_METHOD2(OnResult, void(int32_t resultCode, const Attributes &finalResult));
36     MOCK_CONST_METHOD3(
37         OnAcquireInfo, void(ExecutorRole src, int32_t moduleType, const std::vector<uint8_t> &acquireMsg));
38     MOCK_METHOD1(SetTraceUserId, void(int32_t userId));
39     MOCK_METHOD1(SetTraceRemainTime, void(int32_t remainTime));
40     MOCK_METHOD1(SetTraceFreezingTime, void(int32_t freezingTime));
41     MOCK_METHOD1(SetTraceSdkVersion, void(int32_t version));
42     MOCK_METHOD1(SetTraceCallingUid, void(uint64_t callingUid));
43     MOCK_METHOD1(SetTraceAuthType, void(AuthType authType));
44     MOCK_METHOD1(SetTraceAuthTrustLevel, void(AuthTrustLevel atl));
45     MOCK_METHOD1(SetTraceCombinedAuthType, void(uint32_t authWidgetType));
46     MOCK_METHOD1(SetTraceSubAuth, void(bool isSubAuth));
47     MOCK_METHOD1(SetTraceWindowMode, void(WindowModeType windowMode));
48     MOCK_METHOD1(SetTraceAuthWidgetType, void(uint32_t authWidgetType));
49     MOCK_METHOD1(SetTraceNavigation, void(bool hasNaviBnt));
50     MOCK_METHOD1(SetCleaner, void(Context::ContextStopCallback callback));
51 };
52 
53 class MockContext final : public Context {
54 public:
55     MOCK_METHOD0(Start, bool());
56     MOCK_METHOD0(Stop, bool());
57     MOCK_CONST_METHOD0(GetContextId, uint64_t());
58     MOCK_CONST_METHOD0(GetContextType, ContextType());
59     MOCK_CONST_METHOD1(GetScheduleNode, std::shared_ptr<ScheduleNode>(uint64_t scheduleId));
60     MOCK_CONST_METHOD0(GetLatestError, int32_t());
61     MOCK_CONST_METHOD0(GetTokenId, uint32_t());
62 
CreateWithContextId(uint64_t contextId)63     static std::shared_ptr<Context> CreateWithContextId(uint64_t contextId)
64     {
65         using namespace testing;
66         auto context = Common::MakeShared<MockContext>();
67         if (context == nullptr) {
68             EXPECT_NE(context, nullptr);
69             return nullptr;
70         };
71         EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(contextId));
72         return context;
73     }
74 
CreateContextWithScheduleNode(uint64_t contextId,std::set<uint64_t> scheduleIdList)75     static std::shared_ptr<Context> CreateContextWithScheduleNode(uint64_t contextId, std::set<uint64_t> scheduleIdList)
76     {
77         using namespace testing;
78         auto context = Common::MakeShared<MockContext>();
79         if (context == nullptr) {
80             EXPECT_NE(context, nullptr);
81             return nullptr;
82         };
83         EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(contextId));
84         EXPECT_CALL(*context, GetScheduleNode(_)).Times(AnyNumber());
85 
86         ON_CALL(*context, GetScheduleNode)
87             .WillByDefault([scheduleIdList](uint64_t id) -> std::shared_ptr<ScheduleNode> {
88                 auto iter = scheduleIdList.find(id);
89                 if (iter != scheduleIdList.end()) {
90                     return MockScheduleNode::CreateWithScheduleId(id);
91                 }
92                 return nullptr;
93             });
94         return context;
95     }
96 
CreateContextWithScheduleNode(uint64_t contextId,const std::set<std::shared_ptr<ScheduleNode>> & scheduleIdList)97     static std::shared_ptr<Context> CreateContextWithScheduleNode(
98         uint64_t contextId, const std::set<std::shared_ptr<ScheduleNode>> &scheduleIdList)
99     {
100         using namespace testing;
101         auto context = Common::MakeShared<MockContext>();
102         if (context == nullptr) {
103             EXPECT_NE(context, nullptr);
104             return nullptr;
105         };
106         EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(contextId));
107         EXPECT_CALL(*context, GetScheduleNode(_)).Times(AnyNumber());
108 
109         ON_CALL(*context, GetScheduleNode)
110             .WillByDefault([scheduleIdList](uint64_t id) -> std::shared_ptr<ScheduleNode> {
111                 for (auto const &node : scheduleIdList) {
112                     if (node->GetScheduleId() == id) {
113                         return node;
114                     }
115                 }
116                 return nullptr;
117             });
118         return context;
119     }
120 
121 protected:
122     MOCK_METHOD1(SetLatestError, void(int32_t error));
123 };
124 } // namespace UserAuth
125 } // namespace UserIam
126 } // namespace OHOS
127 #endif // IAM_MOCK_CONTEXT_H