Enable MSVC warning for unused locals.
[chromium-blink-merge.git] / chrome / installer / util / installation_validation_helper.cc
blob147990f009daf1333e9836d59cbfaafe2e341e44
1 // Copyright (c) 2011 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.
5 // This file declares helper functions for use in tests that expect a valid
6 // installation, possibly of a specific type. Validation violations result in
7 // test failures.
9 #include "chrome/installer/util/installation_validation_helper.h"
11 #include "base/logging.h"
12 #include "base/strings/string_piece.h"
13 #include "chrome/installer/util/installation_state.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace installer {
18 namespace {
20 // A helper class that installs a log message handler to add a test failure for
21 // each ERROR message. Only one instance of this class may be live at a time.
22 class FailureLogHelper {
23 public:
24 FailureLogHelper();
25 ~FailureLogHelper();
27 private:
28 static bool AddFailureForLogMessage(int severity,
29 const char* file,
30 int line,
31 size_t message_start,
32 const std::string& str);
34 static const logging::LogSeverity kViolationSeverity_;
35 static logging::LogMessageHandlerFunction old_message_handler_;
36 static int old_min_log_level_;
39 // InstallationValidator logs all violations at ERROR level.
40 // static
41 const logging::LogSeverity
42 FailureLogHelper::kViolationSeverity_ = logging::LOG_ERROR;
44 // static
45 logging::LogMessageHandlerFunction
46 FailureLogHelper::old_message_handler_ = NULL;
48 // static
49 int FailureLogHelper::old_min_log_level_ =
50 FailureLogHelper::kViolationSeverity_;
52 FailureLogHelper::FailureLogHelper() {
53 LOG_ASSERT(old_message_handler_ == NULL);
55 // The validator logs at ERROR level. Ensure that it generates messages so we
56 // can transform them into test failures.
57 old_min_log_level_ = logging::GetMinLogLevel();
58 if (old_min_log_level_ > kViolationSeverity_)
59 logging::SetMinLogLevel(kViolationSeverity_);
61 old_message_handler_ = logging::GetLogMessageHandler();
62 logging::SetLogMessageHandler(&AddFailureForLogMessage);
65 FailureLogHelper::~FailureLogHelper() {
66 logging::SetLogMessageHandler(old_message_handler_);
67 old_message_handler_ = NULL;
69 if (old_min_log_level_ > kViolationSeverity_)
70 logging::SetMinLogLevel(old_min_log_level_);
73 // A logging::LogMessageHandlerFunction that adds a non-fatal test failure
74 // (i.e., similar to an unmet EXPECT_FOO) for each non-empty message logged at
75 // the severity of validation violations. All other messages are sent through
76 // the default logging pipeline.
77 // static
78 bool FailureLogHelper::AddFailureForLogMessage(int severity,
79 const char* file,
80 int line,
81 size_t message_start,
82 const std::string& str) {
83 if (severity == kViolationSeverity_ && !str.empty()) {
84 // Remove the trailing newline, if present.
85 size_t message_length = str.size() - message_start;
86 if (*str.rbegin() == '\n')
87 --message_length;
88 ADD_FAILURE_AT(file, line)
89 << base::StringPiece(str.c_str() + message_start, message_length);
90 return true;
93 if (old_message_handler_ != NULL)
94 return (old_message_handler_)(severity, file, line, message_start, str);
96 return false;
99 } // namespace
101 InstallationValidator::InstallationType ExpectValidInstallation(
102 bool system_level) {
103 FailureLogHelper log_helper;
104 InstallationValidator::InstallationType found_type =
105 InstallationValidator::NO_PRODUCTS;
107 EXPECT_TRUE(InstallationValidator::ValidateInstallationType(system_level,
108 &found_type));
109 return found_type;
112 InstallationValidator::InstallationType ExpectValidInstallationForState(
113 const InstallationState& machine_state,
114 bool system_level) {
115 FailureLogHelper log_helper;
116 InstallationValidator::InstallationType found_type =
117 InstallationValidator::NO_PRODUCTS;
119 EXPECT_TRUE(InstallationValidator::ValidateInstallationTypeForState(
120 machine_state, system_level, &found_type));
121 return found_type;
124 void ExpectInstallationOfType(bool system_level,
125 InstallationValidator::InstallationType type) {
126 EXPECT_EQ(type, ExpectValidInstallation(system_level));
129 void ExpectInstallationOfTypeForState(
130 const InstallationState& machine_state,
131 bool system_level,
132 InstallationValidator::InstallationType type) {
133 EXPECT_EQ(type, ExpectValidInstallationForState(machine_state, system_level));
136 } // namespace installer