summaryrefslogtreecommitdiffstats
path: root/chrome/browser/browser_focus_uitest.cc
blob: ca680349725bea15e560f9bb673278dc352d5fc6 (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
// 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.

#include "chrome/browser/view_ids.h"
#include "chrome/views/view.h"
#include "chrome/test/automation/browser_proxy.h"
#include "chrome/test/automation/window_proxy.h"
#include "chrome/test/automation/tab_proxy.h"
#include "chrome/test/ui/ui_test.h"
#include "net/url_request/url_request_unittest.h"

namespace {

// The delay waited after sending an OS simulated event.
const int kActionDelayMs = 500;

const wchar_t kDocRoot[] = L"chrome/test/data";

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";

class BrowserFocusTest : public UITest {
 public:
  BrowserFocusTest() {
    show_window_ = true;
    dom_automation_enabled_ = true;
  }
};

// Activate a tab by clicking on it.  Returns true if the call was successful
// (meaning the messages were correctly sent, but does not guarantee the tab
// has been changed).
bool ActivateTabByClick(AutomationProxy* automation,
                        WindowProxy* browser_window,
                        int tab_index) {
  // Click on the tab.
  gfx::Rect bounds;

  if (!browser_window->GetViewBounds(VIEW_ID_TAB_0 + tab_index, &bounds, true))
    return false;

  POINT click(bounds.CenterPoint().ToPOINT());
  if (!browser_window->SimulateOSClick(click,
                                       views::Event::EF_LEFT_BUTTON_DOWN))
    return false;

  // Wait a bit to let the click be processed.
  ::Sleep(kActionDelayMs);

  return true;
}

}  // namespace

TEST_F(BrowserFocusTest, BrowsersRememberFocus) {
  scoped_refptr<HTTPTestServer> server =
      HTTPTestServer::CreateServer(kDocRoot, NULL);
  ASSERT_TRUE(NULL != server.get());

  // First we navigate to our test page.
  GURL url = server->TestServerPageW(kSimplePage);
  scoped_ptr<TabProxy> tab(GetActiveTab());
  EXPECT_NE(AUTOMATION_MSG_NAVIGATION_ERROR, tab->NavigateToURL(url));

  // The focus should be on the Tab contents.
  scoped_ptr<WindowProxy> window(automation()->GetActiveWindow());
  ASSERT_TRUE(window.get() != NULL);

  scoped_ptr<BrowserProxy> browser(window->GetBrowser());
  ASSERT_TRUE(browser.get() != NULL);

  int focused_view_id;
  EXPECT_TRUE(window->GetFocusedViewID(&focused_view_id));
  EXPECT_EQ(VIEW_ID_TAB_CONTAINER, focused_view_id);

  // Now hide the window, show it again, the focus should not have changed.
  EXPECT_TRUE(window->SetVisible(false));
  EXPECT_TRUE(window->SetVisible(true));
  EXPECT_TRUE(window->GetFocusedViewID(&focused_view_id));
  EXPECT_EQ(VIEW_ID_TAB_CONTAINER, focused_view_id);

  // Click on the location bar.
  gfx::Rect bounds;
  EXPECT_TRUE(window->GetViewBounds(VIEW_ID_LOCATION_BAR, &bounds, false));
  POINT click(bounds.CenterPoint().ToPOINT());

  EXPECT_TRUE(window->SimulateOSClick(click,
      views::Event::EF_LEFT_BUTTON_DOWN));
  ::Sleep(kActionDelayMs);
  EXPECT_TRUE(window->GetFocusedViewID(&focused_view_id));
  EXPECT_EQ(VIEW_ID_LOCATION_BAR, focused_view_id);

  // Hide the window, show it again, the focus should not have changed.
  EXPECT_TRUE(window->SetVisible(false));
  EXPECT_TRUE(window->SetVisible(true));
  EXPECT_TRUE(window->GetFocusedViewID(&focused_view_id));
  EXPECT_EQ(VIEW_ID_LOCATION_BAR, focused_view_id);

  // Open a new browser window.
  EXPECT_TRUE(automation()->OpenNewBrowserWindow(SW_SHOWNORMAL));
  scoped_ptr<WindowProxy> new_window(automation()->GetActiveWindow());
  ASSERT_TRUE(new_window.get() != NULL);
  scoped_ptr<BrowserProxy> new_browser(new_window->GetBrowser());
  ASSERT_TRUE(new_browser.get() != NULL);

  // Let's make sure we have 2 different browser windows.
  EXPECT_TRUE(browser->handle() != new_browser->handle());

  tab.reset(new_browser->GetActiveTab());
  EXPECT_TRUE(tab.get());
  tab->NavigateToURL(url);

  // Switch to the 1st browser window, focus should still be on the location
  // bar and the second browser should have nothing focused.
  EXPECT_TRUE(window->Activate());
  EXPECT_TRUE(window->GetFocusedViewID(&focused_view_id));
  EXPECT_EQ(VIEW_ID_LOCATION_BAR, focused_view_id);
  EXPECT_TRUE(new_window->GetFocusedViewID(&focused_view_id));
  EXPECT_EQ(-1, focused_view_id);

  // Switch back to the second browser, focus should still be on the page.
  EXPECT_TRUE(new_window->Activate());
  EXPECT_TRUE(new_window->GetFocusedViewID(&focused_view_id));
  EXPECT_EQ(VIEW_ID_TAB_CONTAINER, focused_view_id);
  EXPECT_TRUE(window->GetFocusedViewID(&focused_view_id));
  EXPECT_EQ(-1, focused_view_id);
}

// Tabs remember focus.
TEST_F(BrowserFocusTest, TabsRememberFocus) {
  scoped_refptr<HTTPTestServer> server =
      HTTPTestServer::CreateServer(kDocRoot, NULL);
  ASSERT_TRUE(NULL != server.get());

  scoped_ptr<WindowProxy> window(automation()->GetActiveWindow());
  ASSERT_TRUE(window.get() != NULL);
  scoped_ptr<BrowserProxy> browser(window->GetBrowser());
  ASSERT_TRUE(browser.get() != NULL);

  // First we navigate to our test page.
  GURL url = server->TestServerPageW(kSimplePage);
  scoped_ptr<TabProxy> tab(GetActiveTab());
  tab->NavigateToURL(url);

  // Create several tabs.
  EXPECT_TRUE(browser->AppendTab(url));
  EXPECT_TRUE(browser->AppendTab(url));
  EXPECT_TRUE(browser->AppendTab(url));
  EXPECT_TRUE(browser->AppendTab(url));

  int tab_count;
  EXPECT_TRUE(browser->GetTabCount(&tab_count));
  ASSERT_EQ(5, tab_count);

  // 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++) {
      // Click on the tab.
      ActivateTabByClick(automation(), window.get(), j);

      // Activate the location bar or the page.
      int view_id = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER :
                                       VIEW_ID_LOCATION_BAR;

      gfx::Rect bounds;
      EXPECT_TRUE(window->GetViewBounds(view_id, &bounds, true));
      POINT click(bounds.CenterPoint().ToPOINT());
      EXPECT_TRUE(window->SimulateOSClick(click,
          views::Event::EF_LEFT_BUTTON_DOWN));
      ::Sleep(kActionDelayMs);
    }

    // Now come back to the tab and check the right view is focused.
    for (int j = 0; j < 5; j++) {
      // Click on the tab.
      ActivateTabByClick(automation(), window.get(), j);

      // Activate the location bar or the page.
      int exp_view_id = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER :
                                           VIEW_ID_LOCATION_BAR;
      int focused_view_id;
      EXPECT_TRUE(window->GetFocusedViewID(&focused_view_id));
      EXPECT_EQ(exp_view_id, focused_view_id);
    }
  }
}

