Home | History | Annotate | Download | only in input_method
      1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/strings/utf_string_conversions.h"
      6 #include "chrome/browser/chromeos/input_method/textinput_test_helper.h"
      7 #include "chrome/browser/ui/browser.h"
      8 #include "chrome/browser/ui/tabs/tab_strip_model.h"
      9 #include "chrome/test/base/interactive_test_utils.h"
     10 #include "content/public/browser/web_contents.h"
     11 #include "content/public/test/browser_test_utils.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 #include "ui/base/ime/composition_underline.h"
     14 
     15 namespace chromeos {
     16 
     17 typedef TextInputTestBase TextInput_SurroundingTextChangedTest;
     18 
     19 IN_PROC_BROWSER_TEST_F(TextInput_SurroundingTextChangedTest,
     20                        SurroundingTextChangedWithInsertText) {
     21   TextInputTestHelper helper;
     22   GURL url = ui_test_utils::GetTestUrl(
     23       base::FilePath(FILE_PATH_LITERAL("textinput")),
     24       base::FilePath(FILE_PATH_LITERAL("simple_textarea.html")));
     25   ui_test_utils::NavigateToURL(browser(), url);
     26   EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE, helper.GetTextInputType());
     27 
     28   content::WebContents* tab =
     29       browser()->tab_strip_model()->GetActiveWebContents();
     30 
     31   ASSERT_TRUE(content::ExecuteScript(
     32       tab,
     33       "document.getElementById('text_id').focus()"));
     34   helper.WaitForTextInputStateChanged(ui::TEXT_INPUT_TYPE_TEXT_AREA);
     35   EXPECT_EQ(ui::TEXT_INPUT_TYPE_TEXT_AREA, helper.GetTextInputType());
     36 
     37   const base::string16 sample_text1 = base::UTF8ToUTF16("abcde");
     38   const base::string16 sample_text2 = base::UTF8ToUTF16("fghij");
     39   const base::string16 surrounding_text2 = sample_text1 + sample_text2;
     40   gfx::Range expected_range1(5, 5);
     41   gfx::Range expected_range2(10, 10);
     42 
     43   ASSERT_TRUE(helper.GetTextInputClient());
     44 
     45   helper.GetTextInputClient()->InsertText(sample_text1);
     46   helper.WaitForSurroundingTextChanged(sample_text1, expected_range1);
     47   EXPECT_EQ(sample_text1, helper.GetSurroundingText());
     48   EXPECT_EQ(expected_range1, helper.GetSelectionRange());
     49 
     50   helper.GetTextInputClient()->InsertText(sample_text2);
     51   helper.WaitForSurroundingTextChanged(surrounding_text2, expected_range2);
     52   EXPECT_EQ(surrounding_text2, helper.GetSurroundingText());
     53   EXPECT_EQ(expected_range2, helper.GetSelectionRange());
     54 }
     55 
     56 IN_PROC_BROWSER_TEST_F(TextInput_SurroundingTextChangedTest,
     57                        SurroundingTextChangedWithComposition) {
     58   TextInputTestHelper helper;
     59   GURL url = ui_test_utils::GetTestUrl(
     60       base::FilePath(FILE_PATH_LITERAL("textinput")),
     61       base::FilePath(FILE_PATH_LITERAL("simple_textarea.html")));
     62   ui_test_utils::NavigateToURL(browser(), url);
     63   EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE, helper.GetTextInputType());
     64 
     65   content::WebContents* tab =
     66       browser()->tab_strip_model()->GetActiveWebContents();
     67 
     68   ASSERT_TRUE(content::ExecuteScript(
     69       tab,
     70       "document.getElementById('text_id').focus()"));
     71   helper.WaitForTextInputStateChanged(ui::TEXT_INPUT_TYPE_TEXT_AREA);
     72   EXPECT_EQ(ui::TEXT_INPUT_TYPE_TEXT_AREA, helper.GetTextInputType());
     73 
     74   const base::string16 sample_text = base::UTF8ToUTF16("abcde");
     75   gfx::Range expected_range(5, 5);
     76 
     77   ui::CompositionText composition_text;
     78   composition_text.text = sample_text;
     79   composition_text.selection.set_start(expected_range.length());
     80   composition_text.selection.set_end(expected_range.length());
     81 
     82   ASSERT_TRUE(helper.GetTextInputClient());
     83   helper.GetTextInputClient()->SetCompositionText(composition_text);
     84   ASSERT_TRUE(helper.GetTextInputClient()->HasCompositionText());
     85   // TODO(nona): Make sure there is no IPC from renderer.
     86   helper.GetTextInputClient()->InsertText(sample_text);
     87   helper.GetTextInputClient()->ClearCompositionText();
     88 
     89   ASSERT_FALSE(helper.GetTextInputClient()->HasCompositionText());
     90   helper.WaitForSurroundingTextChanged(sample_text, expected_range);
     91   EXPECT_EQ(sample_text, helper.GetSurroundingText());
     92   EXPECT_EQ(expected_range, helper.GetSelectionRange());
     93 }
     94 
     95 IN_PROC_BROWSER_TEST_F(TextInput_SurroundingTextChangedTest,
     96                        FocusToTextContainingTextAreaByClickingCase) {
     97   TextInputTestHelper helper;
     98   GURL url = ui_test_utils::GetTestUrl(
     99       base::FilePath(FILE_PATH_LITERAL("textinput")),
    100       base::FilePath(FILE_PATH_LITERAL("textarea_with_preset_text.html")));
    101   ui_test_utils::NavigateToURL(browser(), url);
    102   EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE, helper.GetTextInputType());
    103 
    104   content::WebContents* tab =
    105       browser()->tab_strip_model()->GetActiveWebContents();
    106   const gfx::Range zero_range(0, 0);
    107 
    108   // We expect no surrounding texts.
    109   helper.ClickElement("empty_textarea", tab);
    110   helper.WaitForTextInputStateChanged(ui::TEXT_INPUT_TYPE_TEXT_AREA);
    111   EXPECT_EQ(ui::TEXT_INPUT_TYPE_TEXT_AREA, helper.GetTextInputType());
    112   helper.WaitForSurroundingTextChanged(base::string16(), zero_range);
    113   EXPECT_TRUE(helper.GetSurroundingText().empty());
    114   EXPECT_EQ(zero_range, helper.GetSelectionRange());
    115 
    116   // Click textarea containing text, so expecting new surrounding text comes.
    117   helper.ClickElement("filled_textarea", tab);
    118   const base::string16 expected_text = base::UTF8ToUTF16("abcde");
    119   const gfx::Range expected_range(5, 5);
    120   helper.WaitForSurroundingTextChanged(expected_text, expected_range);
    121   EXPECT_EQ(expected_text, helper.GetSurroundingText());
    122   EXPECT_EQ(expected_range, helper.GetSelectionRange());
    123 
    124   // Then, back to empty text area: expecting empty string.
    125   helper.ClickElement("empty_textarea", tab);
    126   helper.WaitForTextInputStateChanged(ui::TEXT_INPUT_TYPE_TEXT_AREA);
    127   EXPECT_EQ(ui::TEXT_INPUT_TYPE_TEXT_AREA, helper.GetTextInputType());
    128   helper.WaitForSurroundingTextChanged(base::string16(), zero_range);
    129   EXPECT_TRUE(helper.GetSurroundingText().empty());
    130   EXPECT_EQ(zero_range, helper.GetSelectionRange());
    131 }
    132 
    133 // TODO(nona): Add test for JavaScript focusing to textarea containing text.
    134 // TODO(nona): Add test for text changing by JavaScript.
    135 // TODO(nona): Add test for onload focusing to textarea containing text.
    136 } // namespace chromeos
    137