This source file includes following definitions.
- AddFailureForLogMessage
- ExpectValidInstallation
- ExpectValidInstallationForState
- ExpectInstallationOfType
- ExpectInstallationOfTypeForState
#include "chrome/installer/util/installation_validation_helper.h"
#include "base/logging.h"
#include "base/strings/string_piece.h"
#include "chrome/installer/util/installation_state.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace installer {
namespace {
class FailureLogHelper {
public:
FailureLogHelper();
~FailureLogHelper();
private:
static bool AddFailureForLogMessage(int severity,
const char* file,
int line,
size_t message_start,
const std::string& str);
static const logging::LogSeverity kViolationSeverity_;
static logging::LogMessageHandlerFunction old_message_handler_;
static int old_min_log_level_;
};
const logging::LogSeverity
FailureLogHelper::kViolationSeverity_ = logging::LOG_ERROR;
logging::LogMessageHandlerFunction
FailureLogHelper::old_message_handler_ = NULL;
int FailureLogHelper::old_min_log_level_ =
FailureLogHelper::kViolationSeverity_;
FailureLogHelper::FailureLogHelper() {
LOG_ASSERT(old_message_handler_ == NULL);
old_min_log_level_ = logging::GetMinLogLevel();
if (old_min_log_level_ > kViolationSeverity_)
logging::SetMinLogLevel(kViolationSeverity_);
old_message_handler_ = logging::GetLogMessageHandler();
logging::SetLogMessageHandler(&AddFailureForLogMessage);
}
FailureLogHelper::~FailureLogHelper() {
logging::SetLogMessageHandler(old_message_handler_);
old_message_handler_ = NULL;
if (old_min_log_level_ > kViolationSeverity_)
logging::SetMinLogLevel(old_min_log_level_);
}
bool FailureLogHelper::AddFailureForLogMessage(int severity,
const char* file,
int line,
size_t message_start,
const std::string& str) {
if (severity == kViolationSeverity_ && !str.empty()) {
size_t message_length = str.size() - message_start;
if (*str.rbegin() == '\n')
--message_length;
ADD_FAILURE_AT(file, line)
<< base::StringPiece(str.c_str() + message_start, message_length);
return true;
}
if (old_message_handler_ != NULL)
return (old_message_handler_)(severity, file, line, message_start, str);
return false;
}
}
InstallationValidator::InstallationType ExpectValidInstallation(
bool system_level) {
FailureLogHelper log_helper;
InstallationValidator::InstallationType found_type =
InstallationValidator::NO_PRODUCTS;
EXPECT_TRUE(InstallationValidator::ValidateInstallationType(system_level,
&found_type));
return found_type;
}
InstallationValidator::InstallationType ExpectValidInstallationForState(
const InstallationState& machine_state,
bool system_level) {
FailureLogHelper log_helper;
InstallationValidator::InstallationType found_type =
InstallationValidator::NO_PRODUCTS;
EXPECT_TRUE(InstallationValidator::ValidateInstallationTypeForState(
machine_state, system_level, &found_type));
return found_type;
}
void ExpectInstallationOfType(bool system_level,
InstallationValidator::InstallationType type) {
EXPECT_EQ(type, ExpectValidInstallation(system_level));
}
void ExpectInstallationOfTypeForState(
const InstallationState& machine_state,
bool system_level,
InstallationValidator::InstallationType type) {
EXPECT_EQ(type, ExpectValidInstallationForState(machine_state, system_level));
}
}