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

Side by Side Diff: ui/gfx/render_text_unittest.cc

Issue 378723003: RenderText: Allow setting display offset explicitly (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added test Created 6 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 | Annotate | Revision Log
« ui/gfx/render_text.cc ('K') | « ui/gfx/render_text.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "ui/gfx/render_text.h" 5 #include "ui/gfx/render_text.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/format_macros.h" 9 #include "base/format_macros.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 1022 matching lines...) Expand 10 before | Expand all | Expand 10 after
1033 render_text->SelectAll(false); 1033 render_text->SelectAll(false);
1034 EXPECT_EQ(render_text->selection_model(), expected_forwards); 1034 EXPECT_EQ(render_text->selection_model(), expected_forwards);
1035 render_text->SelectAll(true); 1035 render_text->SelectAll(true);
1036 EXPECT_EQ(render_text->selection_model(), expected_reversed); 1036 EXPECT_EQ(render_text->selection_model(), expected_reversed);
1037 } 1037 }
1038 } 1038 }
1039 1039
1040 EXPECT_EQ(was_rtl, base::i18n::IsRTL()); 1040 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
1041 } 1041 }
1042 1042
1043 TEST_F(RenderTextTest, MoveCursorLeftRightWithSelection) { 1043 TEST_F(RenderTextTest, MoveCursorLeftRightWithSelection) {
1044 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); 1044 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1045 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2")); 1045 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
1046 // Left arrow on select ranging (6, 4). 1046 // Left arrow on select ranging (6, 4).
1047 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false); 1047 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1048 EXPECT_EQ(Range(6), render_text->selection()); 1048 EXPECT_EQ(Range(6), render_text->selection());
1049 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false); 1049 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1050 EXPECT_EQ(Range(4), render_text->selection()); 1050 EXPECT_EQ(Range(4), render_text->selection());
1051 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false); 1051 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1052 EXPECT_EQ(Range(5), render_text->selection()); 1052 EXPECT_EQ(Range(5), render_text->selection());
1053 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false); 1053 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after
1481 const int kEnlargement = 2; 1481 const int kEnlargement = 2;
1482 const Size font_size(render_text->GetContentWidth() + kEnlargement, 1482 const Size font_size(render_text->GetContentWidth() + kEnlargement,
1483 render_text->GetStringSize().height()); 1483 render_text->GetStringSize().height());
1484 Rect display_rect(font_size); 1484 Rect display_rect(font_size);
1485 render_text->SetDisplayRect(display_rect); 1485 render_text->SetDisplayRect(display_rect);
1486 Vector2d offset = render_text->GetLineOffset(0); 1486 Vector2d offset = render_text->GetLineOffset(0);
1487 EXPECT_EQ(kEnlargement, offset.x()); 1487 EXPECT_EQ(kEnlargement, offset.x());
1488 SetRTL(was_rtl); 1488 SetRTL(was_rtl);
1489 } 1489 }
1490 1490
1491 TEST_F(RenderTextTest, SetDisplayOffset) {
1492 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1493 render_text->SetText(ASCIIToUTF16("abcdefg"));
1494 render_text->SetFontList(FontList("Arial, 13px"));
1495
1496 const Size font_size(render_text->GetContentWidth(),
1497 render_text->font_list().GetHeight());
1498 const int kEnlargement = 10;
1499
1500 // Set display width |kEnlargement| pixels greater than content width and test
1501 // different possible situations. In this case the only possible display
1502 // offset is zero.
1503 Rect display_rect(font_size);
1504 display_rect.Inset(0, 0, -kEnlargement, 0);
1505 render_text->SetDisplayRect(display_rect);
1506
1507 struct {
1508 HorizontalAlignment alignment;
1509 int offset;
1510 } small_content_cases[] = {
1511 { ALIGN_LEFT, -kEnlargement },
1512 { ALIGN_LEFT, 0 },
1513 { ALIGN_LEFT, kEnlargement },
1514 { ALIGN_RIGHT, -kEnlargement },
1515 { ALIGN_RIGHT, 0 },
1516 { ALIGN_RIGHT, kEnlargement },
1517 { ALIGN_CENTER, -kEnlargement },
1518 { ALIGN_CENTER, 0 },
1519 { ALIGN_CENTER, kEnlargement },
1520 };
1521
1522 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(small_content_cases); i++) {
1523 render_text->SetHorizontalAlignment(small_content_cases[i].alignment);
1524 render_text->SetDisplayOffset(small_content_cases[i].offset);
1525 EXPECT_EQ(0, render_text->GetUpdatedDisplayOffset().x());
1526 }
1527
1528 // Set display width |kEnlargement| pixels less than content width and test
1529 // different possible situations.
1530 display_rect = Rect(font_size);
1531 display_rect.Inset(0, 0, kEnlargement, 0);
1532 render_text->SetDisplayRect(display_rect);
1533
1534 struct {
1535 HorizontalAlignment alignment;
1536 int offset;
1537 int expected_offset;
1538 } large_content_cases[] = {
1539 // When text is left-aligned, display offset can be in range
1540 // [-kEnlargement, 0].
1541 { ALIGN_LEFT, -2 * kEnlargement, -kEnlargement },
1542 { ALIGN_LEFT, -kEnlargement / 2, -kEnlargement / 2 },
1543 { ALIGN_LEFT, kEnlargement, 0 },
1544 // When text is right-aligned, display offset can be in range
1545 // [0, kEnlargement].
1546 { ALIGN_RIGHT, -kEnlargement, 0 },
1547 { ALIGN_RIGHT, kEnlargement / 2, kEnlargement / 2 },
1548 { ALIGN_RIGHT, 2 * kEnlargement, kEnlargement },
1549 // When text is center-aligned, display offset can be in range
1550 // [-kEnlargement / 2, kEnlargement / 2].
1551 { ALIGN_CENTER, -kEnlargement, -kEnlargement / 2 },
1552 { ALIGN_CENTER, -kEnlargement / 4, -kEnlargement / 4 },
1553 { ALIGN_CENTER, kEnlargement / 4, kEnlargement / 4 },
1554 { ALIGN_CENTER, kEnlargement, kEnlargement / 2 },
1555 };
1556
1557 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(large_content_cases); i++) {
1558 render_text->SetHorizontalAlignment(large_content_cases[i].alignment);
1559 render_text->SetDisplayOffset(large_content_cases[i].offset);
1560 EXPECT_EQ(large_content_cases[i].expected_offset,
1561 render_text->GetUpdatedDisplayOffset().x());
1562 }
1563 }
1564
1491 TEST_F(RenderTextTest, SameFontForParentheses) { 1565 TEST_F(RenderTextTest, SameFontForParentheses) {
1492 struct { 1566 struct {
1493 const base::char16 left_char; 1567 const base::char16 left_char;
1494 const base::char16 right_char; 1568 const base::char16 right_char;
1495 } punctuation_pairs[] = { 1569 } punctuation_pairs[] = {
1496 { '(', ')' }, 1570 { '(', ')' },
1497 { '{', '}' }, 1571 { '{', '}' },
1498 { '<', '>' }, 1572 { '<', '>' },
1499 }; 1573 };
1500 struct { 1574 struct {
(...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after
2034 for (size_t i = 0; i < arraysize(kTestStrings); ++i) { 2108 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2035 render_text->SetText(WideToUTF16(kTestStrings[i])); 2109 render_text->SetText(WideToUTF16(kTestStrings[i]));
2036 render_text->EnsureLayout(); 2110 render_text->EnsureLayout();
2037 2111
2038 for (size_t j = 0; j < render_text->text().length(); ++j) 2112 for (size_t j = 0; j < render_text->text().length(); ++j)
2039 EXPECT_FALSE(render_text->GetGlyphBounds(j).is_empty()); 2113 EXPECT_FALSE(render_text->GetGlyphBounds(j).is_empty());
2040 } 2114 }
2041 } 2115 }
2042 2116
2043 } // namespace gfx 2117 } // namespace gfx
OLDNEW
« ui/gfx/render_text.cc ('K') | « ui/gfx/render_text.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698