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

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

Powered by Google App Engine
This is Rietveld 408576698