1 /*
2  * Copyright (c) 2021 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 UNITTEST_OHOS_ABILITY_RUNTIME_MOCK_DATA_ABILITY_OBSERVER_STUB_DATAOBS_MGR_INNER_EXT_TEST_H
17 #define UNITTEST_OHOS_ABILITY_RUNTIME_MOCK_DATA_ABILITY_OBSERVER_STUB_DATAOBS_MGR_INNER_EXT_TEST_H
18 #include <atomic>
19 #include "gmock/gmock.h"
20 #include "semaphore_ex.h"
21 #include "data_ability_observer_stub.h"
22 #include "dataobs_mgr_inner_ext.h"
23 
24 namespace OHOS {
25 namespace DataObsMgrInnerExtTest {
26 using namespace AAFwk;
27 class MockDataAbilityObserverStub : public AAFwk::DataAbilityObserverStub {
28 public:
OnChange()29     void OnChange() override
30     {
31     }
32 
OnChangeExt(const ChangeInfo & changeInfo)33     void OnChangeExt(const ChangeInfo &changeInfo) override
34     {
35         onChangeCall_ += changeInfo.uris_.size();
36         changeInfo_ = changeInfo;
37         if (func) {
38             func();
39         }
40     }
41 
ReSet()42     void ReSet()
43     {
44         onChangeCall_.store(0);
45         changeInfo_ = {};
46     }
47 
Wait()48     void Wait()
49     {
50         std::unique_lock<std::mutex> lck(mtx_);
51         while (!flag_) {
52             cv_.wait(lck);
53         }
54     }
55 
Notify()56     void Notify()
57     {
58         std::unique_lock<std::mutex> lck(mtx_);
59         flag_ = true;
60         cv_.notify_one();
61     }
62 
63 private:
64     std::atomic_int onChangeCall_ = 0;
65     ChangeInfo changeInfo_;
66     std::mutex mtx_;
67     std::condition_variable cv_;
68     bool flag_ = false;
69     std::function<void(void)> func;
70 };
71 
72 }  // namespace AAFwk
73 }  // namespace OHOS
74 
75 #endif  // UNITTEST_OHOS_ABILITY_RUNTIME_MOCK_DATA_ABILITY_OBSERVER_STUB_CALL_H
76