// Background window does not steal focus.
TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) {
  scoped_refptr<HTTPTestServer> server =
      HTTPTestServer::CreateServer(kDocRoot, NULL);
  ASSERT_TRUE(NULL != server.get());

  // First we navigate to our test page.
  GURL simple_page_url = server->TestServerPageW(kSimplePage);
  scoped_ptr<TabProxy> tab(GetActiveTab());
  tab->NavigateToURL(simple_page_url);

  scoped_ptr<WindowProxy> window(automation()->GetActiveWindow());
  ASSERT_TRUE(window.get() != NULL);
  scoped_ptr<BrowserProxy> browser(window->GetBrowser());
  ASSERT_TRUE(browser.get() != NULL);

  // Open a new browser window.
  EXPECT_TRUE(automation()->OpenNewBrowserWindow(SW_SHOWNORMAL));
  scoped_ptr<WindowProxy> new_window(automation()->GetActiveWindow());
  ASSERT_TRUE(window.get() != NULL);
  scoped_ptr<BrowserProxy> new_browser(new_window->GetBrowser());
  ASSERT_TRUE(new_browser.get() != NULL);

  GURL steal_focus_url = server->TestServerPageW(kStealFocusPage);
  new_browser->AppendTab(steal_focus_url);

  // Make the first browser active
  EXPECT_TRUE(window->Activate());

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

  // Make sure the 1st browser window is still active.
  bool is_active = false;
  EXPECT_TRUE(window->IsActive(&is_active));
  EXPECT_TRUE(is_active);
}

