summaryrefslogtreecommitdiffstats
path: root/chrome/browser/browser_focus_uitest.cc
blob: 712f9550b9f09caf680cd005d232dcdaedfbddcc (plain)
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
// Copyright (c) 2009 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.

#include "base/message_loop.h"
#include "base/ref_counted.h"
#include "chrome/browser/automation/ui_controls.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/renderer_host/render_widget_host_view.h"
#include "chrome/browser/tab_contents/interstitial_page.h"
#include "chrome/browser/view_ids.h"
#include "chrome/browser/views/frame/browser_view.h"
#include "chrome/browser/views/location_bar_view.h"
#include "chrome/browser/views/tab_contents/tab_contents_container.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/in_process_browser_test.h"
#include "chrome/test/ui_test_utils.h"
#include "views/focus/focus_manager.h"
#include "views/view.h"
#include "views/window/window.h"

namespace {

// The delay waited in some cases where we don't have a notifications for an
// action we take.
const int kActionDelayMs = 500;

const wchar_t kSimplePage[] = L"files/focus/page_with_focus.html";
const wchar_t kStealFocusPage[] = L"files/focus/page_steals_focus.html";
const wchar_t kTypicalPage[] = L"files/focus/typical_page.html";
const wchar_t kTypicalPageName[] = L"typical_page.html";

class BrowserFocusTest : public InProcessBrowserTest {
 public:
  BrowserFocusTest() {
    set_show_window(true);
    EnableDOMAutomation();
  }
};

class TestInterstitialPage : public InterstitialPage {
 public:
  TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url)
      : InterstitialPage(tab, new_navigation, url),
        waiting_for_dom_response_(false) {
    std::wstring file_path;
    bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
    EXPECT_TRUE(r);
    file_util::AppendToPath(&file_path, L"focus");
    file_util::AppendToPath(&file_path, kTypicalPageName);
    r = file_util::ReadFileToString(file_path, &html_contents_);
    EXPECT_TRUE(r);
  }

  virtual std::string GetHTMLContents() {
    return html_contents_;
  }

  virtual void DomOperationResponse(const std::string& json_string,
                                    int automation_id) {
    if (waiting_for_dom_response_) {
      dom_response_ = json_string;
      waiting_for_dom_response_ = false;
      MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
      return;
    }
    InterstitialPage::DomOperationResponse(json_string, automation_id);
  }

  std::string GetFocusedElement() {
    std::wstring script = L"window.domAutomationController.setAutomationId(0);"
        L"window.domAutomationController.send(getFocusedElement());";

    render_view_host()->ExecuteJavascriptInWebFrame(L"", script);
    DCHECK(!waiting_for_dom_response_);
    waiting_for_dom_response_ = true;
    ui_test_utils::RunMessageLoop();
    // Remove the JSON extra quotes.
    if (dom_response_.size() >= 2 && dom_response_[0] == '"' &&
        dom_response_[dom_response_.size() - 1] == '"') {
      dom_response_ = dom_response_.substr(1, dom_response_.size() - 2);
    }
    return dom_response_;
  }

  bool HasFocus() {
    return render_view_host()->view()->HasFocus();
  }

 private:
  std::string html_contents_;

  bool waiting_for_dom_response_;
  std::string dom_response_;

};
}  // namespace

IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) {
  HTTPTestServer* server = StartHTTPServer();

  // First we navigate to our test page.
  GURL url = server->TestServerPageW(kSimplePage);
  ui_test_utils::NavigateToURL(browser(), url);

  // The focus should be on the Tab contents.
  HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
  BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd);
  ASSERT_TRUE(browser_view);
  views::FocusManager* focus_manager =
      views::FocusManager::GetFocusManager(hwnd);
  ASSERT_TRUE(focus_manager);

  EXPECT_EQ(browser_view->contents_container()->GetFocusView(),
            focus_manager->GetFocusedView());

  // Now hide the window, show it again, the focus should not have changed.
  // TODO(jcampan): retrieve the WidgetWin and show/hide on it instead of
  // using Windows API.
  ::ShowWindow(hwnd, SW_HIDE);
  ::ShowWindow(hwnd, SW_SHOW);
  EXPECT_EQ(browser_view->contents_container()->GetFocusView(),
            focus_manager->GetFocusedView());

  // Click on the location bar.
  LocationBarView* location_bar = browser_view->GetLocationBarView();
  ui_controls::MoveMouseToCenterAndPress(location_bar,
                                         ui_controls::LEFT,
                                         ui_controls::DOWN | ui_controls::UP,
                                         new MessageLoop::QuitTask());
  ui_test_utils::RunMessageLoop();
  // Location bar should have focus.
  EXPECT_EQ(location_bar, focus_manager->GetFocusedView());

  // Hide the window, show it again, the focus should not have changed.
  ::ShowWindow(hwnd, SW_HIDE);
  ::ShowWindow(hwnd, SW_SHOW);
  EXPECT_EQ(location_bar, focus_manager->GetFocusedView());

  // Open a new browser window.
  Browser* browser2 = Browser::Create(browser()->profile());
  ASSERT_TRUE(browser2);
  browser2->tabstrip_model()->delegate()->AddBlankTab(true);
  browser2->window()->Show();
  ui_test_utils::NavigateToURL(browser2, url);

  HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle());
  BrowserView* browser_view2 =
      BrowserView::GetBrowserViewForNativeWindow(hwnd2);
  ASSERT_TRUE(browser_view2);
  views::FocusManager* focus_manager2 =
      views::FocusManager::GetFocusManager(hwnd2);
  ASSERT_TRUE(focus_manager2);
  EXPECT_EQ(browser_view2->contents_container()->GetFocusView(),
            focus_manager2->GetFocusedView());

  // Switch to the 1st browser window, focus should still be on the location
  // bar and the second browser should have nothing focused.
  browser()->window()->Activate();
  EXPECT_EQ(location_bar, focus_manager->GetFocusedView());
  EXPECT_EQ(NULL, focus_manager2->GetFocusedView());

  // Switch back to the second browser, focus should still be on the page.
  browser2->window()->Activate();
  EXPECT_EQ(NULL, focus_manager->GetFocusedView());
  EXPECT_EQ(browser_view->contents_container()->GetFocusView(),
            focus_manager2->GetFocusedView());

  // Close the 2nd browser to avoid a DCHECK().
  browser_view2->Close();
}

// Tabs remember focus.
IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) {
  HTTPTestServer* server = StartHTTPServer();

  // First we navigate to our test page.
  GURL url = server->TestServerPageW(kSimplePage);
  ui_test_utils::NavigateToURL(browser(), url);

  HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
  BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd);
  ASSERT_TRUE(browser_view);

  views::FocusManager* focus_manager =
      views::FocusManager::GetFocusManager(hwnd);
  ASSERT_TRUE(focus_manager);

  // Create several tabs.
  for (int i = 0; i < 4; ++i) {
    browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, true, -1, false,
                             NULL);
  }

  // Alternate focus for the tab.
  const bool kFocusPage[3][5] = {
    { true, true, true, true, false },
    { false, false, false, false, false },
    { false, true, false, true, false }
  };

  for (int i = 1; i < 3; i++) {
    for (int j = 0; j < 5; j++) {
      // Activate the tab.
      browser()->SelectTabContentsAt(j, true);

      // Activate the location bar or the page.
      views::View* view_to_focus = kFocusPage[i][j] ?
          browser_view->contents_container()->GetFocusView() :
          browser_view->GetLocationBarView();

      ui_controls::MoveMouseToCenterAndPress(view_to_focus,
                                             ui_controls::LEFT,
                                             ui_controls::DOWN |
                                                 ui_controls::UP,
                                             new MessageLoop::QuitTask());
      ui_test_utils::RunMessageLoop();
    }

    // Now come back to the tab and check the right view is focused.
    for (int j = 0; j < 5; j++) {
      // Activate the tab.
      browser()->SelectTabContentsAt(j, true);

      // Activate the location bar or the page.
      views::View* view = kFocusPage[i][j] ?
          browser_view->contents_container()->GetFocusView() :
          browser_view->GetLocationBarView();
      EXPECT_EQ(view, focus_manager->GetFocusedView());
    }
  }
}

// Background window does not steal focus.
IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) {
  HTTPTestServer* server = StartHTTPServer();

  // First we navigate to our test page.
  GURL url = server->TestServerPageW(kSimplePage);
  ui_test_utils::NavigateToURL(browser(), url);

  // Open a new browser window.
  Browser* browser2 = Browser::Create(browser()->profile());
  ASSERT_TRUE(browser2);
  browser2->tabstrip_model()->delegate()->AddBlankTab(true);
  browser2->window()->Show();
  GURL steal_focus_url = server->TestServerPageW(kStealFocusPage);
  ui_test_utils::NavigateToURL(browser2, steal_focus_url);

  // Activate the first browser.
  browser()->window()->Activate();

  // Wait for the focus to be stolen by the other browser.
  ::Sleep(2000);

  // Make sure the first browser is still active.
  HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
  BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd);
  ASSERT_TRUE(browser_view);
  EXPECT_TRUE(browser_view->frame()->GetWindow()->IsActive());

  // Close the 2nd browser to avoid a DCHECK().
  HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle());
  BrowserView* browser_view2 =
      BrowserView::GetBrowserViewForNativeWindow(hwnd2);
  browser_view2->Close();
}

