droger | 0a8d9a6 | 2015-03-06 20:39:20 | [diff] [blame] | 1 | // Copyright 2015 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 | #include "chrome/browser/browser_process_impl.h" |
| 6 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
droger | 0a8d9a6 | 2015-03-06 20:39:20 | [diff] [blame] | 9 | #include "base/command_line.h" |
fdoray | bdbbd9b | 2017-02-11 00:12:49 | [diff] [blame] | 10 | #include "base/memory/ptr_util.h" |
droger | 0a8d9a6 | 2015-03-06 20:39:20 | [diff] [blame] | 11 | #include "base/message_loop/message_loop.h" |
beaudoin | d5c435e | 2016-04-22 14:17:10 | [diff] [blame] | 12 | #include "base/metrics/user_metrics.h" |
droger | 0a8d9a6 | 2015-03-06 20:39:20 | [diff] [blame] | 13 | #include "base/run_loop.h" |
Francois Doray | 5b0f1e2 | 2017-07-31 16:14:40 | [diff] [blame] | 14 | #include "base/task_scheduler/task_scheduler.h" |
gab | b15e1907 | 2016-05-11 20:45:41 | [diff] [blame] | 15 | #include "base/threading/thread_task_runner_handle.h" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 16 | #include "build/build_config.h" |
droger | 0a8d9a6 | 2015-03-06 20:39:20 | [diff] [blame] | 17 | #include "chrome/browser/profiles/profile_manager.h" |
| 18 | #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" |
| 19 | #include "chrome/test/base/testing_profile.h" |
| 20 | #include "content/public/test/test_browser_thread.h" |
| 21 | #include "testing/gtest/include/gtest/gtest.h" |
| 22 | |
Francois Doray | 5b0f1e2 | 2017-07-31 16:14:40 | [diff] [blame] | 23 | #if defined(OS_WIN) |
| 24 | #include "base/win/scoped_com_initializer.h" |
| 25 | #endif |
| 26 | |
droger | 0a8d9a6 | 2015-03-06 20:39:20 | [diff] [blame] | 27 | class BrowserProcessImplTest : public ::testing::Test { |
| 28 | protected: |
| 29 | BrowserProcessImplTest() |
| 30 | : stashed_browser_process_(g_browser_process), |
| 31 | loop_(base::MessageLoop::TYPE_UI), |
| 32 | ui_thread_(content::BrowserThread::UI, &loop_), |
| 33 | file_thread_( |
| 34 | new content::TestBrowserThread(content::BrowserThread::FILE)), |
| 35 | io_thread_(new content::TestBrowserThread(content::BrowserThread::IO)), |
| 36 | command_line_(base::CommandLine::NO_PROGRAM), |
| 37 | browser_process_impl_( |
| 38 | new BrowserProcessImpl(base::ThreadTaskRunnerHandle::Get().get(), |
| 39 | command_line_)) { |
beaudoin | d5c435e | 2016-04-22 14:17:10 | [diff] [blame] | 40 | base::SetRecordActionTaskRunner(loop_.task_runner()); |
droger | 0a8d9a6 | 2015-03-06 20:39:20 | [diff] [blame] | 41 | browser_process_impl_->SetApplicationLocale("en"); |
| 42 | } |
| 43 | |
| 44 | ~BrowserProcessImplTest() override { |
| 45 | g_browser_process = nullptr; |
| 46 | browser_process_impl_.reset(); |
| 47 | // Restore the original browser process. |
| 48 | g_browser_process = stashed_browser_process_; |
| 49 | } |
| 50 | |
| 51 | // Creates the secondary threads (all threads except the UI thread). |
| 52 | // The UI thread needs to be alive while BrowserProcessImpl is alive, and is |
| 53 | // managed separately. |
| 54 | void StartSecondaryThreads() { |
Francois Doray | 5b0f1e2 | 2017-07-31 16:14:40 | [diff] [blame] | 55 | base::TaskScheduler::CreateAndStartWithDefaultParams( |
| 56 | "BrowserProcessImplTest"); |
droger | 0a8d9a6 | 2015-03-06 20:39:20 | [diff] [blame] | 57 | file_thread_->StartIOThread(); |
| 58 | io_thread_->StartIOThread(); |
| 59 | } |
| 60 | |
| 61 | // Destroys the secondary threads (all threads except the UI thread). |
| 62 | // The UI thread needs to be alive while BrowserProcessImpl is alive, and is |
| 63 | // managed separately. |
| 64 | void DestroySecondaryThreads() { |
| 65 | // Spin the runloop to allow posted tasks to be processed. |
| 66 | base::RunLoop().RunUntilIdle(); |
| 67 | io_thread_.reset(); |
| 68 | base::RunLoop().RunUntilIdle(); |
| 69 | file_thread_.reset(); |
| 70 | base::RunLoop().RunUntilIdle(); |
Francois Doray | 5b0f1e2 | 2017-07-31 16:14:40 | [diff] [blame] | 71 | base::TaskScheduler::GetInstance()->Shutdown(); |
| 72 | base::TaskScheduler::GetInstance()->JoinForTesting(); |
| 73 | base::TaskScheduler::SetInstance(nullptr); |
droger | 0a8d9a6 | 2015-03-06 20:39:20 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | BrowserProcessImpl* browser_process_impl() { |
| 77 | return browser_process_impl_.get(); |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | BrowserProcess* stashed_browser_process_; |
| 82 | base::MessageLoop loop_; |
| 83 | content::TestBrowserThread ui_thread_; |
Francois Doray | 5b0f1e2 | 2017-07-31 16:14:40 | [diff] [blame] | 84 | #if defined(OS_WIN) |
| 85 | base::win::ScopedCOMInitializer scoped_com_initializer_; |
| 86 | #endif |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 87 | std::unique_ptr<content::TestBrowserThread> file_thread_; |
| 88 | std::unique_ptr<content::TestBrowserThread> io_thread_; |
droger | 0a8d9a6 | 2015-03-06 20:39:20 | [diff] [blame] | 89 | base::CommandLine command_line_; |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 90 | std::unique_ptr<BrowserProcessImpl> browser_process_impl_; |
droger | 0a8d9a6 | 2015-03-06 20:39:20 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | |
| 94 | // Android does not have the NTPResourceCache. |
| 95 | // This test crashes on ChromeOS because it relies on NetworkHandler which |
| 96 | // cannot be used in test. |
| 97 | #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 98 | TEST_F(BrowserProcessImplTest, LifeCycle) { |
| 99 | // Setup the BrowserProcessImpl and the threads. |
| 100 | browser_process_impl()->PreCreateThreads(); |
| 101 | StartSecondaryThreads(); |
| 102 | browser_process_impl()->PreMainMessageLoopRun(); |
| 103 | |
| 104 | // Force the creation of the NTPResourceCache, to test the destruction order. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 105 | std::unique_ptr<Profile> profile(new TestingProfile); |
droger | 0a8d9a6 | 2015-03-06 20:39:20 | [diff] [blame] | 106 | NTPResourceCache* cache = |
| 107 | NTPResourceCacheFactory::GetForProfile(profile.get()); |
| 108 | ASSERT_TRUE(cache); |
| 109 | // Pass ownership to the ProfileManager so that it manages the destruction. |
| 110 | browser_process_impl()->profile_manager()->RegisterTestingProfile( |
| 111 | profile.release(), false, false); |
| 112 | |
| 113 | // Tear down the BrowserProcessImpl and the threads. |
| 114 | browser_process_impl()->StartTearDown(); |
| 115 | DestroySecondaryThreads(); |
| 116 | browser_process_impl()->PostDestroyThreads(); |
| 117 | } |
| 118 | #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |