1 /* 2 * Copyright (c) 2022 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(OnResult, void(int32_t resultCode, const Attributes &finalResult)); 34 MOCK_CONST_METHOD3( 35 OnAcquireInfo, void(ExecutorRole src, int32_t moduleType, const std::vector<uint8_t> &acquireMsg)); 36 MOCK_METHOD1(SetTraceUserId, void(int32_t userId)); 37 MOCK_METHOD1(SetTraceRemainTime, void(int32_t remainTime)); 38 MOCK_METHOD1(SetTraceFreezingTime, void(int32_t freezingTime)); 39 MOCK_METHOD1(SetTraceSdkVersion, void(int32_t version)); 40 MOCK_METHOD1(SetTraceCallingUid, void(uint64_t callingUid)); 41 MOCK_METHOD1(SetTraceAuthType, void(AuthType authType)); 42 MOCK_METHOD1(SetTraceAuthTrustLevel, void(AuthTrustLevel atl)); 43 MOCK_METHOD1(SetCleaner, void(Context::ContextStopCallback callback)); 44 }; 45 46 class MockContext final : public Context { 47 public: 48 MOCK_METHOD0(Start, bool()); 49 MOCK_METHOD0(Stop, bool()); 50 MOCK_CONST_METHOD0(GetContextId, uint64_t()); 51 MOCK_CONST_METHOD0(GetContextType, ContextType()); 52 MOCK_CONST_METHOD1(GetScheduleNode, std::shared_ptr<ScheduleNode>(uint64_t scheduleId)); 53 MOCK_CONST_METHOD0(GetLatestError, int32_t()); 54 CreateWithContextId(uint64_t contextId)55 static std::shared_ptr<Context> CreateWithContextId(uint64_t contextId) 56 { 57 using namespace testing; 58 auto context = Common::MakeShared<MockContext>(); 59 if (context == nullptr) { 60 EXPECT_NE(context, nullptr); 61 return nullptr; 62 }; 63 EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(contextId)); 64 return context; 65 } 66 CreateContextWithScheduleNode(uint64_t contextId,std::set<uint64_t> scheduleIdList)67 static std::shared_ptr<Context> CreateContextWithScheduleNode(uint64_t contextId, std::set<uint64_t> scheduleIdList) 68 { 69 using namespace testing; 70 auto context = Common::MakeShared<MockContext>(); 71 if (context == nullptr) { 72 EXPECT_NE(context, nullptr); 73 return nullptr; 74 }; 75 EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(contextId)); 76 EXPECT_CALL(*context, GetScheduleNode(_)).Times(AnyNumber()); 77 78 ON_CALL(*context, GetScheduleNode) 79 .WillByDefault([scheduleIdList](uint64_t id) -> std::shared_ptr<ScheduleNode> { 80 auto iter = scheduleIdList.find(id); 81 if (iter != scheduleIdList.end()) { 82 return MockScheduleNode::CreateWithScheduleId(id); 83 } 84 return nullptr; 85 }); 86 return context; 87 } 88 CreateContextWithScheduleNode(uint64_t contextId,const std::set<std::shared_ptr<ScheduleNode>> & scheduleIdList)89 static std::shared_ptr<Context> CreateContextWithScheduleNode( 90 uint64_t contextId, const std::set<std::shared_ptr<ScheduleNode>> &scheduleIdList) 91 { 92 using namespace testing; 93 auto context = Common::MakeShared<MockContext>(); 94 if (context == nullptr) { 95 EXPECT_NE(context, nullptr); 96 return nullptr; 97 }; 98 EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(contextId)); 99 EXPECT_CALL(*context, GetScheduleNode(_)).Times(AnyNumber()); 100 101 ON_CALL(*context, GetScheduleNode) 102 .WillByDefault([scheduleIdList](uint64_t id) -> std::shared_ptr<ScheduleNode> { 103 for (auto const &node : scheduleIdList) { 104 if (node->GetScheduleId() == id) { 105 return node; 106 } 107 } 108 return nullptr; 109 }); 110 return context; 111 } 112 113 protected: 114 MOCK_METHOD1(SetLatestError, void(int32_t error)); 115 }; 116 } // namespace UserAuth 117 } // namespace UserIam 118 } // namespace OHOS 119 #endif // IAM_MOCK_CONTEXT_H