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

Side by Side Diff: views/focus/focus_manager_unittest.cc

Issue 8508055: Move views::Accelerator to ui in order to use it from aura code. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Rebase Created 9 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « views/focus/focus_manager.cc ('k') | views/view.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/logging.h" 5 #include "base/logging.h"
6 #include "base/string16.h" 6 #include "base/string16.h"
7 #include "base/string_number_conversions.h" 7 #include "base/string_number_conversions.h"
8 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
9 #include "third_party/skia/include/core/SkColor.h" 9 #include "third_party/skia/include/core/SkColor.h"
10 #include "ui/base/keycodes/keyboard_codes.h" 10 #include "ui/base/keycodes/keyboard_codes.h"
11 #include "ui/base/models/accelerator.h"
11 #include "ui/base/models/combobox_model.h" 12 #include "ui/base/models/combobox_model.h"
12 #include "ui/gfx/rect.h" 13 #include "ui/gfx/rect.h"
13 #include "views/background.h" 14 #include "views/background.h"
14 #include "views/border.h" 15 #include "views/border.h"
15 #include "views/controls/button/checkbox.h" 16 #include "views/controls/button/checkbox.h"
16 #include "views/controls/button/radio_button.h" 17 #include "views/controls/button/radio_button.h"
17 #include "views/controls/combobox/combobox.h" 18 #include "views/controls/combobox/combobox.h"
18 #include "views/controls/combobox/native_combobox_wrapper.h" 19 #include "views/controls/combobox/native_combobox_wrapper.h"
19 #include "views/controls/label.h" 20 #include "views/controls/label.h"
20 #include "views/controls/link.h" 21 #include "views/controls/link.h"
(...skipping 1263 matching lines...) Expand 10 before | Expand all | Expand 10 after
1284 GetFocusManager()->AdvanceFocus(true); 1285 GetFocusManager()->AdvanceFocus(true);
1285 View* focused_view = GetFocusManager()->GetFocusedView(); 1286 View* focused_view = GetFocusManager()->GetFocusedView();
1286 EXPECT_TRUE(focused_view != NULL); 1287 EXPECT_TRUE(focused_view != NULL);
1287 if (focused_view) 1288 if (focused_view)
1288 EXPECT_EQ(kRightTraversalIDs[j], focused_view->id()); 1289 EXPECT_EQ(kRightTraversalIDs[j], focused_view->id());
1289 } 1290 }
1290 } 1291 }
1291 } 1292 }
1292 1293
1293 // Counts accelerator calls. 1294 // Counts accelerator calls.
1294 class TestAcceleratorTarget : public AcceleratorTarget { 1295 class TestAcceleratorTarget : public ui::AcceleratorTarget {
1295 public: 1296 public:
1296 explicit TestAcceleratorTarget(bool process_accelerator) 1297 explicit TestAcceleratorTarget(bool process_accelerator)
1297 : accelerator_count_(0), process_accelerator_(process_accelerator) {} 1298 : accelerator_count_(0), process_accelerator_(process_accelerator) {}
1298 1299
1299 virtual bool AcceleratorPressed(const Accelerator& accelerator) { 1300 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) {
1300 ++accelerator_count_; 1301 ++accelerator_count_;
1301 return process_accelerator_; 1302 return process_accelerator_;
1302 } 1303 }
1303 1304
1304 int accelerator_count() const { return accelerator_count_; } 1305 int accelerator_count() const { return accelerator_count_; }
1305 1306
1306 private: 1307 private:
1307 int accelerator_count_; // number of times that the accelerator is activated 1308 int accelerator_count_; // number of times that the accelerator is activated
1308 bool process_accelerator_; // return value of AcceleratorPressed 1309 bool process_accelerator_; // return value of AcceleratorPressed
1309 1310
1310 DISALLOW_COPY_AND_ASSIGN(TestAcceleratorTarget); 1311 DISALLOW_COPY_AND_ASSIGN(TestAcceleratorTarget);
1311 }; 1312 };
1312 1313
1313 TEST_F(FocusManagerTest, CallsNormalAcceleratorTarget) { 1314 TEST_F(FocusManagerTest, CallsNormalAcceleratorTarget) {
1314 FocusManager* focus_manager = GetFocusManager(); 1315 FocusManager* focus_manager = GetFocusManager();
1315 Accelerator return_accelerator(ui::VKEY_RETURN, false, false, false); 1316 ui::Accelerator return_accelerator(ui::VKEY_RETURN, false, false, false);
1316 Accelerator escape_accelerator(ui::VKEY_ESCAPE, false, false, false); 1317 ui::Accelerator escape_accelerator(ui::VKEY_ESCAPE, false, false, false);
1317 1318
1318 TestAcceleratorTarget return_target(true); 1319 TestAcceleratorTarget return_target(true);
1319 TestAcceleratorTarget escape_target(true); 1320 TestAcceleratorTarget escape_target(true);
1320 EXPECT_EQ(return_target.accelerator_count(), 0); 1321 EXPECT_EQ(return_target.accelerator_count(), 0);
1321 EXPECT_EQ(escape_target.accelerator_count(), 0); 1322 EXPECT_EQ(escape_target.accelerator_count(), 0);
1322 EXPECT_EQ(NULL, 1323 EXPECT_EQ(NULL,
1323 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); 1324 focus_manager->GetCurrentTargetForAccelerator(return_accelerator));
1324 EXPECT_EQ(NULL, 1325 EXPECT_EQ(NULL,
1325 focus_manager->GetCurrentTargetForAccelerator(escape_accelerator)); 1326 focus_manager->GetCurrentTargetForAccelerator(escape_accelerator));
1326 1327
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
1396 // Hitting the return key and the escape key. Nothing should happen. 1397 // Hitting the return key and the escape key. Nothing should happen.
1397 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); 1398 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1398 EXPECT_EQ(return_target.accelerator_count(), 2); 1399 EXPECT_EQ(return_target.accelerator_count(), 2);
1399 EXPECT_EQ(return_target2.accelerator_count(), 2); 1400 EXPECT_EQ(return_target2.accelerator_count(), 2);
1400 EXPECT_EQ(return_target3.accelerator_count(), 2); 1401 EXPECT_EQ(return_target3.accelerator_count(), 2);
1401 EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator)); 1402 EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator));
1402 EXPECT_EQ(escape_target.accelerator_count(), 1); 1403 EXPECT_EQ(escape_target.accelerator_count(), 1);
1403 } 1404 }
1404 1405
1405 // Unregisters itself when its accelerator is invoked. 1406 // Unregisters itself when its accelerator is invoked.
1406 class SelfUnregisteringAcceleratorTarget : public AcceleratorTarget { 1407 class SelfUnregisteringAcceleratorTarget : public ui::AcceleratorTarget {
1407 public: 1408 public:
1408 SelfUnregisteringAcceleratorTarget(Accelerator accelerator, 1409 SelfUnregisteringAcceleratorTarget(ui::Accelerator accelerator,
1409 FocusManager* focus_manager) 1410 FocusManager* focus_manager)
1410 : accelerator_(accelerator), 1411 : accelerator_(accelerator),
1411 focus_manager_(focus_manager), 1412 focus_manager_(focus_manager),
1412 accelerator_count_(0) { 1413 accelerator_count_(0) {
1413 } 1414 }
1414 1415
1415 virtual bool AcceleratorPressed(const Accelerator& accelerator) { 1416 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) {
1416 ++accelerator_count_; 1417 ++accelerator_count_;
1417 focus_manager_->UnregisterAccelerator(accelerator, this); 1418 focus_manager_->UnregisterAccelerator(accelerator, this);
1418 return true; 1419 return true;
1419 } 1420 }
1420 1421
1421 int accelerator_count() const { return accelerator_count_; } 1422 int accelerator_count() const { return accelerator_count_; }
1422 1423
1423 private: 1424 private:
1424 Accelerator accelerator_; 1425 ui::Accelerator accelerator_;
1425 FocusManager* focus_manager_; 1426 FocusManager* focus_manager_;
1426 int accelerator_count_; 1427 int accelerator_count_;
1427 1428
1428 DISALLOW_COPY_AND_ASSIGN(SelfUnregisteringAcceleratorTarget); 1429 DISALLOW_COPY_AND_ASSIGN(SelfUnregisteringAcceleratorTarget);
1429 }; 1430 };
1430 1431
1431 TEST_F(FocusManagerTest, CallsSelfDeletingAcceleratorTarget) { 1432 TEST_F(FocusManagerTest, CallsSelfDeletingAcceleratorTarget) {
1432 FocusManager* focus_manager = GetFocusManager(); 1433 FocusManager* focus_manager = GetFocusManager();
1433 Accelerator return_accelerator(ui::VKEY_RETURN, false, false, false); 1434 ui::Accelerator return_accelerator(ui::VKEY_RETURN, false, false, false);
1434 SelfUnregisteringAcceleratorTarget target(return_accelerator, focus_manager); 1435 SelfUnregisteringAcceleratorTarget target(return_accelerator, focus_manager);
1435 EXPECT_EQ(target.accelerator_count(), 0); 1436 EXPECT_EQ(target.accelerator_count(), 0);
1436 EXPECT_EQ(NULL, 1437 EXPECT_EQ(NULL,
1437 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); 1438 focus_manager->GetCurrentTargetForAccelerator(return_accelerator));
1438 1439
1439 // Register the target. 1440 // Register the target.
1440 focus_manager->RegisterAccelerator(return_accelerator, &target); 1441 focus_manager->RegisterAccelerator(return_accelerator, &target);
1441 EXPECT_EQ(&target, 1442 EXPECT_EQ(&target,
1442 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); 1443 focus_manager->GetCurrentTargetForAccelerator(return_accelerator));
1443 1444
(...skipping 16 matching lines...) Expand all
1460 virtual bool OnKeyPressed(const KeyEvent& e) { 1461 virtual bool OnKeyPressed(const KeyEvent& e) {
1461 keys_pressed_.push_back(e.key_code()); 1462 keys_pressed_.push_back(e.key_code());
1462 return true; 1463 return true;
1463 } 1464 }
1464 1465
1465 virtual bool OnKeyReleased(const KeyEvent& e) { 1466 virtual bool OnKeyReleased(const KeyEvent& e) {
1466 keys_released_.push_back(e.key_code()); 1467 keys_released_.push_back(e.key_code());
1467 return true; 1468 return true;
1468 } 1469 }
1469 1470
1470 virtual bool AcceleratorPressed(const Accelerator& accelerator) { 1471 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) {
1471 accelerator_pressed_ = true; 1472 accelerator_pressed_ = true;
1472 return true; 1473 return true;
1473 } 1474 }
1474 1475
1475 void Reset() { 1476 void Reset() {
1476 accelerator_pressed_ = false; 1477 accelerator_pressed_ = false;
1477 keys_pressed_.clear(); 1478 keys_pressed_.clear();
1478 keys_released_.clear(); 1479 keys_released_.clear();
1479 } 1480 }
1480 1481
(...skipping 18 matching lines...) Expand all
1499 }; 1500 };
1500 1501
1501 #if defined(OS_WIN) 1502 #if defined(OS_WIN)
1502 // This test is now Windows only. Linux Views port does not handle accelerator 1503 // This test is now Windows only. Linux Views port does not handle accelerator
1503 // keys in AcceleratorHandler anymore. The logic has been moved into 1504 // keys in AcceleratorHandler anymore. The logic has been moved into
1504 // NativeWidgetGtk::OnKeyEvent(). 1505 // NativeWidgetGtk::OnKeyEvent().
1505 // Tests that the keyup messages are eaten for accelerators. 1506 // Tests that the keyup messages are eaten for accelerators.
1506 TEST_F(FocusManagerTest, IgnoreKeyupForAccelerators) { 1507 TEST_F(FocusManagerTest, IgnoreKeyupForAccelerators) {
1507 FocusManager* focus_manager = GetFocusManager(); 1508 FocusManager* focus_manager = GetFocusManager();
1508 MessageTrackingView* mtv = new MessageTrackingView(); 1509 MessageTrackingView* mtv = new MessageTrackingView();
1509 mtv->AddAccelerator(Accelerator(ui::VKEY_0, false, false, false)); 1510 mtv->AddAccelerator(ui::Accelerator(ui::VKEY_0, false, false, false));
1510 mtv->AddAccelerator(Accelerator(ui::VKEY_1, false, false, false)); 1511 mtv->AddAccelerator(ui::Accelerator(ui::VKEY_1, false, false, false));
1511 content_view_->AddChildView(mtv); 1512 content_view_->AddChildView(mtv);
1512 focus_manager->SetFocusedView(mtv); 1513 focus_manager->SetFocusedView(mtv);
1513 1514
1514 // First send a non-accelerator key sequence. 1515 // First send a non-accelerator key sequence.
1515 PostKeyDown(ui::VKEY_9); 1516 PostKeyDown(ui::VKEY_9);
1516 PostKeyUp(ui::VKEY_9); 1517 PostKeyUp(ui::VKEY_9);
1517 AcceleratorHandler accelerator_handler; 1518 AcceleratorHandler accelerator_handler;
1518 MessageLoopForUI::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); 1519 MessageLoopForUI::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
1519 MessageLoopForUI::current()->RunWithDispatcher(&accelerator_handler); 1520 MessageLoopForUI::current()->RunWithDispatcher(&accelerator_handler);
1520 // Make sure we get a key-up and key-down. 1521 // Make sure we get a key-up and key-down.
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
1753 ASSERT_EQ(3, static_cast<int>(dtor_tracker_.size())); 1754 ASSERT_EQ(3, static_cast<int>(dtor_tracker_.size()));
1754 1755
1755 // Focus manager should be the last one to destruct. 1756 // Focus manager should be the last one to destruct.
1756 ASSERT_STREQ("FocusManagerDtorTracked", dtor_tracker_[2].c_str()); 1757 ASSERT_STREQ("FocusManagerDtorTracked", dtor_tracker_[2].c_str());
1757 1758
1758 // Clear window_ so that we don't try to close it again. 1759 // Clear window_ so that we don't try to close it again.
1759 window_ = NULL; 1760 window_ = NULL;
1760 } 1761 }
1761 1762
1762 } // namespace views 1763 } // namespace views
OLDNEW
« no previous file with comments | « views/focus/focus_manager.cc ('k') | views/view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698