AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
MockMessageRouterObserver.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef ALEXA_CLIENT_SDK_ACL_TEST_TRANSPORT_MOCKMESSAGEROUTEROBSERVER_H_
17 #define ALEXA_CLIENT_SDK_ACL_TEST_TRANSPORT_MOCKMESSAGEROUTEROBSERVER_H_
18 
20 
21 #include <gmock/gmock.h>
22 
23 #include <memory>
24 
25 namespace alexaClientSDK {
26 namespace acl {
27 namespace transport {
28 namespace test {
29 
30 // Cannot actually mock this class, since it is used exclusively through friend relationship
32 public:
33  void reset() {
34  std::lock_guard<std::mutex> lock(m_mutex);
35  m_notifiedOfReceive = false;
36  m_notifiedOfStatusChanged = false;
37  }
38 
40  std::lock_guard<std::mutex> lock(m_mutex);
41  return m_notifiedOfStatusChanged;
42  }
43 
45  std::lock_guard<std::mutex> lock(m_mutex);
46  return m_notifiedOfReceive;
47  }
48 
50  std::lock_guard<std::mutex> lock(m_mutex);
51  return m_status;
52  }
53 
55  std::lock_guard<std::mutex> lock(m_mutex);
56  return m_reason;
57  }
58 
60  std::chrono::milliseconds timeout,
63  std::unique_lock<std::mutex> lock(m_mutex);
64  auto result = m_cv.wait_for(lock, timeout, [this, status, reason]() {
65  return m_notifiedOfStatusChanged && m_status == status && m_reason == reason;
66  });
67  return result;
68  }
69 
71  std::lock_guard<std::mutex> lock(m_mutex);
72  return m_message;
73  }
74 
76  std::lock_guard<std::mutex> lock(m_mutex);
77  return m_attachmentContextId;
78  }
79 
80 private:
81  void onConnectionStatusChanged(
83  const std::vector<avsCommon::sdkInterfaces::ConnectionStatusObserverInterface::EngineConnectionStatus>&
84  engineConnectionStatuses) override {
85  if (engineConnectionStatuses.empty()) {
86  return;
87  }
88  std::lock_guard<std::mutex> lock(m_mutex);
89  m_notifiedOfStatusChanged = true;
90  m_status = status;
91  for (auto connectionStatus : engineConnectionStatuses) {
92  if (connectionStatus.engineType == avsCommon::sdkInterfaces::ENGINE_TYPE_ALEXA_VOICE_SERVICES) {
93  m_reason = connectionStatus.reason;
94  }
95  }
96  m_cv.notify_all();
97  }
98 
99  void receive(const std::string& contextId, const std::string& message) override {
100  std::lock_guard<std::mutex> lock(m_mutex);
101  m_notifiedOfReceive = true;
102  m_attachmentContextId = contextId;
103  m_message = message;
104  }
105 
107  std::mutex m_mutex;
108 
110  std::condition_variable m_cv;
111 
114 
117 
119  std::string m_attachmentContextId;
120 
122  std::string m_message;
123 
125  bool m_notifiedOfStatusChanged;
126 
128  bool m_notifiedOfReceive;
129 };
130 
131 } // namespace test
132 } // namespace transport
133 } // namespace acl
134 } // namespace alexaClientSDK
135 
136 #endif // ALEXA_CLIENT_SDK_ACL_TEST_TRANSPORT_MOCKMESSAGEROUTEROBSERVER_H_
Definition: MockMessageRouterObserver.h:31
bool wasNotifiedOfStatusChange()
Definition: MockMessageRouterObserver.h:39
ChangedReason
Definition: ConnectionStatusObserverInterface.h:50
Definition: MessageRouterObserverInterface.h:31
Definition: AmdMetricWrapperTest.cpp:24
::std::string string
Definition: gtest-port.h:1097
avsCommon::sdkInterfaces::ConnectionStatusObserverInterface::Status getLatestConnectionStatus()
Definition: MockMessageRouterObserver.h:49
void reset()
Definition: MockMessageRouterObserver.h:33
std::string getLatestMessage()
Definition: MockMessageRouterObserver.h:70
std::string getAttachmentContextId()
Definition: MockMessageRouterObserver.h:75
bool wasNotifiedOfReceive()
Definition: MockMessageRouterObserver.h:44
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
avsCommon::sdkInterfaces::ConnectionStatusObserverInterface::ChangedReason getLatestConnectionChangedReason()
Definition: MockMessageRouterObserver.h:54
bool waitForStatusChange(std::chrono::milliseconds timeout, avsCommon::sdkInterfaces::ConnectionStatusObserverInterface::Status status, avsCommon::sdkInterfaces::ConnectionStatusObserverInterface::ChangedReason reason)
Definition: MockMessageRouterObserver.h:59
tuple message
Definition: gtest_output_test.py:331
Status
Definition: ConnectionStatusObserverInterface.h:36
static const int ENGINE_TYPE_ALEXA_VOICE_SERVICES
Definition: ConnectionStatusObserverInterface.h:26

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0