blob: 3104d1feb7d4ef21a0861654943b03859d636218 [file] [log] [blame]
droger0a8d9a62015-03-06 20:39:201// 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
dcheng4af48582016-04-19 00:29:357#include <memory>
8
droger0a8d9a62015-03-06 20:39:209#include "base/command_line.h"
fdoraybdbbd9b2017-02-11 00:12:4910#include "base/memory/ptr_util.h"
droger0a8d9a62015-03-06 20:39:2011#include "base/message_loop/message_loop.h"
beaudoind5c435e2016-04-22 14:17:1012#include "base/metrics/user_metrics.h"
droger0a8d9a62015-03-06 20:39:2013#include "base/run_loop.h"
Francois Doray5b0f1e22017-07-31 16:14:4014#include "base/task_scheduler/task_scheduler.h"
gabb15e19072016-05-11 20:45:4115#include "base/threading/thread_task_runner_handle.h"
avie4d7b6f2015-12-26 00:59:1816#include "build/build_config.h"
droger0a8d9a62015-03-06 20:39:2017#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 Doray5b0f1e22017-07-31 16:14:4023#if defined(OS_WIN)
24#include "base/win/scoped_com_initializer.h"
25#endif
26
droger0a8d9a62015-03-06 20:39:2027class 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_)) {
beaudoind5c435e2016-04-22 14:17:1040 base::SetRecordActionTaskRunner(loop_.task_runner());
droger0a8d9a62015-03-06 20:39:2041 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 Doray5b0f1e22017-07-31 16:14:4055 base::TaskScheduler::CreateAndStartWithDefaultParams(
56 "BrowserProcessImplTest");
droger0a8d9a62015-03-06 20:39:2057 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 Doray5b0f1e22017-07-31 16:14:4071 base::TaskScheduler::GetInstance()->Shutdown();
72 base::TaskScheduler::GetInstance()->JoinForTesting();
73 base::TaskScheduler::SetInstance(nullptr);
droger0a8d9a62015-03-06 20:39:2074 }
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 Doray5b0f1e22017-07-31 16:14:4084#if defined(OS_WIN)
85 base::win::ScopedCOMInitializer scoped_com_initializer_;
86#endif
dcheng4af48582016-04-19 00:29:3587 std::unique_ptr<content::TestBrowserThread> file_thread_;
88 std::unique_ptr<content::TestBrowserThread> io_thread_;
droger0a8d9a62015-03-06 20:39:2089 base::CommandLine command_line_;
dcheng4af48582016-04-19 00:29:3590 std::unique_ptr<BrowserProcessImpl> browser_process_impl_;
droger0a8d9a62015-03-06 20:39:2091};
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)
98TEST_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.
dcheng4af48582016-04-19 00:29:35105 std::unique_ptr<Profile> profile(new TestingProfile);
droger0a8d9a62015-03-06 20:39:20106 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)