summaryrefslogtreecommitdiffstats
path: root/chrome/test/ui/ui_test.cc
blob: dec0ff2397b4a260db0bedd12faeb8b779fbb60e (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
// 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 <set>
#include <vector>

#include "chrome/test/ui/ui_test.h"

#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/process_util.h"
#include "base/scoped_ptr.h"
#include "base/string_util.h"
#include "base/time.h"
#include "chrome/browser/url_fixer_upper.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_process_filter.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/debug_flags.h"
#include "chrome/common/logging_chrome.h"
#include "chrome/common/json_value_serializer.h"
#include "chrome/test/automation/browser_proxy.h"
#include "chrome/test/automation/tab_proxy.h"
#include "chrome/test/automation/window_proxy.h"
#include "chrome/test/test_file_util.h"
#include "googleurl/src/gurl.h"

bool UITest::in_process_renderer_ = false;
bool UITest::in_process_plugins_ = false;
bool UITest::no_sandbox_ = false;
bool UITest::full_memory_dump_ = false;
bool UITest::safe_plugins_ = false;
bool UITest::show_error_dialogs_ = true;
bool UITest::default_use_existing_browser_ = false;
bool UITest::dump_histograms_on_exit_ = false;
bool UITest::enable_dcheck_ = false;
bool UITest::silent_dump_on_dcheck_ = false;
bool UITest::disable_breakpad_ = false;
int UITest::timeout_ms_ = 20 * 60 * 1000;

// Uncomment this line to have the spawned process wait for the debugger to
// attach.
// #define WAIT_FOR_DEBUGGER_ON_OPEN 1

// static
bool UITest::DieFileDie(const std::wstring& file, bool recurse) {
  if (!file_util::PathExists(file))
    return true;

  // Sometimes Delete fails, so try a few more times.
  for (int i = 0; i < 10; ++i) {
    if (file_util::Delete(file, recurse))
      return true;
    Sleep(kWaitForActionMaxMsec / 10);
  }
  return false;
}

UITest::UITest()
    : testing::Test(),
      expected_errors_(0),
      expected_crashes_(0),
      wait_for_initial_loads_(true),
      homepage_(L"about:blank"),
      dom_automation_enabled_(false),
      process_(NULL),
      show_window_(false),
      clear_profile_(true),
      include_testing_id_(true),
      use_existing_browser_(default_use_existing_browser_) {
  PathService::Get(chrome::DIR_APP, &browser_directory_);
  PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_);
  GetSystemTimeAsFileTime(&test_start_time_);
}

void UITest::SetUp() {
  if (!use_existing_browser_) {
    AssertAppNotRunning(L"Please close any other instances "
                        L"of the app before testing.");
  }

  LaunchBrowserAndServer();
}

void UITest::TearDown() {
  CloseBrowserAndServer();

  // Make sure that we didn't encounter any assertion failures
  logging::AssertionList assertions;
  logging::GetFatalAssertions(&assertions);

  // If there were errors, get all the error strings for display.
  std::wstring failures =
    L"The following error(s) occurred in the application during this test:";
  if (static_cast<int>(assertions.size()) > expected_errors_) {
    logging::AssertionList::const_iterator iter = assertions.begin();
    for (; iter != assertions.end(); ++iter) {
      failures.append(L"\n\n");
      failures.append(*iter);
    }
  }
  EXPECT_EQ(expected_errors_, assertions.size()) << failures;

  // Check for crashes during the test
  std::wstring crash_dump_path;
  PathService::Get(chrome::DIR_CRASH_DUMPS, &crash_dump_path);
  // Each crash creates two dump files, so we divide by two here.
  int actual_crashes =
    file_util::CountFilesCreatedAfter(crash_dump_path, test_start_time_) / 2;
  std::wstring error_msg =
      L"Encountered an unexpected crash in the program during this test.";
  if (expected_crashes_ > 0 && actual_crashes == 0)
    error_msg += L"  Have you started crash_service.exe?";
  EXPECT_EQ(expected_crashes_, actual_crashes) << error_msg;
}

void UITest::LaunchBrowserAndServer() {
  // Set up IPC testing interface server.
  server_.reset(new AutomationProxy);

  LaunchBrowser(launch_arguments_, clear_profile_);
  if (wait_for_initial_loads_)
    ASSERT_TRUE(server_->WaitForInitialLoads());
  else
    Sleep(2000);

  automation()->SetFilteredInet(true);
}

void UITest::CloseBrowserAndServer() {
  QuitBrowser();
  CleanupAppProcesses();

  // Shut down IPC testing interface.
  server_.reset();
}

