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