Commit c04fd53a authored by Daniel Zhang's avatar Daniel Zhang Committed by Commit Bot

cros: Add unit tests for autocomplete gesture and mouse events.

1. Functionality was landed in separate CLs.
2. Refactoring existing unit tests to make it simpler to land
additional unit tests in the future.

Bug: 882050
Change-Id: I86fa9ebb457f9f17342ce88bc1c3fc7edd274e07
Reviewed-on: https://chromium-review.googlesource.com/1213518
Commit-Queue: Daniel Zhang <oxyflush@google.com>
Reviewed-by: default avatarAlex Newcomer <newcomer@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#590397}
parent ed2352a8
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "ui/chromeos/search_box/search_box_constants.h" #include "ui/chromeos/search_box/search_box_constants.h"
#include "ui/chromeos/search_box/search_box_view_delegate.h" #include "ui/chromeos/search_box/search_box_view_delegate.h"
#include "ui/events/base_event_utils.h"
#include "ui/gfx/image/image_skia.h" #include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_unittest_util.h" #include "ui/gfx/image/image_unittest_util.h"
#include "ui/gfx/paint_vector_icon.h" #include "ui/gfx/paint_vector_icon.h"
...@@ -117,18 +118,6 @@ class SearchBoxViewTest : public views::test::WidgetTest, ...@@ -117,18 +118,6 @@ class SearchBoxViewTest : public views::test::WidgetTest,
} }
} }
void CreateSearchResult(ash::SearchResultDisplayType display_type,
double display_score,
const base::string16& title,
const base::string16& details) {
auto search_result = std::make_unique<SearchResult>();
search_result->set_display_type(display_type);
search_result->set_display_score(display_score);
search_result->set_title(title);
search_result->set_details(details);
results()->Add(std::move(search_result));
}
std::string GetLastQueryAndReset() { std::string GetLastQueryAndReset() {
base::string16 query = last_query_; base::string16 query = last_query_;
last_query_.clear(); last_query_.clear();
...@@ -301,6 +290,123 @@ class SearchBoxViewAutocompleteTest ...@@ -301,6 +290,123 @@ class SearchBoxViewAutocompleteTest
SearchBoxViewTest::SetUp(); SearchBoxViewTest::SetUp();
} }
// Creates a SearchResult with the given parameters.
void CreateSearchResult(ash::SearchResultDisplayType display_type,
double display_score,
const base::string16& title,
const base::string16& details) {
auto search_result = std::make_unique<SearchResult>();
search_result->set_display_type(display_type);
search_result->set_display_score(display_score);
search_result->set_title(title);
search_result->set_details(details);
results()->Add(std::move(search_result));
}
// Expect the entire autocomplete suggestion if |should_autocomplete| is true,
// expect only typed characters otherwise.
void ExpectAutocompleteSuggestion(bool should_autocomplete) {
if (should_autocomplete) {
// Search box autocomplete suggestion is accepted and reflected in Search
// Model.
EXPECT_EQ(view()->search_box()->text(),
view_delegate()->GetSearchModel()->search_box()->text());
EXPECT_EQ(view()->search_box()->text(),
base::ASCIIToUTF16("hello world!"));
} else {
// Search box autocomplete suggestion is removed and is reflected in
// SearchModel.
EXPECT_EQ(view()->search_box()->text(),
view_delegate()->GetSearchModel()->search_box()->text());
EXPECT_EQ(view()->search_box()->text(), base::ASCIIToUTF16("he"));
// ProcessAutocomplete should be a no-op.
view()->ProcessAutocomplete();
// The autocomplete suggestion should still not be present.
EXPECT_EQ(view()->search_box()->text(), base::ASCIIToUTF16("he"));
}
}
// Sets up the test by creating a SearchResult and displaying an autocomplete
// suggestion.
void SetupAutocompleteBehaviorTest() {
// Add a search result with a non-empty title field.
CreateSearchResult(ash::SearchResultDisplayType::kList, 1.0,
base::ASCIIToUTF16("hello world!"), base::string16());
// Send H, E to the SearchBoxView textfield, then trigger an autocomplete.
KeyPress(ui::VKEY_H);
KeyPress(ui::VKEY_E);
view()->ProcessAutocomplete();
}
// Clears all existing text from search_box() and all existing SearchResults
// from results().
void ResetAutocompleteBehaviorTest() {
view()->search_box()->SetText(base::string16());
results()->RemoveAll();
}
// Test a GestureEvent's autocomplete behavior according to
// |should_autocomplete|. Expect the entire autocomplete suggestion if
// |should_autocomplete| is true, expect only typed characters otherwise.
void TestGestureEvent(const ui::GestureEvent& gesture_event,
bool should_autocomplete) {
SetupAutocompleteBehaviorTest();
// Forward |gesture_event| to HandleGestureEvent() directly because we
// cannot test GestureEvents properly due to not having ash dependencies.
// Static cast to TextfieldController because HandleGestureEvent() is
// private in SearchBoxView. TODO(crbug.com/878984): Derive
// SearchBoxViewTest from AshTestBase in order to test events using
// EventGenerator instead.
static_cast<views::TextfieldController*>(view())->HandleGestureEvent(
view()->search_box(), gesture_event);
ExpectAutocompleteSuggestion(should_autocomplete);
// Reset search box text and SearchResults for next test.
ResetAutocompleteBehaviorTest();
}
// Test a KeyEvent's autocomplete behavior according to |should_autocomplete|.
// Expect the entire autocomplete suggestion if |should_autocomplete| is true,
// expect only typed characters otherwise.
void TestKeyEvent(const ui::KeyEvent& key_event, bool should_autocomplete) {
SetupAutocompleteBehaviorTest();
// TODO(crbug.com/878984): Change KeyPress() to use EventGenerator::PressKey
// instead.
if (key_event.key_code() == ui::VKEY_BACK) {
// Use KeyPress() to mimic backspace. HandleKeyEvent() will not delete the
// text.
KeyPress(key_event.key_code());
} else {
// Forward |key_event| to HandleKeyEvent(). We use HandleKeyEvent()
// because KeyPress() will replace the existing highlighted text. Static
// cast to TextfieldController because HandleGestureEvent() is private in
// SearchBoxView.
static_cast<views::TextfieldController*>(view())->HandleKeyEvent(
view()->search_box(), key_event);
}
ExpectAutocompleteSuggestion(should_autocomplete);
// Reset search box text and SearchResults for next test.
ResetAutocompleteBehaviorTest();
}
// Test a MouseEvent's autocomplete behavior according to
// |should_autocomplete|. Expect the entire autocomplete suggestion if
// |should_autocomplete| is true, expect only typed characters otherwise.
void TestMouseEvent(const ui::MouseEvent& mouse_event,
bool should_autocomplete) {
SetupAutocompleteBehaviorTest();
// Forward |mouse_event| to HandleMouseEvent() directly because we cannot
// test MouseEvents properly due to not having ash dependencies. Static cast
// to TextfieldController because HandleGestureEvent() is a private method
// in SearchBoxView. TODO(crbug.com/878984): Derive SearchBoxViewTest from
// AshTestBase in order to test events using EventGenerator instead.
static_cast<views::TextfieldController*>(view())->HandleMouseEvent(
view()->search_box(), mouse_event);
ExpectAutocompleteSuggestion(should_autocomplete);
// Reset search box text and SearchResults for next test.
ResetAutocompleteBehaviorTest();
}
ui::KeyboardCode key_code() const { return GetParam(); } ui::KeyboardCode key_code() const { return GetParam(); }
private: private:
...@@ -438,63 +544,27 @@ TEST_F(SearchBoxViewAutocompleteTest, SearchBoxAutocompletesAcceptsNextChar) { ...@@ -438,63 +544,27 @@ TEST_F(SearchBoxViewAutocompleteTest, SearchBoxAutocompletesAcceptsNextChar) {
} }
// Tests that autocomplete suggestion is accepted and displayed in SearchModel // Tests that autocomplete suggestion is accepted and displayed in SearchModel
// after hitting certain control keys. // after pressing the tab key, clicking on the search box, or gesture tapping on
TEST_F(SearchBoxViewAutocompleteTest, SearchBoxAcceptsAutocompleteForTab) { // the search box.
// Add a search result with a non-empty title field. TEST_F(SearchBoxViewAutocompleteTest,
CreateSearchResult(ash::SearchResultDisplayType::kList, 1.0, SearchBoxAcceptsAutocompleteForTabClickTap) {
base::ASCIIToUTF16("hello world!"), base::string16()); TestKeyEvent(ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_TAB, ui::EF_NONE),
true);
// Send H, E to the SearchBoxView textfield, then trigger an autocomplete. TestMouseEvent(ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(),
KeyPress(ui::VKEY_H); gfx::Point(), ui::EventTimeForNow(), 0, 0),
KeyPress(ui::VKEY_E); true);
view()->ProcessAutocomplete(); TestGestureEvent(
// Forward the tab key to HandleKeyEvent(). We use HandleKeyEvent() ui::GestureEvent(0, 0, 0, ui::EventTimeForNow(),
// because KeyPress() will replace the existing highlighted text and add ui::GestureEventDetails(ui::ET_GESTURE_TAP)),
// a repeat character. true);
ui::KeyEvent event(ui::ET_KEY_PRESSED, ui::VKEY_TAB, ui::EF_NONE);
static_cast<views::TextfieldController*>(view())->HandleKeyEvent(
view()->search_box(), event);
// Search box autocomplete suggestion is accepted and is reflected in
// SearchModel.
EXPECT_EQ(view()->search_box()->text(),
view_delegate()->GetSearchModel()->search_box()->text());
EXPECT_EQ(view()->search_box()->text(), base::ASCIIToUTF16("hello world!"));
} }
// Tests that only the autocomplete suggestion text is deleted after hitting up, // Tests that only the autocomplete suggestion text is deleted after pressing
// down, left, right, or backspace. // up, down, left, right, or backspace.
TEST_P(SearchBoxViewAutocompleteTest, TEST_P(SearchBoxViewAutocompleteTest,
SearchBoxDeletesAutocompleteTextOnlyAfterUpDownLeftRightBackspace) { SearchBoxDeletesAutocompleteTextOnlyAfterUpDownLeftRightBackspace) {
// Add a search result with a non-empty title field. TestKeyEvent(ui::KeyEvent(ui::ET_KEY_PRESSED, key_code(), ui::EF_NONE),
CreateSearchResult(ash::SearchResultDisplayType::kList, 1.0, false);
base::ASCIIToUTF16("hello world!"), base::string16());
// Send H, E to the SearchBoxView textfield, then trigger an autocomplete.
KeyPress(ui::VKEY_H);
KeyPress(ui::VKEY_E);
view()->ProcessAutocomplete();
// TODO(crbug.com/878984): Change KeyPress() to use EventGenerator::PressKey
// instead.
if (key_code() == ui::VKEY_BACK) {
// Use KeyPress() to mimic backspace. HandleKeyEvent() will not delete the
// text.
KeyPress(key_code());
} else {
// Forward the next parameter to HandleKeyEvent(). We use HandleKeyEvent()
// because KeyPress() will replace the existing highlighted text.
ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code(), ui::EF_NONE);
static_cast<views::TextfieldController*>(view())->HandleKeyEvent(
view()->search_box(), event);
}
// Search box autocomplete suggestion is removed and is reflected in
// SearchModel.
EXPECT_EQ(view()->search_box()->text(),
view_delegate()->GetSearchModel()->search_box()->text());
EXPECT_EQ(view()->search_box()->text(), base::ASCIIToUTF16("he"));
// ProcessAutocomplete should be a no-op.
view()->ProcessAutocomplete();
// The autocomplete suggestion should still not be present.
EXPECT_EQ(view()->search_box()->text(), base::ASCIIToUTF16("he"));
} }
} // namespace test } // namespace test
......
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