void UITest::LaunchBrowser(const std::wstring& arguments, bool clear_profile) {
  std::wstring command_line(browser_directory_);
  file_util::AppendToPath(&command_line,
                          chrome::kBrowserProcessExecutableName);

  // We need cookies on file:// for things like the page cycler.
  CommandLine::AppendSwitch(&command_line, switches::kEnableFileCookies);

  if (dom_automation_enabled_)
    CommandLine::AppendSwitch(&command_line,
                              switches::kDomAutomationController);

  if (include_testing_id_) {
    if (use_existing_browser_) {
      // TODO(erikkay): The new switch depends on a browser instance already
      // running, it won't open a new browser window if it's not.  We could fix
      // this by passing an url (e.g. about:blank) on the command line, but
      // I decided to keep using the old switch in the existing use case to
      // minimize changes in behavior.
      CommandLine::AppendSwitchWithValue(&command_line,
                                         switches::kAutomationClientChannelID,
                                         server_->channel_id());
    } else {
      CommandLine::AppendSwitchWithValue(&command_line,
                                         switches::kTestingChannelID,
                                         server_->channel_id());
    }
  }

  if (!show_error_dialogs_)
    CommandLine::AppendSwitch(&command_line, switches::kNoErrorDialogs);
  if (in_process_renderer_)
    CommandLine::AppendSwitch(&command_line, switches::kSingleProcess);
  if (in_process_plugins_)
    CommandLine::AppendSwitch(&command_line, switches::kInProcessPlugins);
  if (no_sandbox_)
    CommandLine::AppendSwitch(&command_line, switches::kNoSandbox);
  if (full_memory_dump_)
    CommandLine::AppendSwitch(&command_line, switches::kFullMemoryCrashReport);
  if (safe_plugins_)
    CommandLine::AppendSwitch(&command_line, switches::kSafePlugins);
  if (enable_dcheck_)
    CommandLine::AppendSwitch(&command_line, switches::kEnableDCHECK);
  if (silent_dump_on_dcheck_)
    CommandLine::AppendSwitch(&command_line, switches::kSilentDumpOnDCHECK);
  if (disable_breakpad_)
    CommandLine::AppendSwitch(&command_line, switches::kDisableBreakpad);
  if (!homepage_.empty())
    CommandLine::AppendSwitchWithValue(&command_line,
                                       switches::kHomePage,
                                       homepage_);
  PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_);
  if (!user_data_dir_.empty())
    CommandLine::AppendSwitchWithValue(&command_line,
                                       switches::kUserDataDir,
                                       user_data_dir_);

  CommandLine::AppendSwitch(&command_line, switches::kDisableMetricsReporting);

  // We always want to enable chrome logging
  CommandLine::AppendSwitch(&command_line, switches::kEnableLogging);

  if (dump_histograms_on_exit_)
    CommandLine::AppendSwitch(&command_line, switches::kDumpHistogramsOnExit);

#ifdef WAIT_FOR_DEBUGGER_ON_OPEN
  CommandLine::AppendSwitch(&command_line, switches::kDebugOnStart);
#endif

  DebugFlags::ProcessDebugFlags(&command_line, DebugFlags::UNKNOWN, false);
  command_line.append(L" " + arguments);

  // Clear user data directory to make sure test environment is consistent
  // We balk on really short (absolute) user_data_dir directory names, because
  // we're worried that they'd accidentally be root or something.
  ASSERT_LT(10, static_cast<int>(user_data_dir_.size())) <<
                "The user data directory name passed into this test was too "
                "short to delete safely.  Please check the user-data-dir "
                "argument and try again.";
  if (clear_profile)
    ASSERT_TRUE(DieFileDie(user_data_dir_, true));

  if (!template_user_data_.empty()) {
    // Recursively copy the template directory to the user_data_dir.
    ASSERT_TRUE(file_util::CopyRecursiveDirNoCache(template_user_data_,
                                                   user_data_dir_));
  }

  browser_launch_time_ = TimeTicks::Now();

  bool started = process_util::LaunchApp(
                     command_line,
                     false,  // Don't wait for process object (doesn't work for
                             // us)
                     !show_window_,
                     &process_);
  ASSERT_EQ(started, true);

  if (use_existing_browser_) {
    DWORD pid = 0;
    HWND hwnd = FindWindowEx(HWND_MESSAGE, NULL, chrome::kMessageWindowClass,
                         user_data_dir_.c_str());
    GetWindowThreadProcessId(hwnd, &pid);
    // This mode doesn't work if we wound up launching a new browser ourselves.
    ASSERT_NE(pid, process_util::GetProcId(process_));
    CloseHandle(process_);
    process_ = OpenProcess(SYNCHRONIZE, false, pid);
  }
}

