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