| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| (...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 529 EXPECT_EQ(6U, model.GetCursorPosition()); | 529 EXPECT_EQ(6U, model.GetCursorPosition()); |
| 530 | 530 |
| 531 // Copy with non-empty selection. | 531 // Copy with non-empty selection. |
| 532 model.SelectAll(false); | 532 model.SelectAll(false); |
| 533 EXPECT_TRUE(model.Copy()); | 533 EXPECT_TRUE(model.Copy()); |
| 534 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &clipboard_text); | 534 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &clipboard_text); |
| 535 EXPECT_STR_EQ("HELLO ", clipboard_text); | 535 EXPECT_STR_EQ("HELLO ", clipboard_text); |
| 536 EXPECT_STR_EQ("HELLO ", model.text()); | 536 EXPECT_STR_EQ("HELLO ", model.text()); |
| 537 EXPECT_EQ(6U, model.GetCursorPosition()); | 537 EXPECT_EQ(6U, model.GetCursorPosition()); |
| 538 | 538 |
| 539 // Test that paste works regardless of the obscured bit. | 539 // Test that paste works regardless of the obscured bit. Please note that |
| 540 // trailing spaces and tabs in clipboard strings will be stripped. |
| 540 model.MoveCursor(gfx::LINE_BREAK, gfx::CURSOR_RIGHT, false); | 541 model.MoveCursor(gfx::LINE_BREAK, gfx::CURSOR_RIGHT, false); |
| 541 EXPECT_TRUE(model.Paste()); | 542 EXPECT_TRUE(model.Paste()); |
| 542 EXPECT_STR_EQ("HELLO HELLO ", model.text()); | 543 EXPECT_STR_EQ("HELLO HELLO", model.text()); |
| 543 EXPECT_EQ(12U, model.GetCursorPosition()); | 544 EXPECT_EQ(11U, model.GetCursorPosition()); |
| 544 model.render_text()->SetObscured(true); | 545 model.render_text()->SetObscured(true); |
| 545 EXPECT_TRUE(model.Paste()); | 546 EXPECT_TRUE(model.Paste()); |
| 546 EXPECT_STR_EQ("HELLO HELLO HELLO ", model.text()); | 547 EXPECT_STR_EQ("HELLO HELLOHELLO", model.text()); |
| 547 EXPECT_EQ(18U, model.GetCursorPosition()); | 548 EXPECT_EQ(16U, model.GetCursorPosition()); |
| 548 } | 549 } |
| 549 | 550 |
| 550 static void SelectWordTestVerifier( | 551 static void SelectWordTestVerifier( |
| 551 const TextfieldModel& model, | 552 const TextfieldModel& model, |
| 552 const base::string16 &expected_selected_string, | 553 const base::string16 &expected_selected_string, |
| 553 size_t expected_cursor_pos) { | 554 size_t expected_cursor_pos) { |
| 554 EXPECT_EQ(expected_selected_string, model.GetSelectedText()); | 555 EXPECT_EQ(expected_selected_string, model.GetSelectedText()); |
| 555 EXPECT_EQ(expected_cursor_pos, model.GetCursorPosition()); | 556 EXPECT_EQ(expected_cursor_pos, model.GetCursorPosition()); |
| 556 } | 557 } |
| 557 | 558 |
| (...skipping 914 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1472 EXPECT_STR_EQ("1234", model.text()); | 1473 EXPECT_STR_EQ("1234", model.text()); |
| 1473 EXPECT_TRUE(model.Undo()); | 1474 EXPECT_TRUE(model.Undo()); |
| 1474 EXPECT_STR_EQ("ABCDE", model.text()); | 1475 EXPECT_STR_EQ("ABCDE", model.text()); |
| 1475 EXPECT_TRUE(model.Redo()); | 1476 EXPECT_TRUE(model.Redo()); |
| 1476 EXPECT_STR_EQ("1234", model.text()); | 1477 EXPECT_STR_EQ("1234", model.text()); |
| 1477 EXPECT_FALSE(model.Redo()); | 1478 EXPECT_FALSE(model.Redo()); |
| 1478 | 1479 |
| 1479 // TODO(oshima): Test the behavior with an IME. | 1480 // TODO(oshima): Test the behavior with an IME. |
| 1480 } | 1481 } |
| 1481 | 1482 |
| 1483 // Tests that clipboard text with leading, trailing and interspersed tabs |
| 1484 // spaces etc is pasted correctly. Leading and trailing tabs should be |
| 1485 // stripped. Text separated by multiple tabs/spaces should be collapsed into |
| 1486 // one space. Text with just tabs and spaces should be pasted as one space. |
| 1487 TEST_F(TextfieldModelTest, Clipboard_WhiteSpaceStringTest) { |
| 1488 // Test 1 |
| 1489 // Clipboard text with a leading tab should be pasted with the tab stripped. |
| 1490 ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_COPY_PASTE) |
| 1491 .WriteText(base::ASCIIToUTF16("\tB")); |
| 1492 |
| 1493 TextfieldModel model(NULL); |
| 1494 model.Append(base::ASCIIToUTF16("HELLO WORLD")); |
| 1495 EXPECT_STR_EQ("HELLO WORLD", model.text()); |
| 1496 model.MoveCursor(gfx::LINE_BREAK, gfx::CURSOR_RIGHT, false); |
| 1497 EXPECT_EQ(11U, model.GetCursorPosition()); |
| 1498 |
| 1499 EXPECT_TRUE(model.Paste()); |
| 1500 EXPECT_STR_EQ("HELLO WORLDB", model.text()); |
| 1501 |
| 1502 model.SelectAll(false); |
| 1503 model.DeleteSelection(); |
| 1504 EXPECT_STR_EQ("", model.text()); |
| 1505 |
| 1506 // Test 2 |
| 1507 // Clipboard text with multiple leading tabs and spaces should be pasted with |
| 1508 // all tabs and spaces stripped. |
| 1509 ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_COPY_PASTE) |
| 1510 .WriteText(base::ASCIIToUTF16("\t\t\t B")); |
| 1511 |
| 1512 model.Append(base::ASCIIToUTF16("HELLO WORLD")); |
| 1513 EXPECT_STR_EQ("HELLO WORLD", model.text()); |
| 1514 model.MoveCursor(gfx::LINE_BREAK, gfx::CURSOR_RIGHT, false); |
| 1515 EXPECT_EQ(11U, model.GetCursorPosition()); |
| 1516 EXPECT_TRUE(model.Paste()); |
| 1517 EXPECT_STR_EQ("HELLO WORLDB", model.text()); |
| 1518 |
| 1519 model.SelectAll(false); |
| 1520 model.DeleteSelection(); |
| 1521 EXPECT_STR_EQ("", model.text()); |
| 1522 |
| 1523 // Test 3 |
| 1524 // Clipboard text with multiple tabs separating the words should be pasted |
| 1525 // with one space replacing all tabs. |
| 1526 ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_COPY_PASTE) |
| 1527 .WriteText(base::ASCIIToUTF16("FOO \t\t BAR")); |
| 1528 |
| 1529 model.Append(base::ASCIIToUTF16("HELLO WORLD")); |
| 1530 EXPECT_STR_EQ("HELLO WORLD", model.text()); |
| 1531 model.MoveCursor(gfx::LINE_BREAK, gfx::CURSOR_RIGHT, false); |
| 1532 EXPECT_EQ(11U, model.GetCursorPosition()); |
| 1533 EXPECT_TRUE(model.Paste()); |
| 1534 EXPECT_STR_EQ("HELLO WORLDFOO BAR", model.text()); |
| 1535 |
| 1536 model.SelectAll(false); |
| 1537 model.DeleteSelection(); |
| 1538 EXPECT_STR_EQ("", model.text()); |
| 1539 |
| 1540 // Test 4 |
| 1541 // Clipboard text with multiple leading tabs and multiple tabs separating |
| 1542 // the words should be pasted with the leading tabs stripped and one space |
| 1543 // replacing the intermediate tabs. |
| 1544 ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_COPY_PASTE) |
| 1545 .WriteText(base::ASCIIToUTF16("\t\tFOO \t\t BAR")); |
| 1546 |
| 1547 EXPECT_TRUE(model.Paste()); |
| 1548 EXPECT_STR_EQ("FOO BAR", model.text()); |
| 1549 |
| 1550 model.SelectAll(false); |
| 1551 model.DeleteSelection(); |
| 1552 EXPECT_STR_EQ("", model.text()); |
| 1553 |
| 1554 // Test 5 |
| 1555 // Clipboard text with multiple trailing tabs should be pasted with all |
| 1556 // trailing tabs stripped. |
| 1557 ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_COPY_PASTE) |
| 1558 .WriteText(base::ASCIIToUTF16("FOO BAR\t\t\t")); |
| 1559 EXPECT_TRUE(model.Paste()); |
| 1560 EXPECT_STR_EQ("FOO BAR", model.text()); |
| 1561 |
| 1562 model.SelectAll(false); |
| 1563 model.DeleteSelection(); |
| 1564 EXPECT_STR_EQ("", model.text()); |
| 1565 |
| 1566 // Test 6 |
| 1567 // Clipboard text with only spaces and tabs should be pasted as a single |
| 1568 // space. |
| 1569 ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_COPY_PASTE) |
| 1570 .WriteText(base::ASCIIToUTF16(" \t\t")); |
| 1571 EXPECT_TRUE(model.Paste()); |
| 1572 EXPECT_STR_EQ(" ", model.text()); |
| 1573 |
| 1574 model.SelectAll(false); |
| 1575 model.DeleteSelection(); |
| 1576 EXPECT_STR_EQ("", model.text()); |
| 1577 |
| 1578 // Test 7 |
| 1579 // Clipboard text with lots of spaces between words should have all spaces |
| 1580 // replaced with a single space. |
| 1581 ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_COPY_PASTE) |
| 1582 .WriteText(base::ASCIIToUTF16("FOO BAR")); |
| 1583 EXPECT_TRUE(model.Paste()); |
| 1584 EXPECT_STR_EQ("FOO BAR", model.text()); |
| 1585 } |
| 1586 |
| 1482 } // namespace views | 1587 } // namespace views |
| OLD | NEW |