void UITest::QuitBrowser() {
  typedef std::vector<HWND> HandleVector;

  // There's nothing to do here if the browser is not running.
  if (IsBrowserRunning()) {
    automation()->SetFilteredInet(false);
    HandleVector handles;

    // Build up a list of HWNDs; we do this as a separate step so that closing
    // the windows doesn't mess up the iteration.
    int window_count = 0;
    EXPECT_TRUE(automation()->GetBrowserWindowCount(&window_count));

    for (int i = 0; i < window_count; ++i) {
      HWND window_handle;
      scoped_ptr<BrowserProxy> browser(automation()->GetBrowserWindow(i));
      scoped_ptr<WindowProxy> window(
          automation()->GetWindowForBrowser(browser.get()));
      EXPECT_TRUE(window->GetHWND(&window_handle));
      handles.push_back(window_handle);
    }

    for (HandleVector::iterator iter = handles.begin(); iter != handles.end();
      ++iter) {
      ::PostMessage(*iter, WM_CLOSE, 0, 0);
    }
    
    // Now, drop the automation IPC channel so that the automation provider in
    // the browser notices and drops its reference to the browser process.
    server_->Disconnect();

    // Wait for the browser process to quit. It should quit once all tabs have
    // been closed.
    int timeout = 5000;
#ifdef WAIT_FOR_DEBUGGER_ON_OPEN
    timeout = 500000;
#endif
    if (WAIT_TIMEOUT == WaitForSingleObject(process_, timeout)) {
      // We need to force the browser to quit because it didn't quit fast
      // enough. Take no chance and kill every chrome processes.
      CleanupAppProcesses();
    }
  }

  // Don't forget to close the handle
  CloseHandle(process_);
  process_ = NULL;
}

void UITest::AssertAppNotRunning(const std::wstring& error_message) {
  ASSERT_EQ(0, GetBrowserProcessCount()) << error_message;
}

void UITest::CleanupAppProcesses() {
  BrowserProcessFilter filter(L"");

  // Make sure that no instances of the browser remain.
  const int kExitTimeoutMs = 5000;
  const int kExitCode = 1;
  process_util::CleanupProcesses(
    chrome::kBrowserProcessExecutableName, kExitTimeoutMs, kExitCode,
    &filter);

  // Suppress spammy failures that seem to be occurring when running
  // the UI tests in single-process mode.
  // TODO(jhughes): figure out why this is necessary at all, and fix it
  if (!in_process_renderer_) {
    AssertAppNotRunning(L"Unable to quit all browser processes.");
  }
}

TabProxy* UITest::GetActiveTab() {
  scoped_ptr<BrowserProxy> window_proxy(automation()->GetBrowserWindow(0));
  if (!window_proxy.get())
    return NULL;

  int active_tab_index = -1;
  EXPECT_TRUE(window_proxy->GetActiveTabIndex(&active_tab_index));
  if (active_tab_index == -1)
    return NULL;

  return window_proxy->GetTab(active_tab_index);
}

void UITest::NavigateToURL(const GURL& url) {
  scoped_ptr<TabProxy> tab_proxy(GetActiveTab());
  ASSERT_TRUE(tab_proxy.get());
  if (!tab_proxy.get())
    return;

  bool is_timeout = true;
  ASSERT_TRUE(tab_proxy->NavigateToURLWithTimeout(url, kMaxTestExecutionTime,
                                                  &is_timeout)) << url.spec();
  ASSERT_FALSE(is_timeout) << url.spec();
}

bool UITest::WaitForDownloadShelfVisible(TabProxy* tab) {
  const int kCycles = 20;
  for (int i = 0; i < kCycles; i++) {
    bool visible = false;
    if (!tab->IsShelfVisible(&visible))
      return false;  // Some error.
    if (visible)
      return true;  // Got the download shelf.

    // Give it a chance to catch up.
    Sleep(kWaitForActionMaxMsec / kCycles);
  }
  return false;
}

GURL UITest::GetActiveTabURL() {
  scoped_ptr<TabProxy> tab_proxy(GetActiveTab());
  if (!tab_proxy.get())
    return GURL();

  GURL url;
  if (!tab_proxy->GetCurrentURL(&url))
    return GURL();
  return url;
}

std::wstring UITest::GetActiveTabTitle() {
  std::wstring title;
  scoped_ptr<TabProxy> tab_proxy(GetActiveTab());
  if (!tab_proxy.get())
    return title;

  EXPECT_TRUE(tab_proxy->GetTabTitle(&title));
  return title;
}

bool UITest::IsBrowserRunning() {
  return CrashAwareSleep(0);
}

bool UITest::CrashAwareSleep(int time_out_ms) {
  return WAIT_TIMEOUT == WaitForSingleObject(process_, time_out_ms);
}

