| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <memory> | |
| 6 #include <sstream> | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/at_exit.h" | |
| 10 #include "base/strings/stringprintf.h" | |
| 11 #include "blimp/common/create_blimp_message.h" | |
| 12 #include "blimp/common/logging.h" | |
| 13 #include "blimp/common/proto/blimp_message.pb.h" | |
| 14 #include "blimp/common/proto/blob_channel.pb.h" | |
| 15 #include "testing/gmock/include/gmock/gmock.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 using testing::Return; | |
| 19 | |
| 20 namespace blimp { | |
| 21 namespace { | |
| 22 | |
| 23 const int kTargetTab = 123; | |
| 24 | |
| 25 // Verifies that the logged form of |msg| matches |expected|, modulo prefix | |
| 26 // and suffix. | |
| 27 void VerifyLogOutput(const std::string& expected_fragment, | |
| 28 const BlimpMessage& msg) { | |
| 29 std::string expected = "<BlimpMessage " + expected_fragment + " byte_size=" + | |
| 30 std::to_string(msg.ByteSize()) + ">"; | |
| 31 std::stringstream outstream; | |
| 32 outstream << msg; | |
| 33 EXPECT_EQ(expected, outstream.str()); | |
| 34 } | |
| 35 | |
| 36 class LoggingTest : public testing::Test { | |
| 37 public: | |
| 38 LoggingTest() {} | |
| 39 ~LoggingTest() override {} | |
| 40 | |
| 41 private: | |
| 42 // Deletes the singleton on test termination. | |
| 43 base::ShadowingAtExitManager at_exit_; | |
| 44 }; | |
| 45 | |
| 46 TEST_F(LoggingTest, Compositor) { | |
| 47 BlimpMessage base_msg; | |
| 48 base_msg.mutable_compositor(); | |
| 49 base_msg.set_target_tab_id(kTargetTab); | |
| 50 VerifyLogOutput("type=COMPOSITOR render_widget_id=0 target_tab_id=123", | |
| 51 base_msg); | |
| 52 } | |
| 53 | |
| 54 TEST_F(LoggingTest, Input) { | |
| 55 const char* fragment_format = | |
| 56 "type=INPUT render_widget_id=1 timestamp_seconds=2.000000 subtype=%s" | |
| 57 " target_tab_id=123"; | |
| 58 | |
| 59 BlimpMessage base_msg; | |
| 60 base_msg.set_target_tab_id(kTargetTab); | |
| 61 base_msg.mutable_input()->set_type(InputMessage::Type_GestureScrollBegin); | |
| 62 base_msg.mutable_input()->set_render_widget_id(1); | |
| 63 base_msg.mutable_input()->set_timestamp_seconds(2); | |
| 64 VerifyLogOutput(base::StringPrintf(fragment_format, "GestureScrollBegin"), | |
| 65 base_msg); | |
| 66 | |
| 67 base_msg.mutable_input()->set_type(InputMessage::Type_GestureScrollEnd); | |
| 68 VerifyLogOutput(base::StringPrintf(fragment_format, "GestureScrollEnd"), | |
| 69 base_msg); | |
| 70 | |
| 71 base_msg.mutable_input()->set_type(InputMessage::Type_GestureScrollUpdate); | |
| 72 VerifyLogOutput(base::StringPrintf(fragment_format, "GestureScrollUpdate"), | |
| 73 base_msg); | |
| 74 | |
| 75 base_msg.mutable_input()->set_type(InputMessage::Type_GestureFlingStart); | |
| 76 VerifyLogOutput(base::StringPrintf(fragment_format, "GestureFlingStart"), | |
| 77 base_msg); | |
| 78 | |
| 79 base_msg.mutable_input()->set_type(InputMessage::Type_GestureTap); | |
| 80 VerifyLogOutput(base::StringPrintf(fragment_format, "GestureTap"), base_msg); | |
| 81 | |
| 82 base_msg.mutable_input()->set_type(InputMessage::Type_GesturePinchBegin); | |
| 83 VerifyLogOutput(base::StringPrintf(fragment_format, "GesturePinchBegin"), | |
| 84 base_msg); | |
| 85 | |
| 86 base_msg.mutable_input()->set_type(InputMessage::Type_GesturePinchEnd); | |
| 87 VerifyLogOutput(base::StringPrintf(fragment_format, "GesturePinchEnd"), | |
| 88 base_msg); | |
| 89 | |
| 90 base_msg.mutable_input()->set_type(InputMessage::Type_GesturePinchUpdate); | |
| 91 VerifyLogOutput(base::StringPrintf(fragment_format, "GesturePinchUpdate"), | |
| 92 base_msg); | |
| 93 | |
| 94 base_msg.mutable_input()->set_type(InputMessage::Type_GestureFlingCancel); | |
| 95 base_msg.mutable_input() | |
| 96 ->mutable_gesture_fling_cancel() | |
| 97 ->set_prevent_boosting(true); | |
| 98 VerifyLogOutput( | |
| 99 "type=INPUT render_widget_id=1 timestamp_seconds=2.000000 " | |
| 100 "subtype=GestureFlingCancel prevent_boosting=true target_tab_id=123", | |
| 101 base_msg); | |
| 102 } | |
| 103 | |
| 104 TEST_F(LoggingTest, Navigation) { | |
| 105 BlimpMessage base_msg; | |
| 106 base_msg.set_target_tab_id(kTargetTab); | |
| 107 | |
| 108 BlimpMessage navigation_state_msg = base_msg; | |
| 109 navigation_state_msg.mutable_navigation()->set_type( | |
| 110 NavigationMessage::NAVIGATION_STATE_CHANGED); | |
| 111 navigation_state_msg.mutable_navigation() | |
| 112 ->mutable_navigation_state_changed() | |
| 113 ->set_url("http://foo.com"); | |
| 114 navigation_state_msg.mutable_navigation() | |
| 115 ->mutable_navigation_state_changed() | |
| 116 ->set_favicon("bytes!"); | |
| 117 navigation_state_msg.mutable_navigation() | |
| 118 ->mutable_navigation_state_changed() | |
| 119 ->set_title("FooCo"); | |
| 120 navigation_state_msg.mutable_navigation() | |
| 121 ->mutable_navigation_state_changed() | |
| 122 ->set_loading(true); | |
| 123 VerifyLogOutput( | |
| 124 "type=NAVIGATION subtype=NAVIGATION_STATE_CHANGED url=\"http://foo.com\" " | |
| 125 "favicon_size=6 title=\"FooCo\" loading=true target_tab_id=123", | |
| 126 navigation_state_msg); | |
| 127 | |
| 128 BlimpMessage load_url_msg = base_msg; | |
| 129 load_url_msg.mutable_navigation()->set_type(NavigationMessage::LOAD_URL); | |
| 130 load_url_msg.mutable_navigation()->mutable_load_url()->set_url( | |
| 131 "http://foo.com"); | |
| 132 VerifyLogOutput( | |
| 133 "type=NAVIGATION subtype=LOAD_URL url=\"http://foo.com\" " | |
| 134 "target_tab_id=123", | |
| 135 load_url_msg); | |
| 136 | |
| 137 BlimpMessage go_back_msg = base_msg; | |
| 138 go_back_msg.mutable_navigation()->set_type(NavigationMessage::GO_BACK); | |
| 139 VerifyLogOutput("type=NAVIGATION subtype=GO_BACK target_tab_id=123", | |
| 140 go_back_msg); | |
| 141 | |
| 142 BlimpMessage go_forward_msg = base_msg; | |
| 143 go_forward_msg.mutable_navigation()->set_type(NavigationMessage::GO_FORWARD); | |
| 144 VerifyLogOutput("type=NAVIGATION subtype=GO_FORWARD target_tab_id=123", | |
| 145 go_forward_msg); | |
| 146 | |
| 147 BlimpMessage reload_msg = base_msg; | |
| 148 reload_msg.mutable_navigation()->set_type(NavigationMessage::RELOAD); | |
| 149 VerifyLogOutput("type=NAVIGATION subtype=RELOAD target_tab_id=123", | |
| 150 reload_msg); | |
| 151 } | |
| 152 | |
| 153 TEST_F(LoggingTest, TabControl) { | |
| 154 BlimpMessage base_msg; | |
| 155 base_msg.set_target_tab_id(kTargetTab); | |
| 156 | |
| 157 BlimpMessage create_tab_msg = base_msg; | |
| 158 create_tab_msg.mutable_tab_control()->mutable_create_tab(); | |
| 159 VerifyLogOutput("type=TAB_CONTROL subtype=CREATE_TAB target_tab_id=123", | |
| 160 create_tab_msg); | |
| 161 | |
| 162 BlimpMessage close_tab_msg = base_msg; | |
| 163 close_tab_msg.mutable_tab_control()->mutable_close_tab(); | |
| 164 VerifyLogOutput("type=TAB_CONTROL subtype=CLOSE_TAB target_tab_id=123", | |
| 165 close_tab_msg); | |
| 166 | |
| 167 BlimpMessage size_msg = base_msg; | |
| 168 size_msg.mutable_tab_control()->mutable_size(); | |
| 169 size_msg.mutable_tab_control()->mutable_size()->set_width(640); | |
| 170 size_msg.mutable_tab_control()->mutable_size()->set_height(480); | |
| 171 size_msg.mutable_tab_control()->mutable_size()->set_device_pixel_ratio(2); | |
| 172 VerifyLogOutput( | |
| 173 "type=TAB_CONTROL subtype=SIZE size=640x480:2.00 target_tab_id=123", | |
| 174 size_msg); | |
| 175 } | |
| 176 | |
| 177 TEST_F(LoggingTest, Geolocation) { | |
| 178 BlimpMessage base_msg; | |
| 179 | |
| 180 BlimpMessage interest_level_msg = base_msg; | |
| 181 interest_level_msg.mutable_geolocation()->mutable_set_interest_level() | |
| 182 ->set_level(GeolocationSetInterestLevelMessage::HIGH_ACCURACY); | |
| 183 VerifyLogOutput("type=GEOLOCATION subtype=SET_INTEREST_LEVEL " | |
| 184 "level=1", interest_level_msg); | |
| 185 | |
| 186 BlimpMessage request_refresh_msg = base_msg; | |
| 187 request_refresh_msg.mutable_geolocation()->mutable_request_refresh(); | |
| 188 VerifyLogOutput("type=GEOLOCATION subtype=REQUEST_REFRESH", | |
| 189 request_refresh_msg); | |
| 190 | |
| 191 BlimpMessage coordinates_msg = base_msg; | |
| 192 coordinates_msg.mutable_geolocation()->mutable_coordinates() | |
| 193 ->set_latitude(140); | |
| 194 coordinates_msg.mutable_geolocation()->mutable_coordinates() | |
| 195 ->set_longitude(150); | |
| 196 coordinates_msg.mutable_geolocation()->mutable_coordinates() | |
| 197 ->set_altitude(160); | |
| 198 coordinates_msg.mutable_geolocation()->mutable_coordinates() | |
| 199 ->set_accuracy(50); | |
| 200 coordinates_msg.mutable_geolocation()->mutable_coordinates() | |
| 201 ->set_altitude_accuracy(60); | |
| 202 coordinates_msg.mutable_geolocation()->mutable_coordinates() | |
| 203 ->set_heading(70); | |
| 204 coordinates_msg.mutable_geolocation()->mutable_coordinates()->set_speed(80); | |
| 205 VerifyLogOutput("type=GEOLOCATION subtype=COORDINATES " | |
| 206 "latitude=140.000000 longitude=150.000000 " | |
| 207 "altitude=160.000000 accuracy=50.000000 " | |
| 208 "altitude_accuracy=60.000000 heading=70.000000 " | |
| 209 "speed=80.000000", | |
| 210 coordinates_msg); | |
| 211 | |
| 212 BlimpMessage error_msg = base_msg; | |
| 213 error_msg.mutable_geolocation()->mutable_error()->set_error_code( | |
| 214 GeolocationErrorMessage::TIMEOUT); | |
| 215 error_msg.mutable_geolocation()->mutable_error()->set_error_message( | |
| 216 "Timeout occured."); | |
| 217 VerifyLogOutput("type=GEOLOCATION subtype=ERROR " | |
| 218 "error_code=3 error_message=\"Timeout occured.\"", | |
| 219 error_msg); | |
| 220 } | |
| 221 | |
| 222 TEST_F(LoggingTest, ProtocolControl) { | |
| 223 BlimpMessage base_msg; | |
| 224 | |
| 225 BlimpMessage start_connection_msg = base_msg; | |
| 226 start_connection_msg.mutable_protocol_control()->mutable_start_connection(); | |
| 227 start_connection_msg.mutable_protocol_control() | |
| 228 ->mutable_start_connection() | |
| 229 ->set_client_auth_token("token"); | |
| 230 start_connection_msg.mutable_protocol_control() | |
| 231 ->mutable_start_connection() | |
| 232 ->set_protocol_version(2); | |
| 233 VerifyLogOutput( | |
| 234 "type=PROTOCOL_CONTROL subtype=START_CONNECTION " | |
| 235 "client_token=\"token\" protocol_version=2", | |
| 236 start_connection_msg); | |
| 237 | |
| 238 start_connection_msg.mutable_protocol_control()->mutable_checkpoint_ack(); | |
| 239 start_connection_msg.mutable_protocol_control() | |
| 240 ->mutable_checkpoint_ack() | |
| 241 ->set_checkpoint_id(123); | |
| 242 VerifyLogOutput( | |
| 243 "type=PROTOCOL_CONTROL subtype=CHECKPOINT_ACK " | |
| 244 "checkpoint_id=123", | |
| 245 start_connection_msg); | |
| 246 } | |
| 247 | |
| 248 TEST_F(LoggingTest, RenderWidget) { | |
| 249 BlimpMessage base_msg; | |
| 250 base_msg.mutable_render_widget()->set_render_widget_id(123); | |
| 251 | |
| 252 BlimpMessage initialize_msg = base_msg; | |
| 253 initialize_msg.mutable_render_widget()->set_type( | |
| 254 RenderWidgetMessage::INITIALIZE); | |
| 255 VerifyLogOutput("type=RENDER_WIDGET subtype=INITIALIZE render_widget_id=123", | |
| 256 initialize_msg); | |
| 257 | |
| 258 BlimpMessage created_msg = base_msg; | |
| 259 created_msg.mutable_render_widget()->set_type( | |
| 260 RenderWidgetMessage::CREATED); | |
| 261 VerifyLogOutput("type=RENDER_WIDGET subtype=CREATED render_widget_id=123", | |
| 262 created_msg); | |
| 263 | |
| 264 BlimpMessage deleted_msg = base_msg; | |
| 265 deleted_msg.mutable_render_widget()->set_type(RenderWidgetMessage::DELETED); | |
| 266 VerifyLogOutput("type=RENDER_WIDGET subtype=DELETED render_widget_id=123", | |
| 267 deleted_msg); | |
| 268 } | |
| 269 | |
| 270 TEST_F(LoggingTest, BlobChannel) { | |
| 271 BlobChannelMessage* blob_message = nullptr; | |
| 272 std::unique_ptr<BlimpMessage> blimp_message = | |
| 273 CreateBlimpMessage(&blob_message); | |
| 274 blob_message->mutable_transfer_blob()->set_blob_id("AAA"); | |
| 275 blob_message->mutable_transfer_blob()->set_payload("123"); | |
| 276 | |
| 277 VerifyLogOutput( | |
| 278 "type=BLOB_CHANNEL subtype=TRANSFER_BLOB id=\"414141\" payload_size=3", | |
| 279 *blimp_message); | |
| 280 } | |
| 281 | |
| 282 TEST_F(LoggingTest, Settings) { | |
| 283 BlimpMessage message; | |
| 284 message.mutable_settings() | |
| 285 ->mutable_engine_settings() | |
| 286 ->set_record_whole_document(true); | |
| 287 message.mutable_settings()->mutable_engine_settings()->set_client_os_info( | |
| 288 "wibble"); | |
| 289 VerifyLogOutput( | |
| 290 "type=SETTINGS subtype=ENGINE_SETTINGS record_whole_document=true " | |
| 291 "client_os_info=\"wibble\"", | |
| 292 message); | |
| 293 } | |
| 294 | |
| 295 TEST_F(LoggingTest, Ime) { | |
| 296 BlimpMessage message; | |
| 297 message.mutable_ime()->set_render_widget_id(1); | |
| 298 | |
| 299 // Test SHOW_IME. | |
| 300 message.mutable_ime()->set_type(ImeMessage::SHOW_IME); | |
| 301 message.mutable_ime()->set_text_input_type(ImeMessage::NONE); | |
| 302 VerifyLogOutput( | |
| 303 "type=IME render_widget_id=1 subtype=SHOW_IME text_input_type=0", | |
| 304 message); | |
| 305 | |
| 306 // Test HIDE_IME. | |
| 307 message.mutable_ime()->set_type(ImeMessage::HIDE_IME); | |
| 308 VerifyLogOutput( | |
| 309 "type=IME render_widget_id=1 subtype=HIDE_IME", | |
| 310 message); | |
| 311 | |
| 312 // Test SET_TEXT. | |
| 313 message.mutable_ime()->set_type(ImeMessage::SET_TEXT); | |
| 314 message.mutable_ime()->set_ime_text("1234"); | |
| 315 VerifyLogOutput( | |
| 316 "type=IME render_widget_id=1 subtype=SET_TEXT ime_text(length)=4", | |
| 317 message); | |
| 318 } | |
| 319 | |
| 320 } // namespace | |
| 321 } // namespace blimp | |
| OLD | NEW |