// Page cannot steal focus when focus is on location bar.
TEST_F(BrowserFocusTest, LocationBarLockFocus) {
  scoped_refptr<HTTPTestServer> server =
      HTTPTestServer::CreateServer(kDocRoot, NULL);
  ASSERT_TRUE(NULL != server.get());

  // Open the page that steals focus.
  GURL url = server->TestServerPageW(kStealFocusPage);
  scoped_ptr<TabProxy> tab(GetActiveTab());
  tab->NavigateToURL(url);

  scoped_ptr<WindowProxy> window(automation()->GetActiveWindow());
  ASSERT_TRUE(window.get() != NULL);
  scoped_ptr<BrowserProxy> browser(window->GetBrowser());
  ASSERT_TRUE(browser.get() != NULL);

  // Click on the location bar.
  gfx::Rect bounds;
  EXPECT_TRUE(window->GetViewBounds(VIEW_ID_LOCATION_BAR, &bounds, true));
  POINT click(bounds.CenterPoint().ToPOINT());
  EXPECT_TRUE(window->SimulateOSClick(click,
      views::Event::EF_LEFT_BUTTON_DOWN));
  ::Sleep(kActionDelayMs);

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

  // Make sure the location bar is still focused.
  int focused_view_id;
  EXPECT_TRUE(window->GetFocusedViewID(&focused_view_id));
  EXPECT_EQ(VIEW_ID_LOCATION_BAR, focused_view_id);
}

