1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Disabled right now as this won't work on BuildBots right now as this test
// require the box it runs on to be unlocked (and no screen-savers).
// The test actually simulates mouse and key events, so if the screen is locked,
// the events don't go to the Chrome window.
#include "testing/gtest/include/gtest/gtest.h"
#include "app/resource_bundle.h"
#include "base/gfx/rect.h"
#include "base/string_util.h"
#include "third_party/skia/include/core/SkColor.h"
#include "views/background.h"
#include "views/border.h"
#include "views/controls/button/checkbox.h"
#include "views/controls/button/native_button.h"
#include "views/controls/button/radio_button.h"
#include "views/controls/combobox/combobox.h"
#include "views/controls/label.h"
#include "views/controls/link.h"
#include "views/controls/scroll_view.h"
#include "views/controls/tabbed_pane.h"
#include "views/controls/textfield/textfield.h"
#include "views/widget/accelerator_handler.h"
#include "views/widget/root_view.h"
#include "views/widget/widget_win.h"
#include "views/window/window.h"
#include "views/window/window_delegate.h"
namespace {
static const int kWindowWidth = 600;
static const int kWindowHeight = 500;
static int count = 1;
static const int kTopCheckBoxID = count++; // 1
static const int kLeftContainerID = count++;
static const int kAppleLabelID = count++;
static const int kAppleTextfieldID = count++;
static const int kOrangeLabelID = count++; // 5
static const int kOrangeTextfieldID = count++;
static const int kBananaLabelID = count++;
static const int kBananaTextfieldID = count++;
static const int kKiwiLabelID = count++;
static const int kKiwiTextfieldID = count++; // 10
static const int kFruitButtonID = count++;
static const int kFruitCheckBoxID = count++;
static const int kComboboxID = count++;
static const int kRightContainerID = count++;
static const int kAsparagusButtonID = count++; // 15
static const int kBroccoliButtonID = count++;
static const int kCauliflowerButtonID = count++;
static const int kInnerContainerID = count++;
static const int kScrollViewID = count++;
static const int kScrollContentViewID = count++; // 20
static const int kRosettaLinkID = count++;
static const int kStupeurEtTremblementLinkID = count++;
static const int kDinerGameLinkID = count++;
static const int kRidiculeLinkID = count++;
static const int kClosetLinkID = count++; // 25
static const int kVisitingLinkID = count++;
static const int kAmelieLinkID = count++;
static const int kJoyeuxNoelLinkID = count++;
static const int kCampingLinkID = count++;
static const int kBriceDeNiceLinkID = count++; // 30
static const int kTaxiLinkID = count++;
static const int kAsterixLinkID = count++;
static const int kOKButtonID = count++;
static const int kCancelButtonID = count++;
static const int kHelpButtonID = count++; // 35
static const int kStyleContainerID = count++;
static const int kBoldCheckBoxID = count++;
static const int kItalicCheckBoxID = count++;
static const int kUnderlinedCheckBoxID = count++;
static const int kSearchContainerID = count++; // 40
static const int kSearchTextfieldID = count++;
static const int kSearchButtonID = count++;
static const int kHelpLinkID = count++;
static const int kThumbnailContainerID = count++;
static const int kThumbnailStarID = count++; // 45
static const int kThumbnailSuperStarID = count++;
class FocusManagerTest;
// BorderView is a NativeControl that creates a tab control as its child and
// takes a View to add as the child of the tab control. The tab control is used
// to give a nice background for the view. At some point we'll have a real
// wrapper for TabControl, and this can be nuked in favor of it.
// Taken from keyword_editor_view.cc.
// It is interesting in our test as it is a native control containing another
// RootView.
class BorderView : public views::NativeControl {
public:
explicit BorderView(View* child) : child_(child) {
DCHECK(child);
SetFocusable(false);
}
virtual ~BorderView() {}
virtual HWND CreateNativeControl(HWND parent_container) {
// Create the tab control.
HWND tab_control = ::CreateWindowEx(GetAdditionalExStyle(),
WC_TABCONTROL,
L"",
WS_CHILD,
0, 0, width(), height(),
parent_container, NULL, NULL, NULL);
// Create the view container which is a child of the TabControl.
widget_ = new views::WidgetWin();
widget_->Init(tab_control, gfx::Rect(), false);
widget_->SetContentsView(child_);
widget_->SetFocusTraversableParentView(this);
ResizeContents(tab_control);
return tab_control;
}
virtual LRESULT OnNotify(int w_param, LPNMHDR l_param) {
return 0;
}
virtual void Layout() {
NativeControl::Layout();
ResizeContents(GetNativeControlHWND());
}
virtual views::RootView* GetContentsRootView() {
return widget_->GetRootView();
}
virtual views::FocusTraversable* GetFocusTraversable() {
return widget_;
}
virtual void ViewHierarchyChanged(bool is_add, View *parent, View *child) {
NativeControl::ViewHierarchyChanged(is_add, parent, child);
if (child == this && is_add) {
// We have been added to a view hierarchy, update the FocusTraversable
// parent.
widget_->SetFocusTraversableParent(GetRootView());
}
}
private:
void ResizeContents(HWND tab_control) {
DCHECK(tab_control);
CRect content_bounds;
if (!GetClientRect(tab_control, &content_bounds))
return;
TabCtrl_AdjustRect(tab_control, FALSE, &content_bounds);
widget_->MoveWindow(content_bounds.left, content_bounds.top,
content_bounds.Width(), content_bounds.Height(),
TRUE);
}
View* child_;
views::WidgetWin* widget_;
DISALLOW_COPY_AND_ASSIGN(BorderView);
};
class DummyComboboxModel : public views::Combobox::Model {
public:
virtual int GetItemCount(views::Combobox* source) { return 10; }
virtual std::wstring GetItemAt(views::Combobox* source, int index) {
return L"Item " + IntToWString(index);
}
};
class TestViewWindow : public views::WidgetWin {
public:
explicit TestViewWindow(FocusManagerTest* test);
~TestViewWindow() { }
void Init();
views::View* contents() const { return contents_; }
// Return the ID of the component that currently has the focus.
int GetFocusedComponentID();
// Simulate pressing the tab button in the window.
void PressTab(bool shift_pressed, bool ctrl_pressed);
views::RootView* GetContentsRootView() const {
return contents_->GetRootView();
}
views::RootView* GetStyleRootView() const {
return style_tab_->GetContentsRootView();
}
views::RootView* GetSearchRootView() const {
return search_border_view_->GetContentsRootView();
}
private:
views::View* contents_;
views::TabbedPane* style_tab_;
BorderView* search_border_view_;
FocusManagerTest* test_;
DummyComboboxModel combobox_model_;
DISALLOW_COPY_AND_ASSIGN(TestViewWindow);
};
class FocusManagerTest : public testing::Test {
public:
TestViewWindow* GetWindow();
~FocusManagerTest();
protected:
FocusManagerTest();
virtual void SetUp();
virtual void TearDown();
MessageLoopForUI message_loop_;
TestViewWindow* test_window_;
};
////////////////////////////////////////////////////////////////////////////////
// TestViewWindow
////////////////////////////////////////////////////////////////////////////////
TestViewWindow::TestViewWindow(FocusManagerTest* test)
: test_(test),
contents_(NULL),
style_tab_(NULL),
search_border_view_(NULL) {
}
// Initializes and shows the window with the contents view.
void TestViewWindow::Init() {
gfx::Rect bounds(0, 0, 600, 460);
contents_ = new views::View();
contents_->set_background(
views::Background::CreateSolidBackground(255, 255, 255));
WidgetWin::Init(NULL, bounds, true);
SetContentsView(contents_);
views::Checkbox* cb =
new views::Checkbox(L"This is a checkbox");
contents_->AddChildView(cb);
// In this fast paced world, who really has time for non hard-coded layout?
cb->SetBounds(10, 10, 200, 20);
cb->SetID(kTopCheckBoxID);
views::View* left_container = new views::View();
left_container->set_border(
views::Border::CreateSolidBorder(1, SK_ColorBLACK));
left_container->set_background(
views::Background::CreateSolidBackground(240, 240, 240));
left_container->SetID(kLeftContainerID);
contents_->AddChildView(left_container);
left_container->SetBounds(10, 35, 250, 200);
int label_x = 5;
int label_width = 50;
int label_height = 15;
int text_field_width = 150;
int y = 10;
int gap_between_labels = 10;
views::Label* label = new views::Label(L"Apple:");
label->SetID(kAppleLabelID);
left_container->AddChildView(label);
label->SetBounds(label_x, y, label_width, label_height);
views::Textfield* text_field = new views::Textfield();
text_field->SetID(kAppleTextfieldID);
left_container->AddChildView(text_field);
text_field->SetBounds(label_x + label_width + 5, y,
text_field_width, label_height);
y += label_height + gap_between_labels;
label = new views::Label(L"Orange:");
label->SetID(kOrangeLabelID);
left_container->AddChildView(label);
label->SetBounds(label_x, y, label_width, label_height);
text_field = new views::Textfield();
text_field->SetID(kOrangeTextfieldID);
left_container->AddChildView(text_field);
text_field->SetBounds(label_x + label_width + 5, y,
text_field_width, label_height);
y += label_height + gap_between_labels;
label = new views::Label(L"Banana:");
label->SetID(kBananaLabelID);
left_container->AddChildView(label);
label->SetBounds(label_x, y, label_width, label_height);
text_field = new views::Textfield();
text_field->SetID(kBananaTextfieldID);
left_container->AddChildView(text_field);
text_field->SetBounds(label_x + label_width + 5, y,
text_field_width, label_height);
y += label_height + gap_between_labels;
label = new views::Label(L"Kiwi:");
label->SetID(kKiwiLabelID);
left_container->AddChildView(label);
label->SetBounds(label_x, y, label_width, label_height);
text_field = new views::Textfield();
text_field->SetID(kKiwiTextfieldID);
left_container->AddChildView(text_field);
text_field->SetBounds(label_x + label_width + 5, y,
text_field_width, label_height);
y += label_height + gap_between_labels;
views::NativeButton* button = new views::NativeButton(NULL, L"Click me");
button->SetBounds(label_x, y + 10, 50, 20);
button->SetID(kFruitButtonID);
left_container->AddChildView(button);
y += 40;
cb = new views::Checkbox(L"This is another check box");
cb->SetBounds(label_x + label_width + 5, y, 150, 20);
cb->SetID(kFruitCheckBoxID);
left_container->AddChildView(cb);
y += 20;
views::Combobox* combobox = new views::Combobox(&combobox_model_);
combobox->SetBounds(label_x + label_width + 5, y, 150, 20);
combobox->SetID(kComboboxID);
left_container->AddChildView(combobox);
views::View* right_container = new views::View();
right_container->set_border(
views::Border::CreateSolidBorder(1, SK_ColorBLACK));
right_container->set_background(
views::Background::CreateSolidBackground(240, 240, 240));
right_container->SetID(kRightContainerID);
contents_->AddChildView(right_container);
right_container->SetBounds(270, 35, 300, 200);
y = 10;
int radio_button_height = 15;
int gap_between_radio_buttons = 10;
views::View* radio_button =
new views::RadioButton(L"Asparagus", 1);
radio_button->SetID(kAsparagusButtonID);
right_container->AddChildView(radio_button);
radio_button->SetBounds(5, y, 70, radio_button_height);
radio_button->SetGroup(1);
y += radio_button_height + gap_between_radio_buttons;
radio_button = new views::RadioButton(L"Broccoli", 1);
radio_button->SetID(kBroccoliButtonID);
right_container->AddChildView(radio_button);
radio_button->SetBounds(5, y, 70, radio_button_height);
radio_button->SetGroup(1);
y += radio_button_height + gap_between_radio_buttons;
radio_button = new views::RadioButton(L"Cauliflower", 1);
radio_button->SetID(kCauliflowerButtonID);
right_container->AddChildView(radio_button);
radio_button->SetBounds(5, y, 70, radio_button_height);
radio_button->SetGroup(1);
y += radio_button_height + gap_between_radio_buttons;
views::View* inner_container = new views::View();
inner_container->set_border(
views::Border::CreateSolidBorder(1, SK_ColorBLACK));
inner_container->set_background(
views::Background::CreateSolidBackground(230, 230, 230));
inner_container->SetID(kInnerContainerID);
right_container->AddChildView(inner_container);
inner_container->SetBounds(100, 10, 150, 180);
views::ScrollView* scroll_view = new views::ScrollView();
scroll_view->SetID(kScrollViewID);
inner_container->AddChildView(scroll_view);
scroll_view->SetBounds(1, 1, 148, 178);
views::View* scroll_content = new views::View();
scroll_content->SetBounds(0, 0, 200, 200);
scroll_content->set_background(
views::Background::CreateSolidBackground(200, 200, 200));
scroll_view->SetContents(scroll_content);
static const wchar_t* const kTitles[] = {
L"Rosetta", L"Stupeur et tremblement", L"The diner game",
L"Ridicule", L"Le placard", L"Les Visiteurs", L"Amelie",
L"Joyeux Noel", L"Camping", L"Brice de Nice",
L"Taxi", L"Asterix"
};
static const int kIDs[] = {
kRosettaLinkID, kStupeurEtTremblementLinkID, kDinerGameLinkID,
kRidiculeLinkID, kClosetLinkID, kVisitingLinkID, kAmelieLinkID,
kJoyeuxNoelLinkID, kCampingLinkID, kBriceDeNiceLinkID,
kTaxiLinkID, kAsterixLinkID
};
DCHECK(arraysize(kTitles) == arraysize(kIDs));
y = 5;
for (int i = 0; i < arraysize(kTitles); ++i) {
views::Link* link = new views::Link(kTitles[i]);
link->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
link->SetID(kIDs[i]);
scroll_content->AddChildView(link);
link->SetBounds(5, y, 300, 15);
y += 15;
}
y = 250;
int width = 50;
button = new views::NativeButton(NULL, L"OK");
button->SetID(kOKButtonID);
contents_->AddChildView(button);
button->SetBounds(150, y, width, 20);
button = new views::NativeButton(NULL, L"Cancel");
button->SetID(kCancelButtonID);
contents_->AddChildView(button);
button->SetBounds(250, y, width, 20);
button = new views::NativeButton(NULL, L"Help");
button->SetID(kHelpButtonID);
contents_->AddChildView(button);
button->SetBounds(350, y, width, 20);
y += 40;
// Left bottom box with style checkboxes.
views::View* contents = new views::View();
contents->set_background(
views::Background::CreateSolidBackground(SK_ColorWHITE));
cb = new views::Checkbox(L"Bold");
contents->AddChildView(cb);
cb->SetBounds(10, 10, 50, 20);
cb->SetID(kBoldCheckBoxID);
cb = new views::Checkbox(L"Italic");
contents->AddChildView(cb);
cb->SetBounds(70, 10, 50, 20);
cb->SetID(kItalicCheckBoxID);
cb = new views::Checkbox(L"Underlined");
contents->AddChildView(cb);
cb->SetBounds(130, 10, 70, 20);
cb->SetID(kUnderlinedCheckBoxID);
style_tab_ = new views::TabbedPane();
style_tab_->SetID(kStyleContainerID);
contents_->AddChildView(style_tab_);
style_tab_->SetBounds(10, y, 210, 50);
style_tab_->AddTab(L"Style", contents);
style_tab_->AddTab(L"Other", new views::View());
// Right bottom box with search.
contents = new views::View();
contents->set_background(
views::Background::CreateSolidBackground(SK_ColorWHITE));
text_field = new views::Textfield();
contents->AddChildView(text_field);
text_field->SetBounds(10, 10, 100, 20);
text_field->SetID(kSearchTextfieldID);
button = new views::NativeButton(NULL, L"Search");
contents->AddChildView(button);
button->SetBounds(115, 10, 50, 20);
button->SetID(kSearchButtonID);
views::Link* link = new views::Link(L"Help");
link->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
link->SetID(kHelpLinkID);
contents->AddChildView(link);
link->SetBounds(170, 10, 30, 15);
search_border_view_ = new BorderView(contents);
search_border_view_->SetID(kSearchContainerID);
contents_->AddChildView(search_border_view_);
search_border_view_->SetBounds(300, y, 200, 50);
y += 60;
contents = new views::View();
contents->SetFocusable(true);
contents->set_background(
views::Background::CreateSolidBackground(SK_ColorBLUE));
contents->SetID(kThumbnailContainerID);
button = new views::NativeButton(NULL, L"Star");
contents->AddChildView(button);
button->SetBounds(5, 5, 50, 20);
button->SetID(kThumbnailStarID);
button = new views::NativeButton(NULL, L"SuperStar");
contents->AddChildView(button);
button->SetBounds(60, 5, 100, 20);
button->SetID(kThumbnailSuperStarID);
contents_->AddChildView(contents);
contents->SetBounds(200, y, 200, 50);
}
////////////////////////////////////////////////////////////////////////////////
// FocusManagerTest
////////////////////////////////////////////////////////////////////////////////
FocusManagerTest::FocusManagerTest() {
}
FocusManagerTest::~FocusManagerTest() {
}
TestViewWindow* FocusManagerTest::GetWindow() {
return test_window_;
}
void FocusManagerTest::SetUp() {
OleInitialize(NULL);
test_window_ = new TestViewWindow(this);
test_window_->Init();
ShowWindow(test_window_->GetNativeView(), SW_SHOW);
}
void FocusManagerTest::TearDown() {
test_window_->CloseNow();
// Flush the message loop to make Purify happy.
message_loop_.RunAllPending();
OleUninitialize();
}
////////////////////////////////////////////////////////////////////////////////
// The tests
////////////////////////////////////////////////////////////////////////////////
TEST_F(FocusManagerTest, NormalTraversal) {
const int kTraversalIDs[] = { kTopCheckBoxID, kAppleTextfieldID,
kOrangeTextfieldID, kBananaTextfieldID, kKiwiTextfieldID,
kFruitButtonID, kFruitCheckBoxID, kComboboxID, kAsparagusButtonID,
kRosettaLinkID, kStupeurEtTremblementLinkID,
kDinerGameLinkID, kRidiculeLinkID, kClosetLinkID, kVisitingLinkID,
kAmelieLinkID, kJoyeuxNoelLinkID, kCampingLinkID, kBriceDeNiceLinkID,
kTaxiLinkID, kAsterixLinkID, kOKButtonID, kCancelButtonID, kHelpButtonID,
kStyleContainerID, kBoldCheckBoxID, kItalicCheckBoxID,
kUnderlinedCheckBoxID, kSearchTextfieldID, kSearchButtonID, kHelpLinkID,
kThumbnailContainerID, kThumbnailStarID, kThumbnailSuperStarID };
// Uncomment the following line if you want to test manually the UI of this
// test.
// MessageLoopForUI::current()->Run(new views::AcceleratorHandler());
views::FocusManager* focus_manager =
views::FocusManager::GetFocusManager(test_window_->GetNativeView());
// Let's traverse the whole focus hierarchy (several times, to make sure it
// loops OK).
focus_manager->SetFocusedView(NULL);
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < arraysize(kTraversalIDs); j++) {
focus_manager->AdvanceFocus(false);
views::View* focused_view = focus_manager->GetFocusedView();
EXPECT_TRUE(focused_view != NULL);
if (focused_view)
EXPECT_EQ(kTraversalIDs[j], focused_view->GetID());
}
}
// Focus the 1st item.
views::RootView* root_view = test_window_->GetContentsRootView();
focus_manager->SetFocusedView(root_view->GetViewByID(kTraversalIDs[0]));
/* BROKEN because of bug #1153276. The reverse order of traversal in Tabbed
Panes is broken (we go to the tab before going to the content
// Let's traverse in reverse order.
for (int i = 0; i < 3; ++i) {
for (int j = arraysize(kTraversalIDs) - 1; j >= 0; --j) {
focus_manager->AdvanceFocus(true);
views::View* focused_view = focus_manager->GetFocusedView();
EXPECT_TRUE(focused_view != NULL);
if (focused_view)
EXPECT_EQ(kTraversalIDs[j], focused_view->GetID());
}
}
*/
}
TEST_F(FocusManagerTest, TraversalWithNonEnabledViews) {
const int kMainContentsDisabledIDs[] = {
kBananaTextfieldID, kFruitCheckBoxID, kComboboxID, kAsparagusButtonID,
kCauliflowerButtonID, kClosetLinkID, kVisitingLinkID, kBriceDeNiceLinkID,
kTaxiLinkID, kAsterixLinkID, kHelpButtonID };
const int kStyleContentsDisabledIDs[] = { kBoldCheckBoxID };
const int kSearchContentsDisabledIDs[] = { kSearchTextfieldID, kHelpLinkID };
const int kTraversalIDs[] = { kTopCheckBoxID, kAppleTextfieldID,
kOrangeTextfieldID, kKiwiTextfieldID, kFruitButtonID, kBroccoliButtonID,
kRosettaLinkID, kStupeurEtTremblementLinkID, kDinerGameLinkID,
kRidiculeLinkID, kAmelieLinkID, kJoyeuxNoelLinkID, kCampingLinkID,
kOKButtonID, kCancelButtonID, kStyleContainerID,
kItalicCheckBoxID, kUnderlinedCheckBoxID, kSearchButtonID,
kThumbnailContainerID, kThumbnailStarID, kThumbnailSuperStarID };
// Let's disable some views.
views::RootView* root_view = test_window_->GetContentsRootView();
for (int i = 0; i < arraysize(kMainContentsDisabledIDs); i++) {
views::View* v = root_view->GetViewByID(kMainContentsDisabledIDs[i]);
ASSERT_TRUE(v != NULL);
if (v)
v->SetEnabled(false);
}
root_view = test_window_->GetStyleRootView();
for (int i = 0; i < arraysize(kStyleContentsDisabledIDs); i++) {
views::View* v = root_view->GetViewByID(kStyleContentsDisabledIDs[i]);
ASSERT_TRUE(v != NULL);
if (v)
v->SetEnabled(false);
}
root_view = test_window_->GetSearchRootView();
for (int i = 0; i < arraysize(kSearchContentsDisabledIDs); i++) {
views::View* v =
root_view->GetViewByID(kSearchContentsDisabledIDs[i]);
ASSERT_TRUE(v != NULL);
if (v)
v->SetEnabled(false);
}
views::FocusManager* focus_manager =
views::FocusManager::GetFocusManager(test_window_->GetNativeView());
views::View* focused_view;
// Let's do one traversal (several times, to make sure it loops ok).
for (int i = 0; i < 3;++i) {
for (int j = 0; j < arraysize(kTraversalIDs); j++) {
focus_manager->AdvanceFocus(false);
focused_view = focus_manager->GetFocusedView();
EXPECT_TRUE(focused_view != NULL);
if (focused_view)
EXPECT_EQ(kTraversalIDs[j], focused_view->GetID());
}
}
// Focus the 1st item.
focus_manager->AdvanceFocus(false);
focused_view = focus_manager->GetFocusedView();
EXPECT_TRUE(focused_view != NULL);
if (focused_view)
EXPECT_EQ(kTraversalIDs[0], focused_view->GetID());
// Same thing in reverse.
/* BROKEN because of bug #1153276. The reverse order of traversal in Tabbed
Panes is broken (we go to the tab before going to the content
for (int i = 0; i < 3; ++i) {
for (int j = arraysize(kTraversalIDs) - 1; j >= 0; --j) {
focus_manager->AdvanceFocus(true);
focused_view = focus_manager->GetFocusedView();
EXPECT_TRUE(focused_view != NULL);
if (focused_view)
EXPECT_EQ(kTraversalIDs[j], focused_view->GetID());
}
}
*/
}
// Counts accelerator calls.
class TestAcceleratorTarget : public views::AcceleratorTarget {
public:
explicit TestAcceleratorTarget(bool process_accelerator)
: accelerator_count_(0), process_accelerator_(process_accelerator) {}
virtual bool AcceleratorPressed(const views::Accelerator& accelerator) {
++accelerator_count_;
return process_accelerator_;
}
int accelerator_count() const { return accelerator_count_; }
private:
int accelerator_count_; // number of times that the accelerator is activated
bool process_accelerator_; // return value of AcceleratorPressed
DISALLOW_COPY_AND_ASSIGN(TestAcceleratorTarget);
};
TEST_F(FocusManagerTest, CallsNormalAcceleratorTarget) {
views::FocusManager* focus_manager =
views::FocusManager::GetFocusManager(test_window_->GetNativeView());
views::Accelerator return_accelerator(VK_RETURN, false, false, false);
views::Accelerator escape_accelerator(VK_ESCAPE, false, false, false);
TestAcceleratorTarget return_target(true);
TestAcceleratorTarget escape_target(true);
EXPECT_EQ(return_target.accelerator_count(), 0);
EXPECT_EQ(escape_target.accelerator_count(), 0);
EXPECT_EQ(NULL,
focus_manager->GetCurrentTargetForAccelerator(return_accelerator));
EXPECT_EQ(NULL,
focus_manager->GetCurrentTargetForAccelerator(escape_accelerator));
// Register targets.
focus_manager->RegisterAccelerator(return_accelerator, &return_target);
focus_manager->RegisterAccelerator(escape_accelerator, &escape_target);
// Checks if the correct target is registered.
EXPECT_EQ(&return_target,
focus_manager->GetCurrentTargetForAccelerator(return_accelerator));
EXPECT_EQ(&escape_target,
focus_manager->GetCurrentTargetForAccelerator(escape_accelerator));
// Hitting the return key.
EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(return_target.accelerator_count(), 1);
EXPECT_EQ(escape_target.accelerator_count(), 0);
// Hitting the escape key.
EXPECT_TRUE(focus_manager->ProcessAccelerator(escape_accelerator));
EXPECT_EQ(return_target.accelerator_count(), 1);
EXPECT_EQ(escape_target.accelerator_count(), 1);
// Register another target for the return key.
TestAcceleratorTarget return_target2(true);
EXPECT_EQ(return_target2.accelerator_count(), 0);
focus_manager->RegisterAccelerator(return_accelerator, &return_target2);
EXPECT_EQ(&return_target2,
focus_manager->GetCurrentTargetForAccelerator(return_accelerator));
// Hitting the return key; return_target2 has the priority.
EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(return_target.accelerator_count(), 1);
EXPECT_EQ(return_target2.accelerator_count(), 1);
// Register a target that does not process the accelerator event.
TestAcceleratorTarget return_target3(false);
EXPECT_EQ(return_target3.accelerator_count(), 0);
focus_manager->RegisterAccelerator(return_accelerator, &return_target3);
EXPECT_EQ(&return_target3,
focus_manager->GetCurrentTargetForAccelerator(return_accelerator));
// Hitting the return key.
// Since the event handler of return_target3 returns false, return_target2
// should be called too.
EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(return_target.accelerator_count(), 1);
EXPECT_EQ(return_target2.accelerator_count(), 2);
EXPECT_EQ(return_target3.accelerator_count(), 1);
// Unregister return_target2.
focus_manager->UnregisterAccelerator(return_accelerator, &return_target2);
EXPECT_EQ(&return_target3,
focus_manager->GetCurrentTargetForAccelerator(return_accelerator));
// Hitting the return key. return_target3 and return_target should be called.
EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(return_target.accelerator_count(), 2);
EXPECT_EQ(return_target2.accelerator_count(), 2);
EXPECT_EQ(return_target3.accelerator_count(), 2);
// Unregister targets.
focus_manager->UnregisterAccelerator(return_accelerator, &return_target);
focus_manager->UnregisterAccelerator(return_accelerator, &return_target3);
focus_manager->UnregisterAccelerator(escape_accelerator, &escape_target);
// Now there is no target registered.
EXPECT_EQ(NULL,
focus_manager->GetCurrentTargetForAccelerator(return_accelerator));
EXPECT_EQ(NULL,
focus_manager->GetCurrentTargetForAccelerator(escape_accelerator));
// Hitting the return key and the escape key. Nothing should happen.
EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(return_target.accelerator_count(), 2);
EXPECT_EQ(return_target2.accelerator_count(), 2);
EXPECT_EQ(return_target3.accelerator_count(), 2);
EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator));
EXPECT_EQ(escape_target.accelerator_count(), 1);
}
// Unregisters itself when its accelerator is invoked.
class SelfUnregisteringAcceleratorTarget : public views::AcceleratorTarget {
public:
SelfUnregisteringAcceleratorTarget(views::Accelerator accelerator,
views::FocusManager* focus_manager)
: accelerator_(accelerator),
focus_manager_(focus_manager),
accelerator_count_(0) {
}
virtual bool AcceleratorPressed(const views::Accelerator& accelerator) {
++accelerator_count_;
focus_manager_->UnregisterAccelerator(accelerator, this);
return true;
}
int accelerator_count() const { return accelerator_count_; }
private:
views::Accelerator accelerator_;
views::FocusManager* focus_manager_;
int accelerator_count_;
DISALLOW_COPY_AND_ASSIGN(SelfUnregisteringAcceleratorTarget);
};
TEST_F(FocusManagerTest, CallsSelfDeletingAcceleratorTarget) {
views::FocusManager* focus_manager =
views::FocusManager::GetFocusManager(test_window_->GetNativeView());
views::Accelerator return_accelerator(VK_RETURN, false, false, false);
SelfUnregisteringAcceleratorTarget target(return_accelerator, focus_manager);
EXPECT_EQ(target.accelerator_count(), 0);
EXPECT_EQ(NULL,
focus_manager->GetCurrentTargetForAccelerator(return_accelerator));
// Register the target.
focus_manager->RegisterAccelerator(return_accelerator, &target);
EXPECT_EQ(&target,
focus_manager->GetCurrentTargetForAccelerator(return_accelerator));
// Hitting the return key. The target will be unregistered.
EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(target.accelerator_count(), 1);
EXPECT_EQ(NULL,
focus_manager->GetCurrentTargetForAccelerator(return_accelerator));
// Hitting the return key again; nothing should happen.
EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(target.accelerator_count(), 1);
}
} // anonymous namespace
|