1 // Copyright (c) 2012 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 // The test scrubber uses a Google Test event listener to trigger scrubs.
6 // Scrubs are performed at the start of the test program and at the conclusion
9 #include "chrome_frame/test/test_scrubber.h"
13 #include "base/compiler_specific.h"
14 #include "base/file_util.h"
15 #include "base/files/file_path.h"
16 #include "base/lazy_instance.h"
17 #include "base/logging.h"
18 #include "base/process/kill.h"
19 #include "base/strings/string16.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/test/test_process_killer_win.h"
22 #include "base/win/registry.h"
23 #include "base/win/scoped_handle.h"
24 #include "chrome/common/chrome_constants.h"
25 #include "chrome/common/chrome_switches.h"
26 #include "chrome_frame/test/chrome_frame_test_utils.h"
27 #include "testing/gtest/include/gtest/gtest.h"
29 namespace chrome_frame_test
{
38 // Initializes the instance, causing it to provide its services for all
40 void Initialize(testing::UnitTest
* unit_test
);
42 // Sets the user data directory for the current test.
43 void set_data_directory_override(const base::StringPiece16
& user_data_dir
) {
44 user_data_dir
.as_string().swap(data_directory_override_
);
47 // Kills all instances of IE and all instances of chrome.exe that have
48 // --chrome-frame on their command-lines. Also deletes the user data
49 // directory used by the test. Ordinarily, this is the default Chrome Frame
50 // user data directory for IE. Individual tests that use a specific directory
51 // can override this via |set_data_directory_override|.
52 void CleanUpFromTestRun();
55 // A listener that calls back to the scrubber at the start of the test program
56 // and at the end of each test.
57 class EventListener
: public testing::EmptyTestEventListener
{
59 explicit EventListener(TestScrubber
* scrubber
) : scrubber_(scrubber
) {
62 virtual void OnTestProgramStart(const testing::UnitTest
&) OVERRIDE
{
63 scrubber_
->CleanUpFromTestRun();
66 virtual void OnTestEnd(const testing::TestInfo
&) OVERRIDE
{
67 scrubber_
->CleanUpFromTestRun();
71 TestScrubber
* scrubber_
;
72 DISALLOW_COPY_AND_ASSIGN(EventListener
);
75 bool is_initialized() const { return !default_data_directory_
.empty(); }
77 string16 default_data_directory_
;
78 string16 data_directory_override_
;
80 DISALLOW_COPY_AND_ASSIGN(TestScrubber
);
83 TestScrubber::TestScrubber() {
86 TestScrubber::~TestScrubber() {
89 void TestScrubber::Initialize(testing::UnitTest
* unit_test
) {
90 DCHECK(!is_initialized());
92 default_data_directory_
= GetProfilePathForIE().value();
93 data_directory_override_
.clear();
94 unit_test
->listeners().Append(new EventListener(this));
97 void TestScrubber::CleanUpFromTestRun() {
98 // Kill all iexplore.exe and ieuser.exe processes.
99 base::KillProcesses(chrome_frame_test::kIEImageName
, 0, NULL
);
100 base::KillProcesses(chrome_frame_test::kIEBrokerImageName
, 0, NULL
);
102 // Kill all chrome_launcher processes trying to launch Chrome.
103 base::KillProcesses(chrome_frame_test::kChromeLauncher
, 0, NULL
);
105 // Kill all chrome.exe processes with --chrome-frame.
106 base::KillAllNamedProcessesWithArgument(
107 chrome::kBrowserProcessExecutableName
,
108 ASCIIToWide(switches::kChromeFrame
));
110 // Delete the user data directory.
111 base::FilePath
data_directory(data_directory_override_
.empty() ?
112 default_data_directory_
:
113 data_directory_override_
);
115 VLOG_IF(1, base::PathExists(data_directory
))
116 << __FUNCTION__
<< " deleting user data directory "
117 << data_directory
.value();
118 bool deleted
= base::DeleteFile(data_directory
, true);
119 LOG_IF(ERROR
, !deleted
)
120 << "Failed to delete user data directory directory "
121 << data_directory
.value();
123 // Clear the overridden data directory for the next test.
124 data_directory_override_
.clear();
127 base::LazyInstance
<TestScrubber
> g_scrubber
= LAZY_INSTANCE_INITIALIZER
;
131 void InstallTestScrubber(testing::UnitTest
* unit_test
) {
132 // Must be called before running any tests.
134 DCHECK(!unit_test
->current_test_case());
136 g_scrubber
.Get().Initialize(unit_test
);
139 void OverrideDataDirectoryForThisTest(
140 const base::StringPiece16
& user_data_dir
) {
141 // Must be called within the context of a test.
142 DCHECK(testing::UnitTest::GetInstance()->current_test_info());
144 g_scrubber
.Get().set_data_directory_override(user_data_dir
);
147 } // namespace chrome_frame_test