| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_UI_COCOA_BROWSER_TEST_HELPER_H_ | 5 #ifndef CHROME_BROWSER_UI_COCOA_BROWSER_TEST_HELPER_H_ |
| 6 #define CHROME_BROWSER_UI_COCOA_BROWSER_TEST_HELPER_H_ | 6 #define CHROME_BROWSER_UI_COCOA_BROWSER_TEST_HELPER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "chrome/browser/browser_thread.h" | 9 #include "chrome/browser/browser_thread.h" |
| 10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/test/testing_profile.h" | 11 #include "chrome/test/testing_profile.h" |
| 12 | 12 |
| 13 // Base class which contains a valid Browser*. Lots of boilerplate to | 13 // Base class which contains a valid Browser*. Lots of boilerplate to |
| 14 // recycle between unit test classes. | 14 // recycle between unit test classes. |
| 15 // | 15 // |
| 16 // This class creates fake UI, file, and IO threads because many objects that | 16 // This class creates fake UI, file, and IO threads because many objects that |
| 17 // are attached to the TestingProfile (and other objects) have traits that limit | 17 // are attached to the TestingProfile (and other objects) have traits that limit |
| 18 // their destruction to certain threads. For example, the net::URLRequestContext | 18 // their destruction to certain threads. For example, the net::URLRequestContext |
| 19 // can only be deleted on the IO thread; without this fake IO thread, the object | 19 // can only be deleted on the IO thread; without this fake IO thread, the object |
| 20 // would never be deleted and would report as a leak under Valgrind. Note that | 20 // would never be deleted and would report as a leak under Valgrind. Note that |
| 21 // these are fake threads and they all share the same MessageLoop. | 21 // these are fake threads and they all share the same MessageLoop. |
| 22 // | 22 // |
| 23 // TODO(jrg): move up a level (chrome/browser/ui/cocoa --> | 23 // TODO(jrg): move up a level (chrome/browser/ui/cocoa --> |
| 24 // chrome/browser), and use in non-Mac unit tests such as | 24 // chrome/browser), and use in non-Mac unit tests such as |
| 25 // back_forward_menu_model_unittest.cc, | 25 // back_forward_menu_model_unittest.cc, |
| 26 // navigation_controller_unittest.cc, .. | 26 // navigation_controller_unittest.cc, .. |
| 27 class BrowserTestHelper { | 27 class BrowserTestHelper { |
| 28 public: | 28 public: |
| 29 BrowserTestHelper() | 29 BrowserTestHelper(); |
| 30 : ui_thread_(BrowserThread::UI, &message_loop_), | 30 virtual ~BrowserTestHelper(); |
| 31 file_thread_(new BrowserThread(BrowserThread::FILE, &message_loop_)), | |
| 32 io_thread_(new BrowserThread(BrowserThread::IO, &message_loop_)) { | |
| 33 profile_.reset(new TestingProfile()); | |
| 34 profile_->CreateBookmarkModel(true); | |
| 35 profile_->BlockUntilBookmarkModelLoaded(); | |
| 36 | 31 |
| 37 // TODO(shess): These are needed in case someone creates a browser | 32 virtual TestingProfile* profile() const; |
| 38 // window off of browser_. pkasting indicates that other | |
| 39 // platforms use a stub |BrowserWindow| and thus don't need to do | |
| 40 // this. | |
| 41 // http://crbug.com/39725 | |
| 42 profile_->CreateAutocompleteClassifier(); | |
| 43 profile_->CreateTemplateURLModel(); | |
| 44 | |
| 45 browser_.reset(new Browser(Browser::TYPE_NORMAL, profile_.get())); | |
| 46 } | |
| 47 | |
| 48 virtual ~BrowserTestHelper() { | |
| 49 // Delete the testing profile on the UI thread. But first release the | |
| 50 // browser, since it may trigger accesses to the profile upon destruction. | |
| 51 browser_.reset(); | |
| 52 | |
| 53 // Drop any new tasks for the IO and FILE threads. | |
| 54 io_thread_.reset(); | |
| 55 file_thread_.reset(); | |
| 56 | |
| 57 message_loop_.DeleteSoon(FROM_HERE, profile_.release()); | |
| 58 message_loop_.RunAllPending(); | |
| 59 } | |
| 60 | |
| 61 virtual TestingProfile* profile() const { return profile_.get(); } | |
| 62 Browser* browser() const { return browser_.get(); } | 33 Browser* browser() const { return browser_.get(); } |
| 63 | 34 |
| 64 // Creates the browser window. To close this window call |CloseBrowserWindow|. | 35 // Creates the browser window. To close this window call |CloseBrowserWindow|. |
| 65 // Do NOT call close directly on the window. | 36 // Do NOT call close directly on the window. |
| 66 BrowserWindow* CreateBrowserWindow() { | 37 BrowserWindow* CreateBrowserWindow(); |
| 67 browser_->CreateBrowserWindow(); | |
| 68 return browser_->window(); | |
| 69 } | |
| 70 | 38 |
| 71 // Closes the window for this browser. This must only be called after | 39 // Closes the window for this browser. This must only be called after |
| 72 // CreateBrowserWindow(). | 40 // CreateBrowserWindow(). |
| 73 void CloseBrowserWindow() { | 41 void CloseBrowserWindow(); |
| 74 // Check to make sure a window was actually created. | |
| 75 DCHECK(browser_->window()); | |
| 76 browser_->CloseAllTabs(); | |
| 77 browser_->CloseWindow(); | |
| 78 // |browser_| will be deleted by its BrowserWindowController. | |
| 79 ignore_result(browser_.release()); | |
| 80 } | |
| 81 | 42 |
| 82 private: | 43 private: |
| 83 scoped_ptr<TestingProfile> profile_; | 44 scoped_ptr<TestingProfile> profile_; |
| 84 scoped_ptr<Browser> browser_; | 45 scoped_ptr<Browser> browser_; |
| 85 MessageLoopForUI message_loop_; | 46 MessageLoopForUI message_loop_; |
| 86 BrowserThread ui_thread_; | 47 BrowserThread ui_thread_; |
| 87 scoped_ptr<BrowserThread> file_thread_; | 48 scoped_ptr<BrowserThread> file_thread_; |
| 88 scoped_ptr<BrowserThread> io_thread_; | 49 scoped_ptr<BrowserThread> io_thread_; |
| 89 }; | 50 }; |
| 90 | 51 |
| 91 #endif // CHROME_BROWSER_UI_COCOA_BROWSER_TEST_HELPER_H_ | 52 #endif // CHROME_BROWSER_UI_COCOA_BROWSER_TEST_HELPER_H_ |
| OLD | NEW |