| 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 "ui/views/controls/textfield/textfield.h" | 5 #include "ui/views/controls/textfield/textfield.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <set> | 10 #include <set> |
| (...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 on_after_user_action_(0), | 371 on_after_user_action_(0), |
| 372 copied_to_clipboard_(ui::CLIPBOARD_TYPE_LAST) { | 372 copied_to_clipboard_(ui::CLIPBOARD_TYPE_LAST) { |
| 373 input_method_ = new MockInputMethod(); | 373 input_method_ = new MockInputMethod(); |
| 374 ui::SetUpInputMethodForTesting(input_method_); | 374 ui::SetUpInputMethodForTesting(input_method_); |
| 375 } | 375 } |
| 376 | 376 |
| 377 // ::testing::Test: | 377 // ::testing::Test: |
| 378 void TearDown() override { | 378 void TearDown() override { |
| 379 if (widget_) | 379 if (widget_) |
| 380 widget_->Close(); | 380 widget_->Close(); |
| 381 // Clear kill buffer used for "Yank" text editing command so that no state |
| 382 // persists between tests. |
| 383 TextfieldModel::ClearKillBuffer(); |
| 381 ViewsTestBase::TearDown(); | 384 ViewsTestBase::TearDown(); |
| 382 } | 385 } |
| 383 | 386 |
| 384 ui::ClipboardType GetAndResetCopiedToClipboard() { | 387 ui::ClipboardType GetAndResetCopiedToClipboard() { |
| 385 ui::ClipboardType clipboard_type = copied_to_clipboard_; | 388 ui::ClipboardType clipboard_type = copied_to_clipboard_; |
| 386 copied_to_clipboard_ = ui::CLIPBOARD_TYPE_LAST; | 389 copied_to_clipboard_ = ui::CLIPBOARD_TYPE_LAST; |
| 387 return clipboard_type; | 390 return clipboard_type; |
| 388 } | 391 } |
| 389 | 392 |
| 390 // TextfieldController: | 393 // TextfieldController: |
| (...skipping 1440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1831 EXPECT_STR_EQ("ab", textfield_->text()); | 1834 EXPECT_STR_EQ("ab", textfield_->text()); |
| 1832 SendKeyEvent(ui::VKEY_Z, true, true); | 1835 SendKeyEvent(ui::VKEY_Z, true, true); |
| 1833 EXPECT_STR_EQ("b", textfield_->text()); | 1836 EXPECT_STR_EQ("b", textfield_->text()); |
| 1834 SendKeyEvent(ui::VKEY_Z, true, true); | 1837 SendKeyEvent(ui::VKEY_Z, true, true); |
| 1835 EXPECT_STR_EQ("", textfield_->text()); | 1838 EXPECT_STR_EQ("", textfield_->text()); |
| 1836 SendKeyEvent(ui::VKEY_Z, true, true); | 1839 SendKeyEvent(ui::VKEY_Z, true, true); |
| 1837 EXPECT_STR_EQ("", textfield_->text()); | 1840 EXPECT_STR_EQ("", textfield_->text()); |
| 1838 } | 1841 } |
| 1839 | 1842 |
| 1840 // Most platforms support Ctrl+Y as an alternative to Ctrl+Shift+Z, but on Mac | 1843 // Most platforms support Ctrl+Y as an alternative to Ctrl+Shift+Z, but on Mac |
| 1841 // that is bound to "Show full history", so is not mapped as an editing | 1844 // Ctrl+Y is bound to "Yank" and Cmd+Y is bound to "Show full history". So, on |
| 1842 // command. So, on Mac, send Cmd+Shift+Z. | 1845 // Mac, Cmd+Shift+Z is sent for the tests above and the Ctrl+Y test below is |
| 1846 // skipped. |
| 1843 #if !defined(OS_MACOSX) | 1847 #if !defined(OS_MACOSX) |
| 1844 | 1848 |
| 1845 // Test that Ctrl+Y works for Redo, as well as Ctrl+Shift+Z. | 1849 // Test that Ctrl+Y works for Redo, as well as Ctrl+Shift+Z. |
| 1846 TEST_F(TextfieldTest, RedoWithCtrlY) { | 1850 TEST_F(TextfieldTest, RedoWithCtrlY) { |
| 1847 InitTextfield(); | 1851 InitTextfield(); |
| 1848 SendKeyEvent(ui::VKEY_A); | 1852 SendKeyEvent(ui::VKEY_A); |
| 1849 EXPECT_STR_EQ("a", textfield_->text()); | 1853 EXPECT_STR_EQ("a", textfield_->text()); |
| 1850 SendKeyEvent(ui::VKEY_Z, false, true); | 1854 SendKeyEvent(ui::VKEY_Z, false, true); |
| 1851 EXPECT_STR_EQ("", textfield_->text()); | 1855 EXPECT_STR_EQ("", textfield_->text()); |
| 1852 SendKeyEvent(ui::VKEY_Y, false, true); | 1856 SendKeyEvent(ui::VKEY_Y, false, true); |
| 1853 EXPECT_STR_EQ("a", textfield_->text()); | 1857 EXPECT_STR_EQ("a", textfield_->text()); |
| 1854 SendKeyEvent(ui::VKEY_Z, false, true); | 1858 SendKeyEvent(ui::VKEY_Z, false, true); |
| 1855 EXPECT_STR_EQ("", textfield_->text()); | 1859 EXPECT_STR_EQ("", textfield_->text()); |
| 1856 SendKeyEvent(ui::VKEY_Z, true, true); | 1860 SendKeyEvent(ui::VKEY_Z, true, true); |
| 1857 EXPECT_STR_EQ("a", textfield_->text()); | 1861 EXPECT_STR_EQ("a", textfield_->text()); |
| 1858 } | 1862 } |
| 1859 | 1863 |
| 1860 #endif // !defined(OS_MACOSX) | 1864 #endif // !defined(OS_MACOSX) |
| 1861 | 1865 |
| 1866 // Non-Mac platforms don't have a key binding for Yank. Since this test is only |
| 1867 // run on Mac, it uses some Mac specific key bindings. |
| 1868 #if defined(OS_MACOSX) |
| 1869 |
| 1870 TEST_F(TextfieldTest, Yank) { |
| 1871 InitTextfields(2); |
| 1872 textfield_->SetText(ASCIIToUTF16("abcdef")); |
| 1873 textfield_->SelectRange(gfx::Range(2, 4)); |
| 1874 |
| 1875 // Press Ctrl+Y to yank. |
| 1876 SendKeyPress(ui::VKEY_Y, ui::EF_CONTROL_DOWN); |
| 1877 |
| 1878 // Initially the kill buffer should be empty. Hence yanking should delete the |
| 1879 // selected text. |
| 1880 EXPECT_STR_EQ("abef", textfield_->text()); |
| 1881 EXPECT_EQ(gfx::Range(2), textfield_->GetSelectedRange()); |
| 1882 |
| 1883 // Press Ctrl+K to delete to end of paragraph. This should place the deleted |
| 1884 // text in the kill buffer. |
| 1885 SendKeyPress(ui::VKEY_K, ui::EF_CONTROL_DOWN); |
| 1886 |
| 1887 EXPECT_STR_EQ("ab", textfield_->text()); |
| 1888 EXPECT_EQ(gfx::Range(2), textfield_->GetSelectedRange()); |
| 1889 |
| 1890 // Yank twice. |
| 1891 SendKeyPress(ui::VKEY_Y, ui::EF_CONTROL_DOWN); |
| 1892 SendKeyPress(ui::VKEY_Y, ui::EF_CONTROL_DOWN); |
| 1893 EXPECT_STR_EQ("abefef", textfield_->text()); |
| 1894 EXPECT_EQ(gfx::Range(6), textfield_->GetSelectedRange()); |
| 1895 |
| 1896 // Verify pressing backspace does not modify the kill buffer. |
| 1897 SendKeyEvent(ui::VKEY_BACK); |
| 1898 SendKeyPress(ui::VKEY_Y, ui::EF_CONTROL_DOWN); |
| 1899 EXPECT_STR_EQ("abefeef", textfield_->text()); |
| 1900 EXPECT_EQ(gfx::Range(7), textfield_->GetSelectedRange()); |
| 1901 |
| 1902 // Move focus to next textfield. |
| 1903 widget_->GetFocusManager()->AdvanceFocus(false); |
| 1904 EXPECT_EQ(2, GetFocusedView()->id()); |
| 1905 Textfield* textfield2 = static_cast<Textfield*>(GetFocusedView()); |
| 1906 EXPECT_TRUE(textfield2->text().empty()); |
| 1907 |
| 1908 // Verify yanked text persists across multiple textfields. |
| 1909 SendKeyPress(ui::VKEY_Y, ui::EF_CONTROL_DOWN); |
| 1910 EXPECT_STR_EQ("ef", textfield2->text()); |
| 1911 EXPECT_EQ(gfx::Range(2), textfield2->GetSelectedRange()); |
| 1912 } |
| 1913 |
| 1914 #endif // defined(OS_MACOSX) |
| 1915 |
| 1862 TEST_F(TextfieldTest, CutCopyPaste) { | 1916 TEST_F(TextfieldTest, CutCopyPaste) { |
| 1863 InitTextfield(); | 1917 InitTextfield(); |
| 1864 | 1918 |
| 1865 // Ensure IDS_APP_CUT cuts. | 1919 // Ensure IDS_APP_CUT cuts. |
| 1866 textfield_->SetText(ASCIIToUTF16("123")); | 1920 textfield_->SetText(ASCIIToUTF16("123")); |
| 1867 textfield_->SelectAll(false); | 1921 textfield_->SelectAll(false); |
| 1868 EXPECT_TRUE(textfield_->IsCommandIdEnabled(IDS_APP_CUT)); | 1922 EXPECT_TRUE(textfield_->IsCommandIdEnabled(IDS_APP_CUT)); |
| 1869 textfield_->ExecuteCommand(IDS_APP_CUT, 0); | 1923 textfield_->ExecuteCommand(IDS_APP_CUT, 0); |
| 1870 EXPECT_STR_EQ("123", GetClipboardText(ui::CLIPBOARD_TYPE_COPY_PASTE)); | 1924 EXPECT_STR_EQ("123", GetClipboardText(ui::CLIPBOARD_TYPE_COPY_PASTE)); |
| 1871 EXPECT_STR_EQ("", textfield_->text()); | 1925 EXPECT_STR_EQ("", textfield_->text()); |
| (...skipping 827 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2699 | 2753 |
| 2700 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); | 2754 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); |
| 2701 ui::AXViewState state_protected; | 2755 ui::AXViewState state_protected; |
| 2702 textfield_->GetAccessibleState(&state_protected); | 2756 textfield_->GetAccessibleState(&state_protected); |
| 2703 EXPECT_EQ(ui::AX_ROLE_TEXT_FIELD, state_protected.role); | 2757 EXPECT_EQ(ui::AX_ROLE_TEXT_FIELD, state_protected.role); |
| 2704 EXPECT_EQ(ASCIIToUTF16("********"), state_protected.value); | 2758 EXPECT_EQ(ASCIIToUTF16("********"), state_protected.value); |
| 2705 EXPECT_TRUE(state_protected.HasStateFlag(ui::AX_STATE_PROTECTED)); | 2759 EXPECT_TRUE(state_protected.HasStateFlag(ui::AX_STATE_PROTECTED)); |
| 2706 } | 2760 } |
| 2707 | 2761 |
| 2708 } // namespace views | 2762 } // namespace views |
| OLD | NEW |