Home | History | Annotate | Download | only in cocoa
      1 // Copyright (c) 2012 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/ui/cocoa/cocoa_profile_test.h"
      6 
      7 #include "base/run_loop.h"
      8 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
      9 #include "chrome/browser/bookmarks/bookmark_test_helpers.h"
     10 #include "chrome/browser/browser_process.h"
     11 #include "chrome/browser/search_engines/template_url_service_factory.h"
     12 #include "chrome/browser/ui/browser.h"
     13 #include "chrome/browser/ui/browser_commands.h"
     14 #include "chrome/browser/ui/browser_tabstrip.h"
     15 #include "chrome/browser/ui/host_desktop.h"
     16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     17 #include "chrome/test/base/testing_browser_process.h"
     18 #include "chrome/test/base/testing_profile.h"
     19 #include "content/public/test/test_browser_thread_bundle.h"
     20 
     21 CocoaProfileTest::CocoaProfileTest()
     22     : profile_manager_(TestingBrowserProcess::GetGlobal()),
     23       profile_(NULL),
     24       thread_bundle_(new content::TestBrowserThreadBundle) {}
     25 
     26 CocoaProfileTest::~CocoaProfileTest() {
     27   // Delete the testing profile on the UI thread. But first release the
     28   // browser, since it may trigger accesses to the profile upon destruction.
     29   browser_.reset();
     30 
     31   base::RunLoop().RunUntilIdle();
     32 
     33   // Some services created on the TestingProfile require deletion on the UI
     34   // thread. If the scoper in TestingBrowserProcess, owned by ChromeTestSuite,
     35   // were to delete the ProfileManager, the UI thread would at that point no
     36   // longer exist.
     37   TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
     38 
     39   // Make sure any pending tasks run before we destroy other threads.
     40   base::RunLoop().RunUntilIdle();
     41 }
     42 
     43 void CocoaProfileTest::SetUp() {
     44   CocoaTest::SetUp();
     45 
     46   ASSERT_TRUE(profile_manager_.SetUp());
     47 
     48   profile_ = profile_manager_.CreateTestingProfile("default");
     49   ASSERT_TRUE(profile_);
     50 
     51   profile_->CreateBookmarkModel(true);
     52   test::WaitForBookmarkModelToLoad(profile_);
     53 
     54   // TODO(shess): These are needed in case someone creates a browser
     55   // window off of browser_.  pkasting indicates that other
     56   // platforms use a stub |BrowserWindow| and thus don't need to do
     57   // this.
     58   // http://crbug.com/39725
     59   TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
     60       profile_, &TemplateURLServiceFactory::BuildInstanceFor);
     61   AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse(
     62       profile_, &AutocompleteClassifierFactory::BuildInstanceFor);
     63 
     64   browser_.reset(CreateBrowser());
     65   ASSERT_TRUE(browser_.get());
     66 }
     67 
     68 void CocoaProfileTest::TearDown() {
     69   if (browser_.get() && browser_->window())
     70     CloseBrowserWindow();
     71 
     72   CocoaTest::TearDown();
     73 }
     74 
     75 void CocoaProfileTest::CloseBrowserWindow() {
     76   // Check to make sure a window was actually created.
     77   DCHECK(browser_->window());
     78   browser_->tab_strip_model()->CloseAllTabs();
     79   chrome::CloseWindow(browser_.get());
     80   // |browser_| will be deleted by its BrowserWindowController.
     81   ignore_result(browser_.release());
     82 }
     83 
     84 Browser* CocoaProfileTest::CreateBrowser() {
     85   return new Browser(Browser::CreateParams(profile(),
     86                                            chrome::GetActiveDesktop()));
     87 }
     88