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 DATA_OBJECT_MOCK_APP_DEVICE_CHANGE_LISTENER_H
16 #define DATA_OBJECT_MOCK_APP_DEVICE_CHANGE_LISTENER_H
17 
18 #include <gmock/gmock.h>
19 
20 #include <condition_variable>
21 #include <memory>
22 
23 #include "app_device_status_change_listener.h"
24 namespace OHOS {
25 namespace ObjectStore {
26 class MockAppDeviceStatusChangeListener : public AppDeviceStatusChangeListener {
27 public:
MockAppDeviceStatusChangeListener()28     MockAppDeviceStatusChangeListener()
29     {
30     }
OnDeviceChanged(const DeviceInfo & info,const DeviceChangeType & type)31     void OnDeviceChanged(const DeviceInfo &info, const DeviceChangeType &type) const override
32     {
33         std::unique_lock<std::mutex> lock(mutex_);
34         deviceInfo_ = info;
35         result_ = true;
36         cond_.notify_all();
37     }
38 
Wait()39     void Wait()
40     {
41         std::unique_lock<std::mutex> lock(mutex_);
42         if (!result_) {
43             cond_.wait(lock, [this] { return result_; });
44         }
45     }
46 
Compare(const DeviceInfo & deviceInfo)47     bool Compare(const DeviceInfo &deviceInfo)
48     {
49         if (deviceInfo_.deviceId == "" && deviceInfo_.deviceName == deviceInfo.deviceName
50             && deviceInfo_.deviceType == deviceInfo.deviceType) {
51             return true;
52         }
53         return false;
54     }
55 
56 public:
57     mutable std::mutex mutex_;
58     mutable std::condition_variable cond_;
59     mutable bool result_;
60     mutable DeviceInfo deviceInfo_;
61 };
62 class MockAppDeviceStatusChangeListenerLow : public MockAppDeviceStatusChangeListener {
GetChangeLevelType()63     ChangeLevelType GetChangeLevelType() const override
64     {
65         return ChangeLevelType::LOW;
66     }
67 };
68 
69 class MockAppDeviceStatusChangeListenerHigh : public MockAppDeviceStatusChangeListener {
70 public:
GetChangeLevelType()71     ChangeLevelType GetChangeLevelType() const override
72     {
73         return ChangeLevelType::HIGH;
74     }
75 };
76 
77 class MockAppDeviceStatusChangeListenerMin : public MockAppDeviceStatusChangeListener {
78 public:
GetChangeLevelType()79     ChangeLevelType GetChangeLevelType() const override
80     {
81         return ChangeLevelType::MIN;
82     }
83 };
84 } // namespace ObjectStore
85 } // namespace OHOS
86 
87 #endif // DATA_OBJECT_MOCK_APP_DEVICE_CHANGE_LISTENER_H
88