Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: chromeos/components/tether/message_wrapper_unittest.cc

Issue 2926503002: [CrOS Tether] Create proto_test_util.cc/h and move CreateTestDeviceStatus() there. (Closed)
Patch Set: Included missing dep. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos/components/tether/message_wrapper.h" 5 #include "chromeos/components/tether/message_wrapper.h"
6 6
7 #include <sstream> 7 #include <sstream>
8 8
9 #include "base/base64url.h" 9 #include "base/base64url.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "chromeos/components/tether/proto/tether.pb.h" 12 #include "chromeos/components/tether/proto/tether.pb.h"
13 #include "chromeos/components/tether/proto_test_util.h"
13 #include "testing/gmock/include/gmock/gmock.h" 14 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 16
16 namespace chromeos { 17 namespace chromeos {
17 18
18 namespace tether { 19 namespace tether {
19 20
20 namespace { 21 namespace {
21 22
22 DeviceStatus CreateFakeDeviceStatus() {
23 WifiStatus wifi_status;
24 wifi_status.set_status_code(
25 WifiStatus_StatusCode::WifiStatus_StatusCode_CONNECTED);
26 wifi_status.set_ssid("Google A");
27
28 DeviceStatus device_status;
29 device_status.set_battery_percentage(75);
30 device_status.set_cell_provider("Google Fi");
31 device_status.set_connection_strength(4);
32 device_status.mutable_wifi_status()->CopyFrom(wifi_status);
33
34 return device_status;
35 }
36
37 TetherAvailabilityResponse CreateTetherAvailabilityResponse() { 23 TetherAvailabilityResponse CreateTetherAvailabilityResponse() {
38 TetherAvailabilityResponse response; 24 TetherAvailabilityResponse response;
39 response.set_response_code( 25 response.set_response_code(
40 TetherAvailabilityResponse_ResponseCode:: 26 TetherAvailabilityResponse_ResponseCode::
41 TetherAvailabilityResponse_ResponseCode_TETHER_AVAILABLE); 27 TetherAvailabilityResponse_ResponseCode_TETHER_AVAILABLE);
42 response.mutable_device_status()->CopyFrom(CreateFakeDeviceStatus()); 28 response.mutable_device_status()->CopyFrom(
29 CreateDeviceStatusWithFakeFields());
43 return response; 30 return response;
44 } 31 }
45 32
46 void VerifyProtoConversion(const google::protobuf::MessageLite* proto, 33 void VerifyProtoConversion(const google::protobuf::MessageLite* proto,
47 const MessageWrapper& wrapper, 34 const MessageWrapper& wrapper,
48 const MessageType& expected_message_type) { 35 const MessageType& expected_message_type) {
49 std::string raw_message = wrapper.ToRawMessage(); 36 std::string raw_message = wrapper.ToRawMessage();
50 EXPECT_TRUE(raw_message.length() > 0); 37 EXPECT_TRUE(raw_message.length() > 0);
51 38
52 std::unique_ptr<MessageWrapper> wrapper_from_raw_message = 39 std::unique_ptr<MessageWrapper> wrapper_from_raw_message =
(...skipping 21 matching lines...) Expand all
74 VerifyProtoConversion(&request, wrapper, 61 VerifyProtoConversion(&request, wrapper,
75 MessageType::CONNECT_TETHERING_REQUEST); 62 MessageType::CONNECT_TETHERING_REQUEST);
76 } 63 }
77 64
78 TEST_F(MessageWrapperTest, TestToAndFromRawMessage_ConnectTetheringResponse) { 65 TEST_F(MessageWrapperTest, TestToAndFromRawMessage_ConnectTetheringResponse) {
79 ConnectTetheringResponse response; 66 ConnectTetheringResponse response;
80 response.set_ssid("Instant Tethering 123456"); 67 response.set_ssid("Instant Tethering 123456");
81 response.set_password("password"); 68 response.set_password("password");
82 response.set_response_code(ConnectTetheringResponse_ResponseCode:: 69 response.set_response_code(ConnectTetheringResponse_ResponseCode::
83 ConnectTetheringResponse_ResponseCode_SUCCESS); 70 ConnectTetheringResponse_ResponseCode_SUCCESS);
84 response.mutable_device_status()->CopyFrom(CreateFakeDeviceStatus()); 71 response.mutable_device_status()->CopyFrom(
72 CreateDeviceStatusWithFakeFields());
85 73
86 MessageWrapper wrapper(response); 74 MessageWrapper wrapper(response);
87 VerifyProtoConversion(&response, wrapper, 75 VerifyProtoConversion(&response, wrapper,
88 MessageType::CONNECT_TETHERING_RESPONSE); 76 MessageType::CONNECT_TETHERING_RESPONSE);
89 } 77 }
90 78
91 TEST_F(MessageWrapperTest, TestToAndFromRawMessage_DisconnectTetheringRequest) { 79 TEST_F(MessageWrapperTest, TestToAndFromRawMessage_DisconnectTetheringRequest) {
92 DisconnectTetheringRequest request; 80 DisconnectTetheringRequest request;
93 81
94 MessageWrapper wrapper(request); 82 MessageWrapper wrapper(request);
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 << response.SerializeAsString() // Do not convert to base-64. 149 << response.SerializeAsString() // Do not convert to base-64.
162 << "\"}"; 150 << "\"}";
163 151
164 EXPECT_FALSE(MessageWrapper::FromRawMessage(ss.str())); 152 EXPECT_FALSE(MessageWrapper::FromRawMessage(ss.str()));
165 } 153 }
166 154
167 TEST_F(MessageWrapperTest, TestFromRawMessage_StringLiteral) { 155 TEST_F(MessageWrapperTest, TestFromRawMessage_StringLiteral) {
168 // Type 2 is TETHER_AVAILABILITY_RESPONSE, and the data supplied is 156 // Type 2 is TETHER_AVAILABILITY_RESPONSE, and the data supplied is
169 // CreateTetherAvailabilityResponse() encoded in base-64. 157 // CreateTetherAvailabilityResponse() encoded in base-64.
170 std::string raw_message = 158 std::string raw_message =
171 "{\"type\":2,\"data\":\"CAESHQhLEglHb29nbGUgRmkYBCIMCAESCEdvb2dsZSBB\"}"; 159 "{\"type\":2,\"data\":\"CAESHQhLEglHb29nbGUgRmkYBCIMCAESCFdpZmlTc2lk\"}";
172 160
173 std::unique_ptr<MessageWrapper> wrapper = 161 std::unique_ptr<MessageWrapper> wrapper =
174 MessageWrapper::FromRawMessage(raw_message); 162 MessageWrapper::FromRawMessage(raw_message);
175 EXPECT_TRUE(wrapper); 163 EXPECT_TRUE(wrapper);
176 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, 164 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE,
177 wrapper->GetMessageType()); 165 wrapper->GetMessageType());
178 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), 166 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(),
179 wrapper->GetProto()->SerializeAsString()); 167 wrapper->GetProto()->SerializeAsString());
180 } 168 }
181 169
(...skipping 15 matching lines...) Expand all
197 EXPECT_TRUE(wrapper); 185 EXPECT_TRUE(wrapper);
198 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, 186 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE,
199 wrapper->GetMessageType()); 187 wrapper->GetMessageType());
200 EXPECT_EQ(response.SerializeAsString(), 188 EXPECT_EQ(response.SerializeAsString(),
201 wrapper->GetProto()->SerializeAsString()); 189 wrapper->GetProto()->SerializeAsString());
202 } 190 }
203 191
204 } // namespace tether 192 } // namespace tether
205 193
206 } // namespace cryptauth 194 } // namespace cryptauth
OLDNEW
« no previous file with comments | « chromeos/components/tether/message_transfer_operation_unittest.cc ('k') | chromeos/components/tether/proto_test_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698