// Focus traversal
TEST_F(BrowserFocusTest, FocusTraversal) {
  scoped_refptr<HTTPTestServer> server =
      HTTPTestServer::CreateServer(kDocRoot, NULL);
  ASSERT_TRUE(NULL != server.get());

  // Open the page the test page.
  GURL url = server->TestServerPageW(kTypicalPage);
  scoped_ptr<TabProxy> tab(GetActiveTab());
  tab->NavigateToURL(url);

  scoped_ptr<WindowProxy> window(automation()->GetActiveWindow());
  ASSERT_TRUE(window.get() != NULL);
  scoped_ptr<BrowserProxy> browser(window->GetBrowser());
  ASSERT_TRUE(browser.get() != NULL);

  // Click on the location bar.
  gfx::Rect bounds;
  EXPECT_TRUE(window->GetViewBounds(VIEW_ID_LOCATION_BAR, &bounds, true));
  POINT click(bounds.CenterPoint().ToPOINT());
  EXPECT_TRUE(window->SimulateOSClick(click,
      views::Event::EF_LEFT_BUTTON_DOWN));
  ::Sleep(kActionDelayMs);

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

  // Test forward focus traversal.
  for (int i = 0; i < 3; ++i) {
    // Location bar should be focused.
    int focused_view_id;
    EXPECT_TRUE(window->GetFocusedViewID(&focused_view_id));
    EXPECT_EQ(VIEW_ID_LOCATION_BAR, focused_view_id);

    // 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::wstring actual;
      ASSERT_TRUE(tab->ExecuteAndExtractString(L"",
          L"window.domAutomationController.send(getFocusedElement());",
          &actual));
      ASSERT_STREQ(kExpElementIDs[j], actual.c_str());

      window->SimulateOSKeyPress(L'\t', 0);
      ::Sleep(kActionDelayMs);
    }
  }

  // Now let's try reverse focus traversal.
  for (int i = 0; i < 3; ++i) {
    // Location bar should be focused.
    int focused_view_id;
    EXPECT_TRUE(window->GetFocusedViewID(&focused_view_id));
    EXPECT_EQ(VIEW_ID_LOCATION_BAR, focused_view_id);

    // Now let's press tab to move the focus.
    for (int j = 0; j < 7; ++j) {
      window->SimulateOSKeyPress(L'\t', views::Event::EF_SHIFT_DOWN);
      ::Sleep(kActionDelayMs);

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

// Make sure Find box can request focus, even when it is already open.
TEST_F(BrowserFocusTest, FindFocusTest) {
  scoped_refptr<HTTPTestServer> server =
      HTTPTestServer::CreateServer(kDocRoot, NULL);
  ASSERT_TRUE(NULL != server.get());

  // Open some page (any page that doesn't steal focus).
  GURL url = server->TestServerPageW(kTypicalPage);
  scoped_ptr<TabProxy> tab(GetActiveTab());
  tab->NavigateToURL(url);

  scoped_ptr<WindowProxy> window(automation()->GetActiveWindow());
  ASSERT_TRUE(window.get() != NULL);
  scoped_ptr<BrowserProxy> browser(window->GetBrowser());
  ASSERT_TRUE(browser.get() != NULL);

  // Press Ctrl+F, which will make the Find box open and request focus.
  static const int VK_F = 0x46;
  EXPECT_TRUE(window->SimulateOSKeyPress(VK_F, views::Event::EF_CONTROL_DOWN));
  ::Sleep(kActionDelayMs);
  int focused_view_id;
  EXPECT_TRUE(window->GetFocusedViewID(&focused_view_id));
  EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view_id);

  // Click on the location bar.
  gfx::Rect bounds;
  EXPECT_TRUE(window->GetViewBounds(VIEW_ID_LOCATION_BAR, &bounds, true));
  POINT click(bounds.CenterPoint().ToPOINT());
  EXPECT_TRUE(window->SimulateOSClick(click,
              views::Event::EF_LEFT_BUTTON_DOWN));
  ::Sleep(kActionDelayMs);
  // Make sure the location bar is focused.
  EXPECT_TRUE(window->GetFocusedViewID(&focused_view_id));
  EXPECT_EQ(VIEW_ID_LOCATION_BAR, focused_view_id);

  // Now press Ctrl+F again and focus should move to the Find box.
  EXPECT_TRUE(window->SimulateOSKeyPress(VK_F, views::Event::EF_CONTROL_DOWN));
  ::Sleep(kActionDelayMs);
  EXPECT_TRUE(window->GetFocusedViewID(&focused_view_id));
  EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view_id);

  // Set focus to the page.
  EXPECT_TRUE(window->GetViewBounds(VIEW_ID_TAB_CONTAINER, &bounds, true));
  click = bounds.CenterPoint().ToPOINT();
  EXPECT_TRUE(window->SimulateOSClick(click,
                                      views::Event::EF_LEFT_BUTTON_DOWN));
  ::Sleep(kActionDelayMs);
  EXPECT_TRUE(window->GetFocusedViewID(&focused_view_id));
  EXPECT_EQ(VIEW_ID_TAB_CONTAINER, focused_view_id);

  // Now press Ctrl+F again and focus should move to the Find box.
  EXPECT_TRUE(window->SimulateOSKeyPress(VK_F, views::Event::EF_CONTROL_DOWN));
  ::Sleep(kActionDelayMs);
  EXPECT_TRUE(window->GetFocusedViewID(&focused_view_id));
  EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, focused_view_id);
}