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