OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 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 "ui/base/ime/android/cursor_anchor_info_controller.h" |
| 6 |
| 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "base/tuple.h" |
| 9 #include "cc/output/compositor_frame_metadata.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "ui/base/ime/android/cursor_anchor_info_builder.h" |
| 12 #include "ui/base/ime/android/cursor_anchor_info_sender.h" |
| 13 |
| 14 namespace ui { |
| 15 namespace { |
| 16 |
| 17 using ::testing::AssertionFailure; |
| 18 using ::testing::AssertionResult; |
| 19 using ::testing::AssertionSuccess; |
| 20 using ::testing::FloatLE; |
| 21 |
| 22 const uint32 kFlagHasVisibleRegion = |
| 23 CursorAnchorInfoBuilder::kFlagHasVisibleRegion; |
| 24 const uint32 kFlagHasInvisibleRegion = |
| 25 CursorAnchorInfoBuilder::kFlagHasInvisibleRegion; |
| 26 |
| 27 const uint32 kCursorUpdateModeImmediate = |
| 28 CursorAnchorInfoController::kCursorUpdateModeImmediate; |
| 29 const uint32 kCursorUpdateModeMonitor = |
| 30 CursorAnchorInfoController::kCursorUpdateModeMonitor; |
| 31 |
| 32 template <typename T> |
| 33 std::vector<T> Vec() { |
| 34 return std::vector<T>(); |
| 35 } |
| 36 |
| 37 template <typename T> |
| 38 std::vector<T> Vec(const T& param1) { |
| 39 std::vector<T> result; |
| 40 result.push_back(param1); |
| 41 return result; |
| 42 } |
| 43 |
| 44 template <typename T> |
| 45 std::vector<T> Vec(const T& param1, const T& param2) { |
| 46 std::vector<T> result; |
| 47 result.push_back(param1); |
| 48 result.push_back(param2); |
| 49 return result; |
| 50 } |
| 51 |
| 52 template <typename T> |
| 53 std::vector<T> Vec(const T& param1, const T& param2, const T& param3) { |
| 54 std::vector<T> result; |
| 55 result.push_back(param1); |
| 56 result.push_back(param2); |
| 57 result.push_back(param3); |
| 58 return result; |
| 59 } |
| 60 |
| 61 typedef Tuple<int, base::string16> SetComposingTextParams; |
| 62 typedef Tuple<int, gfx::RectF, uint32> AddCharacterBoundsParams; |
| 63 typedef Tuple<float, float, float, float, uint32> |
| 64 SetInsertionMarkerLocationParams; |
| 65 typedef Tuple<gfx::Matrix3F> SetMatrixParams; |
| 66 typedef Tuple<gfx::Range> SetSelectionRangeParams; |
| 67 |
| 68 class CursorAnchorInfoBuilderMock final |
| 69 : public CursorAnchorInfoBuilder { |
| 70 public: |
| 71 |
| 72 CursorAnchorInfoBuilderMock() |
| 73 : build_count_(0), |
| 74 reset_count_(0) { |
| 75 } |
| 76 |
| 77 ~CursorAnchorInfoBuilderMock() override { |
| 78 } |
| 79 |
| 80 void AddCharacterBounds(int index, |
| 81 const gfx::RectF& rect, |
| 82 uint32 flags) override { |
| 83 add_character_bounds_call_log_.push_back(MakeTuple(index, rect, flags)); |
| 84 } |
| 85 |
| 86 base::android::ScopedJavaLocalRef<jobject> Build() override { |
| 87 ++build_count_; |
| 88 return base::android::ScopedJavaLocalRef<jobject>(); |
| 89 } |
| 90 |
| 91 void Reset() override { |
| 92 ++reset_count_; |
| 93 } |
| 94 |
| 95 void SetComposingText(int composing_text_start, |
| 96 base::StringPiece16 composing_text) override { |
| 97 set_composing_text_call_log_.push_back( |
| 98 MakeTuple(composing_text_start, |
| 99 composing_text.as_string())); |
| 100 } |
| 101 |
| 102 void SetInsertionMarkerLocation(float horizontal_position, |
| 103 float line_top, |
| 104 float line_baseline, |
| 105 float line_bottom, |
| 106 uint32 flags) override { |
| 107 set_insertion_marker_location_call_log_.push_back(MakeTuple( |
| 108 horizontal_position, line_top, line_baseline, line_bottom, flags)); |
| 109 } |
| 110 |
| 111 void SetMatrix(const gfx::Matrix3F& matrix) override { |
| 112 set_matrix_call_log_.push_back(MakeTuple(matrix)); |
| 113 } |
| 114 |
| 115 void SetSelectionRange(const gfx::Range& range) override { |
| 116 set_selection_range_call_log_.push_back(MakeTuple(range)); |
| 117 } |
| 118 |
| 119 size_t build_count() const { |
| 120 return build_count_; |
| 121 } |
| 122 |
| 123 size_t reset_count() const { |
| 124 return reset_count_; |
| 125 } |
| 126 |
| 127 const std::vector<SetComposingTextParams>& |
| 128 set_composing_text_call_log() const { |
| 129 return set_composing_text_call_log_; |
| 130 } |
| 131 |
| 132 const std::vector<SetInsertionMarkerLocationParams>& |
| 133 set_insertion_marker_location_call_log() const { |
| 134 return set_insertion_marker_location_call_log_; |
| 135 } |
| 136 |
| 137 const std::vector<SetMatrixParams>& set_matrix_call_log() const { |
| 138 return set_matrix_call_log_; |
| 139 } |
| 140 |
| 141 const std::vector<AddCharacterBoundsParams>& |
| 142 add_character_bounds_call_log() const { |
| 143 return add_character_bounds_call_log_; |
| 144 } |
| 145 |
| 146 const std::vector<SetSelectionRangeParams>& |
| 147 set_selection_range_call_log() const { |
| 148 return set_selection_range_call_log_; |
| 149 } |
| 150 |
| 151 private: |
| 152 size_t build_count_; |
| 153 size_t reset_count_; |
| 154 std::vector<SetComposingTextParams> set_composing_text_call_log_; |
| 155 std::vector<SetInsertionMarkerLocationParams> |
| 156 set_insertion_marker_location_call_log_; |
| 157 std::vector<SetMatrixParams> set_matrix_call_log_; |
| 158 std::vector<AddCharacterBoundsParams> add_character_bounds_call_log_; |
| 159 std::vector<SetSelectionRangeParams> set_selection_range_call_log_; |
| 160 |
| 161 DISALLOW_COPY_AND_ASSIGN(CursorAnchorInfoBuilderMock); |
| 162 }; |
| 163 |
| 164 class CursorAnchorInfoSenderMock final |
| 165 : public CursorAnchorInfoSender { |
| 166 public: |
| 167 |
| 168 CursorAnchorInfoSenderMock() |
| 169 : send_count_(0) { |
| 170 } |
| 171 |
| 172 ~CursorAnchorInfoSenderMock() override { |
| 173 } |
| 174 |
| 175 void SendCursorAnchorInfo(ui::CursorAnchorInfoBuilder* builder) override { |
| 176 ++send_count_; |
| 177 } |
| 178 |
| 179 size_t send_count() const { |
| 180 return send_count_; |
| 181 } |
| 182 |
| 183 private: |
| 184 size_t send_count_; |
| 185 |
| 186 DISALLOW_COPY_AND_ASSIGN(CursorAnchorInfoSenderMock); |
| 187 }; |
| 188 |
| 189 enum UpdateControllerType { |
| 190 kCallOnTextInputStateChanged = 1 << 0, |
| 191 kCallOnCompositionRangeChanged = 1 << 1, |
| 192 kCallOnFrameMetadataUpdated = 1 << 2, |
| 193 }; |
| 194 |
| 195 cc::ViewportSelectionBound CreateViewportSelectionBoundForTest( |
| 196 cc::SelectionBoundType type, |
| 197 const gfx::PointF& edge_top, |
| 198 const gfx::PointF& edge_bottom, |
| 199 bool visible) { |
| 200 cc::ViewportSelectionBound selection_bound; |
| 201 selection_bound.type = type; |
| 202 selection_bound.edge_top = edge_top; |
| 203 selection_bound.edge_bottom = edge_bottom; |
| 204 selection_bound.visible = visible; |
| 205 return selection_bound; |
| 206 } |
| 207 |
| 208 cc::CompositorFrameMetadata CreateFrameMetadataForTest( |
| 209 float device_scale_factor, |
| 210 const gfx::Vector2dF& root_scroll_offset, |
| 211 float page_scale_factor, |
| 212 const gfx::Vector2dF& location_bar_offset, |
| 213 const gfx::Vector2dF& location_bar_content_translation, |
| 214 const cc::ViewportSelectionBound& selection_start) { |
| 215 cc::CompositorFrameMetadata frame_metadata; |
| 216 frame_metadata.device_scale_factor = device_scale_factor; |
| 217 frame_metadata.root_scroll_offset = root_scroll_offset; |
| 218 frame_metadata.page_scale_factor = page_scale_factor; |
| 219 frame_metadata.location_bar_offset = location_bar_offset; |
| 220 frame_metadata.location_bar_content_translation = |
| 221 location_bar_content_translation; |
| 222 frame_metadata.selection_start = selection_start; |
| 223 return frame_metadata; |
| 224 } |
| 225 |
| 226 void UpdateController(CursorAnchorInfoController* controller, |
| 227 int update_type, |
| 228 int salt) { |
| 229 if ((update_type & kCallOnTextInputStateChanged) != 0) { |
| 230 const base::string16 kDummyText = base::ASCIIToUTF16("0123456789"); |
| 231 const gfx::Range kDummySelectionRange(0, salt); |
| 232 const gfx::Range kDummyCompositionRange(3, 5); |
| 233 controller->OnTextInputStateChanged(kDummyText, |
| 234 kDummySelectionRange, |
| 235 kDummyCompositionRange); |
| 236 } |
| 237 |
| 238 if ((update_type & kCallOnCompositionRangeChanged) != 0) { |
| 239 const gfx::Range kDummyRange(0, salt); |
| 240 std::vector<gfx::Rect> dummyCompositionRects; |
| 241 dummyCompositionRects.push_back(gfx::Rect(10, 3, 5, 8)); |
| 242 dummyCompositionRects.push_back(gfx::Rect(15, 3, 5, 8)); |
| 243 controller->OnCompositionRangeChanged(kDummyRange, dummyCompositionRects); |
| 244 } |
| 245 |
| 246 if ((update_type & kCallOnFrameMetadataUpdated) != 0) { |
| 247 const auto selection_start = CreateViewportSelectionBoundForTest( |
| 248 cc::SELECTION_BOUND_CENTER, |
| 249 gfx::PointF(240.0f, 41.0f), |
| 250 gfx::PointF(240.0f, 64.0f), |
| 251 true); |
| 252 const cc::CompositorFrameMetadata dummy_frame_metadata = |
| 253 CreateFrameMetadataForTest( |
| 254 2.0f, |
| 255 gfx::Vector2dF(0.0f, 0.0f), |
| 256 1.0f, |
| 257 gfx::Vector2dF(0.0f, 0.0f), |
| 258 gfx::Vector2dF(0.0f, 0.0f), |
| 259 selection_start); |
| 260 const gfx::Vector2d kDummyViewOriginOffset(0, salt); |
| 261 controller->OnFrameMetadataUpdated(dummy_frame_metadata, |
| 262 kDummyViewOriginOffset); |
| 263 } |
| 264 } |
| 265 |
| 266 gfx::Matrix3F CreateMatrix(float scale, float offset_x, float offset_y) { |
| 267 gfx::Matrix3F matrix = gfx::Matrix3F::Zeros(); |
| 268 matrix.set(scale, 0.0f, offset_x, |
| 269 0.0f, scale, offset_y, |
| 270 0.0f, 0.0f, 1.0f); |
| 271 return matrix; |
| 272 } |
| 273 |
| 274 bool FloatAlmostEqual(float a, float b) { |
| 275 return FloatLE("a", "b", a, b) && FloatLE("b", "a", b, a); |
| 276 } |
| 277 |
| 278 AssertionResult AssertSetInsertionMarkerLocation( |
| 279 const std::vector<SetInsertionMarkerLocationParams>& expected_call_log, |
| 280 const cc::ViewportSelectionBound& selection_start) { |
| 281 |
| 282 CursorAnchorInfoSenderMock sender_mock; |
| 283 // This object will be owned by |controller|. |
| 284 CursorAnchorInfoBuilderMock* builder_mock = new CursorAnchorInfoBuilderMock; |
| 285 auto controller = CursorAnchorInfoController::CreateForTest( |
| 286 &sender_mock, |
| 287 scoped_ptr<CursorAnchorInfoBuilder>(builder_mock)); |
| 288 // Mark as editable. |
| 289 controller->OnFocusedNodeChanged(true); |
| 290 |
| 291 // Enable monitor. |
| 292 controller->OnRequestCursorUpdates(kCursorUpdateModeMonitor); |
| 293 |
| 294 controller->OnFrameMetadataUpdated( |
| 295 CreateFrameMetadataForTest( |
| 296 1.0f, |
| 297 gfx::Vector2dF(0.0f, 0.0f), |
| 298 1.0f, |
| 299 gfx::Vector2dF(0.0f, 0.0f), |
| 300 gfx::Vector2dF(0.0f, 0.0f), |
| 301 selection_start), |
| 302 gfx::Vector2d(0, 0)); |
| 303 |
| 304 const auto& actual_call_log = |
| 305 builder_mock->set_insertion_marker_location_call_log(); |
| 306 if (expected_call_log.size() != actual_call_log.size()) { |
| 307 return AssertionFailure() |
| 308 << "Call count of SetInsertionMarkerLocation is unexpected." |
| 309 << "\nExpected call count: " << expected_call_log.size() |
| 310 << "\n Actual call count: " << actual_call_log.size(); |
| 311 } |
| 312 |
| 313 for (size_t i = 0; i < expected_call_log.size(); ++i) { |
| 314 const auto& expected = expected_call_log[i]; |
| 315 const auto& actual = actual_call_log[i]; |
| 316 if (!FloatAlmostEqual(get<0>(expected), get<0>(actual))) { |
| 317 return AssertionFailure() |
| 318 << "SetInsertionMarkerLocation is called with an unexpected " |
| 319 << "parameter." |
| 320 << "\n parameter: horizontal_position" |
| 321 << "\n expected: " << get<0>(expected) |
| 322 << "\n actual: " << get<0>(actual); |
| 323 } |
| 324 if (!FloatAlmostEqual(get<1>(expected), get<1>(actual))) { |
| 325 return AssertionFailure() |
| 326 << "SetInsertionMarkerLocation is called with an unexpected " |
| 327 << "parameter." |
| 328 << "\n parameter: line_top" |
| 329 << "\n expected: " << get<1>(expected) |
| 330 << "\n actual: " << get<1>(actual); |
| 331 } |
| 332 if (!FloatAlmostEqual(get<2>(expected), get<2>(actual))) { |
| 333 return AssertionFailure() |
| 334 << "SetInsertionMarkerLocation is called with an unexpected " |
| 335 << "parameter." |
| 336 << "\n parameter: line_baseline" |
| 337 << "\n expected: " << get<2>(expected) |
| 338 << "\n actual: " << get<2>(actual); |
| 339 } |
| 340 if (!FloatAlmostEqual(get<3>(expected), get<3>(actual))) { |
| 341 return AssertionFailure() |
| 342 << "SetInsertionMarkerLocation is called with an unexpected parameter." |
| 343 << "\n parameter: line_bottom" |
| 344 << "\n expected: " << get<3>(expected) |
| 345 << "\n actual: " << get<3>(actual); |
| 346 } |
| 347 if (get<4>(expected) != get<4>(actual)) { |
| 348 return AssertionFailure() |
| 349 << "SetInsertionMarkerLocation is called with an unexpected " |
| 350 << "parameter." |
| 351 << "\n parameter: flags" |
| 352 << "\n expected: " << get<4>(expected) |
| 353 << "\n actual: " << get<4>(actual); |
| 354 } |
| 355 } |
| 356 return AssertionSuccess(); |
| 357 } |
| 358 |
| 359 AssertionResult AssertMatrixEqual(const gfx::Matrix3F& expected, |
| 360 const gfx::Matrix3F& actual) { |
| 361 for (int i = 0; i < 3; ++i) { |
| 362 for (int j = 0; j < 3; ++j) { |
| 363 const float expected_value = expected.get(i, j); |
| 364 const float actual_value = actual.get(i, j); |
| 365 if (!FloatAlmostEqual(expected_value, actual_value)) { |
| 366 return AssertionFailure() << "Matrices differ at (i, j) == (" |
| 367 << i << ", " << j << ")" |
| 368 << "\nExpected: " << expected_value |
| 369 << "\n Actual: " << actual_value; |
| 370 } |
| 371 } |
| 372 } |
| 373 return AssertionSuccess(); |
| 374 } |
| 375 |
| 376 AssertionResult AssertSetMatrix( |
| 377 const gfx::Matrix3F& expected_matrix, |
| 378 const cc::CompositorFrameMetadata& frame_metadata, |
| 379 const gfx::Vector2d& view_origin_offset) { |
| 380 CursorAnchorInfoSenderMock sender_mock; |
| 381 // This object will be owned by |controller|. |
| 382 CursorAnchorInfoBuilderMock* builder_mock = new CursorAnchorInfoBuilderMock; |
| 383 auto controller = CursorAnchorInfoController::CreateForTest( |
| 384 &sender_mock, |
| 385 scoped_ptr<CursorAnchorInfoBuilder>(builder_mock)); |
| 386 |
| 387 controller->OnFocusedNodeChanged(true); |
| 388 controller->OnRequestCursorUpdates(kCursorUpdateModeMonitor); |
| 389 |
| 390 controller->OnFrameMetadataUpdated(frame_metadata, view_origin_offset); |
| 391 |
| 392 const auto& log = builder_mock->set_matrix_call_log(); |
| 393 if (log.size() != 1) { |
| 394 return AssertionFailure() << "SetMatrix is not called just once. " |
| 395 << "Actual call count: " << log.size(); |
| 396 } |
| 397 const auto& actual_matrix = get<0>(log[0]); |
| 398 return AssertMatrixEqual(expected_matrix, actual_matrix); |
| 399 } |
| 400 |
| 401 AssertionResult AssertOnTextInputStateChanged( |
| 402 const std::vector<SetSelectionRangeParams>& |
| 403 expected_set_selection_range_call_log, |
| 404 const std::vector<SetComposingTextParams>& |
| 405 expected_set_composing_text_call_log, |
| 406 const base::string16& actual_text, |
| 407 const gfx::Range& actual_selection_range, |
| 408 const gfx::Range& actual_composition_range) { |
| 409 CursorAnchorInfoSenderMock sender_mock; |
| 410 // This object will be owned by |controller|. |
| 411 CursorAnchorInfoBuilderMock* builder_mock = new CursorAnchorInfoBuilderMock; |
| 412 auto controller = CursorAnchorInfoController::CreateForTest( |
| 413 &sender_mock, |
| 414 scoped_ptr<CursorAnchorInfoBuilder>(builder_mock)); |
| 415 |
| 416 controller->OnFocusedNodeChanged(true); |
| 417 controller->OnRequestCursorUpdates(kCursorUpdateModeMonitor); |
| 418 |
| 419 controller->OnTextInputStateChanged(actual_text, |
| 420 actual_selection_range, |
| 421 actual_composition_range); |
| 422 // Calling OnFrameMetadataUpdated is mandatory to build an object. |
| 423 UpdateController(controller.get(), kCallOnFrameMetadataUpdated, 0); |
| 424 |
| 425 { |
| 426 const auto& expected_call_log = expected_set_selection_range_call_log; |
| 427 const auto& actual_call_log = builder_mock->set_selection_range_call_log(); |
| 428 if (expected_call_log.size() != actual_call_log.size()) { |
| 429 return AssertionFailure() |
| 430 << "Call count of SetSelectionRange is unexpected." |
| 431 << "\nExpected call count: " << expected_call_log.size() |
| 432 << "\n Actual call count: " << actual_call_log.size(); |
| 433 } |
| 434 |
| 435 for (size_t i = 0; i < expected_call_log.size(); ++i) { |
| 436 const auto& expected = expected_call_log[i]; |
| 437 const auto& actual = actual_call_log[i]; |
| 438 if (get<0>(expected) != get<0>(actual)) { |
| 439 return AssertionFailure() |
| 440 << "SetSelectionRange is called with an unexpected parameter." |
| 441 << "\n parameter: range" |
| 442 << "\n expected: " << get<0>(expected) |
| 443 << "\n actual: " << get<0>(actual); |
| 444 } |
| 445 } |
| 446 } |
| 447 |
| 448 { |
| 449 const auto& expected_call_log = expected_set_composing_text_call_log; |
| 450 const auto& actual_call_log = builder_mock->set_composing_text_call_log(); |
| 451 if (expected_call_log.size() != actual_call_log.size()) { |
| 452 return AssertionFailure() |
| 453 << "Call count of SetComposingText is unexpected." |
| 454 << "\nExpected call count: " << expected_call_log.size() |
| 455 << "\n Actual call count: " << actual_call_log.size(); |
| 456 } |
| 457 |
| 458 for (size_t i = 0; i < expected_call_log.size(); ++i) { |
| 459 const auto& expected = expected_call_log[i]; |
| 460 const auto& actual = actual_call_log[i]; |
| 461 if (get<0>(expected) != get<0>(actual)) { |
| 462 return AssertionFailure() |
| 463 << "SetComposingText is called with an unexpected parameter." |
| 464 << "\n parameter: range" |
| 465 << "\n expected: " << get<0>(expected) |
| 466 << "\n actual: " << get<0>(actual); |
| 467 } |
| 468 if (get<1>(expected) != get<1>(actual)) { |
| 469 return AssertionFailure() |
| 470 << "SetComposingText is called with an unexpected parameter." |
| 471 << "\n parameter: range" |
| 472 << "\n expected: " << get<1>(expected) |
| 473 << "\n actual: " << get<1>(actual); |
| 474 } |
| 475 } |
| 476 } |
| 477 |
| 478 return AssertionSuccess(); |
| 479 } |
| 480 |
| 481 AssertionResult AssertOnCompositionRangeChanged( |
| 482 const std::vector<AddCharacterBoundsParams>& expected_call_log, |
| 483 const gfx::Range& actual_range, |
| 484 const std::vector<gfx::Rect>& actual_character_bounds) { |
| 485 |
| 486 CursorAnchorInfoSenderMock sender_mock; |
| 487 // This object will be owned by |controller|. |
| 488 CursorAnchorInfoBuilderMock* builder_mock = new CursorAnchorInfoBuilderMock; |
| 489 auto controller = CursorAnchorInfoController::CreateForTest( |
| 490 &sender_mock, |
| 491 scoped_ptr<CursorAnchorInfoBuilder>(builder_mock)); |
| 492 |
| 493 controller->OnFocusedNodeChanged(true); |
| 494 controller->OnRequestCursorUpdates(kCursorUpdateModeMonitor); |
| 495 |
| 496 controller->OnCompositionRangeChanged(actual_range, actual_character_bounds); |
| 497 |
| 498 // Calling OnFrameMetadataUpdated is mandatory to build an object. |
| 499 UpdateController(controller.get(), kCallOnFrameMetadataUpdated, 0); |
| 500 |
| 501 { |
| 502 const auto& actual_call_log = builder_mock->add_character_bounds_call_log(); |
| 503 if (expected_call_log.size() != actual_call_log.size()) { |
| 504 return AssertionFailure() |
| 505 << "Call count of AddCharacterBounds is unexpected." |
| 506 << "\nExpected call count: " << expected_call_log.size() |
| 507 << "\n Actual call count: " << actual_call_log.size(); |
| 508 } |
| 509 |
| 510 for (size_t i = 0; i < expected_call_log.size(); ++i) { |
| 511 const auto& expected = expected_call_log[i]; |
| 512 const auto& actual = actual_call_log[i]; |
| 513 if (get<0>(expected) != get<0>(actual)) { |
| 514 return AssertionFailure() |
| 515 << "AddCharacterBounds is called with an unexpected parameter." |
| 516 << "\n parameter: index" |
| 517 << "\n expected: " << get<0>(expected) |
| 518 << "\n actual: " << get<0>(actual); |
| 519 } |
| 520 if (get<1>(expected) != get<1>(actual)) { |
| 521 return AssertionFailure() |
| 522 << "AddCharacterBounds is called with an unexpected parameter." |
| 523 << "\n parameter: rect" |
| 524 << "\n expected: " << get<1>(expected).ToString() |
| 525 << "\n actual: " << get<1>(actual).ToString(); |
| 526 } |
| 527 if (get<2>(expected) != get<2>(actual)) { |
| 528 return AssertionFailure() |
| 529 << "AddCharacterBounds is called with an unexpected parameter." |
| 530 << "\n parameter: flags" |
| 531 << "\n expected: " << get<2>(expected) |
| 532 << "\n actual: " << get<2>(actual); |
| 533 } |
| 534 } |
| 535 |
| 536 } |
| 537 |
| 538 return AssertionSuccess(); |
| 539 } |
| 540 |
| 541 ///////////////////////////////////////////////////// |
| 542 |
| 543 TEST(CursorAnchorInfoControllerTest, OnFocusedNodeChanged) { |
| 544 CursorAnchorInfoSenderMock sender_mock; |
| 545 // This object will be owned by |controller|. |
| 546 CursorAnchorInfoBuilderMock* builder_mock = new CursorAnchorInfoBuilderMock; |
| 547 auto controller = CursorAnchorInfoController::CreateForTest( |
| 548 &sender_mock, |
| 549 scoped_ptr<CursorAnchorInfoBuilder>(builder_mock)); |
| 550 |
| 551 // Check the initial state. |
| 552 ASSERT_EQ(0u, sender_mock.send_count()); |
| 553 |
| 554 ASSERT_TRUE(controller->OnRequestCursorUpdates(kCursorUpdateModeMonitor)); |
| 555 |
| 556 int salt = 0; |
| 557 ASSERT_EQ(0u, sender_mock.send_count()) |
| 558 << "Data must not be sent when the node is not editable."; |
| 559 UpdateController(controller.get(), kCallOnFrameMetadataUpdated, salt); |
| 560 ASSERT_EQ(0u, sender_mock.send_count()) |
| 561 << "Data must not be sent when the node is not editable."; |
| 562 |
| 563 controller->OnFocusedNodeChanged(true); |
| 564 |
| 565 UpdateController(controller.get(), kCallOnFrameMetadataUpdated, salt); |
| 566 ASSERT_EQ(1u, sender_mock.send_count()) |
| 567 << "Data can be sent when the node is editable."; |
| 568 |
| 569 controller->OnFocusedNodeChanged(false); |
| 570 ASSERT_TRUE(controller->OnRequestCursorUpdates(kCursorUpdateModeMonitor)); |
| 571 |
| 572 UpdateController(controller.get(), kCallOnFrameMetadataUpdated, salt); |
| 573 ASSERT_EQ(1u, sender_mock.send_count()) |
| 574 << "Data must not be sent when the node is not editable."; |
| 575 |
| 576 controller->OnFocusedNodeChanged(true); |
| 577 ASSERT_TRUE(controller->OnRequestCursorUpdates(kCursorUpdateModeMonitor)); |
| 578 |
| 579 UpdateController(controller.get(), kCallOnFrameMetadataUpdated, salt); |
| 580 ASSERT_EQ(2u, sender_mock.send_count()) << |
| 581 "Data must be sent even with the same data (derived from the same salt)" |
| 582 " because OnFocusedNodeChanged resets internal state."; |
| 583 } |
| 584 |
| 585 TEST(CursorAnchorInfoControllerTest, CursorUpdateModeImmediate) { |
| 586 CursorAnchorInfoSenderMock sender_mock; |
| 587 // This object will be owned by |controller|. |
| 588 CursorAnchorInfoBuilderMock* builder_mock = new CursorAnchorInfoBuilderMock; |
| 589 auto controller = CursorAnchorInfoController::CreateForTest( |
| 590 &sender_mock, |
| 591 scoped_ptr<CursorAnchorInfoBuilder>(builder_mock)); |
| 592 controller->OnFocusedNodeChanged(true); |
| 593 |
| 594 // Check the initial state. |
| 595 ASSERT_EQ(0u, sender_mock.send_count()); |
| 596 |
| 597 int salt = 0; |
| 598 |
| 599 ASSERT_TRUE(controller->OnRequestCursorUpdates(kCursorUpdateModeImmediate)); |
| 600 ASSERT_EQ(0u, sender_mock.send_count()) |
| 601 << "Data must not be sent except for |OnFrameMetadataUpdated|."; |
| 602 ++salt; |
| 603 UpdateController(controller.get(), kCallOnTextInputStateChanged, salt); |
| 604 ASSERT_EQ(0u, sender_mock.send_count()) |
| 605 << "Data must not be sent except for |OnFrameMetadataUpdated|."; |
| 606 ++salt; |
| 607 UpdateController(controller.get(), kCallOnCompositionRangeChanged, salt); |
| 608 ASSERT_EQ(0u, sender_mock.send_count()) |
| 609 << "Data must not be sent except for |OnFrameMetadataUpdated|."; |
| 610 ++salt; |
| 611 UpdateController(controller.get(), kCallOnFrameMetadataUpdated, salt); |
| 612 ASSERT_EQ(1u, sender_mock.send_count()) |
| 613 << "Data must be sent from |OnFrameMetadataUpdated| when requested."; |
| 614 |
| 615 ++salt; |
| 616 UpdateController(controller.get(), kCallOnFrameMetadataUpdated, salt); |
| 617 ASSERT_EQ(1u, sender_mock.send_count()) |
| 618 << "Don't need to send data twice in the immediate mode."; |
| 619 } |
| 620 |
| 621 TEST(CursorAnchorInfoControllerTest, CursorUpdateModeMonitor) { |
| 622 CursorAnchorInfoSenderMock sender_mock; |
| 623 // This object will be owned by |controller|. |
| 624 CursorAnchorInfoBuilderMock* builder_mock = new CursorAnchorInfoBuilderMock; |
| 625 auto controller = CursorAnchorInfoController::CreateForTest( |
| 626 &sender_mock, |
| 627 scoped_ptr<CursorAnchorInfoBuilder>(builder_mock)); |
| 628 controller->OnFocusedNodeChanged(true); |
| 629 |
| 630 // Check the initial state. |
| 631 ASSERT_EQ(0u, sender_mock.send_count()); |
| 632 |
| 633 int frame_metadata_salt = 0; |
| 634 int salt = 0; |
| 635 |
| 636 // Test monitor mode: |
| 637 ASSERT_TRUE(controller->OnRequestCursorUpdates(kCursorUpdateModeMonitor)); |
| 638 ASSERT_EQ(0u, sender_mock.send_count()); |
| 639 |
| 640 ++salt; |
| 641 UpdateController(controller.get(), kCallOnTextInputStateChanged, salt); |
| 642 ASSERT_EQ(0u, sender_mock.send_count()) |
| 643 << "Data must not be sent except for |OnFrameMetadataUpdated|."; |
| 644 ++salt; |
| 645 UpdateController(controller.get(), kCallOnCompositionRangeChanged, salt); |
| 646 ASSERT_EQ(0u, sender_mock.send_count()) |
| 647 << "Data must not be sent except for |OnFrameMetadataUpdated|."; |
| 648 ++salt; |
| 649 UpdateController(controller.get(), kCallOnFrameMetadataUpdated, salt); |
| 650 ASSERT_EQ(1u, sender_mock.send_count()) |
| 651 << "Data must be sent from |OnFrameMetadataUpdated| when requested."; |
| 652 frame_metadata_salt = salt; |
| 653 |
| 654 UpdateController(controller.get(), |
| 655 kCallOnFrameMetadataUpdated, |
| 656 frame_metadata_salt); |
| 657 ASSERT_EQ(1u, sender_mock.send_count()) |
| 658 << "Data must not be sent because no data is changed."; |
| 659 |
| 660 ++salt; |
| 661 UpdateController(controller.get(), kCallOnTextInputStateChanged, salt); |
| 662 ASSERT_EQ(1u, sender_mock.send_count()) |
| 663 << "Data must not be sent except for |OnFrameMetadataUpdated|."; |
| 664 UpdateController(controller.get(), |
| 665 kCallOnFrameMetadataUpdated, |
| 666 frame_metadata_salt); |
| 667 ASSERT_EQ(2u, sender_mock.send_count()) |
| 668 << "Data must be sent because |OnTextInputStateChanged| changed data."; |
| 669 UpdateController(controller.get(), kCallOnTextInputStateChanged, salt); |
| 670 ASSERT_EQ(2u, sender_mock.send_count()) |
| 671 << "Data must not be sent except for |OnFrameMetadataUpdated|."; |
| 672 UpdateController(controller.get(), |
| 673 kCallOnFrameMetadataUpdated, |
| 674 frame_metadata_salt); |
| 675 ASSERT_EQ(2u, sender_mock.send_count()) |
| 676 << "Data must not be sent because no data is changed."; |
| 677 |
| 678 ++salt; |
| 679 UpdateController(controller.get(), kCallOnCompositionRangeChanged, salt); |
| 680 ASSERT_EQ(2u, sender_mock.send_count()) |
| 681 << "Data must not be sent except for |OnFrameMetadataUpdated|."; |
| 682 UpdateController(controller.get(), |
| 683 kCallOnFrameMetadataUpdated, |
| 684 frame_metadata_salt); |
| 685 ASSERT_EQ(3u, sender_mock.send_count()) |
| 686 << "Data must be sent because |OnCompositionRangeChanged| changed data."; |
| 687 UpdateController(controller.get(), kCallOnCompositionRangeChanged, salt); |
| 688 UpdateController(controller.get(), |
| 689 kCallOnFrameMetadataUpdated, |
| 690 frame_metadata_salt); |
| 691 ASSERT_EQ(3u, sender_mock.send_count()) |
| 692 << "Data must not be sent because no data is changed."; |
| 693 |
| 694 // Test if we can turn off the monitor mode. |
| 695 ASSERT_TRUE(controller->OnRequestCursorUpdates(0)); |
| 696 |
| 697 ++salt; |
| 698 UpdateController(controller.get(), kCallOnFrameMetadataUpdated, salt); |
| 699 ASSERT_EQ(3u, sender_mock.send_count()) |
| 700 << "Data must not be sent because the monitor mode is turned off."; |
| 701 |
| 702 // Test if we can turn on the monitor mode and immediate mode at the same |
| 703 // time. |
| 704 ASSERT_TRUE(controller->OnRequestCursorUpdates( |
| 705 kCursorUpdateModeMonitor | kCursorUpdateModeImmediate)); |
| 706 ASSERT_EQ(4u, sender_mock.send_count()) |
| 707 << "Data must be sent because the immediate mode is requested."; |
| 708 |
| 709 ++salt; |
| 710 UpdateController(controller.get(), kCallOnFrameMetadataUpdated, salt); |
| 711 ASSERT_EQ(5u, sender_mock.send_count()) |
| 712 << "Data must be sent from |OnFrameMetadataUpdated| when requested."; |
| 713 |
| 714 |
| 715 // Test if |OnFocusedNodeChanged(true)| resets the monitor mode. |
| 716 controller->OnFocusedNodeChanged(true); |
| 717 |
| 718 ++salt; |
| 719 UpdateController(controller.get(), kCallOnFrameMetadataUpdated, salt); |
| 720 ASSERT_EQ(6u, sender_mock.send_count()) |
| 721 << "Data must be sent because |Reset| doesn't reset the monitor " |
| 722 << "mode"; |
| 723 |
| 724 } |
| 725 |
| 726 TEST(CursorAnchorInfoControllerTest, SetInsertionMarkerLocation) { |
| 727 // Case 1: The insertion marker is visible |
| 728 EXPECT_TRUE(AssertSetInsertionMarkerLocation( |
| 729 Vec(MakeTuple(3.0f, 4.0f, 14.0f, 14.0f, kFlagHasVisibleRegion)), |
| 730 CreateViewportSelectionBoundForTest( |
| 731 cc::SELECTION_BOUND_CENTER, |
| 732 gfx::PointF(3.0f, 4.0f), |
| 733 gfx::PointF(3.0f, 14.0f), |
| 734 true))); |
| 735 |
| 736 // Case 2: The insertion marker is invisible |
| 737 EXPECT_TRUE(AssertSetInsertionMarkerLocation( |
| 738 Vec(MakeTuple(3.0f, 4.0f, 14.0f, 14.0f, kFlagHasInvisibleRegion)), |
| 739 CreateViewportSelectionBoundForTest( |
| 740 cc::SELECTION_BOUND_CENTER, |
| 741 gfx::PointF(3.0f, 4.0f), |
| 742 gfx::PointF(3.0f, 14.0f), |
| 743 false))); |
| 744 |
| 745 // Case 3: No insertion marker |
| 746 EXPECT_TRUE(AssertSetInsertionMarkerLocation( |
| 747 Vec<SetInsertionMarkerLocationParams>(), |
| 748 CreateViewportSelectionBoundForTest( |
| 749 cc::SELECTION_BOUND_EMPTY, |
| 750 gfx::PointF(0.0f, 0.0f), |
| 751 gfx::PointF(0.0f, 0.0f), |
| 752 false))); |
| 753 } |
| 754 |
| 755 TEST(CursorAnchorInfoControllerTest, SetMatrix) { |
| 756 const auto empty_selection_start = CreateViewportSelectionBoundForTest( |
| 757 cc::SELECTION_BOUND_EMPTY, |
| 758 gfx::PointF(0.0f, 0.0f), |
| 759 gfx::PointF(0.0f, 0.0f), |
| 760 false); |
| 761 |
| 762 // No transformation |
| 763 EXPECT_TRUE(AssertSetMatrix( |
| 764 CreateMatrix(1.0f, 0.0f, 0.0f), |
| 765 CreateFrameMetadataForTest( |
| 766 1.0f, |
| 767 gfx::Vector2dF(0.0f, 0.0f), |
| 768 1.0f, |
| 769 gfx::Vector2dF(0.0f, 0.0f), |
| 770 gfx::Vector2dF(0.0f, 0.0f), |
| 771 empty_selection_start), |
| 772 gfx::Vector2d(0, 0))); |
| 773 |
| 774 // device_scale_factor == 2.0 |
| 775 EXPECT_TRUE(AssertSetMatrix( |
| 776 CreateMatrix(2.0f, 0.0f, 0.0f), |
| 777 CreateFrameMetadataForTest( |
| 778 2.0f, |
| 779 gfx::Vector2dF(0.0f, 0.0f), |
| 780 1.0f, |
| 781 gfx::Vector2dF(0.0f, 0.0f), |
| 782 gfx::Vector2dF(0.0f, 0.0f), |
| 783 empty_selection_start), |
| 784 gfx::Vector2d(0, 0))); |
| 785 |
| 786 // device_scale_factor == 2.0f |
| 787 // view_origin_offset == (10, 141) |
| 788 EXPECT_TRUE(AssertSetMatrix( |
| 789 CreateMatrix(2.0f, 10.0f, 141.0f), |
| 790 CreateFrameMetadataForTest( |
| 791 2.0f, |
| 792 gfx::Vector2dF(0.0f, 0.0f), |
| 793 1.0f, |
| 794 gfx::Vector2dF(0.0f, 0.0f), |
| 795 gfx::Vector2dF(0.0f, 0.0f), |
| 796 empty_selection_start), |
| 797 gfx::Vector2d(10, 141))); |
| 798 |
| 799 // device_scale_factor == 2.0f |
| 800 // root_scroll_offset == (1.0f, 2.0f) |
| 801 // view_origin_offset == (10.0f, 141.0f) |
| 802 EXPECT_TRUE(AssertSetMatrix( |
| 803 CreateMatrix(2.0f, 10.0f - 2.0f, 141.0f - 4.0f), |
| 804 CreateFrameMetadataForTest( |
| 805 2.0f, |
| 806 gfx::Vector2dF(1.0f, 2.0f), |
| 807 1.0f, |
| 808 gfx::Vector2dF(0.0f, 0.0f), |
| 809 gfx::Vector2dF(0.0f, 0.0f), |
| 810 empty_selection_start), |
| 811 gfx::Vector2d(10, 141))); |
| 812 |
| 813 // device_scale_factor == 2.0f |
| 814 // page_scale_factor == 3.0f |
| 815 // root_scroll_offset == (1.0f, 2.0f) |
| 816 // view_origin_offset == (10.0f, 141.0f) |
| 817 EXPECT_TRUE(AssertSetMatrix( |
| 818 CreateMatrix(6.0f, 10.0f - 6.0f, 141.0f - 12.0f), |
| 819 CreateFrameMetadataForTest( |
| 820 2.0f, |
| 821 gfx::Vector2dF(1.0f, 2.0f), |
| 822 3.0f, |
| 823 gfx::Vector2dF(0.0f, 0.0f), |
| 824 gfx::Vector2dF(0.0f, 0.0f), |
| 825 empty_selection_start), |
| 826 gfx::Vector2d(10, 141))); |
| 827 |
| 828 // device_scale_factor == 2.0f |
| 829 // page_scale_factor == 3.0f |
| 830 // root_scroll_offset == (1.0f, 2.0f) |
| 831 // location_bar_offset == (20.0f, 30.0f) |
| 832 // view_origin_offset == (10.0f, 141.0f) |
| 833 EXPECT_TRUE(AssertSetMatrix( |
| 834 CreateMatrix(6.0f, 30.0f - 6.0f, 171.0f - 12.0f), |
| 835 CreateFrameMetadataForTest( |
| 836 2.0f, |
| 837 gfx::Vector2dF(1.0f, 2.0f), |
| 838 3.0f, |
| 839 gfx::Vector2dF(20.0f, 30.0f), |
| 840 gfx::Vector2dF(0.0f, 0.0f), |
| 841 empty_selection_start), |
| 842 gfx::Vector2d(10, 141))); |
| 843 |
| 844 // device_scale_factor == 2.0f |
| 845 // page_scale_factor == 3.0f |
| 846 // root_scroll_offset == (1.0f, 2.0f) |
| 847 // location_bar_offset == (10.0f, 20.0f) |
| 848 // location_bar_content_translation == (30.0f, 40.0f) |
| 849 // view_origin_offset == (10.0f, 141.0f) |
| 850 EXPECT_TRUE(AssertSetMatrix( |
| 851 CreateMatrix(6.0f, 70.0f - 6.0f, 221.0f - 12.0f), |
| 852 CreateFrameMetadataForTest( |
| 853 2.0f, |
| 854 gfx::Vector2dF(1.0f, 2.0f), |
| 855 3.0f, |
| 856 gfx::Vector2dF(20.0f, 30.0f), |
| 857 gfx::Vector2dF(40.0f, 50.0f), |
| 858 empty_selection_start), |
| 859 gfx::Vector2d(10, 141))); |
| 860 } |
| 861 |
| 862 TEST(CursorAnchorInfoControllerTest, OnTextInputStateChanged) { |
| 863 EXPECT_TRUE(AssertOnTextInputStateChanged( |
| 864 Vec(MakeTuple(gfx::Range(1, 9))), |
| 865 Vec(MakeTuple(3, base::ASCIIToUTF16("34"))), |
| 866 base::ASCIIToUTF16("0123456789"), |
| 867 gfx::Range(1, 9), |
| 868 gfx::Range(3, 5))); |
| 869 |
| 870 // Test if the composing range is out of range. |
| 871 EXPECT_TRUE(AssertOnTextInputStateChanged( |
| 872 Vec(MakeTuple(gfx::Range(1, 9))), |
| 873 Vec(MakeTuple(100, base::ASCIIToUTF16(""))), |
| 874 base::ASCIIToUTF16("0123456789"), |
| 875 gfx::Range(1, 9), |
| 876 gfx::Range(100, 0))); |
| 877 |
| 878 // Test if the composing range is out of range. |
| 879 EXPECT_TRUE(AssertOnTextInputStateChanged( |
| 880 Vec(MakeTuple(gfx::Range(1, 9))), |
| 881 Vec(MakeTuple(-1, base::ASCIIToUTF16(""))), |
| 882 base::ASCIIToUTF16(""), |
| 883 gfx::Range(1, 9), |
| 884 gfx::Range(-1, 1))); |
| 885 } |
| 886 |
| 887 TEST(CursorAnchorInfoControllerTest, OnCompositionRangeChanged) { |
| 888 EXPECT_TRUE(AssertOnCompositionRangeChanged( |
| 889 Vec<AddCharacterBoundsParams>(), |
| 890 gfx::Range(0, 0), |
| 891 Vec<gfx::Rect>())); |
| 892 |
| 893 EXPECT_TRUE(AssertOnCompositionRangeChanged( |
| 894 Vec(MakeTuple(1, |
| 895 gfx::RectF(1.0f, 2.0f, 3.0f, 4.0f), |
| 896 kFlagHasVisibleRegion), |
| 897 MakeTuple(2, |
| 898 gfx::RectF(5.0f, 6.0f, 7.0f, 8.0f), |
| 899 kFlagHasVisibleRegion)), |
| 900 gfx::Range(1, 3), |
| 901 Vec(gfx::Rect(1, 2, 3, 4), |
| 902 gfx::Rect(5, 6, 7, 8)))); |
| 903 |
| 904 EXPECT_TRUE(AssertOnCompositionRangeChanged( |
| 905 Vec(MakeTuple(5, |
| 906 gfx::RectF(1.0f, 2.0f, 3.0f, 4.0f), |
| 907 kFlagHasVisibleRegion), |
| 908 MakeTuple(6, |
| 909 gfx::RectF(5.0f, 6.0f, 7.0f, 8.0f), |
| 910 kFlagHasVisibleRegion), |
| 911 MakeTuple(7, |
| 912 gfx::RectF(9.0f, 10.0f, 11.0f, 12.0f), |
| 913 kFlagHasVisibleRegion)), |
| 914 gfx::Range(5, 8), |
| 915 Vec(gfx::Rect(1, 2, 3, 4), |
| 916 gfx::Rect(5, 6, 7, 8), |
| 917 gfx::Rect(9, 10, 11, 12)))); |
| 918 } |
| 919 |
| 920 } // namespace |
| 921 } // namespace ui |
OLD | NEW |