1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// The test scrubber uses a Google Test event listener to trigger scrubs.
// Scrubs are performed at the start of the test program and at the conclusion
// of each test.
#include "chrome_frame/test/test_scrubber.h"
#include <windows.h>
#include "base/compiler_specific.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/process_util.h"
#include "base/string16.h"
#include "base/utf_string_conversions.h"
#include "base/win/registry.h"
#include "base/win/scoped_handle.h"
#include "chrome/common/chrome_constants.h"
#include "chrome_frame/test/chrome_frame_test_utils.h"
#include "content/public/common/content_switches.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chrome_frame_test {
namespace {
class TestScrubber {
public:
TestScrubber();
~TestScrubber();
// Initializes the instance, causing it to provide its services for all
// subsequent tests.
void Initialize(testing::UnitTest* unit_test);
// Sets the user data directory for the current test.
void set_data_directory_override(const base::StringPiece16& user_data_dir) {
user_data_dir.as_string().swap(data_directory_override_);
}
// Kills all instances of IE and all instances of chrome.exe that have
// --chrome-frame on their command-lines. Also deletes the user data
// directory used by the test. Ordinarily, this is the default Chrome Frame
// user data directory for IE. Individual tests that use a specific directory
// can override this via |set_data_directory_override|.
void CleanUpFromTestRun();
private:
// A listener that calls back to the scrubber at the start of the test program
// and at the end of each test.
class EventListener : public testing::EmptyTestEventListener {
public:
explicit EventListener(TestScrubber* scrubber) : scrubber_(scrubber) {
}
virtual void OnTestProgramStart(const testing::UnitTest&) OVERRIDE {
scrubber_->CleanUpFromTestRun();
}
virtual void OnTestEnd(const testing::TestInfo&) OVERRIDE {
scrubber_->CleanUpFromTestRun();
}
private:
TestScrubber* scrubber_;
DISALLOW_COPY_AND_ASSIGN(EventListener);
};
bool is_initialized() const { return !default_data_directory_.empty(); }
string16 default_data_directory_;
string16 data_directory_override_;
DISALLOW_COPY_AND_ASSIGN(TestScrubber);
};
TestScrubber::TestScrubber() {
}
TestScrubber::~TestScrubber() {
}
void TestScrubber::Initialize(testing::UnitTest* unit_test) {
DCHECK(!is_initialized());
default_data_directory_ = GetProfilePathForIE().value();
data_directory_override_.clear();
unit_test->listeners().Append(new EventListener(this));
}
void TestScrubber::CleanUpFromTestRun() {
// Kill all iexplore.exe and ieuser.exe processes.
base::KillProcesses(chrome_frame_test::kIEImageName, 0, NULL);
base::KillProcesses(chrome_frame_test::kIEBrokerImageName, 0, NULL);
// Kill all chrome_launcher processes trying to launch Chrome.
base::KillProcesses(chrome_frame_test::kChromeLauncher, 0, NULL);
// Kill all chrome.exe processes with --chrome-frame.
KillAllNamedProcessesWithArgument(
chrome::kBrowserProcessExecutableName,
ASCIIToWide(switches::kChromeFrame));
// Delete the user data directory.
FilePath data_directory(data_directory_override_.empty() ?
default_data_directory_ :
data_directory_override_);
VLOG_IF(1, file_util::PathExists(data_directory))
<< __FUNCTION__ << " deleting user data directory "
<< data_directory.value();
bool deleted = file_util::Delete(data_directory, true);
LOG_IF(ERROR, !deleted)
<< "Failed to delete user data directory directory "
<< data_directory.value();
// Clear the overridden data directory for the next test.
data_directory_override_.clear();
}
base::LazyInstance<TestScrubber> g_scrubber = LAZY_INSTANCE_INITIALIZER;
} // namespace
void InstallTestScrubber(testing::UnitTest* unit_test) {
// Must be called before running any tests.
DCHECK(unit_test);
DCHECK(!unit_test->current_test_case());
g_scrubber.Get().Initialize(unit_test);
}
void OverrideDataDirectoryForThisTest(
const base::StringPiece16& user_data_dir) {
// Must be called within the context of a test.
DCHECK(testing::UnitTest::GetInstance()->current_test_info());
g_scrubber.Get().set_data_directory_override(user_data_dir);
}
} // namespace chrome_frame_test
|