// Page cannot steal focus when focus is on location bar.
IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
  HTTPTestServer* server = StartHTTPServer();

  // Open the page that steals focus.
  GURL url = server->TestServerPageW(kStealFocusPage);
  ui_test_utils::NavigateToURL(browser(), url);

  HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
  BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd);
  views::FocusManager* focus_manager =
      views::FocusManager::GetFocusManager(hwnd);

  // Click on the location bar.
  LocationBarView* location_bar = browser_view->GetLocationBarView();
  ui_controls::MoveMouseToCenterAndPress(location_bar,
                                         ui_controls::LEFT,
                                         ui_controls::DOWN | ui_controls::UP,
                                         new MessageLoop::QuitTask());
  ui_test_utils::RunMessageLoop();

  // Wait for the page to steal focus.
  ::Sleep(2000);

  // Make sure the location bar is still focused.
  EXPECT_EQ(location_bar, focus_manager->GetFocusedView());
}

// Focus traversal on a regular page.
IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversal) {
  HTTPTestServer* server = StartHTTPServer();

  // First we navigate to our test page.
  GURL url = server->TestServerPageW(kTypicalPage);
  ui_test_utils::NavigateToURL(browser(), url);

  HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
  BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd);
  views::FocusManager* focus_manager =
      views::FocusManager::GetFocusManager(hwnd);

  // Click on the location bar.
  LocationBarView* location_bar = browser_view->GetLocationBarView();
  ui_controls::MoveMouseToCenterAndPress(location_bar,
                                         ui_controls::LEFT,
                                         ui_controls::DOWN | ui_controls::UP,
                                         new MessageLoop::QuitTask());
  ui_test_utils::RunMessageLoop();

  const char* kExpElementIDs[] = {
    "",  // Initially no element in the page should be focused
         // (the location bar is focused).
    "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
    "gmapLink"
  };

  // Test forward focus traversal.
  for (int i = 0; i < 3; ++i) {
    // Location bar should be focused.
    EXPECT_EQ(location_bar, focus_manager->GetFocusedView());

    // Now let's press tab to move the focus.
    for (int j = 0; j < 7; ++j) {
      // Let's make sure the focus is on the expected element in the page.
      std::string actual;
      ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
          browser()->GetSelectedTabContents(),
          L"",
          L"window.domAutomationController.send(getFocusedElement());",
          &actual));
      ASSERT_STREQ(kExpElementIDs[j], actual.c_str());

      ui_controls::SendKeyPressNotifyWhenDone(L'\t', false, false, false,
                                              new MessageLoop::QuitTask());
      ui_test_utils::RunMessageLoop();
      // Ideally, we wouldn't sleep here and instead would use the event
      // processed ack notification from the renderer.  I am reluctant to create
      // a new notification/callback for that purpose just for this test.
      ::Sleep(kActionDelayMs);
    }

    // At this point the renderer has sent us a message asking to advance the
    // focus (as the end of the focus loop was reached in the renderer).
    // We need to run the message loop to process it.
    MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
    ui_test_utils::RunMessageLoop();
  }

  // Now let's try reverse focus traversal.
  for (int i = 0; i < 3; ++i) {
    // Location bar should be focused.
    EXPECT_EQ(location_bar, focus_manager->GetFocusedView());

    // Now let's press shift-tab to move the focus in reverse.
    for (int j = 0; j < 7; ++j) {
      ui_controls::SendKeyPressNotifyWhenDone(L'\t', false, true, false,
                                              new MessageLoop::QuitTask());
      ui_test_utils::RunMessageLoop();
      ::Sleep(kActionDelayMs);

      // Let's make sure the focus is on the expected element in the page.
      std::string actual;
      ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
          browser()->GetSelectedTabContents(),
          L"",
          L"window.domAutomationController.send(getFocusedElement());",
          &actual));
      ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
    }

    // At this point the renderer has sent us a message asking to advance the
    // focus (as the end of the focus loop was reached in the renderer).
    // We need to run the message loop to process it.
    MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
    ui_test_utils::RunMessageLoop();
  }
}

// Focus traversal while an interstitial is showing.
IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusTraversalOnInterstitial) {
  HTTPTestServer* server = StartHTTPServer();

  // First we navigate to our test page.
  GURL url = server->TestServerPageW(kSimplePage);
  ui_test_utils::NavigateToURL(browser(), url);

  HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
  BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd);
  views::FocusManager* focus_manager =
      views::FocusManager::GetFocusManager(hwnd);

  // Focus should be on the page.
  EXPECT_EQ(browser_view->contents_container()->GetFocusView(),
            focus_manager->GetFocusedView());

  // Let's show an interstitial.
  TestInterstitialPage* interstitial_page =
      new TestInterstitialPage(browser()->GetSelectedTabContents(),
                               true, GURL("http://interstitial.com"));
  interstitial_page->Show();
  // Give some time for the interstitial to show.
  MessageLoop::current()->PostDelayedTask(FROM_HERE,
                                          new MessageLoop::QuitTask(),
                                          1000);
  ui_test_utils::RunMessageLoop();

  // Click on the location bar.
  LocationBarView* location_bar = browser_view->GetLocationBarView();
  ui_controls::MoveMouseToCenterAndPress(location_bar,
                                         ui_controls::LEFT,
                                         ui_controls::DOWN | ui_controls::UP,
                                         new MessageLoop::QuitTask());
  ui_test_utils::RunMessageLoop();

  const char* kExpElementIDs[] = {
    "",  // Initially no element in the page should be focused
         // (the location bar is focused).
    "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink",
    "gmapLink"
  };

  // Test forward focus traversal.
  for (int i = 0; i < 2; ++i) {
    // Location bar should be focused.
    EXPECT_EQ(location_bar, focus_manager->GetFocusedView());

    // Now let's press tab to move the focus.
    for (int j = 0; j < 7; ++j) {
      // Let's make sure the focus is on the expected element in the page.
      std::string actual = interstitial_page->GetFocusedElement();
      ASSERT_STREQ(kExpElementIDs[j], actual.c_str());

      ui_controls::SendKeyPressNotifyWhenDone(L'\t', false, false, false,
                                              new MessageLoop::QuitTask());
      ui_test_utils::RunMessageLoop();
      // Ideally, we wouldn't sleep here and instead would use the event
      // processed ack notification from the renderer.  I am reluctant to create
      // a new notification/callback for that purpose just for this test.
      ::Sleep(kActionDelayMs);
    }

    // At this point the renderer has sent us a message asking to advance the
    // focus (as the end of the focus loop was reached in the renderer).
    // We need to run the message loop to process it.
    MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
    ui_test_utils::RunMessageLoop();
  }

  // Now let's try reverse focus traversal.
  for (int i = 0; i < 2; ++i) {
    // Location bar should be focused.
    EXPECT_EQ(location_bar, focus_manager->GetFocusedView());

    // Now let's press shift-tab to move the focus in reverse.
    for (int j = 0; j < 7; ++j) {
      ui_controls::SendKeyPressNotifyWhenDone(L'\t', false, true, false,
                                              new MessageLoop::QuitTask());
      ui_test_utils::RunMessageLoop();
      ::Sleep(kActionDelayMs);

      // Let's make sure the focus is on the expected element in the page.
      std::string actual = interstitial_page->GetFocusedElement();
      ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str());
    }

    // At this point the renderer has sent us a message asking to advance the
    // focus (as the end of the focus loop was reached in the renderer).
    // We need to run the message loop to process it.
    MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
    ui_test_utils::RunMessageLoop();
  }
}

// Focus stays on page with interstitials.
IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
  HTTPTestServer* server = StartHTTPServer();

  // First we navigate to our test page.
  GURL url = server->TestServerPageW(kSimplePage);
  ui_test_utils::NavigateToURL(browser(), url);

  HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
  BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd);
  views::FocusManager* focus_manager =
      views::FocusManager::GetFocusManager(hwnd);

  // Page should have focus.
  EXPECT_EQ(browser_view->contents_container()->GetFocusView(),
            focus_manager->GetFocusedView());
  EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
      HasFocus());

  // Let's show an interstitial.
  TestInterstitialPage* interstitial_page =
      new TestInterstitialPage(browser()->GetSelectedTabContents(),
                               true, GURL("http://interstitial.com"));
  interstitial_page->Show();
  // Give some time for the interstitial to show.
  MessageLoop::current()->PostDelayedTask(FROM_HERE,
                                          new MessageLoop::QuitTask(),
                                          1000);
  ui_test_utils::RunMessageLoop();

  // The interstitial should have focus now.
  EXPECT_EQ(browser_view->contents_container()->GetFocusView(),
            focus_manager->GetFocusedView());
  EXPECT_TRUE(interstitial_page->HasFocus());

  // Hide the interstitial.
  interstitial_page->DontProceed();

  // Focus should be back on the original page.
  EXPECT_EQ(browser_view->contents_container()->GetFocusView(),
            focus_manager->GetFocusedView());
  EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()->
      HasFocus());
}

