Roll src/third_party/WebKit 1cd8cc5:4e01097 (svn 202507:202508)
[chromium-blink-merge.git] / chrome / browser / browser_process_impl_unittest.cc
blob2949c602edeef450bcacec8616b48092156c87e4
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.
5 #include "chrome/browser/browser_process_impl.h"
7 #include "base/command_line.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "content/public/test/test_browser_thread.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 class BrowserProcessImplTest : public ::testing::Test {
19 protected:
20 BrowserProcessImplTest()
21 : stashed_browser_process_(g_browser_process),
22 loop_(base::MessageLoop::TYPE_UI),
23 ui_thread_(content::BrowserThread::UI, &loop_),
24 file_thread_(
25 new content::TestBrowserThread(content::BrowserThread::FILE)),
26 io_thread_(new content::TestBrowserThread(content::BrowserThread::IO)),
27 command_line_(base::CommandLine::NO_PROGRAM),
28 browser_process_impl_(
29 new BrowserProcessImpl(base::ThreadTaskRunnerHandle::Get().get(),
30 command_line_)) {
31 browser_process_impl_->SetApplicationLocale("en");
34 ~BrowserProcessImplTest() override {
35 g_browser_process = nullptr;
36 browser_process_impl_.reset();
37 // Restore the original browser process.
38 g_browser_process = stashed_browser_process_;
41 // Creates the secondary threads (all threads except the UI thread).
42 // The UI thread needs to be alive while BrowserProcessImpl is alive, and is
43 // managed separately.
44 void StartSecondaryThreads() {
45 file_thread_->StartIOThread();
46 io_thread_->StartIOThread();
49 // Destroys the secondary threads (all threads except the UI thread).
50 // The UI thread needs to be alive while BrowserProcessImpl is alive, and is
51 // managed separately.
52 void DestroySecondaryThreads() {
53 // Spin the runloop to allow posted tasks to be processed.
54 base::RunLoop().RunUntilIdle();
55 io_thread_.reset();
56 base::RunLoop().RunUntilIdle();
57 file_thread_.reset();
58 base::RunLoop().RunUntilIdle();
61 BrowserProcessImpl* browser_process_impl() {
62 return browser_process_impl_.get();
65 private:
66 BrowserProcess* stashed_browser_process_;
67 base::MessageLoop loop_;
68 content::TestBrowserThread ui_thread_;
69 scoped_ptr<content::TestBrowserThread> file_thread_;
70 scoped_ptr<content::TestBrowserThread> io_thread_;
71 base::CommandLine command_line_;
72 scoped_ptr<BrowserProcessImpl> browser_process_impl_;
76 // Android does not have the NTPResourceCache.
77 // This test crashes on ChromeOS because it relies on NetworkHandler which
78 // cannot be used in test.
79 #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
80 TEST_F(BrowserProcessImplTest, LifeCycle) {
81 // Setup the BrowserProcessImpl and the threads.
82 browser_process_impl()->PreCreateThreads();
83 StartSecondaryThreads();
84 browser_process_impl()->PreMainMessageLoopRun();
86 // Force the creation of the NTPResourceCache, to test the destruction order.
87 scoped_ptr<Profile> profile(new TestingProfile);
88 NTPResourceCache* cache =
89 NTPResourceCacheFactory::GetForProfile(profile.get());
90 ASSERT_TRUE(cache);
91 // Pass ownership to the ProfileManager so that it manages the destruction.
92 browser_process_impl()->profile_manager()->RegisterTestingProfile(
93 profile.release(), false, false);
95 // Tear down the BrowserProcessImpl and the threads.
96 browser_process_impl()->StartTearDown();
97 DestroySecondaryThreads();
98 browser_process_impl()->PostDestroyThreads();
100 #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS)