Home | History | Annotate | Download | only in cocoa
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_UI_COCOA_PROFILE_TEST_H_
      6 #define CHROME_BROWSER_UI_COCOA_PROFILE_TEST_H_
      7 
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/message_loop/message_loop.h"
     10 #include "chrome/browser/ui/browser.h"
     11 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
     12 #include "chrome/test/base/testing_browser_process.h"
     13 #include "chrome/test/base/testing_profile.h"
     14 #include "chrome/test/base/testing_profile_manager.h"
     15 #include "content/public/test/test_browser_thread.h"
     16 
     17 // Base class which contains a valid Browser*.  Lots of boilerplate to
     18 // recycle between unit test classes.
     19 //
     20 // This class creates fake UI, file, and IO threads because many objects that
     21 // are attached to the TestingProfile (and other objects) have traits that limit
     22 // their destruction to certain threads. For example, the net::URLRequestContext
     23 // can only be deleted on the IO thread; without this fake IO thread, the object
     24 // would never be deleted and would report as a leak under Valgrind. Note that
     25 // these are fake threads and they all share the same MessageLoop.
     26 //
     27 // TODO(jrg): move up a level (chrome/browser/ui/cocoa -->
     28 // chrome/browser), and use in non-Mac unit tests such as
     29 // back_forward_menu_model_unittest.cc,
     30 // navigation_controller_unittest.cc, ..
     31 class CocoaProfileTest : public CocoaTest {
     32  public:
     33   CocoaProfileTest();
     34   virtual ~CocoaProfileTest();
     35 
     36   // This constructs a a Browser and a TestingProfile. It is guaranteed to
     37   // succeed, else it will ASSERT and cause the test to fail. Subclasses that
     38   // do work in SetUp should ASSERT that either browser() or profile() are
     39   // non-NULL before proceeding after the call to super (this).
     40   virtual void SetUp() OVERRIDE;
     41 
     42   virtual void TearDown() OVERRIDE;
     43 
     44   TestingProfileManager* testing_profile_manager() {
     45     return &profile_manager_;
     46   }
     47   TestingProfile* profile() { return profile_; }
     48   Browser* browser() { return browser_.get(); }
     49 
     50   // Closes the window for this browser. This will automatically be called as
     51   // part of TearDown() if it's not been done already.
     52   void CloseBrowserWindow();
     53 
     54  protected:
     55   // Overridden by test subclasses to create their own browser, e.g. with a
     56   // test window.
     57   virtual Browser* CreateBrowser();
     58 
     59  private:
     60   base::MessageLoopForUI message_loop_;
     61   content::TestBrowserThread ui_thread_;
     62 
     63   TestingProfileManager profile_manager_;
     64   TestingProfile* profile_;  // Weak; owned by profile_manager_.
     65   scoped_ptr<Browser> browser_;
     66 
     67   scoped_ptr<content::TestBrowserThread> file_user_blocking_thread_;
     68   scoped_ptr<content::TestBrowserThread> file_thread_;
     69   scoped_ptr<content::TestBrowserThread> io_thread_;
     70 };
     71 
     72 #endif  // CHROME_BROWSER_UI_COCOA_PROFILE_TEST_H_
     73