1 /*
2  * Copyright (c) 2021-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 
16 #ifndef MOCK_OHOS_ABILITY_RUNTIME_MOCK_ABILITY_TEST_H
17 #define MOCK_OHOS_ABILITY_RUNTIME_MOCK_ABILITY_TEST_H
18 
19 #include "gmock/gmock.h"
20 
21 #include "ability.h"
22 #include "ability_loader.h"
23 #include "want.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 #define INSERT 20
28 #define UPDATE 33
29 #define OPENRAWFILE 122
30 #define BATCHINSERT 115
31 #define DELETE 234
32 
33 class MockAbilityTest : public Ability {
34 public:
35 
Insert(const Uri & uri,const NativeRdb::ValuesBucket & value)36     int Insert(const Uri& uri, const NativeRdb::ValuesBucket& value)
37     {
38         GTEST_LOG_(INFO) << "MockAbilityTest::Insert called";
39         return INSERT;
40     }
Update(const Uri & uri,const NativeRdb::ValuesBucket & value,const NativeRdb::DataAbilityPredicates & predicates)41     int Update(const Uri& uri, const NativeRdb::ValuesBucket& value, const NativeRdb::DataAbilityPredicates& predicates)
42     {
43         GTEST_LOG_(INFO) << "MockAbilityTest::Update called";
44         return UPDATE;
45     }
46 
GetFileTypes(const Uri & uri,const std::string & mimeTypeFilter)47     std::vector<std::string> GetFileTypes(const Uri& uri, const std::string& mimeTypeFilter)
48     {
49         std::vector<std::string> result;
50         result.push_back("Type1");
51         result.push_back("Type2");
52         result.push_back("Type3");
53         return result;
54     }
55 
OpenFile(const Uri & uri,const std::string & mode)56     int OpenFile(const Uri& uri, const std::string& mode)
57     {
58         int fd;
59         GTEST_LOG_(INFO) << "MockAbilityTest::OpenFile called";
60         FILE* fd1 = fopen("/dataability_openfile_test.txt", "w+");
61         if (fd1 == nullptr) {
62             GTEST_LOG_(INFO) << "MockAbilityTest::OpenFile fd1 == nullptr";
63             return -1;
64         }
65         (void)fputs("123456", fd1);
66         (void)fclose(fd1);
67 
68         FILE* fd2 = fopen("/dataability_openfile_test.txt", "r");
69         if (fd2 == nullptr) {
70             GTEST_LOG_(INFO) << "MockAbilityTest::OpenFile fd2 == nullptr";
71             return -1;
72         }
73         fd = fileno(fd2);
74 
75         return fd;
76     }
77 
OpenRawFile(const Uri & uri,const std::string & mode)78     int OpenRawFile(const Uri& uri, const std::string& mode)
79     {
80         GTEST_LOG_(INFO) << "MockAbilityTest::OpenRawFile called";
81 
82         return OPENRAWFILE;
83     }
84 
BatchInsert(const Uri & uri,const std::vector<NativeRdb::ValuesBucket> & values)85     int BatchInsert(const Uri& uri, const std::vector<NativeRdb::ValuesBucket>& values)
86     {
87         GTEST_LOG_(INFO) << "MockAbilityTest::BatchInsert called";
88         return BATCHINSERT;
89     }
90 
Reload(const Uri & uri,const PacMap & extras)91     bool Reload(const Uri& uri, const PacMap& extras)
92     {
93         GTEST_LOG_(INFO) << "MockAbilityTest::Reload called";
94         return true;
95     }
96 
Delete(const Uri & uri,const NativeRdb::DataAbilityPredicates & predicates)97     int Delete(const Uri& uri, const NativeRdb::DataAbilityPredicates& predicates)
98     {
99         GTEST_LOG_(INFO) << "MockAbilityTest::Delete called";
100         return DELETE;
101     }
102 
GetType(const Uri & uri)103     std::string GetType(const Uri& uri)
104     {
105         GTEST_LOG_(INFO) << "MockAbilityTest::GetType called";
106         std::string type("Type1");
107         return type;
108     }
109 
Query(const Uri & uri,const std::vector<std::string> & columns,const NativeRdb::DataAbilityPredicates & predicates)110     std::shared_ptr<NativeRdb::AbsSharedResultSet> Query(
111         const Uri& uri, const std::vector<std::string>& columns, const NativeRdb::DataAbilityPredicates& predicates)
112     {
113         GTEST_LOG_(INFO) << "MockDataAbility::Query called";
114         std::shared_ptr<NativeRdb::AbsSharedResultSet> set = std::make_shared<NativeRdb::AbsSharedResultSet>(
115             "QueryTest");
116         return set;
117     }
118 
NormalizeUri(const Uri & uri)119     Uri NormalizeUri(const Uri& uri)
120     {
121         GTEST_LOG_(INFO) << "MockAbilityTest::NormalizeUri called";
122         return uri;
123     }
124 
DenormalizeUri(const Uri & uri)125     Uri DenormalizeUri(const Uri& uri)
126     {
127         GTEST_LOG_(INFO) << "MockAbilityTest::DenormalizeUri called";
128         return uri;
129     }
130 };
131 }  // namespace AppExecFwk
132 }  // namespace OHOS
133 #endif  // MOCK_OHOS_ABILITY_RUNTIME_MOCK_ABILITY_TEST_H
134