logging_chrome_uitest.cc revision 06741cbc25cd4227a9fba40dfd0273bfcc1a587a
1// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "build/build_config.h"
6
7#if defined(OS_WIN)
8#include <windows.h>
9#endif
10
11#include <string>
12
13#include "base/basictypes.h"
14#include "base/command_line.h"
15#include "base/env_var.h"
16#include "chrome/common/chrome_switches.h"
17#include "chrome/common/env_vars.h"
18#include "chrome/common/logging_chrome.h"
19#include "chrome/test/automation/browser_proxy.h"
20#include "chrome/test/ui/ui_test.h"
21#include "testing/gtest/include/gtest/gtest.h"
22
23class ChromeLoggingTest : public testing::Test {
24 public:
25  // Stores the current value of the log file name environment
26  // variable and sets the variable to new_value.
27  void SaveEnvironmentVariable(std::string new_value) {
28    scoped_ptr<base::EnvVarGetter> env(base::EnvVarGetter::Create());
29    if (!env->GetEnv(env_vars::kLogFileName, &environment_filename_))
30      environment_filename_ = "";
31
32    env->SetEnv(env_vars::kLogFileName, new_value);
33  }
34
35  // Restores the value of the log file nave environment variable
36  // previously saved by SaveEnvironmentVariable().
37  void RestoreEnvironmentVariable() {
38    scoped_ptr<base::EnvVarGetter> env(base::EnvVarGetter::Create());
39    env->SetEnv(env_vars::kLogFileName, environment_filename_);
40  }
41
42 private:
43  std::string environment_filename_;  // Saves real environment value.
44};
45
46// Tests the log file name getter without an environment variable.
47TEST_F(ChromeLoggingTest, LogFileName) {
48  SaveEnvironmentVariable("");
49
50  FilePath filename = logging::GetLogFileName();
51  ASSERT_NE(FilePath::StringType::npos,
52            filename.value().find(FILE_PATH_LITERAL("chrome_debug.log")));
53
54  RestoreEnvironmentVariable();
55}
56
57// Tests the log file name getter with an environment variable.
58TEST_F(ChromeLoggingTest, EnvironmentLogFileName) {
59  SaveEnvironmentVariable("test value");
60
61  FilePath filename = logging::GetLogFileName();
62  ASSERT_EQ(FilePath(FILE_PATH_LITERAL("test value")).value(),
63            filename.value());
64
65  RestoreEnvironmentVariable();
66}
67
68#if defined(OS_LINUX) && (!defined(NDEBUG) || !defined(USE_LINUX_BREAKPAD))
69// On Linux in Debug mode, Chrome generates a SIGTRAP.
70// we do not catch SIGTRAPs, thus no crash dump.
71// This also does not work if Breakpad is disabled.
72#define EXPECTED_ASSERT_CRASHES 0
73#else
74#define EXPECTED_ASSERT_CRASHES 1
75#endif
76
77#if !defined(NDEBUG)  // We don't have assertions in release builds.
78// Tests whether we correctly fail on browser assertions during tests.
79class AssertionTest : public UITest {
80 protected:
81  AssertionTest() : UITest() {
82    // Initial loads will never complete due to assertion.
83    wait_for_initial_loads_ = false;
84
85    // We're testing the renderer rather than the browser assertion here,
86    // because the browser assertion would flunk the test during SetUp()
87    // (since TAU wouldn't be able to find the browser window).
88    launch_arguments_.AppendSwitch(switches::kRendererAssertTest);
89  }
90};
91
92// Launch the app in assertion test mode, then close the app.
93#if defined(OS_WIN)
94// http://crbug.com/26715
95#define Assertion DISABLED_Assertion
96#elif defined(OS_MACOSX)
97// Crash service doesn't exist for the Mac yet: http://crbug.com/45243
98#define Assertion DISABLED_Assertion
99#endif
100TEST_F(AssertionTest, Assertion) {
101  if (UITest::in_process_renderer()) {
102    // in process mode doesn't do the crashing.
103    expected_errors_ = 0;
104    expected_crashes_ = 0;
105  } else {
106    expected_errors_ = 1;
107    expected_crashes_ = EXPECTED_ASSERT_CRASHES;
108  }
109}
110#endif  // !defined(NDEBUG)
111
112#if !defined(OFFICIAL_BUILD)
113// Only works on Linux in Release mode with CHROME_HEADLESS=1
114class CheckFalseTest : public UITest {
115 protected:
116  CheckFalseTest() : UITest() {
117    // Initial loads will never complete due to assertion.
118    wait_for_initial_loads_ = false;
119
120    // We're testing the renderer rather than the browser assertion here,
121    // because the browser assertion would flunk the test during SetUp()
122    // (since TAU wouldn't be able to find the browser window).
123    launch_arguments_.AppendSwitch(switches::kRendererCheckFalseTest);
124  }
125};
126
127#if defined(OS_WIN)
128// http://crbug.com/38497
129#define CheckFails FLAKY_CheckFails
130#elif defined(OS_MACOSX)
131// Crash service doesn't exist for the Mac yet: http://crbug.com/45243
132#define CheckFails DISABLED_CheckFails
133#elif defined(OS_LINUX)
134// TODO(phajdan) Fix this - http://crbug.com/49838
135#define CheckFails FAILS_CheckFails
136#endif
137// Launch the app in assertion test mode, then close the app.
138TEST_F(CheckFalseTest, CheckFails) {
139  if (UITest::in_process_renderer()) {
140    // in process mode doesn't do the crashing.
141    expected_errors_ = 0;
142    expected_crashes_ = 0;
143  } else {
144    expected_errors_ = 1;
145    expected_crashes_ = EXPECTED_ASSERT_CRASHES;
146  }
147}
148#endif  // !defined(OFFICIAL_BUILD)
149
150// Tests whether we correctly fail on browser crashes during UI Tests.
151class RendererCrashTest : public UITest {
152 protected:
153  RendererCrashTest() : UITest() {
154    // Initial loads will never complete due to crash.
155    wait_for_initial_loads_ = false;
156
157    launch_arguments_.AppendSwitch(switches::kRendererCrashTest);
158  }
159};
160
161#if defined(OS_LINUX) && !defined(USE_LINUX_BREAKPAD)
162// On Linux, do not expect a crash dump if Breakpad is disabled.
163#define EXPECTED_CRASH_CRASHES 0
164#else
165#define EXPECTED_CRASH_CRASHES 1
166#endif
167
168#if defined(OS_WIN)
169// http://crbug.com/32048
170#define Crash FLAKY_Crash
171#elif defined(OS_CHROMEOS)
172// http://crbug.com/43115
173#define Crash DISABLED_Crash
174#elif defined(OS_MACOSX)
175// Crash service doesn't exist for the Mac yet: http://crbug.com/45243
176#define Crash DISABLED_Crash
177#endif
178// Launch the app in renderer crash test mode, then close the app.
179TEST_F(RendererCrashTest, Crash) {
180  if (UITest::in_process_renderer()) {
181    // in process mode doesn't do the crashing.
182    expected_crashes_ = 0;
183  } else {
184    scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0));
185    ASSERT_TRUE(browser.get());
186    ASSERT_TRUE(browser->WaitForTabCountToBecome(1, action_max_timeout_ms()));
187    expected_crashes_ = EXPECTED_CRASH_CRASHES;
188  }
189}
190