blob: b78887934989fed9c0a3a20db5d097eb05af9941 [file] [log] [blame]
[email protected]4a5b56952010-04-27 04:55:061// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#ifndef CHROME_TEST_UNIT_CHROME_TEST_SUITE_H_
6#define CHROME_TEST_UNIT_CHROME_TEST_SUITE_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
initial.commit09911bf2008-07-26 23:55:298
[email protected]4b68e0d2009-05-02 00:56:579#include <string>
10
[email protected]0044b8102009-09-29 23:09:4011#include "build/build_config.h"
12
[email protected]94060c12009-05-15 22:26:1613#include "app/app_paths.h"
[email protected]9929da92009-05-05 02:05:1114#include "app/resource_bundle.h"
[email protected]033704f2010-06-25 02:17:4415#include "base/stats_table.h"
[email protected]0d6335f3612009-05-11 05:32:3116#include "base/file_util.h"
initial.commit09911bf2008-07-26 23:55:2917#include "base/path_service.h"
[email protected]5e057172009-03-27 16:22:2618#include "base/ref_counted.h"
[email protected]f66c110c2008-12-05 20:26:2919#include "base/scoped_nsautorelease_pool.h"
[email protected]fb895c62009-10-09 18:20:3020#include "base/test/test_suite.h"
[email protected]4bf41352010-03-08 21:21:3621#include "base/utf_string_conversions.h"
[email protected]8bcdec92009-02-25 16:15:1822#include "chrome/app/scoped_ole_initializer.h"
[email protected]6f2660e72008-12-08 14:44:4423#include "chrome/browser/browser_process.h"
[email protected]6a271dba2009-10-08 20:03:5424#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2925#include "chrome/common/chrome_paths.h"
26#include "chrome/common/chrome_switches.h"
[email protected]7fc13ed2010-03-06 05:06:2027#include "chrome/common/url_constants.h"
[email protected]6f2660e72008-12-08 14:44:4428#include "chrome/test/testing_browser_process.h"
[email protected]b59ff372009-07-15 22:04:3229#include "net/base/mock_host_resolver.h"
[email protected]7844fc52009-04-07 08:58:4830#include "net/base/net_util.h"
[email protected]5e057172009-03-27 16:22:2631
[email protected]7ba0e1a2009-12-30 14:14:0132#if defined(OS_MACOSX)
33#include "base/mac_util.h"
34#endif
35
36#if defined(OS_POSIX)
37#include "base/shared_memory.h"
38#endif
39
[email protected]4a5b56952010-04-27 04:55:0640static void RemoveSharedMemoryFile(const std::string& filename) {
[email protected]7ba0e1a2009-12-30 14:14:0141 // Stats uses SharedMemory under the hood. On posix, this results in a file
42 // on disk.
43#if defined(OS_POSIX)
44 base::SharedMemory memory;
45 memory.Delete(UTF8ToWide(filename));
46#endif
47}
48
[email protected]5e057172009-03-27 16:22:2649// In many cases it may be not obvious that a test makes a real DNS lookup.
50// We generally don't want to rely on external DNS servers for our tests,
[email protected]b59ff372009-07-15 22:04:3251// so this host resolver procedure catches external queries.
52class WarningHostResolverProc : public net::HostResolverProc {
[email protected]5e057172009-03-27 16:22:2653 public:
[email protected]b59ff372009-07-15 22:04:3254 WarningHostResolverProc() : HostResolverProc(NULL) {}
55
[email protected]123ab1e32009-10-21 19:12:5756 virtual int Resolve(const std::string& host,
57 net::AddressFamily address_family,
[email protected]5ea28dea2010-04-08 15:35:1358 net::HostResolverFlags host_resolver_flags,
[email protected]21526002010-05-16 19:42:4659 net::AddressList* addrlist,
60 int* os_error) {
[email protected]5e057172009-03-27 16:22:2661 const char* kLocalHostNames[] = {"localhost", "127.0.0.1"};
62 bool local = false;
[email protected]7844fc52009-04-07 08:58:4863
64 if (host == net::GetHostName()) {
65 local = true;
66 } else {
67 for (size_t i = 0; i < arraysize(kLocalHostNames); i++)
68 if (host == kLocalHostNames[i]) {
69 local = true;
70 break;
71 }
72 }
[email protected]5e057172009-03-27 16:22:2673
74 // Make the test fail so it's harder to ignore.
[email protected]b59ff372009-07-15 22:04:3275 // If you really need to make real DNS query, use
76 // net::RuleBasedHostResolverProc and its AllowDirectLookup method.
[email protected]5e057172009-03-27 16:22:2677 EXPECT_TRUE(local) << "Making external DNS lookup of " << host;
78
[email protected]5ea28dea2010-04-08 15:35:1379 return ResolveUsingPrevious(host, address_family, host_resolver_flags,
[email protected]21526002010-05-16 19:42:4680 addrlist, os_error);
[email protected]5e057172009-03-27 16:22:2681 }
82};
initial.commit09911bf2008-07-26 23:55:2983
[email protected]85f63352010-08-09 17:32:3284class ChromeTestSuite : public TestSuite {
[email protected]4b68e0d2009-05-02 00:56:5785 public:
[email protected]0044b8102009-09-29 23:09:4086 ChromeTestSuite(int argc, char** argv)
[email protected]85f63352010-08-09 17:32:3287 : TestSuite(argc, argv),
[email protected]4a5b56952010-04-27 04:55:0688 stats_table_(NULL),
89 created_user_data_dir_(false) {
initial.commit09911bf2008-07-26 23:55:2990 }
91
[email protected]09ac4c432010-06-25 01:46:2092 protected:
[email protected]033704f2010-06-25 02:17:4493
initial.commit09911bf2008-07-26 23:55:2994 virtual void Initialize() {
[email protected]f66c110c2008-12-05 20:26:2995 base::ScopedNSAutoreleasePool autorelease_pool;
[email protected]6f2660e72008-12-08 14:44:4496
[email protected]85f63352010-08-09 17:32:3297 TestSuite::Initialize();
initial.commit09911bf2008-07-26 23:55:2998
[email protected]7fc13ed2010-03-06 05:06:2099 chrome::RegisterChromeSchemes();
[email protected]b59ff372009-07-15 22:04:32100 host_resolver_proc_ = new WarningHostResolverProc();
101 scoped_host_resolver_proc_.Init(host_resolver_proc_.get());
[email protected]5e057172009-03-27 16:22:26102
initial.commit09911bf2008-07-26 23:55:29103 chrome::RegisterPathProvider();
[email protected]94060c12009-05-15 22:26:16104 app::RegisterPathProvider();
initial.commit09911bf2008-07-26 23:55:29105 g_browser_process = new TestingBrowserProcess;
106
107 // Notice a user data override, and otherwise default to using a custom
108 // user data directory that lives alongside the current app.
109 // NOTE: The user data directory will be erased before each UI test that
110 // uses it, in order to ensure consistency.
[email protected]c4e52f0d2009-11-06 19:55:16111 FilePath user_data_dir =
112 CommandLine::ForCurrentProcess()->GetSwitchValuePath(
113 switches::kUserDataDir);
initial.commit09911bf2008-07-26 23:55:29114 if (user_data_dir.empty() &&
[email protected]0d6335f3612009-05-11 05:32:31115 file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("chrome_test_"),
116 &user_data_dir)) {
[email protected]4b68e0d2009-05-02 00:56:57117 user_data_dir = user_data_dir.AppendASCII("test_user_data");
[email protected]4a5b56952010-04-27 04:55:06118 created_user_data_dir_ = true;
[email protected]0d6335f3612009-05-11 05:32:31119 }
initial.commit09911bf2008-07-26 23:55:29120 if (!user_data_dir.empty())
[email protected]53c58042009-08-26 20:00:14121 PathService::Override(chrome::DIR_USER_DATA, user_data_dir);
initial.commit09911bf2008-07-26 23:55:29122
[email protected]f4f41b92010-04-02 07:56:27123 if (!browser_dir_.empty()) {
[email protected]76bb9c32010-03-09 18:26:23124 PathService::Override(base::DIR_EXE, browser_dir_);
[email protected]f4f41b92010-04-02 07:56:27125 PathService::Override(base::DIR_MODULE, browser_dir_);
126 }
[email protected]76bb9c32010-03-09 18:26:23127
[email protected]6f33add32009-03-03 16:26:03128#if defined(OS_MACOSX)
[email protected]e4221ad52009-10-07 16:42:16129 // Look in the framework bundle for resources.
[email protected]6f33add32009-03-03 16:26:03130 FilePath path;
131 PathService::Get(base::DIR_EXE, &path);
[email protected]6a271dba2009-10-08 20:03:54132 path = path.Append(chrome::kFrameworkName);
[email protected]6f33add32009-03-03 16:26:03133 mac_util::SetOverrideAppBundlePath(path);
134#endif
135
[email protected]d70539de2009-06-24 22:17:06136 // Force unittests to run using en-US so if we test against string
[email protected]a4258dbc72008-08-26 23:52:27137 // output, it'll pass regardless of the system language.
[email protected]ff622aa2010-08-04 17:07:46138 ResourceBundle::InitSharedInstance("en-US");
initial.commit09911bf2008-07-26 23:55:29139
[email protected]7ba0e1a2009-12-30 14:14:01140 // initialize the global StatsTable for unit_tests (make sure the file
141 // doesn't exist before opening it so the test gets a clean slate)
142 stats_filename_ = "unit_tests";
[email protected]f7011fcb2009-01-28 21:54:32143 std::string pid_string = StringPrintf("-%d", base::GetCurrentProcId());
[email protected]7ba0e1a2009-12-30 14:14:01144 stats_filename_ += pid_string;
145 RemoveSharedMemoryFile(stats_filename_);
146 stats_table_ = new StatsTable(stats_filename_, 20, 200);
initial.commit09911bf2008-07-26 23:55:29147 StatsTable::set_current(stats_table_);
148 }
149
[email protected]ab820df2008-08-26 05:55:10150 virtual void Shutdown() {
151 ResourceBundle::CleanupSharedInstance();
152
[email protected]6f33add32009-03-03 16:26:03153#if defined(OS_MACOSX)
154 mac_util::SetOverrideAppBundle(NULL);
155#endif
156
[email protected]ab820df2008-08-26 05:55:10157 delete g_browser_process;
158 g_browser_process = NULL;
159
160 // Tear down shared StatsTable; prevents unit_tests from leaking it.
161 StatsTable::set_current(NULL);
162 delete stats_table_;
[email protected]7ba0e1a2009-12-30 14:14:01163 RemoveSharedMemoryFile(stats_filename_);
[email protected]8bcdec92009-02-25 16:15:18164
[email protected]0d6335f3612009-05-11 05:32:31165 // Delete the test_user_data dir recursively
[email protected]4a5b56952010-04-27 04:55:06166 // NOTE: user_data_dir will be deleted only if it was automatically
167 // created.
[email protected]0d6335f3612009-05-11 05:32:31168 FilePath user_data_dir;
[email protected]4a5b56952010-04-27 04:55:06169 if (created_user_data_dir_ &&
170 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir) &&
[email protected]0d6335f3612009-05-11 05:32:31171 !user_data_dir.empty()) {
172 file_util::Delete(user_data_dir, true);
173 file_util::Delete(user_data_dir.DirName(), false);
174 }
[email protected]85f63352010-08-09 17:32:32175 TestSuite::Shutdown();
[email protected]ab820df2008-08-26 05:55:10176 }
177
[email protected]76bb9c32010-03-09 18:26:23178 void SetBrowserDirectory(const FilePath& browser_dir) {
179 browser_dir_ = browser_dir;
180 }
181
initial.commit09911bf2008-07-26 23:55:29182 StatsTable* stats_table_;
[email protected]7ba0e1a2009-12-30 14:14:01183 // The name used for the stats file so it can be cleaned up on posix during
184 // test shutdown.
185 std::string stats_filename_;
186
[email protected]76bb9c32010-03-09 18:26:23187 // Alternative path to browser binaries.
188 FilePath browser_dir_;
189
[email protected]8bcdec92009-02-25 16:15:18190 ScopedOleInitializer ole_initializer_;
[email protected]b59ff372009-07-15 22:04:32191 scoped_refptr<WarningHostResolverProc> host_resolver_proc_;
192 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc_;
[email protected]4a5b56952010-04-27 04:55:06193
194 // Flag indicating whether user_data_dir was automatically created or not.
195 bool created_user_data_dir_;
initial.commit09911bf2008-07-26 23:55:29196};
197
[email protected]4b68e0d2009-05-02 00:56:57198#endif // CHROME_TEST_UNIT_CHROME_TEST_SUITE_H_