[email protected] | 0ac3835 | 2013-11-13 06:01:38 | [diff] [blame] | 1 | // Copyright 2013 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 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 5 | #include <memory> |
| 6 | |
[email protected] | 0ac3835 | 2013-11-13 06:01:38 | [diff] [blame] | 7 | #include "base/environment.h" |
| 8 | #include "base/files/file_path.h" |
[email protected] | 0ac3835 | 2013-11-13 06:01:38 | [diff] [blame] | 9 | #include "chrome/common/env_vars.h" |
| 10 | #include "chrome/common/logging_chrome.h" |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | class ChromeLoggingTest : public testing::Test { |
| 14 | public: |
| 15 | // Stores the current value of the log file name environment |
| 16 | // variable and sets the variable to new_value. |
ki.stfu | f38f931 | 2015-09-27 14:44:37 | [diff] [blame] | 17 | void SaveEnvironmentVariable(const std::string& new_value) { |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 18 | std::unique_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 0ac3835 | 2013-11-13 06:01:38 | [diff] [blame] | 19 | if (!env->GetVar(env_vars::kLogFileName, &environment_filename_)) |
| 20 | environment_filename_ = ""; |
| 21 | |
| 22 | env->SetVar(env_vars::kLogFileName, new_value); |
| 23 | } |
| 24 | |
| 25 | // Restores the value of the log file nave environment variable |
| 26 | // previously saved by SaveEnvironmentVariable(). |
| 27 | void RestoreEnvironmentVariable() { |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 28 | std::unique_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 0ac3835 | 2013-11-13 06:01:38 | [diff] [blame] | 29 | env->SetVar(env_vars::kLogFileName, environment_filename_); |
| 30 | } |
| 31 | |
| 32 | private: |
| 33 | std::string environment_filename_; // Saves real environment value. |
| 34 | }; |
| 35 | |
| 36 | // Tests the log file name getter without an environment variable. |
| 37 | TEST_F(ChromeLoggingTest, LogFileName) { |
| 38 | SaveEnvironmentVariable(std::string()); |
| 39 | |
| 40 | base::FilePath filename = logging::GetLogFileName(); |
| 41 | ASSERT_NE(base::FilePath::StringType::npos, |
| 42 | filename.value().find(FILE_PATH_LITERAL("chrome_debug.log"))); |
| 43 | |
| 44 | RestoreEnvironmentVariable(); |
| 45 | } |
| 46 | |
| 47 | // Tests the log file name getter with an environment variable. |
| 48 | TEST_F(ChromeLoggingTest, EnvironmentLogFileName) { |
| 49 | SaveEnvironmentVariable("test value"); |
| 50 | |
| 51 | base::FilePath filename = logging::GetLogFileName(); |
| 52 | ASSERT_EQ(base::FilePath(FILE_PATH_LITERAL("test value")).value(), |
| 53 | filename.value()); |
| 54 | |
| 55 | RestoreEnvironmentVariable(); |
| 56 | } |