forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_receiver_unittest.cc
165 lines (130 loc) · 5.55 KB
/
message_receiver_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/phonehub/message_receiver_impl.h"
#include <netinet/in.h>
#include <memory>
#include "base/strings/strcat.h"
#include "chromeos/components/phonehub/fake_connection_manager.h"
#include "chromeos/components/phonehub/proto/phonehub_api.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace phonehub {
namespace {
class FakeObserver : public MessageReceiver::Observer {
public:
FakeObserver() = default;
~FakeObserver() override = default;
size_t snapshot_num_calls() const {
return phone_status_snapshot_updated_num_calls_;
}
size_t status_updated_num_calls() const {
return phone_status_updated_num_calls_;
}
proto::PhoneStatusSnapshot last_snapshot() const { return last_snapshot_; }
proto::PhoneStatusUpdate last_status_update() const {
return last_status_update_;
}
// MessageReceiver::Observer:
void OnPhoneStatusSnapshotReceived(
proto::PhoneStatusSnapshot phone_status_snapshot) override {
last_snapshot_ = phone_status_snapshot;
++phone_status_snapshot_updated_num_calls_;
}
void OnPhoneStatusUpdateReceived(
proto::PhoneStatusUpdate phone_status_update) override {
last_status_update_ = phone_status_update;
++phone_status_updated_num_calls_;
}
private:
size_t phone_status_snapshot_updated_num_calls_ = 0;
size_t phone_status_updated_num_calls_ = 0;
proto::PhoneStatusSnapshot last_snapshot_;
proto::PhoneStatusUpdate last_status_update_;
};
std::string SerializeMessage(proto::MessageType message_type,
const google::protobuf::MessageLite* request) {
// Add two space characters, followed by the serialized proto.
std::string message = base::StrCat({" ", request->SerializeAsString()});
// Replace the first two characters with |message_type| as a 16-bit int.
uint16_t* ptr =
reinterpret_cast<uint16_t*>(const_cast<char*>(message.data()));
*ptr = htons(static_cast<uint16_t>(message_type));
return message;
}
} // namespace
class MessageReceiverImplTest : public testing::Test {
protected:
MessageReceiverImplTest()
: fake_connection_manager_(std::make_unique<FakeConnectionManager>()) {}
MessageReceiverImplTest(const MessageReceiverImplTest&) = delete;
MessageReceiverImplTest& operator=(const MessageReceiverImplTest&) = delete;
~MessageReceiverImplTest() override = default;
void SetUp() override {
message_receiver_ =
std::make_unique<MessageReceiverImpl>(fake_connection_manager_.get());
message_receiver_->AddObserver(&fake_observer_);
}
void TearDown() override {
message_receiver_->RemoveObserver(&fake_observer_);
}
size_t GetNumPhoneStatusSnapshotCalls() const {
return fake_observer_.snapshot_num_calls();
}
size_t GetNumPhoneStatusUpdatedCalls() const {
return fake_observer_.status_updated_num_calls();
}
proto::PhoneStatusSnapshot GetLastSnapshot() const {
return fake_observer_.last_snapshot();
}
proto::PhoneStatusUpdate GetLastStatusUpdate() const {
return fake_observer_.last_status_update();
}
FakeObserver fake_observer_;
std::unique_ptr<FakeConnectionManager> fake_connection_manager_;
std::unique_ptr<MessageReceiverImpl> message_receiver_;
};
TEST_F(MessageReceiverImplTest, OnPhoneStatusSnapshotReceieved) {
const int32_t expected_battery_percentage = 15;
auto expected_phone_properties = std::make_unique<proto::PhoneProperties>();
expected_phone_properties->set_battery_percentage(
expected_battery_percentage);
proto::PhoneStatusSnapshot expected_snapshot;
expected_snapshot.set_allocated_properties(
expected_phone_properties.release());
expected_snapshot.add_notifications();
// Simulate receiving a message.
const std::string expected_message =
SerializeMessage(proto::PHONE_STATUS_SNAPSHOT, &expected_snapshot);
fake_connection_manager_->NotifyMessageReceived(expected_message);
proto::PhoneStatusSnapshot actual_snapshot = GetLastSnapshot();
EXPECT_EQ(1u, GetNumPhoneStatusSnapshotCalls());
EXPECT_EQ(0u, GetNumPhoneStatusUpdatedCalls());
EXPECT_EQ(expected_battery_percentage,
actual_snapshot.properties().battery_percentage());
EXPECT_EQ(1, actual_snapshot.notifications_size());
}
TEST_F(MessageReceiverImplTest, OnPhoneStatusUpdated) {
const int32_t expected_battery_percentage = 15u;
auto expected_phone_properties = std::make_unique<proto::PhoneProperties>();
expected_phone_properties->set_battery_percentage(
expected_battery_percentage);
proto::PhoneStatusUpdate expected_update;
expected_update.set_allocated_properties(expected_phone_properties.release());
expected_update.add_updated_notifications();
const int64_t expected_removed_id = 24u;
expected_update.add_removed_notification_ids(expected_removed_id);
// Simulate receiving a message.
const std::string expected_message =
SerializeMessage(proto::PHONE_STATUS_UPDATE, &expected_update);
fake_connection_manager_->NotifyMessageReceived(expected_message);
proto::PhoneStatusUpdate actual_update = GetLastStatusUpdate();
EXPECT_EQ(0u, GetNumPhoneStatusSnapshotCalls());
EXPECT_EQ(1u, GetNumPhoneStatusUpdatedCalls());
EXPECT_EQ(expected_battery_percentage,
actual_update.properties().battery_percentage());
EXPECT_EQ(1, actual_update.updated_notifications_size());
EXPECT_EQ(expected_removed_id, actual_update.removed_notification_ids()[0]);
}
} // namespace phonehub
} // namespace chromeos