// Make sure Find box can request focus, even when it is already open.
IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) {
  HTTPTestServer* server = StartHTTPServer();

  // Open some page (any page that doesn't steal focus).
  GURL url = server->TestServerPageW(kTypicalPage);
  ui_test_utils::NavigateToURL(browser(), url);

  HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
  BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd);
  views::FocusManager* focus_manager =
      views::FocusManager::GetFocusManager(hwnd);
  LocationBarView* location_bar = browser_view->GetLocationBarView();

  // Press Ctrl+F, which will make the Find box open and request focus.
  static const int VK_F = 0x46;
  ui_controls::SendKeyPressNotifyWhenDone(L'F', true, false, false,
                                          new MessageLoop::QuitTask());
  ui_test_utils::RunMessageLoop();

  // Ideally, we wouldn't sleep here and instead would intercept the
  // RenderViewHostDelegate::HandleKeyboardEvent() callback.  To do that, we
  // could create a RenderViewHostDelegate wrapper and hook-it up by either:
  // - creating a factory used to create the delegate
  // - making the test a private and overwriting the delegate member directly.
  ::Sleep(kActionDelayMs);
  MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
  ui_test_utils::RunMessageLoop();

  views::View* focused_view = focus_manager->GetFocusedView();
  ASSERT_TRUE(focused_view != NULL);
  EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view->GetID());

  // Click on the location bar.
  ui_controls::MoveMouseToCenterAndPress(location_bar,
                                         ui_controls::LEFT,
                                         ui_controls::DOWN | ui_controls::UP,
                                         new MessageLoop::QuitTask());
  ui_test_utils::RunMessageLoop();

  // Make sure the location bar is focused.
  EXPECT_EQ(location_bar, focus_manager->GetFocusedView());

  // Now press Ctrl+F again and focus should move to the Find box.
  ui_controls::SendKeyPressNotifyWhenDone(L'F', true, false, false,
                                          new MessageLoop::QuitTask());
  ui_test_utils::RunMessageLoop();
  focused_view = focus_manager->GetFocusedView();
  ASSERT_TRUE(focused_view != NULL);
  EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view->GetID());

  // Set focus to the page.
  ui_controls::MoveMouseToCenterAndPress(
      browser_view->contents_container()->GetFocusView(),
      ui_controls::LEFT,
      ui_controls::DOWN | ui_controls::UP,
      new MessageLoop::QuitTask());
  ui_test_utils::RunMessageLoop();
  EXPECT_EQ(browser_view->contents_container()->GetFocusView(),
            focus_manager->GetFocusedView());

  // Now press Ctrl+F again and focus should move to the Find box.
  ui_controls::SendKeyPressNotifyWhenDone(VK_F, true, false, false,
                                          new MessageLoop::QuitTask());
  ui_test_utils::RunMessageLoop();

  // See remark above on why we wait.
  ::Sleep(kActionDelayMs);
  MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
  ui_test_utils::RunMessageLoop();

  focused_view = focus_manager->GetFocusedView();
  ASSERT_TRUE(focused_view != NULL);
  EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view->GetID());
}

// Makes sure the focus is in the right location when opening the different
// types of tabs.
IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) {
  HWND hwnd = reinterpret_cast<HWND>(browser()->window()->GetNativeHandle());
  BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(hwnd);
  ASSERT_TRUE(browser_view);
  views::FocusManager* focus_manager =
      views::FocusManager::GetFocusManager(hwnd);
  ASSERT_TRUE(focus_manager);

  // Open the history tab, focus should be on the tab contents.
  browser()->ShowHistoryTab();
  EXPECT_EQ(browser_view->contents_container()->GetFocusView(),
            focus_manager->GetFocusedView());

  // Open the new tab, focus should be on the location bar.
  browser()->NewTab();
  EXPECT_EQ(browser_view->GetLocationBarView(),
            focus_manager->GetFocusedView());

  // Open the download tab, focus should be on the tab contents.
  browser()->ShowDownloadsTab();
  EXPECT_EQ(browser_view->contents_container()->GetFocusView(),
            focus_manager->GetFocusedView());
}