Home | History | Annotate | Download | only in common
      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/environment.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 
     23 class 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::Environment> env(base::Environment::Create());
     29     if (!env->GetVar(env_vars::kLogFileName, &environment_filename_))
     30       environment_filename_ = "";
     31 
     32     env->SetVar(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::Environment> env(base::Environment::Create());
     39     env->SetVar(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.
     47 TEST_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.
     58 TEST_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.
     79 class 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
    100 TEST_F(AssertionTest, Assertion) {
    101   if (ProxyLauncher::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
    114 class 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 #endif
    134 // Launch the app in assertion test mode, then close the app.
    135 TEST_F(CheckFalseTest, CheckFails) {
    136   if (ProxyLauncher::in_process_renderer()) {
    137     // in process mode doesn't do the crashing.
    138     expected_errors_ = 0;
    139     expected_crashes_ = 0;
    140   } else {
    141     expected_errors_ = 1;
    142     expected_crashes_ = EXPECTED_ASSERT_CRASHES;
    143   }
    144 }
    145 #endif  // !defined(OFFICIAL_BUILD)
    146 
    147 // Tests whether we correctly fail on browser crashes during UI Tests.
    148 class RendererCrashTest : public UITest {
    149  protected:
    150   RendererCrashTest() : UITest() {
    151     // Initial loads will never complete due to crash.
    152     wait_for_initial_loads_ = false;
    153 
    154     launch_arguments_.AppendSwitch(switches::kRendererCrashTest);
    155   }
    156 };
    157 
    158 #if defined(OS_LINUX) && !defined(USE_LINUX_BREAKPAD)
    159 // On Linux, do not expect a crash dump if Breakpad is disabled.
    160 #define EXPECTED_CRASH_CRASHES 0
    161 #else
    162 #define EXPECTED_CRASH_CRASHES 1
    163 #endif
    164 
    165 #if defined(OS_CHROMEOS)
    166 // http://crbug.com/43115
    167 #define Crash DISABLED_Crash
    168 #elif defined(OS_MACOSX)
    169 // Crash service doesn't exist for the Mac yet: http://crbug.com/45243
    170 #define Crash DISABLED_Crash
    171 #endif
    172 // Launch the app in renderer crash test mode, then close the app.
    173 TEST_F(RendererCrashTest, Crash) {
    174   if (ProxyLauncher::in_process_renderer()) {
    175     // in process mode doesn't do the crashing.
    176     expected_crashes_ = 0;
    177   } else {
    178     scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0));
    179     ASSERT_TRUE(browser.get());
    180     ASSERT_TRUE(browser->WaitForTabCountToBecome(1));
    181     expected_crashes_ = EXPECTED_CRASH_CRASHES;
    182   }
    183 }
    184