diff options
Diffstat (limited to 'views/controls/table/table_view_unittest.cc')
-rw-r--r-- | views/controls/table/table_view_unittest.cc | 213 |
1 files changed, 210 insertions, 3 deletions
diff --git a/views/controls/table/table_view_unittest.cc b/views/controls/table/table_view_unittest.cc index d79a5bc..7ced10c 100644 --- a/views/controls/table/table_view_unittest.cc +++ b/views/controls/table/table_view_unittest.cc @@ -10,6 +10,7 @@ #include "base/string_util.h" #include "testing/gtest/include/gtest/gtest.h" #include "views/controls/table/table_view.h" +#include "views/controls/table/table_view2.h" #include "views/window/window_delegate.h" #include "views/window/window_win.h" @@ -29,6 +30,11 @@ class TestTableModel : public TableModel { public: TestTableModel(); + struct CheckNotification { + int row; + bool state; + }; + // Adds a new row at index |row| with values |c1_value| and |c2_value|. void AddRow(int row, int c1_value, int c2_value); @@ -43,6 +49,11 @@ class TestTableModel : public TableModel { virtual std::wstring GetText(int row, int column_id); virtual void SetObserver(TableModelObserver* observer); virtual int CompareValues(int row1, int row2, int column_id); + virtual bool IsChecked(int row); + virtual void SetChecked(int row, bool is_checked); + + // Contains a record of the SetChecked calls. + std::vector<CheckNotification> check_notifications_; private: TableModelObserver* observer_; @@ -99,6 +110,16 @@ int TestTableModel::CompareValues(int row1, int row2, int column_id) { return rows_[row1][column_id] - rows_[row2][column_id]; } +bool TestTableModel::IsChecked(int row) { + // Let's make the first row the only checked one. + return (row == 1); +} + +void TestTableModel::SetChecked(int row, bool is_checked) { + CheckNotification check_notification = { row, is_checked }; + check_notifications_.push_back(check_notification); +} + // TableViewTest --------------------------------------------------------------- class TableViewTest : public testing::Test, views::WindowDelegate { @@ -149,9 +170,8 @@ void TableViewTest::SetUp() { table_ = new TableView(model_.get(), columns, views::ICON_AND_TEXT, false, false, false); window_ = - views::Window::CreateChromeWindow(NULL, - gfx::Rect(100, 100, 512, 512), - this); + views::Window::CreateChromeWindow(NULL, gfx::Rect(100, 100, 512, 512), + this); } void TableViewTest::TearDown() { @@ -381,3 +401,190 @@ TEST_F(NullModelTableViewTest, NullModel) { // There's nothing explicit to test. If there is a bug in TableView relating // to a NULL model we'll crash. } + +//////////////////////////////////////////////////////////////////////////////// +// TableView2 Tests + +class TableView2Test : public testing::Test, views::WindowDelegate { + public: + virtual void SetUp(); + virtual void TearDown(); + + virtual views::View* GetContentsView() { + return table_; + } + + // Returns the contents of a cell in the table. + std::wstring GetCellValue(int row, int column); + + protected: + // Creates the model. + TestTableModel* CreateModel(); + + virtual views::TableTypes GetTableType() { + return views::TEXT_ONLY; + } + + scoped_ptr<TestTableModel> model_; + + // The table. This is owned by the window. + views::TableView2* table_; + + private: + MessageLoopForUI message_loop_; + views::Window* window_; +}; + +void TableView2Test::SetUp() { + OleInitialize(NULL); + model_.reset(CreateModel()); + std::vector<TableColumn> columns; + columns.resize(2); + columns[0].id = 0; + columns[1].id = 1; + table_ = new views::TableView2(model_.get(), columns, GetTableType(), + false, false, false); + window_ = views::Window::CreateChromeWindow(NULL, + gfx::Rect(100, 100, 512, 512), + this); + window_->Show(); +} + +void TableView2Test::TearDown() { + window_->Close(); + // Temporary workaround to avoid leak of RootView::pending_paint_task_. + message_loop_.RunAllPending(); + OleUninitialize(); +} + +TestTableModel* TableView2Test::CreateModel() { + return new TestTableModel(); +} + +std::wstring TableView2Test::GetCellValue(int row, int column) { +#if defined(OS_WIN) + wchar_t str[128] = {0}; + LVITEM item = {0}; + item.mask = LVIF_TEXT; + item.iItem = row; + item.iSubItem = column; + item.pszText = str; + item.cchTextMax = 128; + BOOL r = ListView_GetItem(table_->GetTestingHandle(), &item); + DCHECK(r); + return std::wstring(str); +#else + NOTIMPLEMENTED(); +#endif +} + +// Tests that the table correctly reflects changes to the model. +TEST_F(TableView2Test, ModelChangesTest) { + EXPECT_EQ(3, table_->GetRowCount()); + EXPECT_EQ(L"0", GetCellValue(0, 0)); + EXPECT_EQ(L"1", GetCellValue(1, 0)); + EXPECT_EQ(L"2", GetCellValue(2, 1)); + + // Test adding rows and that OnItemsAdded works. + model_->AddRow(3, 3, 3); + model_->AddRow(4, 4, 4); + table_->OnItemsAdded(3, 2); + EXPECT_EQ(5, table_->GetRowCount()); + EXPECT_EQ(L"3", GetCellValue(3, 0)); + EXPECT_EQ(L"4", GetCellValue(4, 1)); + + // Test removing rows and that OnItemsRemoved works. + model_->RemoveRow(1); + model_->RemoveRow(1); + table_->OnItemsRemoved(1, 2); + EXPECT_EQ(3, table_->GetRowCount()); + EXPECT_EQ(L"0", GetCellValue(0, 0)); + EXPECT_EQ(L"3", GetCellValue(1, 0)); + EXPECT_EQ(L"4", GetCellValue(2, 1)); + + // Test changing rows and that OnItemsChanged works. + model_->ChangeRow(1, 1, 1); + model_->ChangeRow(2, 2, 2); + table_->OnItemsChanged(1, 2); + EXPECT_EQ(L"0", GetCellValue(0, 0)); + EXPECT_EQ(L"1", GetCellValue(1, 0)); + EXPECT_EQ(L"2", GetCellValue(2, 1)); + + // Test adding and removing rows and using OnModelChanged. + model_->RemoveRow(2); + model_->AddRow(2, 5, 5); + model_->AddRow(3, 6, 6); + table_->OnModelChanged(); + EXPECT_EQ(4, table_->GetRowCount()); + EXPECT_EQ(L"0", GetCellValue(0, 0)); + EXPECT_EQ(L"1", GetCellValue(1, 0)); + EXPECT_EQ(L"5", GetCellValue(2, 1)); + EXPECT_EQ(L"6", GetCellValue(3, 1)); +} + +// Test the selection on a single-selection table. +TEST_F(TableView2Test, SingleSelectionTest) { + EXPECT_EQ(0, table_->SelectedRowCount()); + EXPECT_EQ(-1, table_->GetFirstSelectedRow()); + + table_->SelectRow(0); + EXPECT_EQ(1, table_->SelectedRowCount()); + EXPECT_EQ(0, table_->GetFirstSelectedRow()); + + table_->SelectRow(2); + EXPECT_EQ(1, table_->SelectedRowCount()); + EXPECT_EQ(2, table_->GetFirstSelectedRow()); + + table_->ClearSelection(); + EXPECT_EQ(0, table_->SelectedRowCount()); + EXPECT_EQ(-1, table_->GetFirstSelectedRow()); +} + +// Test the row focus on a single-selection table. +TEST_F(TableView2Test, RowFocusTest) { + EXPECT_EQ(-1, table_->GetFirstFocusedRow()); + + table_->FocusRow(0); + EXPECT_EQ(0, table_->GetFirstFocusedRow()); + + table_->FocusRow(2); + EXPECT_EQ(2, table_->GetFirstFocusedRow()); + + table_->ClearRowFocus(); + EXPECT_EQ(-1, table_->GetFirstSelectedRow()); +} + +class CheckTableView2Test : public TableView2Test { + protected: + virtual views::TableTypes GetTableType() { + return views::CHECK_BOX_AND_TEXT; + } + + // Sets the row check state natively. + void SetRowCheckState(int row, bool state) { +#if defined(OS_WIN) + ListView_SetCheckState(table_->GetTestingHandle(), row, state); +#else + NOTIMPLEMENTED(); +#endif + } +}; + +TEST_F(CheckTableView2Test, TestCheckTable) { + // Test that we were notified of the initial check states. + ASSERT_EQ(1U, model_->check_notifications_.size()); + EXPECT_EQ(1, model_->check_notifications_[0].row); + + // Test that we get the notification correctly. + model_->check_notifications_.clear(); + SetRowCheckState(1, false); + SetRowCheckState(0, true); + SetRowCheckState(0, false); + ASSERT_LE(3U, model_->check_notifications_.size()); + EXPECT_EQ(1, model_->check_notifications_[0].row); + EXPECT_FALSE(model_->check_notifications_[0].state); + EXPECT_EQ(0, model_->check_notifications_[1].row); + EXPECT_TRUE(model_->check_notifications_[1].state); + EXPECT_EQ(0, model_->check_notifications_[2].row); + EXPECT_FALSE(model_->check_notifications_[2].state); +} |