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

Side by Side Diff: content/browser/renderer_host/render_widget_host_view_mac_unittest.mm

Issue 10656019: Use cached composition bounds for firstRectForCharacterRange (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fix style nits Created 8 years, 5 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "content/browser/renderer_host/render_widget_host_view_mac.h" 5 #include "content/browser/renderer_host/render_widget_host_view_mac.h"
6 6
7 #include "base/mac/scoped_nsautorelease_pool.h" 7 #include "base/mac/scoped_nsautorelease_pool.h"
8 #include "base/utf_string_conversions.h"
8 #include "content/browser/browser_thread_impl.h" 9 #include "content/browser/browser_thread_impl.h"
9 #include "content/browser/renderer_host/test_render_view_host.h" 10 #include "content/browser/renderer_host/test_render_view_host.h"
10 #include "content/common/gpu/gpu_messages.h" 11 #include "content/common/gpu/gpu_messages.h"
11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/base/test/cocoa_test_event_utils.h" 13 #include "ui/base/test/cocoa_test_event_utils.h"
13 #import "ui/base/test/ui_cocoa_test_helper.h" 14 #import "ui/base/test/ui_cocoa_test_helper.h"
14 #include "webkit/plugins/npapi/webplugin.h" 15 #include "webkit/plugins/npapi/webplugin.h"
15 16
16 using content::BrowserThread; 17 using content::BrowserThread;
17 using content::BrowserThreadImpl; 18 using content::BrowserThreadImpl;
18 using content::RenderViewHostImplTestHarness; 19 using content::RenderViewHostImplTestHarness;
19 20
21 namespace {
22
23 // Generates the |length| of composition rectangle vector and save them to
24 // |output|. It starts from |origin| and each rectangle contains |unit_size|.
25 void GenerateCompositionRectArray(const gfx::Point& origin,
26 const gfx::Size& unit_size,
27 size_t length,
28 const std::vector<size_t>& break_points,
29 std::vector<gfx::Rect>* output) {
30 DCHECK(output);
31 output->clear();
32
33 std::queue<int> break_point_queue;
34 for (size_t i = 0; i < break_points.size(); ++i)
35 break_point_queue.push(break_points[i]);
36 break_point_queue.push(length);
37 size_t next_break_point = break_point_queue.front();
38 break_point_queue.pop();
39
40 gfx::Rect current_rect(origin, unit_size);
41 for (size_t i = 0; i < length; ++i) {
42 if (i == next_break_point) {
43 current_rect.set_x(origin.x());
44 current_rect.set_y(current_rect.y() + current_rect.height());
45 next_break_point = break_point_queue.front();
46 break_point_queue.pop();
47 }
48 output->push_back(current_rect);
49 current_rect.set_x(current_rect.right());
50 }
51 }
52
53 gfx::Rect GetExpectedRect(const gfx::Point& origin,
54 const gfx::Size& size,
55 const ui::Range& range,
56 int line_no) {
57 return gfx::Rect(
58 origin.x() + range.start() * size.width(),
59 origin.y() + line_no * size.height(),
60 range.length() * size.width(),
61 size.height());
62 }
63
64 } // namespace
65
20 class RenderWidgetHostViewMacTest : public RenderViewHostImplTestHarness { 66 class RenderWidgetHostViewMacTest : public RenderViewHostImplTestHarness {
21 public: 67 public:
22 RenderWidgetHostViewMacTest() : old_rwhv_(NULL), rwhv_mac_(NULL) {} 68 RenderWidgetHostViewMacTest() : old_rwhv_(NULL), rwhv_mac_(NULL) {}
23 69
24 virtual void SetUp() { 70 virtual void SetUp() {
25 RenderViewHostImplTestHarness::SetUp(); 71 RenderViewHostImplTestHarness::SetUp();
26 72
27 // TestRenderViewHost's destruction assumes that its view is a 73 // TestRenderViewHost's destruction assumes that its view is a
28 // TestRenderWidgetHostView, so store its view and reset it back to the 74 // TestRenderWidgetHostView, so store its view and reset it back to the
29 // stored view in |TearDown()|. 75 // stored view in |TearDown()|.
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 EXPECT_EQ(rwhv_cocoa_.get(), [window firstResponder]); 245 EXPECT_EQ(rwhv_cocoa_.get(), [window firstResponder]);
200 246
201 // Clean up. 247 // Clean up.
202 rwhv_mac_->DestroyFakePluginWindowHandle(accelerated_handle); 248 rwhv_mac_->DestroyFakePluginWindowHandle(accelerated_handle);
203 } 249 }
204 250
205 TEST_F(RenderWidgetHostViewMacTest, Fullscreen) { 251 TEST_F(RenderWidgetHostViewMacTest, Fullscreen) {
206 rwhv_mac_->InitAsFullscreen(NULL); 252 rwhv_mac_->InitAsFullscreen(NULL);
207 EXPECT_TRUE(rwhv_mac_->pepper_fullscreen_window()); 253 EXPECT_TRUE(rwhv_mac_->pepper_fullscreen_window());
208 } 254 }
255
256 TEST_F(RenderWidgetHostViewMacTest, GetFirstRectForCharaacterRangeCaretCase) {
257 const string16 kDummyString = UTF8ToUTF16("hogehoge");
258 const size_t kDummyOffset = 0;
259
260 gfx::Rect caret_rect(10, 11, 0, 10);
261 ui::Range caret_range(0, 0);
262
263 NSRect rect;
264 NSRange actual_range;
265 rwhv_mac_->SelectionChanged(kDummyString, kDummyOffset, caret_range);
266 rwhv_mac_->SelectionBoundsChanged(caret_rect, caret_rect);
267 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
268 caret_range.ToNSRange(),
269 &rect,
270 &actual_range));
271 EXPECT_EQ(caret_rect, gfx::Rect(NSRectToCGRect(rect)));
272 EXPECT_EQ(caret_range, ui::Range(actual_range));
273
274 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
275 ui::Range(0, 1).ToNSRange(),
276 &rect,
277 &actual_range));
278 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
279 ui::Range(1, 1).ToNSRange(),
280 &rect,
281 &actual_range));
282 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
283 ui::Range(2, 3).ToNSRange(),
284 &rect,
285 &actual_range));
286
287 // Caret moved.
288 caret_rect = gfx::Rect(20, 11, 0, 10);
289 caret_range = ui::Range(1, 1);
290 rwhv_mac_->SelectionChanged(kDummyString, kDummyOffset, caret_range);
291 rwhv_mac_->SelectionBoundsChanged(caret_rect, caret_rect);
292 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
293 caret_range.ToNSRange(),
294 &rect,
295 &actual_range));
296 EXPECT_EQ(caret_rect, gfx::Rect(NSRectToCGRect(rect)));
297 EXPECT_EQ(caret_range, ui::Range(actual_range));
298
299 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
300 ui::Range(0, 0).ToNSRange(),
301 &rect,
302 &actual_range));
303 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
304 ui::Range(1, 2).ToNSRange(),
305 &rect,
306 &actual_range));
307 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
308 ui::Range(2, 3).ToNSRange(),
309 &rect,
310 &actual_range));
311
312 // No caret.
313 caret_range = ui::Range(1, 2);
314 rwhv_mac_->SelectionChanged(kDummyString, kDummyOffset, caret_range);
315 rwhv_mac_->SelectionBoundsChanged(caret_rect, gfx::Rect(30, 11, 0, 10));
316 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
317 ui::Range(0, 0).ToNSRange(),
318 &rect,
319 &actual_range));
320 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
321 ui::Range(0, 1).ToNSRange(),
322 &rect,
323 &actual_range));
324 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
325 ui::Range(1, 1).ToNSRange(),
326 &rect,
327 &actual_range));
328 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
329 ui::Range(1, 2).ToNSRange(),
330 &rect,
331 &actual_range));
332 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
333 ui::Range(2, 2).ToNSRange(),
334 &rect,
335 &actual_range));
336 }
337
338 TEST_F(RenderWidgetHostViewMacTest, UpdateCompositionSinglelineCase) {
339 const gfx::Point kOrigin(10, 11);
340 const gfx::Size kBoundsUnit(10, 20);
341
342 // If there are no update from renderer, always returned caret position.
343 NSRect rect;
344 NSRange actual_range;
345 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
346 ui::Range(0, 0).ToNSRange(),
347 &rect,
348 &actual_range));
349 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
350 ui::Range(0, 1).ToNSRange(),
351 &rect,
352 &actual_range));
353 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
354 ui::Range(1, 0).ToNSRange(),
355 &rect,
356 &actual_range));
357 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
358 ui::Range(1, 1).ToNSRange(),
359 &rect,
360 &actual_range));
361 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
362 ui::Range(1, 2).ToNSRange(),
363 &rect,
364 &actual_range));
365
366 const int kCompositionLength = 10;
367 std::vector<gfx::Rect> composition_bounds;
368 const int kCompositionStart = 3;
369 const ui::Range kCompositionRange(kCompositionStart,
370 kCompositionStart + kCompositionLength);
371 GenerateCompositionRectArray(kOrigin,
372 kBoundsUnit,
373 kCompositionLength,
374 std::vector<size_t>(),
375 &composition_bounds);
376 rwhv_mac_->ImeCompositionRangeChanged(kCompositionRange, composition_bounds);
377
378 // Out of range requests will return caret position.
379 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
380 ui::Range(0, 0).ToNSRange(),
381 &rect,
382 &actual_range));
383 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
384 ui::Range(1, 1).ToNSRange(),
385 &rect,
386 &actual_range));
387 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
388 ui::Range(1, 2).ToNSRange(),
389 &rect,
390 &actual_range));
391 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
392 ui::Range(2, 2).ToNSRange(),
393 &rect,
394 &actual_range));
395 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
396 ui::Range(13, 14).ToNSRange(),
397 &rect,
398 &actual_range));
399 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
400 ui::Range(14, 15).ToNSRange(),
401 &rect,
402 &actual_range));
403
404 for (int i = 0; i <= kCompositionLength; ++i) {
405 for (int j = 0; j <= kCompositionLength - i; ++j) {
406 const ui::Range range(i, i + j);
407 const gfx::Rect expected_rect = GetExpectedRect(kOrigin,
408 kBoundsUnit,
409 range,
410 0);
411 const NSRange request_range = ui::Range(
412 kCompositionStart + range.start(),
413 kCompositionStart + range.end()).ToNSRange();
414 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
415 request_range,
416 &rect,
417 &actual_range));
418 EXPECT_EQ(ui::Range(request_range), ui::Range(actual_range));
419 EXPECT_EQ(expected_rect, gfx::Rect(NSRectToCGRect(rect)));
420 }
421 }
422 }
423
424 TEST_F(RenderWidgetHostViewMacTest, UpdateCompositionMultilineCase) {
425 const gfx::Point kOrigin(10, 11);
426 const gfx::Size kBoundsUnit(10, 20);
427 NSRect rect;
428
429 const int kCompositionLength = 30;
430 std::vector<gfx::Rect> composition_bounds;
431 const ui::Range kCompositionRange(0, kCompositionLength);
432 // Set breaking point at 10 and 20.
433 std::vector<size_t> break_points;
434 break_points.push_back(10);
435 break_points.push_back(20);
436 GenerateCompositionRectArray(kOrigin,
437 kBoundsUnit,
438 kCompositionLength,
439 break_points,
440 &composition_bounds);
441 rwhv_mac_->ImeCompositionRangeChanged(kCompositionRange, composition_bounds);
442
443 // Range doesn't contain line breaking point.
444 ui::Range range;
445 range = ui::Range(5, 8);
446 NSRange actual_range;
447 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
448 &rect,
449 &actual_range));
450 EXPECT_EQ(range, ui::Range(actual_range));
451 EXPECT_EQ(
452 GetExpectedRect(kOrigin, kBoundsUnit, range, 0),
453 gfx::Rect(NSRectToCGRect(rect)));
454 range = ui::Range(15, 18);
455 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
456 &rect,
457 &actual_range));
458 EXPECT_EQ(range, ui::Range(actual_range));
459 EXPECT_EQ(
460 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(5, 8), 1),
461 gfx::Rect(NSRectToCGRect(rect)));
462 range = ui::Range(25, 28);
463 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
464 &rect,
465 &actual_range));
466 EXPECT_EQ(range, ui::Range(actual_range));
467 EXPECT_EQ(
468 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(5, 8), 2),
469 gfx::Rect(NSRectToCGRect(rect)));
470
471 // Range contains line breaking point.
472 range = ui::Range(8, 12);
473 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
474 &rect,
475 &actual_range));
476 EXPECT_EQ(ui::Range(8, 10), ui::Range(actual_range));
477 EXPECT_EQ(
478 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(8, 10), 0),
479 gfx::Rect(NSRectToCGRect(rect)));
480 range = ui::Range(18, 22);
481 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
482 &rect,
483 &actual_range));
484 EXPECT_EQ(ui::Range(18, 20), ui::Range(actual_range));
485 EXPECT_EQ(
486 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(8, 10), 1),
487 gfx::Rect(NSRectToCGRect(rect)));
488
489 // Start point is line breaking point.
490 range = ui::Range(10, 12);
491 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
492 &rect,
493 &actual_range));
494 EXPECT_EQ(ui::Range(10, 12), ui::Range(actual_range));
495 EXPECT_EQ(
496 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(0, 2), 1),
497 gfx::Rect(NSRectToCGRect(rect)));
498 range = ui::Range(20, 22);
499 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
500 &rect,
501 &actual_range));
502 EXPECT_EQ(ui::Range(20, 22), ui::Range(actual_range));
503 EXPECT_EQ(
504 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(0, 2), 2),
505 gfx::Rect(NSRectToCGRect(rect)));
506
507 // End point is line breaking point.
508 range = ui::Range(5, 10);
509 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
510 &rect,
511 &actual_range));
512 EXPECT_EQ(ui::Range(5, 10), ui::Range(actual_range));
513 EXPECT_EQ(
514 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(5, 10), 0),
515 gfx::Rect(NSRectToCGRect(rect)));
516 range = ui::Range(15, 20);
517 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
518 &rect,
519 &actual_range));
520 EXPECT_EQ(ui::Range(15, 20), ui::Range(actual_range));
521 EXPECT_EQ(
522 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(5, 10), 1),
523 gfx::Rect(NSRectToCGRect(rect)));
524
525 // Start and end point are same line breaking point.
526 range = ui::Range(10, 10);
527 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
528 &rect,
529 &actual_range));
530 EXPECT_EQ(ui::Range(10, 10), ui::Range(actual_range));
531 EXPECT_EQ(
532 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(0, 0), 1),
533 gfx::Rect(NSRectToCGRect(rect)));
534 range = ui::Range(20, 20);
535 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
536 &rect,
537 &actual_range));
538 EXPECT_EQ(ui::Range(20, 20), ui::Range(actual_range));
539 EXPECT_EQ(
540 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(0, 0), 2),
541 gfx::Rect(NSRectToCGRect(rect)));
542
543 // Start and end point are different line breaking point.
544 range = ui::Range(10, 20);
545 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
546 &rect,
547 &actual_range));
548 EXPECT_EQ(ui::Range(10, 20), ui::Range(actual_range));
549 EXPECT_EQ(
550 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(0, 10), 1),
551 gfx::Rect(NSRectToCGRect(rect)));
552 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698