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