/*static*/
int UITest::GetBrowserProcessCount() {
  BrowserProcessFilter filter(L"");
  return process_util::GetProcessCount(chrome::kBrowserProcessExecutableName,
                                       &filter);
}

static DictionaryValue* LoadDictionaryValueFromPath(const std::wstring& path) {
  if (path.empty())
    return NULL;

  JSONFileValueSerializer serializer(path);
  Value* root_value = NULL;
  if (serializer.Deserialize(&root_value) &&
      root_value->GetType() != Value::TYPE_DICTIONARY) {
    delete root_value;
    return NULL;
  }
  return static_cast<DictionaryValue*>(root_value);
}

DictionaryValue* UITest::GetLocalState() {
  std::wstring local_state_path;
  PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
  return LoadDictionaryValueFromPath(local_state_path);
}

DictionaryValue* UITest::GetDefaultProfilePreferences() {
   std::wstring path;
  PathService::Get(chrome::DIR_USER_DATA, &path);
  file_util::AppendToPath(&path, chrome::kNotSignedInProfile);
  file_util::AppendToPath(&path, chrome::kPreferencesFilename);
  return LoadDictionaryValueFromPath(path);
}

int UITest::GetTabCount() {
  scoped_ptr<BrowserProxy> first_window(automation()->GetBrowserWindow(0));
  if (!first_window.get())
    return 0;

  int result = 0;
  EXPECT_TRUE(first_window->GetTabCount(&result));

  return result;
}

bool UITest::WaitUntilCookieValue(TabProxy* tab,
                                  const GURL& url,
                                  const char* cookie_name,
                                  int interval_ms,
                                  int time_out_ms,
                                  const char* expected_value) {
  const int kMaxIntervals = time_out_ms / interval_ms;

  std::string cookie_value;
  bool completed = false;
  for (int i = 0; i < kMaxIntervals; ++i) {
    bool browser_survived = CrashAwareSleep(interval_ms);

    tab->GetCookieByName(url, cookie_name, &cookie_value);

    if (cookie_value == expected_value) {
      completed = true;
      break;
    }
    EXPECT_TRUE(browser_survived);
    if (!browser_survived) {
      // The browser process died.
      break;
    }
  }
  return completed;
}

std::string UITest::WaitUntilCookieNonEmpty(TabProxy* tab,
                                            const GURL& url,
                                            const char* cookie_name,
                                            int interval_ms,
                                            int time_out_ms) {
  const int kMaxIntervals = time_out_ms / interval_ms;

  std::string cookie_value;
  for (int i = 0; i < kMaxIntervals; ++i) {
    bool browser_survived = CrashAwareSleep(interval_ms);

    tab->GetCookieByName(url, cookie_name, &cookie_value);

    if (!cookie_value.empty())
      break;

    EXPECT_TRUE(browser_survived);
    if (!browser_survived) {
      // The browser process died.
      break;
    }
  }

  return cookie_value;
}

void UITest::WaitUntilTabCount(int tab_count) {
  for (int i = 0; i < 10; ++i) {
    Sleep(kWaitForActionMaxMsec / 10);
    if (GetTabCount() == tab_count)
      break;
  }
  EXPECT_EQ(tab_count, GetTabCount());
}

std::wstring UITest::GetDownloadDirectory() {
  scoped_ptr<TabProxy> tab_proxy(GetActiveTab());
  if (!tab_proxy.get())
    return false;

  std::wstring download_directory;
  EXPECT_TRUE(tab_proxy->GetDownloadDirectory(&download_directory));
  return download_directory;
}

bool UITest::CloseBrowser(BrowserProxy* browser,
                          bool* application_closed) const {
  DCHECK(application_closed);
  if (!browser->is_valid() || !browser->handle())
    return false;

  IPC::Message* response = NULL;
  bool succeeded = server_->SendAndWaitForResponse(
      new AutomationMsg_CloseBrowserRequest(0, browser->handle()),
      &response, AutomationMsg_CloseBrowserResponse__ID);

  if (!succeeded)
    return false;

  void* iter = NULL;
  bool result = true;
  response->ReadBool(&iter, &result);
  response->ReadBool(&iter, application_closed);

  if (*application_closed) {
    // Let's wait until the process dies (if it is not gone already).
    int r = WaitForSingleObject(process_, INFINITE);
    DCHECK(r != WAIT_FAILED);
  }

  delete response;
  return result;
}

void UITest::PrintResult(const std::wstring& measurement,
                         const std::wstring& modifier,
                         const std::wstring& trace,
                         size_t value,
                         const std::wstring& units,
                         bool important) {
  wprintf(L"%lsRESULT %ls%ls: %ls= %d %ls\n",
          important ? L"*" : L"", measurement.c_str(), modifier.c_str(),
          trace.c_str(), value, units.c_str());
}