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