summaryrefslogtreecommitdiffstats
path: root/chrome/browser/browser_process_impl_unittest.cc
diff options
context:
space:
mode:
authordroger <droger@chromium.org>2015-03-06 12:39:20 -0800
committerCommit bot <commit-bot@chromium.org>2015-03-06 20:40:09 +0000
commit0a8d9a6cb29caf11ee23d8d3bc0a72bb6d0ab998 (patch)
tree70ee7523176f46c60fe691bcf1ffab80c88fe1eb /chrome/browser/browser_process_impl_unittest.cc
parentdce2e0b5d89ebcc00482fe95494c9c2163d2f463 (diff)
downloadchromium_src-0a8d9a6cb29caf11ee23d8d3bc0a72bb6d0ab998.zip
chromium_src-0a8d9a6cb29caf11ee23d8d3bc0a72bb6d0ab998.tar.gz
chromium_src-0a8d9a6cb29caf11ee23d8d3bc0a72bb6d0ab998.tar.bz2
Fix destruction order of PromoResourceService and NTPResourceCache
The existing code was leading to a crash: - NTPResourceCache has a callback subscription on PromoResourceService - PromoResourceService was deleted before NTPResourceCache - When the NTPResourceCache was destroyed, it tried to remove itself from the callback list, which no longer exists and crashes. The bug was introduced by https://codereview.chromium.org/926633002 This CL changes the destruction order so that the PromoResourceService is deleted after NTPResourceCache (which is a keyed service and thus is tied to the profile). BUG=464391 Review URL: https://codereview.chromium.org/988433002 Cr-Commit-Position: refs/heads/master@{#319491}
Diffstat (limited to 'chrome/browser/browser_process_impl_unittest.cc')
-rw-r--r--chrome/browser/browser_process_impl_unittest.cc100
1 files changed, 100 insertions, 0 deletions
diff --git a/chrome/browser/browser_process_impl_unittest.cc b/chrome/browser/browser_process_impl_unittest.cc
new file mode 100644
index 0000000..2949c60
--- /dev/null
+++ b/chrome/browser/browser_process_impl_unittest.cc
@@ -0,0 +1,100 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/browser_process_impl.h"
+
+#include "base/command_line.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/message_loop/message_loop.h"
+#include "base/run_loop.h"
+#include "base/thread_task_runner_handle.h"
+#include "chrome/browser/profiles/profile_manager.h"
+#include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h"
+#include "chrome/test/base/testing_profile.h"
+#include "content/public/test/test_browser_thread.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+class BrowserProcessImplTest : public ::testing::Test {
+ protected:
+ BrowserProcessImplTest()
+ : stashed_browser_process_(g_browser_process),
+ loop_(base::MessageLoop::TYPE_UI),
+ ui_thread_(content::BrowserThread::UI, &loop_),
+ file_thread_(
+ new content::TestBrowserThread(content::BrowserThread::FILE)),
+ io_thread_(new content::TestBrowserThread(content::BrowserThread::IO)),
+ command_line_(base::CommandLine::NO_PROGRAM),
+ browser_process_impl_(
+ new BrowserProcessImpl(base::ThreadTaskRunnerHandle::Get().get(),
+ command_line_)) {
+ browser_process_impl_->SetApplicationLocale("en");
+ }
+
+ ~BrowserProcessImplTest() override {
+ g_browser_process = nullptr;
+ browser_process_impl_.reset();
+ // Restore the original browser process.
+ g_browser_process = stashed_browser_process_;
+ }
+
+ // Creates the secondary threads (all threads except the UI thread).
+ // The UI thread needs to be alive while BrowserProcessImpl is alive, and is
+ // managed separately.
+ void StartSecondaryThreads() {
+ file_thread_->StartIOThread();
+ io_thread_->StartIOThread();
+ }
+
+ // Destroys the secondary threads (all threads except the UI thread).
+ // The UI thread needs to be alive while BrowserProcessImpl is alive, and is
+ // managed separately.
+ void DestroySecondaryThreads() {
+ // Spin the runloop to allow posted tasks to be processed.
+ base::RunLoop().RunUntilIdle();
+ io_thread_.reset();
+ base::RunLoop().RunUntilIdle();
+ file_thread_.reset();
+ base::RunLoop().RunUntilIdle();
+ }
+
+ BrowserProcessImpl* browser_process_impl() {
+ return browser_process_impl_.get();
+ }
+
+ private:
+ BrowserProcess* stashed_browser_process_;
+ base::MessageLoop loop_;
+ content::TestBrowserThread ui_thread_;
+ scoped_ptr<content::TestBrowserThread> file_thread_;
+ scoped_ptr<content::TestBrowserThread> io_thread_;
+ base::CommandLine command_line_;
+ scoped_ptr<BrowserProcessImpl> browser_process_impl_;
+};
+
+
+// Android does not have the NTPResourceCache.
+// This test crashes on ChromeOS because it relies on NetworkHandler which
+// cannot be used in test.
+#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
+TEST_F(BrowserProcessImplTest, LifeCycle) {
+ // Setup the BrowserProcessImpl and the threads.
+ browser_process_impl()->PreCreateThreads();
+ StartSecondaryThreads();
+ browser_process_impl()->PreMainMessageLoopRun();
+
+ // Force the creation of the NTPResourceCache, to test the destruction order.
+ scoped_ptr<Profile> profile(new TestingProfile);
+ NTPResourceCache* cache =
+ NTPResourceCacheFactory::GetForProfile(profile.get());
+ ASSERT_TRUE(cache);
+ // Pass ownership to the ProfileManager so that it manages the destruction.
+ browser_process_impl()->profile_manager()->RegisterTestingProfile(
+ profile.release(), false, false);
+
+ // Tear down the BrowserProcessImpl and the threads.
+ browser_process_impl()->StartTearDown();
+ DestroySecondaryThreads();
+ browser_process_impl()->PostDestroyThreads();
+}
+#endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS)