| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/controls/combobox/combobox.h" | 5 #include "ui/views/controls/combobox/combobox.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 namespace views { | 28 namespace views { |
| 29 | 29 |
| 30 using test::ComboboxTestApi; | 30 using test::ComboboxTestApi; |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 // A wrapper of Combobox to intercept the result of OnKeyPressed() and | 34 // A wrapper of Combobox to intercept the result of OnKeyPressed() and |
| 35 // OnKeyReleased() methods. | 35 // OnKeyReleased() methods. |
| 36 class TestCombobox : public Combobox { | 36 class TestCombobox : public Combobox { |
| 37 public: | 37 public: |
| 38 explicit TestCombobox(ui::ComboboxModel* model) | 38 TestCombobox(ui::ComboboxModel* model, Combobox::Style style) |
| 39 : Combobox(model), | 39 : Combobox(model, style), key_handled_(false), key_received_(false) {} |
| 40 key_handled_(false), | |
| 41 key_received_(false) {} | |
| 42 | 40 |
| 43 bool OnKeyPressed(const ui::KeyEvent& e) override { | 41 bool OnKeyPressed(const ui::KeyEvent& e) override { |
| 44 key_received_ = true; | 42 key_received_ = true; |
| 45 key_handled_ = Combobox::OnKeyPressed(e); | 43 key_handled_ = Combobox::OnKeyPressed(e); |
| 46 return key_handled_; | 44 return key_handled_; |
| 47 } | 45 } |
| 48 | 46 |
| 49 bool OnKeyReleased(const ui::KeyEvent& e) override { | 47 bool OnKeyReleased(const ui::KeyEvent& e) override { |
| 50 key_received_ = true; | 48 key_received_ = true; |
| 51 key_handled_ = Combobox::OnKeyReleased(e); | 49 key_handled_ = Combobox::OnKeyReleased(e); |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 class ComboboxTest : public ViewsTestBase { | 186 class ComboboxTest : public ViewsTestBase { |
| 189 public: | 187 public: |
| 190 ComboboxTest() : widget_(NULL), combobox_(NULL) {} | 188 ComboboxTest() : widget_(NULL), combobox_(NULL) {} |
| 191 | 189 |
| 192 void TearDown() override { | 190 void TearDown() override { |
| 193 if (widget_) | 191 if (widget_) |
| 194 widget_->Close(); | 192 widget_->Close(); |
| 195 ViewsTestBase::TearDown(); | 193 ViewsTestBase::TearDown(); |
| 196 } | 194 } |
| 197 | 195 |
| 198 void InitCombobox(const std::set<int>* separators) { | 196 void InitCombobox(const std::set<int>* separators, Combobox::Style style) { |
| 199 model_.reset(new TestComboboxModel()); | 197 model_.reset(new TestComboboxModel()); |
| 200 | 198 |
| 201 if (separators) | 199 if (separators) |
| 202 model_->SetSeparators(*separators); | 200 model_->SetSeparators(*separators); |
| 203 | 201 |
| 204 ASSERT_FALSE(combobox_); | 202 ASSERT_FALSE(combobox_); |
| 205 combobox_ = new TestCombobox(model_.get()); | 203 combobox_ = new TestCombobox(model_.get(), style); |
| 206 test_api_.reset(new ComboboxTestApi(combobox_)); | 204 test_api_.reset(new ComboboxTestApi(combobox_)); |
| 207 combobox_->set_id(1); | 205 combobox_->set_id(1); |
| 208 | 206 |
| 209 widget_ = new Widget; | 207 widget_ = new Widget; |
| 210 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 208 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 211 params.bounds = gfx::Rect(200, 200, 200, 200); | 209 params.bounds = gfx::Rect(200, 200, 200, 200); |
| 212 widget_->Init(params); | 210 widget_->Init(params); |
| 213 View* container = new View(); | 211 View* container = new View(); |
| 214 widget_->SetContentsView(container); | 212 widget_->SetContentsView(container); |
| 215 container->AddChildView(combobox_); | 213 container->AddChildView(combobox_); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 std::unique_ptr<ComboboxTestApi> test_api_; | 252 std::unique_ptr<ComboboxTestApi> test_api_; |
| 255 | 253 |
| 256 // Combobox does not take ownership of the model, hence it needs to be scoped. | 254 // Combobox does not take ownership of the model, hence it needs to be scoped. |
| 257 std::unique_ptr<TestComboboxModel> model_; | 255 std::unique_ptr<TestComboboxModel> model_; |
| 258 | 256 |
| 259 private: | 257 private: |
| 260 DISALLOW_COPY_AND_ASSIGN(ComboboxTest); | 258 DISALLOW_COPY_AND_ASSIGN(ComboboxTest); |
| 261 }; | 259 }; |
| 262 | 260 |
| 263 TEST_F(ComboboxTest, KeyTest) { | 261 TEST_F(ComboboxTest, KeyTest) { |
| 264 InitCombobox(NULL); | 262 InitCombobox(nullptr, Combobox::STYLE_NORMAL); |
| 265 SendKeyEvent(ui::VKEY_END); | 263 SendKeyEvent(ui::VKEY_END); |
| 266 EXPECT_EQ(combobox_->selected_index() + 1, model_->GetItemCount()); | 264 EXPECT_EQ(combobox_->selected_index() + 1, model_->GetItemCount()); |
| 267 SendKeyEvent(ui::VKEY_HOME); | 265 SendKeyEvent(ui::VKEY_HOME); |
| 268 EXPECT_EQ(combobox_->selected_index(), 0); | 266 EXPECT_EQ(combobox_->selected_index(), 0); |
| 269 SendKeyEvent(ui::VKEY_DOWN); | 267 SendKeyEvent(ui::VKEY_DOWN); |
| 270 SendKeyEvent(ui::VKEY_DOWN); | 268 SendKeyEvent(ui::VKEY_DOWN); |
| 271 EXPECT_EQ(combobox_->selected_index(), 2); | 269 EXPECT_EQ(combobox_->selected_index(), 2); |
| 272 SendKeyEvent(ui::VKEY_RIGHT); | 270 SendKeyEvent(ui::VKEY_RIGHT); |
| 273 EXPECT_EQ(combobox_->selected_index(), 2); | 271 EXPECT_EQ(combobox_->selected_index(), 2); |
| 274 SendKeyEvent(ui::VKEY_LEFT); | 272 SendKeyEvent(ui::VKEY_LEFT); |
| 275 EXPECT_EQ(combobox_->selected_index(), 2); | 273 EXPECT_EQ(combobox_->selected_index(), 2); |
| 276 SendKeyEvent(ui::VKEY_UP); | 274 SendKeyEvent(ui::VKEY_UP); |
| 277 EXPECT_EQ(combobox_->selected_index(), 1); | 275 EXPECT_EQ(combobox_->selected_index(), 1); |
| 278 SendKeyEvent(ui::VKEY_PRIOR); | 276 SendKeyEvent(ui::VKEY_PRIOR); |
| 279 EXPECT_EQ(combobox_->selected_index(), 0); | 277 EXPECT_EQ(combobox_->selected_index(), 0); |
| 280 SendKeyEvent(ui::VKEY_NEXT); | 278 SendKeyEvent(ui::VKEY_NEXT); |
| 281 EXPECT_EQ(combobox_->selected_index(), model_->GetItemCount() - 1); | 279 EXPECT_EQ(combobox_->selected_index(), model_->GetItemCount() - 1); |
| 282 } | 280 } |
| 283 | 281 |
| 284 // Check that if a combobox is disabled before it has a native wrapper, then the | 282 // Check that if a combobox is disabled before it has a native wrapper, then the |
| 285 // native wrapper inherits the disabled state when it gets created. | 283 // native wrapper inherits the disabled state when it gets created. |
| 286 TEST_F(ComboboxTest, DisabilityTest) { | 284 TEST_F(ComboboxTest, DisabilityTest) { |
| 287 model_.reset(new TestComboboxModel()); | 285 model_.reset(new TestComboboxModel()); |
| 288 | 286 |
| 289 ASSERT_FALSE(combobox_); | 287 ASSERT_FALSE(combobox_); |
| 290 combobox_ = new TestCombobox(model_.get()); | 288 combobox_ = new TestCombobox(model_.get(), Combobox::STYLE_NORMAL); |
| 291 combobox_->SetEnabled(false); | 289 combobox_->SetEnabled(false); |
| 292 | 290 |
| 293 widget_ = new Widget; | 291 widget_ = new Widget; |
| 294 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 292 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 295 params.bounds = gfx::Rect(100, 100, 100, 100); | 293 params.bounds = gfx::Rect(100, 100, 100, 100); |
| 296 widget_->Init(params); | 294 widget_->Init(params); |
| 297 View* container = new View(); | 295 View* container = new View(); |
| 298 widget_->SetContentsView(container); | 296 widget_->SetContentsView(container); |
| 299 container->AddChildView(combobox_); | 297 container->AddChildView(combobox_); |
| 300 EXPECT_FALSE(combobox_->enabled()); | 298 EXPECT_FALSE(combobox_->enabled()); |
| 301 } | 299 } |
| 302 | 300 |
| 303 // Verifies that we don't select a separator line in combobox when navigating | 301 // Verifies that we don't select a separator line in combobox when navigating |
| 304 // through keyboard. | 302 // through keyboard. |
| 305 TEST_F(ComboboxTest, SkipSeparatorSimple) { | 303 TEST_F(ComboboxTest, SkipSeparatorSimple) { |
| 306 std::set<int> separators; | 304 std::set<int> separators; |
| 307 separators.insert(2); | 305 separators.insert(2); |
| 308 InitCombobox(&separators); | 306 InitCombobox(&separators, Combobox::STYLE_NORMAL); |
| 309 EXPECT_EQ(0, combobox_->selected_index()); | 307 EXPECT_EQ(0, combobox_->selected_index()); |
| 310 SendKeyEvent(ui::VKEY_DOWN); | 308 SendKeyEvent(ui::VKEY_DOWN); |
| 311 EXPECT_EQ(1, combobox_->selected_index()); | 309 EXPECT_EQ(1, combobox_->selected_index()); |
| 312 SendKeyEvent(ui::VKEY_DOWN); | 310 SendKeyEvent(ui::VKEY_DOWN); |
| 313 EXPECT_EQ(3, combobox_->selected_index()); | 311 EXPECT_EQ(3, combobox_->selected_index()); |
| 314 SendKeyEvent(ui::VKEY_UP); | 312 SendKeyEvent(ui::VKEY_UP); |
| 315 EXPECT_EQ(1, combobox_->selected_index()); | 313 EXPECT_EQ(1, combobox_->selected_index()); |
| 316 SendKeyEvent(ui::VKEY_HOME); | 314 SendKeyEvent(ui::VKEY_HOME); |
| 317 EXPECT_EQ(0, combobox_->selected_index()); | 315 EXPECT_EQ(0, combobox_->selected_index()); |
| 318 SendKeyEvent(ui::VKEY_PRIOR); | 316 SendKeyEvent(ui::VKEY_PRIOR); |
| 319 EXPECT_EQ(0, combobox_->selected_index()); | 317 EXPECT_EQ(0, combobox_->selected_index()); |
| 320 SendKeyEvent(ui::VKEY_END); | 318 SendKeyEvent(ui::VKEY_END); |
| 321 EXPECT_EQ(9, combobox_->selected_index()); | 319 EXPECT_EQ(9, combobox_->selected_index()); |
| 322 } | 320 } |
| 323 | 321 |
| 324 // Verifies that we never select the separator that is in the beginning of the | 322 // Verifies that we never select the separator that is in the beginning of the |
| 325 // combobox list when navigating through keyboard. | 323 // combobox list when navigating through keyboard. |
| 326 TEST_F(ComboboxTest, SkipSeparatorBeginning) { | 324 TEST_F(ComboboxTest, SkipSeparatorBeginning) { |
| 327 std::set<int> separators; | 325 std::set<int> separators; |
| 328 separators.insert(0); | 326 separators.insert(0); |
| 329 InitCombobox(&separators); | 327 InitCombobox(&separators, Combobox::STYLE_NORMAL); |
| 330 EXPECT_EQ(1, combobox_->selected_index()); | 328 EXPECT_EQ(1, combobox_->selected_index()); |
| 331 SendKeyEvent(ui::VKEY_DOWN); | 329 SendKeyEvent(ui::VKEY_DOWN); |
| 332 EXPECT_EQ(2, combobox_->selected_index()); | 330 EXPECT_EQ(2, combobox_->selected_index()); |
| 333 SendKeyEvent(ui::VKEY_DOWN); | 331 SendKeyEvent(ui::VKEY_DOWN); |
| 334 EXPECT_EQ(3, combobox_->selected_index()); | 332 EXPECT_EQ(3, combobox_->selected_index()); |
| 335 SendKeyEvent(ui::VKEY_UP); | 333 SendKeyEvent(ui::VKEY_UP); |
| 336 EXPECT_EQ(2, combobox_->selected_index()); | 334 EXPECT_EQ(2, combobox_->selected_index()); |
| 337 SendKeyEvent(ui::VKEY_HOME); | 335 SendKeyEvent(ui::VKEY_HOME); |
| 338 EXPECT_EQ(1, combobox_->selected_index()); | 336 EXPECT_EQ(1, combobox_->selected_index()); |
| 339 SendKeyEvent(ui::VKEY_PRIOR); | 337 SendKeyEvent(ui::VKEY_PRIOR); |
| 340 EXPECT_EQ(1, combobox_->selected_index()); | 338 EXPECT_EQ(1, combobox_->selected_index()); |
| 341 SendKeyEvent(ui::VKEY_END); | 339 SendKeyEvent(ui::VKEY_END); |
| 342 EXPECT_EQ(9, combobox_->selected_index()); | 340 EXPECT_EQ(9, combobox_->selected_index()); |
| 343 } | 341 } |
| 344 | 342 |
| 345 // Verifies that we never select the separator that is in the end of the | 343 // Verifies that we never select the separator that is in the end of the |
| 346 // combobox list when navigating through keyboard. | 344 // combobox list when navigating through keyboard. |
| 347 TEST_F(ComboboxTest, SkipSeparatorEnd) { | 345 TEST_F(ComboboxTest, SkipSeparatorEnd) { |
| 348 std::set<int> separators; | 346 std::set<int> separators; |
| 349 separators.insert(TestComboboxModel::kItemCount - 1); | 347 separators.insert(TestComboboxModel::kItemCount - 1); |
| 350 InitCombobox(&separators); | 348 InitCombobox(&separators, Combobox::STYLE_NORMAL); |
| 351 combobox_->SetSelectedIndex(8); | 349 combobox_->SetSelectedIndex(8); |
| 352 SendKeyEvent(ui::VKEY_DOWN); | 350 SendKeyEvent(ui::VKEY_DOWN); |
| 353 EXPECT_EQ(8, combobox_->selected_index()); | 351 EXPECT_EQ(8, combobox_->selected_index()); |
| 354 SendKeyEvent(ui::VKEY_UP); | 352 SendKeyEvent(ui::VKEY_UP); |
| 355 EXPECT_EQ(7, combobox_->selected_index()); | 353 EXPECT_EQ(7, combobox_->selected_index()); |
| 356 SendKeyEvent(ui::VKEY_END); | 354 SendKeyEvent(ui::VKEY_END); |
| 357 EXPECT_EQ(8, combobox_->selected_index()); | 355 EXPECT_EQ(8, combobox_->selected_index()); |
| 358 } | 356 } |
| 359 | 357 |
| 360 // Verifies that we never select any of the adjacent separators (multiple | 358 // Verifies that we never select any of the adjacent separators (multiple |
| 361 // consecutive) that appear in the beginning of the combobox list when | 359 // consecutive) that appear in the beginning of the combobox list when |
| 362 // navigating through keyboard. | 360 // navigating through keyboard. |
| 363 TEST_F(ComboboxTest, SkipMultipleSeparatorsAtBeginning) { | 361 TEST_F(ComboboxTest, SkipMultipleSeparatorsAtBeginning) { |
| 364 std::set<int> separators; | 362 std::set<int> separators; |
| 365 separators.insert(0); | 363 separators.insert(0); |
| 366 separators.insert(1); | 364 separators.insert(1); |
| 367 separators.insert(2); | 365 separators.insert(2); |
| 368 InitCombobox(&separators); | 366 InitCombobox(&separators, Combobox::STYLE_NORMAL); |
| 369 EXPECT_EQ(3, combobox_->selected_index()); | 367 EXPECT_EQ(3, combobox_->selected_index()); |
| 370 SendKeyEvent(ui::VKEY_DOWN); | 368 SendKeyEvent(ui::VKEY_DOWN); |
| 371 EXPECT_EQ(4, combobox_->selected_index()); | 369 EXPECT_EQ(4, combobox_->selected_index()); |
| 372 SendKeyEvent(ui::VKEY_UP); | 370 SendKeyEvent(ui::VKEY_UP); |
| 373 EXPECT_EQ(3, combobox_->selected_index()); | 371 EXPECT_EQ(3, combobox_->selected_index()); |
| 374 SendKeyEvent(ui::VKEY_NEXT); | 372 SendKeyEvent(ui::VKEY_NEXT); |
| 375 EXPECT_EQ(9, combobox_->selected_index()); | 373 EXPECT_EQ(9, combobox_->selected_index()); |
| 376 SendKeyEvent(ui::VKEY_HOME); | 374 SendKeyEvent(ui::VKEY_HOME); |
| 377 EXPECT_EQ(3, combobox_->selected_index()); | 375 EXPECT_EQ(3, combobox_->selected_index()); |
| 378 SendKeyEvent(ui::VKEY_END); | 376 SendKeyEvent(ui::VKEY_END); |
| 379 EXPECT_EQ(9, combobox_->selected_index()); | 377 EXPECT_EQ(9, combobox_->selected_index()); |
| 380 SendKeyEvent(ui::VKEY_PRIOR); | 378 SendKeyEvent(ui::VKEY_PRIOR); |
| 381 EXPECT_EQ(3, combobox_->selected_index()); | 379 EXPECT_EQ(3, combobox_->selected_index()); |
| 382 } | 380 } |
| 383 | 381 |
| 384 // Verifies that we never select any of the adjacent separators (multiple | 382 // Verifies that we never select any of the adjacent separators (multiple |
| 385 // consecutive) that appear in the middle of the combobox list when navigating | 383 // consecutive) that appear in the middle of the combobox list when navigating |
| 386 // through keyboard. | 384 // through keyboard. |
| 387 TEST_F(ComboboxTest, SkipMultipleAdjacentSeparatorsAtMiddle) { | 385 TEST_F(ComboboxTest, SkipMultipleAdjacentSeparatorsAtMiddle) { |
| 388 std::set<int> separators; | 386 std::set<int> separators; |
| 389 separators.insert(4); | 387 separators.insert(4); |
| 390 separators.insert(5); | 388 separators.insert(5); |
| 391 separators.insert(6); | 389 separators.insert(6); |
| 392 InitCombobox(&separators); | 390 InitCombobox(&separators, Combobox::STYLE_NORMAL); |
| 393 combobox_->SetSelectedIndex(3); | 391 combobox_->SetSelectedIndex(3); |
| 394 SendKeyEvent(ui::VKEY_DOWN); | 392 SendKeyEvent(ui::VKEY_DOWN); |
| 395 EXPECT_EQ(7, combobox_->selected_index()); | 393 EXPECT_EQ(7, combobox_->selected_index()); |
| 396 SendKeyEvent(ui::VKEY_UP); | 394 SendKeyEvent(ui::VKEY_UP); |
| 397 EXPECT_EQ(3, combobox_->selected_index()); | 395 EXPECT_EQ(3, combobox_->selected_index()); |
| 398 } | 396 } |
| 399 | 397 |
| 400 // Verifies that we never select any of the adjacent separators (multiple | 398 // Verifies that we never select any of the adjacent separators (multiple |
| 401 // consecutive) that appear in the end of the combobox list when navigating | 399 // consecutive) that appear in the end of the combobox list when navigating |
| 402 // through keyboard. | 400 // through keyboard. |
| 403 TEST_F(ComboboxTest, SkipMultipleSeparatorsAtEnd) { | 401 TEST_F(ComboboxTest, SkipMultipleSeparatorsAtEnd) { |
| 404 std::set<int> separators; | 402 std::set<int> separators; |
| 405 separators.insert(7); | 403 separators.insert(7); |
| 406 separators.insert(8); | 404 separators.insert(8); |
| 407 separators.insert(9); | 405 separators.insert(9); |
| 408 InitCombobox(&separators); | 406 InitCombobox(&separators, Combobox::STYLE_NORMAL); |
| 409 combobox_->SetSelectedIndex(6); | 407 combobox_->SetSelectedIndex(6); |
| 410 SendKeyEvent(ui::VKEY_DOWN); | 408 SendKeyEvent(ui::VKEY_DOWN); |
| 411 EXPECT_EQ(6, combobox_->selected_index()); | 409 EXPECT_EQ(6, combobox_->selected_index()); |
| 412 SendKeyEvent(ui::VKEY_UP); | 410 SendKeyEvent(ui::VKEY_UP); |
| 413 EXPECT_EQ(5, combobox_->selected_index()); | 411 EXPECT_EQ(5, combobox_->selected_index()); |
| 414 SendKeyEvent(ui::VKEY_HOME); | 412 SendKeyEvent(ui::VKEY_HOME); |
| 415 EXPECT_EQ(0, combobox_->selected_index()); | 413 EXPECT_EQ(0, combobox_->selected_index()); |
| 416 SendKeyEvent(ui::VKEY_NEXT); | 414 SendKeyEvent(ui::VKEY_NEXT); |
| 417 EXPECT_EQ(6, combobox_->selected_index()); | 415 EXPECT_EQ(6, combobox_->selected_index()); |
| 418 SendKeyEvent(ui::VKEY_PRIOR); | 416 SendKeyEvent(ui::VKEY_PRIOR); |
| 419 EXPECT_EQ(0, combobox_->selected_index()); | 417 EXPECT_EQ(0, combobox_->selected_index()); |
| 420 SendKeyEvent(ui::VKEY_END); | 418 SendKeyEvent(ui::VKEY_END); |
| 421 EXPECT_EQ(6, combobox_->selected_index()); | 419 EXPECT_EQ(6, combobox_->selected_index()); |
| 422 } | 420 } |
| 423 | 421 |
| 424 TEST_F(ComboboxTest, GetTextForRowTest) { | 422 TEST_F(ComboboxTest, GetTextForRowTest) { |
| 425 std::set<int> separators; | 423 std::set<int> separators; |
| 426 separators.insert(0); | 424 separators.insert(0); |
| 427 separators.insert(1); | 425 separators.insert(1); |
| 428 separators.insert(9); | 426 separators.insert(9); |
| 429 InitCombobox(&separators); | 427 InitCombobox(&separators, Combobox::STYLE_NORMAL); |
| 430 for (int i = 0; i < combobox_->GetRowCount(); ++i) { | 428 for (int i = 0; i < combobox_->GetRowCount(); ++i) { |
| 431 if (separators.count(i) != 0) { | 429 if (separators.count(i) != 0) { |
| 432 EXPECT_TRUE(combobox_->GetTextForRow(i).empty()) << i; | 430 EXPECT_TRUE(combobox_->GetTextForRow(i).empty()) << i; |
| 433 } else { | 431 } else { |
| 434 EXPECT_EQ(ASCIIToUTF16(i % 2 == 0 ? "PEANUT BUTTER" : "JELLY"), | 432 EXPECT_EQ(ASCIIToUTF16(i % 2 == 0 ? "PEANUT BUTTER" : "JELLY"), |
| 435 combobox_->GetTextForRow(i)) << i; | 433 combobox_->GetTextForRow(i)) << i; |
| 436 } | 434 } |
| 437 } | 435 } |
| 438 } | 436 } |
| 439 | 437 |
| 440 // Verifies selecting the first matching value (and returning whether found). | 438 // Verifies selecting the first matching value (and returning whether found). |
| 441 TEST_F(ComboboxTest, SelectValue) { | 439 TEST_F(ComboboxTest, SelectValue) { |
| 442 InitCombobox(NULL); | 440 InitCombobox(nullptr, Combobox::STYLE_NORMAL); |
| 443 ASSERT_EQ(model_->GetDefaultIndex(), combobox_->selected_index()); | 441 ASSERT_EQ(model_->GetDefaultIndex(), combobox_->selected_index()); |
| 444 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("PEANUT BUTTER"))); | 442 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("PEANUT BUTTER"))); |
| 445 EXPECT_EQ(0, combobox_->selected_index()); | 443 EXPECT_EQ(0, combobox_->selected_index()); |
| 446 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("JELLY"))); | 444 EXPECT_TRUE(combobox_->SelectValue(ASCIIToUTF16("JELLY"))); |
| 447 EXPECT_EQ(1, combobox_->selected_index()); | 445 EXPECT_EQ(1, combobox_->selected_index()); |
| 448 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("BANANAS"))); | 446 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("BANANAS"))); |
| 449 EXPECT_EQ(1, combobox_->selected_index()); | 447 EXPECT_EQ(1, combobox_->selected_index()); |
| 448 } |
| 450 | 449 |
| 450 TEST_F(ComboboxTest, SelectValueActionStyle) { |
| 451 // With the action style, the selected index is always 0. | 451 // With the action style, the selected index is always 0. |
| 452 combobox_->SetStyle(Combobox::STYLE_ACTION); | 452 InitCombobox(nullptr, Combobox::STYLE_ACTION); |
| 453 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("PEANUT BUTTER"))); | 453 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("PEANUT BUTTER"))); |
| 454 EXPECT_EQ(0, combobox_->selected_index()); | 454 EXPECT_EQ(0, combobox_->selected_index()); |
| 455 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("JELLY"))); | 455 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("JELLY"))); |
| 456 EXPECT_EQ(0, combobox_->selected_index()); | 456 EXPECT_EQ(0, combobox_->selected_index()); |
| 457 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("BANANAS"))); | 457 EXPECT_FALSE(combobox_->SelectValue(ASCIIToUTF16("BANANAS"))); |
| 458 EXPECT_EQ(0, combobox_->selected_index()); | 458 EXPECT_EQ(0, combobox_->selected_index()); |
| 459 } | 459 } |
| 460 | 460 |
| 461 TEST_F(ComboboxTest, SelectIndexActionStyle) { | 461 TEST_F(ComboboxTest, SelectIndexActionStyle) { |
| 462 InitCombobox(NULL); | |
| 463 | |
| 464 // With the action style, the selected index is always 0. | 462 // With the action style, the selected index is always 0. |
| 465 combobox_->SetStyle(Combobox::STYLE_ACTION); | 463 InitCombobox(nullptr, Combobox::STYLE_ACTION); |
| 466 combobox_->SetSelectedIndex(1); | 464 combobox_->SetSelectedIndex(1); |
| 467 EXPECT_EQ(0, combobox_->selected_index()); | 465 EXPECT_EQ(0, combobox_->selected_index()); |
| 468 combobox_->SetSelectedIndex(2); | 466 combobox_->SetSelectedIndex(2); |
| 469 EXPECT_EQ(0, combobox_->selected_index()); | 467 EXPECT_EQ(0, combobox_->selected_index()); |
| 470 combobox_->SetSelectedIndex(3); | 468 combobox_->SetSelectedIndex(3); |
| 471 EXPECT_EQ(0, combobox_->selected_index()); | 469 EXPECT_EQ(0, combobox_->selected_index()); |
| 472 } | 470 } |
| 473 | 471 |
| 474 TEST_F(ComboboxTest, ListenerHandlesDelete) { | 472 TEST_F(ComboboxTest, ListenerHandlesDelete) { |
| 475 TestComboboxModel model; | 473 TestComboboxModel model; |
| 476 | 474 |
| 477 // |combobox| will be deleted on change. | 475 // |combobox| will be deleted on change. |
| 478 TestCombobox* combobox = new TestCombobox(&model); | 476 TestCombobox* combobox = new TestCombobox(&model, Combobox::STYLE_NORMAL); |
| 479 std::unique_ptr<EvilListener> evil_listener(new EvilListener()); | 477 std::unique_ptr<EvilListener> evil_listener(new EvilListener()); |
| 480 combobox->set_listener(evil_listener.get()); | 478 combobox->set_listener(evil_listener.get()); |
| 481 ASSERT_NO_FATAL_FAILURE(ComboboxTestApi(combobox).PerformActionAt(2)); | 479 ASSERT_NO_FATAL_FAILURE(ComboboxTestApi(combobox).PerformActionAt(2)); |
| 482 EXPECT_TRUE(evil_listener->deleted()); | 480 EXPECT_TRUE(evil_listener->deleted()); |
| 483 | 481 |
| 484 // With STYLE_ACTION | 482 // With STYLE_ACTION |
| 485 // |combobox| will be deleted on change. | 483 // |combobox| will be deleted on change. |
| 486 combobox = new TestCombobox(&model); | 484 combobox = new TestCombobox(&model, Combobox::STYLE_ACTION); |
| 487 evil_listener.reset(new EvilListener()); | 485 evil_listener.reset(new EvilListener()); |
| 488 combobox->set_listener(evil_listener.get()); | 486 combobox->set_listener(evil_listener.get()); |
| 489 combobox->SetStyle(Combobox::STYLE_ACTION); | |
| 490 ASSERT_NO_FATAL_FAILURE(ComboboxTestApi(combobox).PerformActionAt(2)); | 487 ASSERT_NO_FATAL_FAILURE(ComboboxTestApi(combobox).PerformActionAt(2)); |
| 491 EXPECT_TRUE(evil_listener->deleted()); | 488 EXPECT_TRUE(evil_listener->deleted()); |
| 492 } | 489 } |
| 493 | 490 |
| 494 TEST_F(ComboboxTest, Click) { | 491 TEST_F(ComboboxTest, Click) { |
| 495 InitCombobox(NULL); | 492 InitCombobox(nullptr, Combobox::STYLE_NORMAL); |
| 496 | 493 |
| 497 TestComboboxListener listener; | 494 TestComboboxListener listener; |
| 498 combobox_->set_listener(&listener); | 495 combobox_->set_listener(&listener); |
| 499 | 496 |
| 500 combobox_->Layout(); | 497 combobox_->Layout(); |
| 501 int menu_show_count = 0; | 498 int menu_show_count = 0; |
| 502 test_api_->InstallTestMenuRunner(&menu_show_count); | 499 test_api_->InstallTestMenuRunner(&menu_show_count); |
| 503 | 500 |
| 504 // Click the left side. The menu is shown. | 501 // Click the left side. The menu is shown. |
| 505 EXPECT_EQ(0, menu_show_count); | 502 EXPECT_EQ(0, menu_show_count); |
| 506 PerformClick(gfx::Point(combobox_->x() + 1, | 503 PerformClick(gfx::Point(combobox_->x() + 1, |
| 507 combobox_->y() + combobox_->height() / 2)); | 504 combobox_->y() + combobox_->height() / 2)); |
| 508 EXPECT_FALSE(listener.on_perform_action_called()); | 505 EXPECT_FALSE(listener.on_perform_action_called()); |
| 509 EXPECT_EQ(1, menu_show_count); | 506 EXPECT_EQ(1, menu_show_count); |
| 510 } | 507 } |
| 511 | 508 |
| 512 TEST_F(ComboboxTest, ClickButDisabled) { | 509 TEST_F(ComboboxTest, ClickButDisabled) { |
| 513 InitCombobox(NULL); | 510 InitCombobox(nullptr, Combobox::STYLE_NORMAL); |
| 514 | 511 |
| 515 TestComboboxListener listener; | 512 TestComboboxListener listener; |
| 516 combobox_->set_listener(&listener); | 513 combobox_->set_listener(&listener); |
| 517 | 514 |
| 518 combobox_->Layout(); | 515 combobox_->Layout(); |
| 519 combobox_->SetEnabled(false); | 516 combobox_->SetEnabled(false); |
| 520 | 517 |
| 521 // Click the left side, but nothing happens since the combobox is disabled. | 518 // Click the left side, but nothing happens since the combobox is disabled. |
| 522 int menu_show_count = 0; | 519 int menu_show_count = 0; |
| 523 test_api_->InstallTestMenuRunner(&menu_show_count); | 520 test_api_->InstallTestMenuRunner(&menu_show_count); |
| 524 PerformClick(gfx::Point(combobox_->x() + 1, | 521 PerformClick(gfx::Point(combobox_->x() + 1, |
| 525 combobox_->y() + combobox_->height() / 2)); | 522 combobox_->y() + combobox_->height() / 2)); |
| 526 EXPECT_FALSE(listener.on_perform_action_called()); | 523 EXPECT_FALSE(listener.on_perform_action_called()); |
| 527 EXPECT_EQ(0, menu_show_count); | 524 EXPECT_EQ(0, menu_show_count); |
| 528 } | 525 } |
| 529 | 526 |
| 530 TEST_F(ComboboxTest, NotifyOnClickWithReturnKey) { | 527 TEST_F(ComboboxTest, NotifyOnClickWithReturnKey) { |
| 531 InitCombobox(NULL); | 528 InitCombobox(nullptr, Combobox::STYLE_NORMAL); |
| 532 | 529 |
| 533 TestComboboxListener listener; | 530 TestComboboxListener listener; |
| 534 combobox_->set_listener(&listener); | 531 combobox_->set_listener(&listener); |
| 535 | 532 |
| 536 // With STYLE_NORMAL, the click event is ignored. | 533 // With STYLE_NORMAL, the click event is ignored. |
| 537 SendKeyEvent(ui::VKEY_RETURN); | 534 SendKeyEvent(ui::VKEY_RETURN); |
| 538 EXPECT_FALSE(listener.on_perform_action_called()); | 535 EXPECT_FALSE(listener.on_perform_action_called()); |
| 536 } |
| 537 |
| 538 TEST_F(ComboboxTest, NotifyOnClickWithReturnKeyActionStyle) { |
| 539 InitCombobox(nullptr, Combobox::STYLE_ACTION); |
| 540 |
| 541 TestComboboxListener listener; |
| 542 combobox_->set_listener(&listener); |
| 539 | 543 |
| 540 // With STYLE_ACTION, the click event is notified. | 544 // With STYLE_ACTION, the click event is notified. |
| 541 combobox_->SetStyle(Combobox::STYLE_ACTION); | |
| 542 SendKeyEvent(ui::VKEY_RETURN); | 545 SendKeyEvent(ui::VKEY_RETURN); |
| 543 EXPECT_TRUE(listener.on_perform_action_called()); | 546 EXPECT_TRUE(listener.on_perform_action_called()); |
| 544 EXPECT_EQ(0, listener.perform_action_index()); | 547 EXPECT_EQ(0, listener.perform_action_index()); |
| 545 } | 548 } |
| 546 | 549 |
| 547 TEST_F(ComboboxTest, NotifyOnClickWithSpaceKey) { | 550 TEST_F(ComboboxTest, NotifyOnClickWithSpaceKey) { |
| 548 InitCombobox(NULL); | 551 InitCombobox(nullptr, Combobox::STYLE_NORMAL); |
| 549 | 552 |
| 550 TestComboboxListener listener; | 553 TestComboboxListener listener; |
| 551 combobox_->set_listener(&listener); | 554 combobox_->set_listener(&listener); |
| 552 | 555 |
| 553 // With STYLE_NORMAL, the click event is ignored. | 556 // With STYLE_NORMAL, the click event is ignored. |
| 554 SendKeyEvent(ui::VKEY_SPACE); | 557 SendKeyEvent(ui::VKEY_SPACE); |
| 555 EXPECT_FALSE(listener.on_perform_action_called()); | 558 EXPECT_FALSE(listener.on_perform_action_called()); |
| 556 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); | 559 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); |
| 557 EXPECT_FALSE(listener.on_perform_action_called()); | 560 EXPECT_FALSE(listener.on_perform_action_called()); |
| 561 } |
| 562 |
| 563 TEST_F(ComboboxTest, NotifyOnClickWithSpaceKeyActionStyle) { |
| 564 InitCombobox(nullptr, Combobox::STYLE_ACTION); |
| 565 |
| 566 TestComboboxListener listener; |
| 567 combobox_->set_listener(&listener); |
| 558 | 568 |
| 559 // With STYLE_ACTION, the click event is notified after releasing. | 569 // With STYLE_ACTION, the click event is notified after releasing. |
| 560 combobox_->SetStyle(Combobox::STYLE_ACTION); | |
| 561 SendKeyEvent(ui::VKEY_SPACE); | 570 SendKeyEvent(ui::VKEY_SPACE); |
| 562 EXPECT_FALSE(listener.on_perform_action_called()); | 571 EXPECT_FALSE(listener.on_perform_action_called()); |
| 563 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); | 572 SendKeyEventWithType(ui::VKEY_SPACE, ui::ET_KEY_RELEASED); |
| 564 EXPECT_TRUE(listener.on_perform_action_called()); | 573 EXPECT_TRUE(listener.on_perform_action_called()); |
| 565 EXPECT_EQ(0, listener.perform_action_index()); | 574 EXPECT_EQ(0, listener.perform_action_index()); |
| 566 } | 575 } |
| 567 | 576 |
| 568 TEST_F(ComboboxTest, NotifyOnClickWithMouse) { | 577 TEST_F(ComboboxTest, NotifyOnClickWithMouse) { |
| 569 InitCombobox(NULL); | 578 InitCombobox(nullptr, Combobox::STYLE_ACTION); |
| 570 | 579 |
| 571 TestComboboxListener listener; | 580 TestComboboxListener listener; |
| 572 combobox_->set_listener(&listener); | 581 combobox_->set_listener(&listener); |
| 573 | 582 |
| 574 combobox_->SetStyle(Combobox::STYLE_ACTION); | |
| 575 combobox_->Layout(); | 583 combobox_->Layout(); |
| 576 | 584 |
| 577 // Click the right side (arrow button). The menu is shown. | 585 // Click the right side (arrow button). The menu is shown. |
| 578 int menu_show_count = 0; | 586 int menu_show_count = 0; |
| 579 test_api_->InstallTestMenuRunner(&menu_show_count); | 587 test_api_->InstallTestMenuRunner(&menu_show_count); |
| 580 EXPECT_EQ(0, menu_show_count); | 588 EXPECT_EQ(0, menu_show_count); |
| 581 PerformClick(gfx::Point(combobox_->x() + combobox_->width() - 1, | 589 PerformClick(gfx::Point(combobox_->x() + combobox_->width() - 1, |
| 582 combobox_->y() + combobox_->height() / 2)); | 590 combobox_->y() + combobox_->height() / 2)); |
| 583 EXPECT_FALSE(listener.on_perform_action_called()); | 591 EXPECT_FALSE(listener.on_perform_action_called()); |
| 584 EXPECT_EQ(1, menu_show_count); | 592 EXPECT_EQ(1, menu_show_count); |
| 585 | 593 |
| 586 // Click the left side (text button). The click event is notified. | 594 // Click the left side (text button). The click event is notified. |
| 587 test_api_->InstallTestMenuRunner(&menu_show_count); | 595 test_api_->InstallTestMenuRunner(&menu_show_count); |
| 588 PerformClick(gfx::Point(combobox_->x() + 1, | 596 PerformClick(gfx::Point(combobox_->x() + 1, |
| 589 combobox_->y() + combobox_->height() / 2)); | 597 combobox_->y() + combobox_->height() / 2)); |
| 590 EXPECT_TRUE(listener.on_perform_action_called()); | 598 EXPECT_TRUE(listener.on_perform_action_called()); |
| 591 EXPECT_EQ(1, menu_show_count); // Unchanged. | 599 EXPECT_EQ(1, menu_show_count); // Unchanged. |
| 592 EXPECT_EQ(0, listener.perform_action_index()); | 600 EXPECT_EQ(0, listener.perform_action_index()); |
| 593 } | 601 } |
| 594 | 602 |
| 595 TEST_F(ComboboxTest, ConsumingPressKeyEvents) { | 603 TEST_F(ComboboxTest, ConsumingPressKeyEvents) { |
| 596 InitCombobox(NULL); | 604 InitCombobox(nullptr, Combobox::STYLE_NORMAL); |
| 597 | 605 |
| 598 EXPECT_FALSE(combobox_->OnKeyPressed( | 606 EXPECT_FALSE(combobox_->OnKeyPressed( |
| 599 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE))); | 607 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE))); |
| 600 EXPECT_FALSE(combobox_->OnKeyPressed( | 608 EXPECT_FALSE(combobox_->OnKeyPressed( |
| 601 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, ui::EF_NONE))); | 609 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, ui::EF_NONE))); |
| 610 } |
| 602 | 611 |
| 612 TEST_F(ComboboxTest, ConsumingPressKeyEventsActionStyle) { |
| 613 InitCombobox(nullptr, Combobox::STYLE_ACTION); |
| 603 // When the combobox's style is STYLE_ACTION, pressing events of a space key | 614 // When the combobox's style is STYLE_ACTION, pressing events of a space key |
| 604 // or an enter key will be consumed. | 615 // or an enter key will be consumed. |
| 605 combobox_->SetStyle(Combobox::STYLE_ACTION); | |
| 606 EXPECT_TRUE(combobox_->OnKeyPressed( | 616 EXPECT_TRUE(combobox_->OnKeyPressed( |
| 607 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE))); | 617 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE))); |
| 608 EXPECT_TRUE(combobox_->OnKeyPressed( | 618 EXPECT_TRUE(combobox_->OnKeyPressed( |
| 609 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, ui::EF_NONE))); | 619 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, ui::EF_NONE))); |
| 610 } | 620 } |
| 611 | 621 |
| 612 TEST_F(ComboboxTest, ContentWidth) { | 622 TEST_F(ComboboxTest, ContentWidth) { |
| 613 std::vector<std::string> values; | 623 std::vector<std::string> values; |
| 614 VectorComboboxModel model(&values); | 624 VectorComboboxModel model(&values); |
| 615 TestCombobox combobox(&model); | 625 TestCombobox combobox(&model, Combobox::STYLE_NORMAL); |
| 626 TestCombobox action_combobox(&model, Combobox::STYLE_ACTION); |
| 616 ComboboxTestApi test_api(&combobox); | 627 ComboboxTestApi test_api(&combobox); |
| 628 ComboboxTestApi action_test_api(&action_combobox); |
| 617 | 629 |
| 618 std::string long_item = "this is the long item"; | 630 std::string long_item = "this is the long item"; |
| 619 std::string short_item = "s"; | 631 std::string short_item = "s"; |
| 620 | 632 |
| 621 values.resize(1); | 633 values.resize(1); |
| 622 values[0] = long_item; | 634 values[0] = long_item; |
| 623 combobox.ModelChanged(); | 635 combobox.ModelChanged(); |
| 636 action_combobox.ModelChanged(); |
| 624 | 637 |
| 625 const int long_item_width = test_api.content_size().width(); | 638 const int long_item_width = test_api.content_size().width(); |
| 626 | 639 |
| 627 values[0] = short_item; | 640 values[0] = short_item; |
| 628 combobox.ModelChanged(); | 641 combobox.ModelChanged(); |
| 642 action_combobox.ModelChanged(); |
| 629 | 643 |
| 630 const int short_item_width = test_api.content_size().width(); | 644 const int short_item_width = test_api.content_size().width(); |
| 631 | 645 |
| 632 values.resize(2); | 646 values.resize(2); |
| 633 values[0] = short_item; | 647 values[0] = short_item; |
| 634 values[1] = long_item; | 648 values[1] = long_item; |
| 635 combobox.ModelChanged(); | 649 combobox.ModelChanged(); |
| 650 action_combobox.ModelChanged(); |
| 636 | 651 |
| 637 // When the style is STYLE_NORMAL, the width will fit with the longest item. | 652 // When the style is STYLE_NORMAL, the width will fit with the longest item. |
| 638 combobox.SetStyle(Combobox::STYLE_NORMAL); | |
| 639 EXPECT_EQ(long_item_width, test_api.content_size().width()); | 653 EXPECT_EQ(long_item_width, test_api.content_size().width()); |
| 640 | 654 |
| 641 // When the style is STYLE_ACTION, the width will fit with the selected item's | 655 // When the style is STYLE_ACTION, the width will fit with the selected item's |
| 642 // width. | 656 // width. |
| 643 combobox.SetStyle(Combobox::STYLE_ACTION); | 657 EXPECT_EQ(short_item_width, action_test_api.content_size().width()); |
| 644 EXPECT_EQ(short_item_width, test_api.content_size().width()); | |
| 645 } | 658 } |
| 646 | 659 |
| 647 // Test that model updates preserve the selected index, so long as it is in | 660 // Test that model updates preserve the selected index, so long as it is in |
| 648 // range. | 661 // range. |
| 649 TEST_F(ComboboxTest, ModelChanged) { | 662 TEST_F(ComboboxTest, ModelChanged) { |
| 650 InitCombobox(nullptr); | 663 InitCombobox(nullptr, Combobox::STYLE_NORMAL); |
| 651 | 664 |
| 652 EXPECT_EQ(0, combobox_->GetSelectedRow()); | 665 EXPECT_EQ(0, combobox_->GetSelectedRow()); |
| 653 EXPECT_EQ(10, combobox_->GetRowCount()); | 666 EXPECT_EQ(10, combobox_->GetRowCount()); |
| 654 | 667 |
| 655 combobox_->SetSelectedIndex(4); | 668 combobox_->SetSelectedIndex(4); |
| 656 EXPECT_EQ(4, combobox_->GetSelectedRow()); | 669 EXPECT_EQ(4, combobox_->GetSelectedRow()); |
| 657 | 670 |
| 658 model_->set_item_count(5); | 671 model_->set_item_count(5); |
| 659 combobox_->ModelChanged(); | 672 combobox_->ModelChanged(); |
| 660 EXPECT_EQ(5, combobox_->GetRowCount()); | 673 EXPECT_EQ(5, combobox_->GetRowCount()); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 682 EXPECT_EQ(1, combobox_->GetSelectedRow()); | 695 EXPECT_EQ(1, combobox_->GetSelectedRow()); |
| 683 | 696 |
| 684 // Test an empty model. | 697 // Test an empty model. |
| 685 model_->set_item_count(0); | 698 model_->set_item_count(0); |
| 686 combobox_->ModelChanged(); | 699 combobox_->ModelChanged(); |
| 687 EXPECT_EQ(0, combobox_->GetRowCount()); | 700 EXPECT_EQ(0, combobox_->GetRowCount()); |
| 688 EXPECT_EQ(0, combobox_->GetSelectedRow()); // Resets. | 701 EXPECT_EQ(0, combobox_->GetSelectedRow()); // Resets. |
| 689 } | 702 } |
| 690 | 703 |
| 691 TEST_F(ComboboxTest, TypingPrefixNotifiesListener) { | 704 TEST_F(ComboboxTest, TypingPrefixNotifiesListener) { |
| 692 InitCombobox(NULL); | 705 InitCombobox(nullptr, Combobox::STYLE_NORMAL); |
| 693 | 706 |
| 694 TestComboboxListener listener; | 707 TestComboboxListener listener; |
| 695 combobox_->set_listener(&listener); | 708 combobox_->set_listener(&listener); |
| 696 ui::TextInputClient* input_client = | 709 ui::TextInputClient* input_client = |
| 697 widget_->GetInputMethod()->GetTextInputClient(); | 710 widget_->GetInputMethod()->GetTextInputClient(); |
| 698 | 711 |
| 699 // Type the first character of the second menu item ("JELLY"). | 712 // Type the first character of the second menu item ("JELLY"). |
| 700 ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_J, ui::DomCode::US_J, 0, | 713 ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_J, ui::DomCode::US_J, 0, |
| 701 ui::DomKey::FromCharacter('J'), ui::EventTimeForNow()); | 714 ui::DomKey::FromCharacter('J'), ui::EventTimeForNow()); |
| 702 | 715 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 725 input_client->InsertChar(key_event); | 738 input_client->InsertChar(key_event); |
| 726 EXPECT_EQ(2, listener.actions_performed()); | 739 EXPECT_EQ(2, listener.actions_performed()); |
| 727 EXPECT_EQ(2, listener.perform_action_index()); | 740 EXPECT_EQ(2, listener.perform_action_index()); |
| 728 } | 741 } |
| 729 | 742 |
| 730 // Test properties on the Combobox menu model. | 743 // Test properties on the Combobox menu model. |
| 731 TEST_F(ComboboxTest, MenuModel) { | 744 TEST_F(ComboboxTest, MenuModel) { |
| 732 const int kSeparatorIndex = 3; | 745 const int kSeparatorIndex = 3; |
| 733 std::set<int> separators; | 746 std::set<int> separators; |
| 734 separators.insert(kSeparatorIndex); | 747 separators.insert(kSeparatorIndex); |
| 735 InitCombobox(&separators); | 748 InitCombobox(&separators, Combobox::STYLE_NORMAL); |
| 736 | 749 |
| 737 ui::MenuModel* menu_model = test_api_->menu_model(); | 750 ui::MenuModel* menu_model = test_api_->menu_model(); |
| 738 | 751 |
| 739 EXPECT_EQ(TestComboboxModel::kItemCount, menu_model->GetItemCount()); | 752 EXPECT_EQ(TestComboboxModel::kItemCount, menu_model->GetItemCount()); |
| 740 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, | 753 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, |
| 741 menu_model->GetTypeAt(kSeparatorIndex)); | 754 menu_model->GetTypeAt(kSeparatorIndex)); |
| 742 | 755 |
| 743 #if defined(OS_MACOSX) | 756 #if defined(OS_MACOSX) |
| 744 // Comboboxes on Mac should have checkmarks, with the selected item checked, | 757 // Comboboxes on Mac should have checkmarks, with the selected item checked, |
| 745 EXPECT_EQ(ui::MenuModel::TYPE_CHECK, menu_model->GetTypeAt(0)); | 758 EXPECT_EQ(ui::MenuModel::TYPE_CHECK, menu_model->GetTypeAt(0)); |
| 746 EXPECT_EQ(ui::MenuModel::TYPE_CHECK, menu_model->GetTypeAt(1)); | 759 EXPECT_EQ(ui::MenuModel::TYPE_CHECK, menu_model->GetTypeAt(1)); |
| 747 EXPECT_TRUE(menu_model->IsItemCheckedAt(0)); | 760 EXPECT_TRUE(menu_model->IsItemCheckedAt(0)); |
| 748 EXPECT_FALSE(menu_model->IsItemCheckedAt(1)); | 761 EXPECT_FALSE(menu_model->IsItemCheckedAt(1)); |
| 749 | 762 |
| 750 combobox_->SetSelectedIndex(1); | 763 combobox_->SetSelectedIndex(1); |
| 751 EXPECT_FALSE(menu_model->IsItemCheckedAt(0)); | 764 EXPECT_FALSE(menu_model->IsItemCheckedAt(0)); |
| 752 EXPECT_TRUE(menu_model->IsItemCheckedAt(1)); | 765 EXPECT_TRUE(menu_model->IsItemCheckedAt(1)); |
| 753 #else | 766 #else |
| 754 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu_model->GetTypeAt(0)); | 767 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu_model->GetTypeAt(0)); |
| 755 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu_model->GetTypeAt(1)); | 768 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu_model->GetTypeAt(1)); |
| 756 #endif | 769 #endif |
| 757 | 770 |
| 758 EXPECT_EQ(ASCIIToUTF16("PEANUT BUTTER"), menu_model->GetLabelAt(0)); | 771 EXPECT_EQ(ASCIIToUTF16("PEANUT BUTTER"), menu_model->GetLabelAt(0)); |
| 759 EXPECT_EQ(ASCIIToUTF16("JELLY"), menu_model->GetLabelAt(1)); | 772 EXPECT_EQ(ASCIIToUTF16("JELLY"), menu_model->GetLabelAt(1)); |
| 760 | 773 |
| 761 // Check that with STYLE_ACTION, the first item (only) is not shown. | |
| 762 EXPECT_TRUE(menu_model->IsVisibleAt(0)); | 774 EXPECT_TRUE(menu_model->IsVisibleAt(0)); |
| 763 combobox_->SetStyle(Combobox::STYLE_ACTION); | 775 } |
| 776 |
| 777 // Check that with STYLE_ACTION, the first item (only) is not shown, and |
| 778 // checkboxes are not shown, even on Mac. |
| 779 TEST_F(ComboboxTest, MenuModelActionStyle) { |
| 780 const int kSeparatorIndex = 3; |
| 781 std::set<int> separators; |
| 782 separators.insert(kSeparatorIndex); |
| 783 InitCombobox(&separators, Combobox::STYLE_ACTION); |
| 784 |
| 785 ui::MenuModel* menu_model = test_api_->menu_model(); |
| 786 |
| 787 EXPECT_EQ(TestComboboxModel::kItemCount, menu_model->GetItemCount()); |
| 788 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, |
| 789 menu_model->GetTypeAt(kSeparatorIndex)); |
| 790 |
| 791 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu_model->GetTypeAt(0)); |
| 792 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu_model->GetTypeAt(1)); |
| 793 |
| 794 EXPECT_EQ(ASCIIToUTF16("PEANUT BUTTER"), menu_model->GetLabelAt(0)); |
| 795 EXPECT_EQ(ASCIIToUTF16("JELLY"), menu_model->GetLabelAt(1)); |
| 796 |
| 764 EXPECT_FALSE(menu_model->IsVisibleAt(0)); | 797 EXPECT_FALSE(menu_model->IsVisibleAt(0)); |
| 765 EXPECT_TRUE(menu_model->IsVisibleAt(1)); | 798 EXPECT_TRUE(menu_model->IsVisibleAt(1)); |
| 766 } | 799 } |
| 767 | 800 |
| 768 } // namespace views | 801 } // namespace views |
| OLD | NEW |