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

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: Use sync call if there are no info. 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 rwhv_mac_->SelectionBoundsChanged(kCaretRect, kCaretRect);
259
260 // If there are no update from renderer, always returned caret position.
261 NSRect rect;
262 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
263 ui::Range(0, 0).ToNSRange(),
264 &rect));
265 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
266 ui::Range(0, 1).ToNSRange(),
267 &rect));
268 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
269 ui::Range(1, 0).ToNSRange(),
270 &rect));
271 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
272 ui::Range(1, 1).ToNSRange(),
273 &rect));
274 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
275 ui::Range(1, 2).ToNSRange(),
276 &rect));
277
278 const int kCompositionLength = 10;
279 std::vector<gfx::Rect> composition_bounds;
280 const int kCompositionStart = 3;
281 const ui::Range kCompositionRange(kCompositionStart,
282 kCompositionStart + kCompositionLength);
283 GenerateCompositionRectArray(kOrigin,
284 kBoundsUnit,
285 kCompositionLength,
286 std::vector<size_t>(),
287 &composition_bounds);
288 rwhv_mac_->ImeCompositionRangeChanged(kCompositionRange, composition_bounds);
289
290 // Out of range requests will return caret position.
291 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
292 ui::Range(0, 0).ToNSRange(),
293 &rect));
294 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
295 ui::Range(1, 1).ToNSRange(),
296 &rect));
297 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
298 ui::Range(1, 2).ToNSRange(),
299 &rect));
300 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
301 ui::Range(2, 2).ToNSRange(),
302 &rect));
303 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
304 ui::Range(13, 14).ToNSRange(),
305 &rect));
306 EXPECT_FALSE(rwhv_mac_->GetCachedFirstRectForCharacterRange(
307 ui::Range(14, 15).ToNSRange(),
308 &rect));
309
310 for (int i = 0; i < kCompositionLength; ++i) {
311 for (int j = 0; j < kCompositionLength - i; ++j) {
312 const ui::Range range(i, i + j);
313 const gfx::Rect expected_rect = GetExpectedRect(kOrigin,
314 kBoundsUnit,
315 range,
316 0);
317 const NSRange request_range = ui::Range(
318 kCompositionStart + range.start(),
319 kCompositionStart + range.end()).ToNSRange();
320 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(request_range,
321 &rect));
322 EXPECT_EQ(expected_rect, gfx::Rect(NSRectToCGRect(rect)));
323 }
324 }
325 }
326
327 TEST_F(RenderWidgetHostViewMacTest, UpdateCompositionMultilineCase) {
328 const gfx::Point kOrigin(10, 11);
329 const gfx::Size kBoundsUnit(10, 20);
330 NSRect rect;
331
332 const int kCompositionLength = 30;
333 std::vector<gfx::Rect> composition_bounds;
334 const ui::Range kCompositionRange(0, kCompositionLength);
335 // Set breaking point at 10 and 20.
336 std::vector<size_t> break_points;
337 break_points.push_back(10);
338 break_points.push_back(20);
339 GenerateCompositionRectArray(kOrigin,
340 kBoundsUnit,
341 kCompositionLength,
342 break_points,
343 &composition_bounds);
344 rwhv_mac_->ImeCompositionRangeChanged(kCompositionRange, composition_bounds);
345
346 // Range doesn't contain line breaking point.
347 ui::Range range;
348 range = ui::Range(5, 8);
349 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
350 &rect));
351 EXPECT_EQ(
352 GetExpectedRect(kOrigin, kBoundsUnit, range, 0),
353 gfx::Rect(NSRectToCGRect(rect)));
354 range = ui::Range(15, 18);
355 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
356 &rect));
357 EXPECT_EQ(
358 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(5, 8), 1),
359 gfx::Rect(NSRectToCGRect(rect)));
360 range = ui::Range(25, 28);
361 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
362 &rect));
363 EXPECT_EQ(
364 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(5, 8), 2),
365 gfx::Rect(NSRectToCGRect(rect)));
366
367 // Range contains line breaking point.
368 range = ui::Range(8, 12);
369 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
370 &rect));
371 EXPECT_EQ(
372 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(8, 10), 0),
373 gfx::Rect(NSRectToCGRect(rect)));
374 range = ui::Range(18, 22);
375 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
376 &rect));
377 EXPECT_EQ(
378 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(8, 10), 1),
379 gfx::Rect(NSRectToCGRect(rect)));
380
381 // Start point is line breaking point.
382 range = ui::Range(10, 12);
383 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
384 &rect));
385 EXPECT_EQ(
386 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(0, 2), 1),
387 gfx::Rect(NSRectToCGRect(rect)));
388 range = ui::Range(20, 22);
389 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
390 &rect));
391 EXPECT_EQ(
392 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(0, 2), 2),
393 gfx::Rect(NSRectToCGRect(rect)));
394
395 // End point is line breaking point.
396 range = ui::Range(5, 10);
397 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
398 &rect));
399 EXPECT_EQ(
400 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(5, 10), 0),
401 gfx::Rect(NSRectToCGRect(rect)));
402 range = ui::Range(15, 20);
403 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
404 &rect));
405 EXPECT_EQ(
406 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(5, 10), 1),
407 gfx::Rect(NSRectToCGRect(rect)));
408
409 // Start and end point are same line breaking point.
410 range = ui::Range(10, 10);
411 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
412 &rect));
413 EXPECT_EQ(
414 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(0, 0), 1),
415 gfx::Rect(NSRectToCGRect(rect)));
416 range = ui::Range(20, 20);
417 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
418 &rect));
419 EXPECT_EQ(
420 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(0, 0), 2),
421 gfx::Rect(NSRectToCGRect(rect)));
422
423 // Start and end point are different line breaking point.
424 range = ui::Range(10, 20);
425 EXPECT_TRUE(rwhv_mac_->GetCachedFirstRectForCharacterRange(range.ToNSRange(),
426 &rect));
427 EXPECT_EQ(
428 GetExpectedRect(kOrigin, kBoundsUnit, ui::Range(0, 10), 1),
429 gfx::Rect(NSRectToCGRect(rect)));
430 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698