• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
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
7 // of each test.
8 
9 #include "chrome_frame/test/test_scrubber.h"
10 
11 #include <windows.h>
12 
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"
28 
29 namespace chrome_frame_test {
30 
31 namespace {
32 
33 class TestScrubber {
34  public:
35   TestScrubber();
36   ~TestScrubber();
37 
38   // Initializes the instance, causing it to provide its services for all
39   // subsequent tests.
40   void Initialize(testing::UnitTest* unit_test);
41 
42   // Sets the user data directory for the current test.
set_data_directory_override(const base::StringPiece16 & user_data_dir)43   void set_data_directory_override(const base::StringPiece16& user_data_dir) {
44     user_data_dir.as_string().swap(data_directory_override_);
45   }
46 
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();
53 
54  private:
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 {
58    public:
EventListener(TestScrubber * scrubber)59     explicit EventListener(TestScrubber* scrubber) : scrubber_(scrubber) {
60     }
61 
OnTestProgramStart(const testing::UnitTest &)62     virtual void OnTestProgramStart(const testing::UnitTest&) OVERRIDE {
63       scrubber_->CleanUpFromTestRun();
64     }
65 
OnTestEnd(const testing::TestInfo &)66     virtual void OnTestEnd(const testing::TestInfo&) OVERRIDE {
67       scrubber_->CleanUpFromTestRun();
68     }
69 
70    private:
71     TestScrubber* scrubber_;
72     DISALLOW_COPY_AND_ASSIGN(EventListener);
73   };
74 
is_initialized() const75   bool is_initialized() const { return !default_data_directory_.empty(); }
76 
77   string16 default_data_directory_;
78   string16 data_directory_override_;
79 
80   DISALLOW_COPY_AND_ASSIGN(TestScrubber);
81 };
82 
TestScrubber()83 TestScrubber::TestScrubber() {
84 }
85 
~TestScrubber()86 TestScrubber::~TestScrubber() {
87 }
88 
Initialize(testing::UnitTest * unit_test)89 void TestScrubber::Initialize(testing::UnitTest* unit_test) {
90   DCHECK(!is_initialized());
91 
92   default_data_directory_ = GetProfilePathForIE().value();
93   data_directory_override_.clear();
94   unit_test->listeners().Append(new EventListener(this));
95 }
96 
CleanUpFromTestRun()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);
101 
102   // Kill all chrome_launcher processes trying to launch Chrome.
103   base::KillProcesses(chrome_frame_test::kChromeLauncher, 0, NULL);
104 
105   // Kill all chrome.exe processes with --chrome-frame.
106   base::KillAllNamedProcessesWithArgument(
107       chrome::kBrowserProcessExecutableName,
108       ASCIIToWide(switches::kChromeFrame));
109 
110   // Delete the user data directory.
111   base::FilePath data_directory(data_directory_override_.empty() ?
112                           default_data_directory_ :
113                           data_directory_override_);
114 
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();
122 
123   // Clear the overridden data directory for the next test.
124   data_directory_override_.clear();
125 }
126 
127 base::LazyInstance<TestScrubber> g_scrubber = LAZY_INSTANCE_INITIALIZER;
128 
129 }  // namespace
130 
InstallTestScrubber(testing::UnitTest * unit_test)131 void InstallTestScrubber(testing::UnitTest* unit_test) {
132   // Must be called before running any tests.
133   DCHECK(unit_test);
134   DCHECK(!unit_test->current_test_case());
135 
136   g_scrubber.Get().Initialize(unit_test);
137 }
138 
OverrideDataDirectoryForThisTest(const base::StringPiece16 & user_data_dir)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());
143 
144   g_scrubber.Get().set_data_directory_override(user_data_dir);
145 }
146 
147 }  // namespace chrome_frame_test
148