Chromium Code Reviews| 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 "core/editing/InputMethodController.h" | 5 #include "core/editing/InputMethodController.h" |
| 6 | 6 |
| 7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
| 8 #include "core/dom/Element.h" | 8 #include "core/dom/Element.h" |
| 9 #include "core/dom/Range.h" | 9 #include "core/dom/Range.h" |
| 10 #include "core/editing/Editor.h" | 10 #include "core/editing/Editor.h" |
| (...skipping 793 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 804 EXPECT_EQ(3u, controller().getSelectionOffsets().end()); | 804 EXPECT_EQ(3u, controller().getSelectionOffsets().end()); |
| 805 } | 805 } |
| 806 | 806 |
| 807 TEST_F(InputMethodControllerTest, CompositionInputEventIsComposing) { | 807 TEST_F(InputMethodControllerTest, CompositionInputEventIsComposing) { |
| 808 document().settings()->setScriptEnabled(true); | 808 document().settings()->setScriptEnabled(true); |
| 809 Element* editable = insertHTMLElement( | 809 Element* editable = insertHTMLElement( |
| 810 "<div id='sample' contentEditable='true'></div>", "sample"); | 810 "<div id='sample' contentEditable='true'></div>", "sample"); |
| 811 Element* script = document().createElement("script", ASSERT_NO_EXCEPTION); | 811 Element* script = document().createElement("script", ASSERT_NO_EXCEPTION); |
| 812 script->setInnerHTML( | 812 script->setInnerHTML( |
| 813 "document.getElementById('sample').addEventListener('beforeinput', " | 813 "document.getElementById('sample').addEventListener('beforeinput', " |
| 814 "function(event) {" | 814 "event => {" |
| 815 " document.title = `beforeinput.isComposing:${event.isComposing};`;" | 815 " document.title = `beforeinput.isComposing:${event.isComposing};`;" |
| 816 "});" | 816 "});" |
| 817 "document.getElementById('sample').addEventListener('input', " | 817 "document.getElementById('sample').addEventListener('input', " |
| 818 "function(event) {" | 818 "event => {" |
| 819 " document.title += `input.isComposing:${event.isComposing};`;" | 819 " document.title += `input.isComposing:${event.isComposing};`;" |
| 820 "});", | 820 "});", |
| 821 ASSERT_NO_EXCEPTION); | 821 ASSERT_NO_EXCEPTION); |
| 822 document().body()->appendChild(script, ASSERT_NO_EXCEPTION); | 822 document().body()->appendChild(script, ASSERT_NO_EXCEPTION); |
| 823 document().view()->updateAllLifecyclePhases(); | 823 document().view()->updateAllLifecyclePhases(); |
| 824 | 824 |
| 825 // Simulate composition in the |contentEditable|. | 825 // Simulate composition in the |contentEditable|. |
| 826 Vector<CompositionUnderline> underlines; | 826 Vector<CompositionUnderline> underlines; |
| 827 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); | 827 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); |
| 828 editable->focus(); | 828 editable->focus(); |
| 829 | 829 |
| 830 document().setTitle(emptyString()); | 830 document().setTitle(emptyString()); |
| 831 controller().setComposition("foo", underlines, 0, 3); | 831 controller().setComposition("foo", underlines, 0, 3); |
| 832 EXPECT_STREQ("beforeinput.isComposing:true;input.isComposing:true;", | 832 EXPECT_STREQ("beforeinput.isComposing:true;input.isComposing:true;", |
| 833 document().title().utf8().data()); | 833 document().title().utf8().data()); |
| 834 | 834 |
| 835 document().setTitle(emptyString()); | 835 document().setTitle(emptyString()); |
| 836 controller().finishComposingText(InputMethodController::KeepSelection); | 836 controller().finishComposingText(InputMethodController::KeepSelection); |
| 837 // Last pair of InputEvent should also be inside composition scope. | 837 // Last pair of InputEvent should also be inside composition scope. |
| 838 EXPECT_STREQ("beforeinput.isComposing:true;input.isComposing:true;", | 838 EXPECT_STREQ("beforeinput.isComposing:true;input.isComposing:true;", |
| 839 document().title().utf8().data()); | 839 document().title().utf8().data()); |
| 840 } | 840 } |
| 841 | 841 |
| 842 TEST_F(InputMethodControllerTest, CompositionInputEventData) { | 842 TEST_F(InputMethodControllerTest, CompositionInputEventData) { |
|
yosin_UTC9
2016/11/16 02:02:10
This test case is too long and it is hard to use f
yabinh
2016/11/16 19:52:15
Done.
| |
| 843 document().settings()->setScriptEnabled(true); | 843 document().settings()->setScriptEnabled(true); |
| 844 Element* editable = insertHTMLElement( | 844 Element* editable = insertHTMLElement( |
| 845 "<div id='sample' contentEditable='true'></div>", "sample"); | 845 "<div id='sample' contentEditable='true'></div>", "sample"); |
| 846 Element* script = document().createElement("script", ASSERT_NO_EXCEPTION); | 846 Element* script = document().createElement("script", ASSERT_NO_EXCEPTION); |
| 847 script->setInnerHTML( | 847 script->setInnerHTML( |
| 848 "document.getElementById('sample').addEventListener('beforeinput', " | 848 "document.getElementById('sample').addEventListener('beforeinput', " |
| 849 "function(event) {" | 849 "event => {" |
| 850 " document.title = `beforeinput.data:${event.data};`;" | 850 " document.title = `beforeinput.data:${event.data};`;" |
| 851 "});" | 851 "});" |
| 852 "document.getElementById('sample').addEventListener('input', " | 852 "document.getElementById('sample').addEventListener('input', " |
| 853 "function(event) {" | 853 "event => {" |
| 854 " document.title += `input.data:${event.data};`;" | 854 " document.title += `input.data:${event.data};`;" |
| 855 "});" | |
| 856 "document.getElementById('sample').addEventListener('compositionend', " | |
| 857 "event => {" | |
| 858 " document.title += `compositionend.data:${event.data};`;" | |
| 859 // Reset the selection to [3,3] on receiving 'compositonend' event. | |
| 860 " const node = document.getElementById('sample').firstChild;" | |
| 861 " getSelection().collapse(node, 3);" | |
|
yosin_UTC9
2016/11/16 02:02:10
Could you add two more tests which set selection
yabinh
2016/11/16 19:52:15
Done.
| |
| 855 "});", | 862 "});", |
| 856 ASSERT_NO_EXCEPTION); | 863 ASSERT_NO_EXCEPTION); |
| 857 document().body()->appendChild(script, ASSERT_NO_EXCEPTION); | 864 document().body()->appendChild(script, ASSERT_NO_EXCEPTION); |
| 858 document().view()->updateAllLifecyclePhases(); | 865 document().view()->updateAllLifecyclePhases(); |
| 859 | 866 |
| 860 // Simulate composition in the |contentEditable|. | 867 // Simulate composition in the |contentEditable|. |
| 861 Vector<CompositionUnderline> underlines; | 868 Vector<CompositionUnderline> underlines; |
| 862 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); | 869 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); |
| 863 editable->focus(); | 870 editable->focus(); |
| 864 | 871 |
| 872 // Create new composition. | |
| 865 document().setTitle(emptyString()); | 873 document().setTitle(emptyString()); |
| 866 controller().setComposition("n", underlines, 0, 1); | 874 controller().setComposition("hell", underlines, 4, 4); |
| 875 EXPECT_STREQ("beforeinput.data:hell;input.data:hell;", | |
| 876 document().title().utf8().data()); | |
| 877 | |
| 878 // Update the existing composition. | |
| 879 // TODO(yabinh): should be "beforeinput.data:hello;input.data:hello;". | |
| 880 document().setTitle(emptyString()); | |
| 881 controller().setComposition("hello", underlines, 0, 0); | |
| 882 EXPECT_STREQ("beforeinput.data:o;input.data:o;", | |
| 883 document().title().utf8().data()); | |
| 884 | |
| 885 // Confirm the ongoing composition. | |
| 886 document().setTitle(emptyString()); | |
| 887 // Move the caret to the end of text. | |
| 888 controller().finishComposingText(InputMethodController::DoNotKeepSelection); | |
| 889 EXPECT_STREQ( | |
| 890 "beforeinput.data:hello;input.data:hello;compositionend.data:hello;", | |
| 891 document().title().utf8().data()); | |
| 892 document().updateStyleAndLayout(); | |
| 893 // Caret has been reseted to [3,3] in 'compositionend' event listener. | |
| 894 EXPECT_EQ(3u, controller().getSelectionOffsets().start()); | |
| 895 EXPECT_EQ(3u, controller().getSelectionOffsets().end()); | |
| 896 | |
| 897 // Delete the existing composition. | |
| 898 document().setTitle(emptyString()); | |
| 899 controller().setComposition("hello", underlines, 5, 5); | |
| 900 EXPECT_STREQ("beforeinput.data:hello;input.data:hello;", | |
| 901 document().title().utf8().data()); | |
| 902 document().setTitle(emptyString()); | |
| 903 controller().setComposition("", underlines, 0, 0); | |
| 904 EXPECT_STREQ("beforeinput.data:;compositionend.data:;", | |
| 905 document().title().utf8().data()); | |
| 906 document().updateStyleAndLayout(); | |
| 907 EXPECT_EQ(3u, controller().getSelectionOffsets().start()); | |
| 908 EXPECT_EQ(3u, controller().getSelectionOffsets().end()); | |
| 909 | |
| 910 // Insert new text without previous composition. | |
| 911 document().setTitle(emptyString()); | |
| 912 controller().commitText("hello", 0); | |
| 913 EXPECT_STREQ("beforeinput.data:hello;input.data:hello;", | |
| 914 document().title().utf8().data()); | |
| 915 | |
| 916 // Insert new text with previous composition. | |
| 917 document().setTitle(emptyString()); | |
| 918 controller().setComposition("n", underlines, 1, 1); | |
| 867 EXPECT_STREQ("beforeinput.data:n;input.data:n;", | 919 EXPECT_STREQ("beforeinput.data:n;input.data:n;", |
| 868 document().title().utf8().data()); | 920 document().title().utf8().data()); |
| 869 | |
| 870 document().setTitle(emptyString()); | 921 document().setTitle(emptyString()); |
| 871 controller().setComposition("ni", underlines, 0, 1); | 922 // Move the caret before "o". |
| 872 EXPECT_STREQ("beforeinput.data:i;input.data:i;", | 923 controller().commitText("hello", -1); |
| 873 document().title().utf8().data()); | 924 EXPECT_STREQ( |
| 874 | 925 "beforeinput.data:hello;input.data:hello;compositionend.data:hello;", |
| 875 document().setTitle(emptyString()); | 926 document().title().utf8().data()); |
| 876 controller().finishComposingText(InputMethodController::KeepSelection); | 927 document().updateStyleAndLayout(); |
| 877 EXPECT_STREQ("beforeinput.data:ni;input.data:ni;", | 928 // Caret has been reseted to [3,3] in 'compositionend' event listener. |
| 878 document().title().utf8().data()); | 929 EXPECT_EQ(3u, controller().getSelectionOffsets().start()); |
| 930 EXPECT_EQ(3u, controller().getSelectionOffsets().end()); | |
| 879 } | 931 } |
| 880 | 932 |
| 881 } // namespace blink | 933 } // namespace blink |
| OLD | NEW |