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

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: Address comments 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 "content/browser/browser_thread_impl.h" 8 #include "content/browser/browser_thread_impl.h"
9 #include "content/browser/renderer_host/test_render_view_host.h" 9 #include "content/browser/renderer_host/test_render_view_host.h"
10 #include "content/common/gpu/gpu_messages.h" 10 #include "content/common/gpu/gpu_messages.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/base/test/cocoa_test_event_utils.h" 12 #include "ui/base/test/cocoa_test_event_utils.h"
13 #import "ui/base/test/ui_cocoa_test_helper.h" 13 #import "ui/base/test/ui_cocoa_test_helper.h"
14 #include "webkit/plugins/npapi/webplugin.h" 14 #include "webkit/plugins/npapi/webplugin.h"
15 15
16 using content::BrowserThread; 16 using content::BrowserThread;
17 using content::BrowserThreadImpl; 17 using content::BrowserThreadImpl;
18 using content::RenderViewHostImplTestHarness; 18 using content::RenderViewHostImplTestHarness;
19 19
20 namespace {
21
22 // Generates the |length| of composition rectangle vector and save them to
23 // |output|. It starts from |origin| and each rectangle contains |unit_size|.
24 void GenerateCompositionRectArray(const gfx::Point& origin,
25 const gfx::Size& unit_size,
26 size_t length,
27 const std::vector<size_t>& break_points,
28 std::vector<gfx::Rect>* output) {
29 DCHECK(output);
30 output->clear();
31
32 std::queue<int> break_point_queue;
33 for (size_t i = 0; i < break_points.size(); ++i)
34 break_point_queue.push(break_points[i]);
35 break_point_queue.push(length);
36 size_t next_break_point = break_point_queue.front();
37 break_point_queue.pop();
38
39 gfx::Rect current_rect(origin, unit_size);
40 for (size_t i = 0; i < length; ++i) {
41 if (i == next_break_point) {
42 current_rect.set_x(origin.x());
43 current_rect.set_y(current_rect.y() + current_rect.height());
44 next_break_point = break_point_queue.front();
45 break_point_queue.pop();
46 }
47 output->push_back(current_rect);
48 current_rect.set_x(current_rect.right());
49 }
50 }
51
52 gfx::Rect GetExpectedRect(const gfx::Point& origin,
53 const gfx::Size& size,
54 const ui::Range& range,
55 int line_no) {
56 return gfx::Rect(
57 origin.x() + range.start() * size.width(),
58 origin.y() + line_no * size.height(),
59 range.length() * size.width(),
60 size.height());
61 }
62
63 } // namespace
64
20 class RenderWidgetHostViewMacTest : public RenderViewHostImplTestHarness { 65 class RenderWidgetHostViewMacTest : public RenderViewHostImplTestHarness {
21 public: 66 public:
22 RenderWidgetHostViewMacTest() : old_rwhv_(NULL), rwhv_mac_(NULL) {} 67 RenderWidgetHostViewMacTest() : old_rwhv_(NULL), rwhv_mac_(NULL) {}
23 68
24 virtual void SetUp() { 69 virtual void SetUp() {
25 RenderViewHostImplTestHarness::SetUp(); 70 RenderViewHostImplTestHarness::SetUp();
26 71
27 // TestRenderViewHost's destruction assumes that its view is a 72 // TestRenderViewHost's destruction assumes that its view is a
28 // TestRenderWidgetHostView, so store its view and reset it back to the 73 // TestRenderWidgetHostView, so store its view and reset it back to the
29 // stored view in |TearDown()|. 74 // stored view in |TearDown()|.
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 EXPECT_EQ(rwhv_cocoa_.get(), [window firstResponder]); 244 EXPECT_EQ(rwhv_cocoa_.get(), [window firstResponder]);
200 245
201 // Clean up. 246 // Clean up.
202 rwhv_mac_->DestroyFakePluginWindowHandle(accelerated_handle); 247 rwhv_mac_->DestroyFakePluginWindowHandle(accelerated_handle);
203 } 248 }
204 249
205 TEST_F(RenderWidgetHostViewMacTest, Fullscreen) { 250 TEST_F(RenderWidgetHostViewMacTest, Fullscreen) {
206 rwhv_mac_->InitAsFullscreen(NULL); 251 rwhv_mac_->InitAsFullscreen(NULL);
207 EXPECT_TRUE(rwhv_mac_->pepper_fullscreen_window()); 252 EXPECT_TRUE(rwhv_mac_->pepper_fullscreen_window());
208 } 253 }
254
255 TEST_F(RenderWidgetHostViewMacTest, UpdateCompositionNormalCase) {
256 const gfx::Point kOrigin(10, 11);
257 const gfx::Size kBoundsUnit(10, 20);
258
259 // If there are no update from renderer, always returned caret position.
260 NSRect rect;
261 NSRange actual_range;
262 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
263 ui::Range(0, 0).ToNSRange(),
264 &rect,
265 &actual_range));
266 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
267 ui::Range(0, 1).ToNSRange(),
268 &rect,
269 &actual_range));
270 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
271 ui::Range(1, 0).ToNSRange(),
272 &rect,
273 &actual_range));
274 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
275 ui::Range(1, 1).ToNSRange(),
276 &rect,
277 &actual_range));
278 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
279 ui::Range(1, 2).ToNSRange(),
280 &rect,
281 &actual_range));
282
283 const int kCompositionLength = 10;
284 std::vector<gfx::Rect> composition_bounds;
285 const int kCompositionStart = 3;
286 const ui::Range kCompositionRange(kCompositionStart,
287 kCompositionStart + kCompositionLength);
288 GenerateCompositionRectArray(kOrigin,
289 kBoundsUnit,
290 kCompositionLength,
291 std::vector<size_t>(),
292 &composition_bounds);
293 rwhv_mac_->ImeCompositionRangeChanged(kCompositionRange, composition_bounds);
294
295 // Out of range requests will return caret position.
296 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
297 ui::Range(0, 0).ToNSRange(),
298 &rect,
299 &actual_range));
300 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
301 ui::Range(1, 1).ToNSRange(),
302 &rect,
303 &actual_range));
304 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
305 ui::Range(1, 2).ToNSRange(),
306 &rect,
307 &actual_range));
308 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
309 ui::Range(2, 2).ToNSRange(),
310 &rect,
311 &actual_range));
312 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
313 ui::Range(13, 14).ToNSRange(),
314 &rect,
315 &actual_range));
316 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
317 ui::Range(14, 15).ToNSRange(),
318 &rect,
319 &actual_range));
320
321 for (int i = 0; i <= kCompositionLength; ++i) {
322 for (int j = 0; j <= kCompositionLength - i; ++j) {
323 const ui::Range range(i, i + j);
324 const gfx::Rect expected_rect = GetExpectedRect(kOrigin,
325 kBoundsUnit,
326 range,
327 0);
328 const NSRange request_range = ui::Range(
329 kCompositionStart + range.start(),
330 kCompositionStart + range.end()).ToNSRange();
331 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
332 request_range,
333 &rect,
334 &actual_range));
335 EXPECT_EQ(ui::Range(request_range), ui::Range(actual_range));
336 EXPECT_EQ(expected_rect, gfx::Rect(NSRectToCGRect(rect)));
337 }
338 }
339 }
340
341 TEST_F(RenderWidgetHostViewMacTest, UpdateCompositionMultilineCase) {
342 const gfx::Point kOrigin(10, 11);
343 const gfx::Size kBoundsUnit(10, 20);
344 NSRect rect;
345
346 const int kCompositionLength = 30;
347 std::vector<gfx::Rect> composition_bounds;
348 const ui::Range kCompositionRange(0, kCompositionLength);
349 // Set breaking point at 10 and 20.
350 std::vector<size_t> break_points;
351 break_points.push_back(10);
352 break_points.push_back(20);
353 GenerateCompositionRectArray(kOrigin,
354 kBoundsUnit,
355 kCompositionLength,
356 break_points,
357 &composition_bounds);
358 rwhv_mac_->ImeCompositionRangeChanged(kCompositionRange, composition_bounds);
359
360 // Range doesn't contain line breaking point.
361 ui::Range range;
362 range = ui::Range(5, 8);
363 NSRange actual_range;
364 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
365 &rect,
366 &actual_range));
367 EXPECT_EQ(range, ui::Range(actual_range));
368 EXPECT_EQ(
369 GetExpectedRect(kOrigin, kBoundsUnit, range, 0),
370 gfx::Rect(NSRectToCGRect(rect)));
371 range = ui::Range(15, 18);
372 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
373 &rect,
374 &actual_range));
375 EXPECT_EQ(range, ui::Range(actual_range));
376 EXPECT_EQ(
377 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(5, 8), 1),
378 gfx::Rect(NSRectToCGRect(rect)));
379 range = ui::Range(25, 28);
380 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
381 &rect,
382 &actual_range));
383 EXPECT_EQ(range, ui::Range(actual_range));
384 EXPECT_EQ(
385 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(5, 8), 2),
386 gfx::Rect(NSRectToCGRect(rect)));
387
388 // Range contains line breaking point.
389 range = ui::Range(8, 12);
390 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
391 &rect,
392 &actual_range));
393 EXPECT_EQ(ui::Range(8, 10), ui::Range(actual_range));
394 EXPECT_EQ(
395 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(8, 10), 0),
396 gfx::Rect(NSRectToCGRect(rect)));
397 range = ui::Range(18, 22);
398 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
399 &rect,
400 &actual_range));
401 EXPECT_EQ(ui::Range(18, 20), ui::Range(actual_range));
402 EXPECT_EQ(
403 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(8, 10), 1),
404 gfx::Rect(NSRectToCGRect(rect)));
405
406 // Start point is line breaking point.
407 range = ui::Range(10, 12);
408 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
409 &rect,
410 &actual_range));
411 EXPECT_EQ(ui::Range(10, 12), ui::Range(actual_range));
412 EXPECT_EQ(
413 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(0, 2), 1),
414 gfx::Rect(NSRectToCGRect(rect)));
415 range = ui::Range(20, 22);
416 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
417 &rect,
418 &actual_range));
419 EXPECT_EQ(ui::Range(20, 22), ui::Range(actual_range));
420 EXPECT_EQ(
421 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(0, 2), 2),
422 gfx::Rect(NSRectToCGRect(rect)));
423
424 // End point is line breaking point.
425 range = ui::Range(5, 10);
426 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
427 &rect,
428 &actual_range));
429 EXPECT_EQ(ui::Range(5, 10), ui::Range(actual_range));
430 EXPECT_EQ(
431 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(5, 10), 0),
432 gfx::Rect(NSRectToCGRect(rect)));
433 range = ui::Range(15, 20);
434 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
435 &rect,
436 &actual_range));
437 EXPECT_EQ(ui::Range(15, 20), ui::Range(actual_range));
438 EXPECT_EQ(
439 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(5, 10), 1),
440 gfx::Rect(NSRectToCGRect(rect)));
441
442 // Start and end point are same line breaking point.
443 range = ui::Range(10, 10);
444 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
445 &rect,
446 &actual_range));
447 EXPECT_EQ(ui::Range(10, 10), ui::Range(actual_range));
448 EXPECT_EQ(
449 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(0, 0), 1),
450 gfx::Rect(NSRectToCGRect(rect)));
451 range = ui::Range(20, 20);
452 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
453 &rect,
454 &actual_range));
455 EXPECT_EQ(ui::Range(20, 20), ui::Range(actual_range));
456 EXPECT_EQ(
457 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(0, 0), 2),
458 gfx::Rect(NSRectToCGRect(rect)));
459
460 // Start and end point are different line breaking point.
461 range = ui::Range(10, 20);
462 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
463 &rect,
464 &actual_range));
465 EXPECT_EQ(ui::Range(10, 20), ui::Range(actual_range));
466 EXPECT_EQ(
467 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(0, 10), 1),
468 gfx::Rect(NSRectToCGRect(rect)));
469 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698