Commit 4a6dd400 authored by Tommy C. Li's avatar Tommy C. Li Committed by Commit Bot

Omnibox Code Health: Port 4 clipboard browser tests to unit tests

Doing this work for a couple reasons:
 - Unit tests are less flaky and faster.
 - These can be written as unit tests without losing any testing power.
 - Query in Omnibox may need to update the clipboard behavior soon,
   this is kind of adjacent work.

Bug: 874592, 751031
Change-Id: Ibf5273f22578be713ab3c48539ab5311e5c7a7cc
Reviewed-on: https://chromium-review.googlesource.com/c/1377174
Commit-Queue: Tommy Li <tommycli@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#618663}
parent f02517f9
...@@ -1604,138 +1604,6 @@ IN_PROC_BROWSER_TEST_F(OmniboxViewTest, Paste) { ...@@ -1604,138 +1604,6 @@ IN_PROC_BROWSER_TEST_F(OmniboxViewTest, Paste) {
// TODO(msw): Test that AltGr+V does not paste. // TODO(msw): Test that AltGr+V does not paste.
} }
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CopyURLToClipboard) {
// Set permanent text thus making sure that omnibox treats 'google.com'
// as URL (not as ordinary user input).
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
SetTestToolbarPermanentText(ASCIIToUTF16("http://www.google.com"));
const char* target_url = "http://www.google.com/calendar";
omnibox_view->SetUserText(ASCIIToUTF16(target_url));
// Location bar must have focus.
chrome::FocusLocationBar(browser());
ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
// Select full URL and copy it to clipboard. General text and html should
// be available.
omnibox_view->SelectAll(true);
EXPECT_TRUE(omnibox_view->IsSelectAll());
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_COPY));
EXPECT_EQ(ASCIIToUTF16(target_url), omnibox_view->GetText());
EXPECT_TRUE(clipboard->IsFormatAvailable(
ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
// Make sure HTML format isn't written. See
// BookmarkNodeData::WriteToClipboard() for details.
EXPECT_FALSE(clipboard->IsFormatAvailable(
ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
// Windows clipboard only supports text URLs.
// Mac clipboard not reporting URL format available for some reason.
// crbug.com/751031
#if defined(OS_LINUX)
EXPECT_TRUE(clipboard->IsFormatAvailable(ui::Clipboard::GetUrlFormatType(),
ui::CLIPBOARD_TYPE_COPY_PASTE));
#endif
std::string url;
clipboard->ReadAsciiText(ui::CLIPBOARD_TYPE_COPY_PASTE, &url);
EXPECT_EQ(target_url, url);
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CutURLToClipboard) {
// Set permanent text thus making sure that omnibox treats 'google.com'
// as URL (not as ordinary user input).
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
SetTestToolbarPermanentText(ASCIIToUTF16("http://www.google.com"));
const char* target_url = "http://www.google.com/calendar";
omnibox_view->SetUserText(ASCIIToUTF16(target_url));
// Location bar must have focus.
chrome::FocusLocationBar(browser());
ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
// Select full URL and cut it. General text and html should be available
// in the clipboard.
omnibox_view->SelectAll(true);
EXPECT_TRUE(omnibox_view->IsSelectAll());
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_CUT));
EXPECT_EQ(base::string16(), omnibox_view->GetText());
EXPECT_TRUE(clipboard->IsFormatAvailable(
ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
// Make sure HTML format isn't written. See
// BookmarkNodeData::WriteToClipboard() for details.
EXPECT_FALSE(clipboard->IsFormatAvailable(
ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
// Windows clipboard only supports text URLs.
// Mac clipboard not reporting URL format available for some reason.
// crbug.com/751031
#if defined(OS_LINUX)
EXPECT_TRUE(clipboard->IsFormatAvailable(ui::Clipboard::GetUrlFormatType(),
ui::CLIPBOARD_TYPE_COPY_PASTE));
#endif
std::string url;
clipboard->ReadAsciiText(ui::CLIPBOARD_TYPE_COPY_PASTE, &url);
EXPECT_EQ(target_url, url);
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CopyTextToClipboard) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
const char* target_text = "foo";
omnibox_view->SetUserText(ASCIIToUTF16(target_text));
// Location bar must have focus.
chrome::FocusLocationBar(browser());
ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
// Select full text and copy it to the clipboard.
omnibox_view->SelectAll(true);
EXPECT_TRUE(omnibox_view->IsSelectAll());
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_COPY));
EXPECT_TRUE(clipboard->IsFormatAvailable(
ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
EXPECT_FALSE(clipboard->IsFormatAvailable(
ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
EXPECT_EQ(ASCIIToUTF16(target_text), omnibox_view->GetText());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CutTextToClipboard) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
const char* target_text = "foo";
omnibox_view->SetUserText(ASCIIToUTF16(target_text));
// Location bar must have focus.
chrome::FocusLocationBar(browser());
ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
// Select full text and cut it to the clipboard.
omnibox_view->SelectAll(true);
EXPECT_TRUE(omnibox_view->IsSelectAll());
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_CUT));
EXPECT_TRUE(clipboard->IsFormatAvailable(
ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
EXPECT_FALSE(clipboard->IsFormatAvailable(
ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
EXPECT_EQ(base::string16(), omnibox_view->GetText());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, EditSearchEngines) { IN_PROC_BROWSER_TEST_F(OmniboxViewTest, EditSearchEngines) {
OmniboxView* omnibox_view = nullptr; OmniboxView* omnibox_view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view)); ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "components/omnibox/browser/test_location_bar_model.h" #include "components/omnibox/browser/test_location_bar_model.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "third_party/metrics_proto/omnibox_event.pb.h" #include "third_party/metrics_proto/omnibox_event.pb.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/ime/input_method.h" #include "ui/base/ime/input_method.h"
#include "ui/base/ime/text_edit_commands.h" #include "ui/base/ime/text_edit_commands.h"
#include "ui/base/ui_base_features.h" #include "ui/base/ui_base_features.h"
...@@ -146,6 +147,13 @@ void TestingOmniboxView::UpdatePopup() { ...@@ -146,6 +147,13 @@ void TestingOmniboxView::UpdatePopup() {
++update_popup_call_count_; ++update_popup_call_count_;
update_popup_text_ = text(); update_popup_text_ = text();
update_popup_selection_range_ = GetSelectedRange(); update_popup_selection_range_ = GetSelectedRange();
// The real view calls OmniboxEditModel::UpdateInput(), which sets input in
// progress and starts autocomplete. Triggering autocomplete and the popup is
// beyond the scope of this test, but setting input in progress is important
// for making some sequences (e.g. uneliding on taking an action) behave
// correctly.
model()->SetInputInProgress(true);
} }
void TestingOmniboxView::SetEmphasis(bool emphasize, const Range& range) { void TestingOmniboxView::SetEmphasis(bool emphasize, const Range& range) {
...@@ -217,9 +225,7 @@ class OmniboxViewViewsTest : public OmniboxViewViewsTestBase { ...@@ -217,9 +225,7 @@ class OmniboxViewViewsTest : public OmniboxViewViewsTestBase {
views::Textfield* omnibox_textfield() const { return omnibox_view(); } views::Textfield* omnibox_textfield() const { return omnibox_view(); }
views::View* omnibox_textfield_view() const { return omnibox_view(); } views::View* omnibox_textfield_view() const { return omnibox_view(); }
ui::TextEditCommand scheduled_text_edit_command() const { views::TextfieldTestApi* textfield_test_api() { return test_api_.get(); }
return test_api_->scheduled_text_edit_command();
}
// Sets |new_text| as the omnibox text, and emphasizes it appropriately. If // Sets |new_text| as the omnibox text, and emphasizes it appropriately. If
// |accept_input| is true, pretends that the user has accepted this input // |accept_input| is true, pretends that the user has accepted this input
...@@ -365,12 +371,13 @@ TEST_F(OmniboxViewViewsTest, EditTextfield) { ...@@ -365,12 +371,13 @@ TEST_F(OmniboxViewViewsTest, EditTextfield) {
TEST_F(OmniboxViewViewsTest, ScheduledTextEditCommand) { TEST_F(OmniboxViewViewsTest, ScheduledTextEditCommand) {
omnibox_textfield()->SetTextEditCommandForNextKeyEvent( omnibox_textfield()->SetTextEditCommandForNextKeyEvent(
ui::TextEditCommand::MOVE_UP); ui::TextEditCommand::MOVE_UP);
EXPECT_EQ(ui::TextEditCommand::MOVE_UP, scheduled_text_edit_command()); EXPECT_EQ(ui::TextEditCommand::MOVE_UP,
textfield_test_api()->scheduled_text_edit_command());
ui::KeyEvent up_pressed(ui::ET_KEY_PRESSED, ui::VKEY_UP, 0); ui::KeyEvent up_pressed(ui::ET_KEY_PRESSED, ui::VKEY_UP, 0);
omnibox_textfield()->OnKeyEvent(&up_pressed); omnibox_textfield()->OnKeyEvent(&up_pressed);
EXPECT_EQ(ui::TextEditCommand::INVALID_COMMAND, EXPECT_EQ(ui::TextEditCommand::INVALID_COMMAND,
scheduled_text_edit_command()); textfield_test_api()->scheduled_text_edit_command());
} }
// Test that Shift+Up and Shift+Down are not captured and let selection mode // Test that Shift+Up and Shift+Down are not captured and let selection mode
...@@ -540,6 +547,87 @@ TEST_F(OmniboxViewViewsTest, BackspaceExitsKeywordMode) { ...@@ -540,6 +547,87 @@ TEST_F(OmniboxViewViewsTest, BackspaceExitsKeywordMode) {
EXPECT_TRUE(omnibox_view()->model()->keyword().empty()); EXPECT_TRUE(omnibox_view()->model()->keyword().empty());
} }
class OmniboxViewViewsClipboardTest
: public OmniboxViewViewsTest,
public ::testing::WithParamInterface<ui::TextEditCommand> {
public:
void SetUp() override {
OmniboxViewViewsTest::SetUp();
location_bar_model()->set_url(GURL("https://test.com/"));
omnibox_view()->model()->ResetDisplayTexts();
omnibox_view()->RevertAll();
}
};
TEST_P(OmniboxViewViewsClipboardTest, ClipboardCopyOrCutURL) {
omnibox_view()->SelectAll(false);
ASSERT_TRUE(omnibox_view()->IsSelectAll());
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
ui::ClipboardType clipboard_type = ui::CLIPBOARD_TYPE_COPY_PASTE;
clipboard->Clear(clipboard_type);
ui::TextEditCommand clipboard_command = GetParam();
textfield_test_api()->ExecuteTextEditCommand(clipboard_command);
base::string16 expected_text;
if (clipboard_command == ui::TextEditCommand::COPY)
expected_text = base::ASCIIToUTF16("https://test.com/");
EXPECT_EQ(expected_text, omnibox_view()->GetText());
// Make sure HTML format isn't written. See
// BookmarkNodeData::WriteToClipboard() for details.
EXPECT_TRUE(clipboard->IsFormatAvailable(
ui::Clipboard::GetPlainTextFormatType(), clipboard_type));
EXPECT_FALSE(clipboard->IsFormatAvailable(ui::Clipboard::GetHtmlFormatType(),
clipboard_type));
// Windows clipboard only supports text URLs.
// Mac clipboard not reporting URL format available for some reason.
// crbug.com/751031
#if defined(OS_LINUX)
EXPECT_TRUE(clipboard->IsFormatAvailable(ui::Clipboard::GetUrlFormatType(),
clipboard_type));
#endif
std::string read_from_clipboard;
clipboard->ReadAsciiText(clipboard_type, &read_from_clipboard);
EXPECT_EQ("https://test.com/", read_from_clipboard);
}
TEST_P(OmniboxViewViewsClipboardTest, ClipboardCopyOrCutUserText) {
omnibox_view()->SetUserText(base::ASCIIToUTF16("user text"));
omnibox_view()->SelectAll(false);
ASSERT_TRUE(omnibox_view()->IsSelectAll());
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
ui::ClipboardType clipboard_type = ui::CLIPBOARD_TYPE_COPY_PASTE;
clipboard->Clear(clipboard_type);
ui::TextEditCommand clipboard_command = GetParam();
textfield_test_api()->ExecuteTextEditCommand(clipboard_command);
if (clipboard_command == ui::TextEditCommand::CUT)
EXPECT_EQ(base::string16(), omnibox_view()->GetText());
// Make sure HTML format isn't written. See
// BookmarkNodeData::WriteToClipboard() for details.
EXPECT_TRUE(clipboard->IsFormatAvailable(
ui::Clipboard::GetPlainTextFormatType(), clipboard_type));
EXPECT_FALSE(clipboard->IsFormatAvailable(ui::Clipboard::GetHtmlFormatType(),
clipboard_type));
std::string read_from_clipboard;
clipboard->ReadAsciiText(clipboard_type, &read_from_clipboard);
EXPECT_EQ("user text", read_from_clipboard);
}
INSTANTIATE_TEST_CASE_P(OmniboxViewViewsClipboardTest,
OmniboxViewViewsClipboardTest,
::testing::Values(ui::TextEditCommand::COPY,
ui::TextEditCommand::CUT));
class OmniboxViewViewsSteadyStateElisionsTest : public OmniboxViewViewsTest { class OmniboxViewViewsSteadyStateElisionsTest : public OmniboxViewViewsTest {
public: public:
OmniboxViewViewsSteadyStateElisionsTest() OmniboxViewViewsSteadyStateElisionsTest()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment