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
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"
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
{
28 static bool AddFailureForLogMessage(int severity
,
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.
41 const logging::LogSeverity
42 FailureLogHelper::kViolationSeverity_
= logging::LOG_ERROR
;
45 logging::LogMessageHandlerFunction
46 FailureLogHelper::old_message_handler_
= NULL
;
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.
78 bool FailureLogHelper::AddFailureForLogMessage(int severity
,
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')
88 ADD_FAILURE_AT(file
, line
)
89 << base::StringPiece(str
.c_str() + message_start
, message_length
);
93 if (old_message_handler_
!= NULL
)
94 return (old_message_handler_
)(severity
, file
, line
, message_start
, str
);
101 InstallationValidator::InstallationType
ExpectValidInstallation(
103 FailureLogHelper log_helper
;
104 InstallationValidator::InstallationType found_type
=
105 InstallationValidator::NO_PRODUCTS
;
107 EXPECT_TRUE(InstallationValidator::ValidateInstallationType(system_level
,
112 InstallationValidator::InstallationType
ExpectValidInstallationForState(
113 const InstallationState
& machine_state
,
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
));
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
,
132 InstallationValidator::InstallationType type
) {
133 EXPECT_EQ(type
, ExpectValidInstallationForState(machine_state
, system_level
));
136 } // namespace installer