Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(321)

Unified Diff: ui/views/controls/table/table_view_unittest.cc

Issue 2780833004: Improve TableView unit tests and fix bugs that were uncovered. (Closed)
Patch Set: docs Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/views/controls/table/table_view_unittest.cc
diff --git a/ui/views/controls/table/table_view_unittest.cc b/ui/views/controls/table/table_view_unittest.cc
index 5c71032e61d3a05b052ff3180785be4c1ebe685b..ae8adfd05b97de8f81e7e7ae2a1b708aece48f77 100644
--- a/ui/views/controls/table/table_view_unittest.cc
+++ b/ui/views/controls/table/table_view_unittest.cc
@@ -7,13 +7,16 @@
#include <stddef.h>
#include "base/macros.h"
+#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/event_utils.h"
+#include "ui/events/test/event_generator.h"
#include "ui/views/controls/table/table_grouper.h"
#include "ui/views/controls/table/table_header.h"
#include "ui/views/controls/table/table_view_observer.h"
+#include "ui/views/test/views_test_base.h"
// Put the tests in the views namespace to make it easier to declare them as
// friend classes.
@@ -203,11 +206,13 @@ class TestTableView : public TableView {
} // namespace
-class TableViewTest : public testing::Test {
+class TableViewTest : public ViewsTestBase {
public:
TableViewTest() : table_(NULL) {}
void SetUp() override {
+ ViewsTestBase::SetUp();
+
model_.reset(new TestTableModel2);
std::vector<ui::TableColumn> columns(2);
columns[0].title = base::ASCIIToUTF16("Title Column 0");
@@ -216,26 +221,35 @@ class TableViewTest : public testing::Test {
columns[1].id = 1;
columns[1].sortable = true;
table_ = new TestTableView(model_.get(), columns);
- parent_.reset(table_->CreateParentIfNecessary());
- parent_->SetBounds(0, 0, 10000, 10000);
- parent_->Layout();
+ View* parent = table_->CreateParentIfNecessary();
+ parent->SetBounds(0, 0, 10000, 10000);
+ parent->Layout();
helper_.reset(new TableViewTestHelper(table_));
+
+ Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
+ params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
+ params.bounds = gfx::Rect(0, 0, 650, 650);
+ widget_.reset(new Widget);
+ widget_->Init(params);
+ widget_->GetContentsView()->AddChildView(parent);
+ widget_->Show();
+ }
+
+ void TearDown() override {
+ widget_.reset();
+ ViewsTestBase::TearDown();
}
void ClickOnRow(int row, int flags) {
- const int y = row * table_->row_height();
- const ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, gfx::Point(0, y),
- gfx::Point(0, y), ui::EventTimeForNow(),
- ui::EF_LEFT_MOUSE_BUTTON | flags,
- ui::EF_LEFT_MOUSE_BUTTON);
- table_->OnMousePressed(pressed);
+ ui::test::EventGenerator generator(widget_->GetNativeWindow());
+ generator.set_flags(flags);
+ generator.set_current_location(GetPointForRow(row));
+ generator.PressLeftButton();
}
void TapOnRow(int row) {
- const int y = row * table_->row_height();
- const ui::GestureEventDetails event_details(ui::ET_GESTURE_TAP);
- ui::GestureEvent tap(0, y, 0, base::TimeTicks(), event_details);
- table_->OnGestureEvent(&tap);
+ ui::test::EventGenerator generator(widget_->GetNativeWindow());
+ generator.GestureTapAt(GetPointForRow(row));
}
// Returns the state of the selection model as a string. The format is:
@@ -256,8 +270,8 @@ class TableViewTest : public testing::Test {
}
void PressKey(ui::KeyboardCode code) {
- ui::KeyEvent event(ui::ET_KEY_PRESSED, code, ui::EF_NONE);
- table_->OnKeyPressed(event);
+ ui::test::EventGenerator generator(widget_->GetNativeWindow());
+ generator.PressKey(code, ui::EF_NONE);
}
protected:
@@ -269,7 +283,12 @@ class TableViewTest : public testing::Test {
std::unique_ptr<TableViewTestHelper> helper_;
private:
- std::unique_ptr<View> parent_;
+ gfx::Point GetPointForRow(int row) {
+ const int y = row * table_->row_height();
+ return table_->GetBoundsInScreen().origin() + gfx::Vector2d(5, y);
+ }
+
+ std::unique_ptr<Widget> widget_;
DISALLOW_COPY_AND_ASSIGN(TableViewTest);
};
@@ -766,7 +785,11 @@ TEST_F(TableViewTest, KeyUpDown) {
// Initially no selection.
EXPECT_EQ("active=-1 anchor=-1 selection=", SelectionStateAsString());
- PressKey(ui::VKEY_DOWN);
+ // Focus selects the first row.
+ table_->RequestFocus();
+ EXPECT_EQ(0, observer.GetChangedCountAndClear());
+ // ... after a delay.
+ base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, observer.GetChangedCountAndClear());
EXPECT_EQ("active=0 anchor=0 selection=0 1", SelectionStateAsString());
@@ -893,17 +916,26 @@ TEST_F(TableViewTest, HomeEnd) {
TableViewObserverImpl observer;
table_->set_observer(&observer);
- // Initially no selection.
- EXPECT_EQ("active=-1 anchor=-1 selection=", SelectionStateAsString());
+ // Focus selects the first row.
+ table_->RequestFocus();
+ EXPECT_EQ(0, observer.GetChangedCountAndClear());
+ // ... after a delay.
+ base::RunLoop().RunUntilIdle();
+ EXPECT_EQ(1, observer.GetChangedCountAndClear());
+ EXPECT_EQ("active=0 anchor=0 selection=0 1", SelectionStateAsString());
PressKey(ui::VKEY_HOME);
- EXPECT_EQ(1, observer.GetChangedCountAndClear());
+ EXPECT_EQ(0, observer.GetChangedCountAndClear());
EXPECT_EQ("active=0 anchor=0 selection=0 1", SelectionStateAsString());
PressKey(ui::VKEY_END);
EXPECT_EQ(1, observer.GetChangedCountAndClear());
EXPECT_EQ("active=4 anchor=4 selection=3 4", SelectionStateAsString());
+ PressKey(ui::VKEY_HOME);
+ EXPECT_EQ(1, observer.GetChangedCountAndClear());
+ EXPECT_EQ("active=0 anchor=0 selection=0 1", SelectionStateAsString());
+
table_->set_observer(NULL);
}
@@ -1026,15 +1058,31 @@ TEST_F(TableViewTest, FocusAfterRemovingAnchor) {
// Tests that focusing the table will activate the first row, but only if
// there's no active row.
TEST_F(TableViewTest, InitialFocusActivatesFirstRow) {
+ // Focus selects the first row if there's nothing selected.
EXPECT_EQ(-1, table_->selection_model().active());
helper_->OnFocus();
+ base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, table_->selection_model().active());
+ // Focus does not affect selection if something's already selected.
ui::ListSelectionModel new_selection;
new_selection.set_active(1);
helper_->SetSelectionModel(new_selection);
EXPECT_EQ(1, table_->selection_model().active());
helper_->OnFocus();
+ base::RunLoop().RunUntilIdle();
+ EXPECT_EQ(1, table_->selection_model().active());
+
+ // Focus does not affect selection if something gets selected synchronously
+ // after focus is taken.
+ new_selection.Clear();
+ helper_->SetSelectionModel(new_selection);
+ EXPECT_EQ(-1, table_->selection_model().active());
+ helper_->OnFocus();
+ new_selection.set_active(1);
+ helper_->SetSelectionModel(new_selection);
+ EXPECT_EQ(1, table_->selection_model().active());
+ base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, table_->selection_model().active());
// Remove all rows; focusing should not activate a non existent first row.
« ui/views/controls/table/table_view.cc ('K') | « ui/views/controls/table/table_view.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698