summaryrefslogtreecommitdiffstats
path: root/chrome/test/browser_with_test_window_test.h
blob: 7790cf435975184b363046887de3340136fb21a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_TEST_BROWSER_WITH_TEST_WINDOW_TEST_H_
#define CHROME_TEST_BROWSER_WITH_TEST_WINDOW_TEST_H_

#include <string>

#include "base/message_loop.h"
#include "chrome/test/test_tab_contents.h"
#include "testing/gtest/include/gtest/gtest.h"

class Browser;
class TestBrowserWindow;
class TestingProfile;

// Base class for browser based unit tests. BrowserWithTestWindowTest creates a
// Browser with a TestingProfile and TestBrowserWindow. To add a tab use
// AddTestingTab. This adds a Tab whose TabContents is a TestTabContents.
// Use the method test_url_with_path to obtain a URL that targets the
// TestTabContents. For example, the following adds a tab and navigates to
// two URLs that target the TestTabContents:
//   AddTestingTab(browser());
//   browser()->OpenURL(test_url_with_path("1"), GURL(), CURRENT_TAB,
//                      PageTransition::TYPED);
//   browser()->OpenURL(test_url_with_path("1"), GURL(), CURRENT_TAB,
//                      PageTransition::TYPED);
//
// Subclasses must invoke BrowserWithTestWindowTest::SetUp as it is responsible
// for creating the various objects of this class.
class BrowserWithTestWindowTest : public testing::Test {
 public:
  BrowserWithTestWindowTest();
  virtual ~BrowserWithTestWindowTest();

  virtual void SetUp();

 protected:
  Browser* browser() const { return browser_.get(); }

  TestingProfile* profile() const { return profile_.get(); }

  TestTabContentsFactory* tab_contents_factory() const {
    return tab_contents_factory_.get();
  }

  // Adds a tab to |browser| whose TabContents comes from a
  // TestTabContentsFactory. Use test_url_with_path to obtain a URL that
  // that uses the newly created TabContents.
  void AddTestingTab(Browser* browser);

  // Returns a GURL that targets the testing TabContents created by way of
  // AddTestingTab.
  GURL test_url_with_path(const std::string& path) const {
    return tab_contents_factory_->test_url_with_path(path);
  }

 private:
  // We need to create a MessageLoop, otherwise a bunch of things fails.
  MessageLoopForUI ui_loop_;

  scoped_ptr<TestingProfile> profile_;
  scoped_ptr<TestTabContentsFactory> tab_contents_factory_;
  scoped_ptr<TestBrowserWindow> window_;
  scoped_ptr<Browser> browser_;

  DISALLOW_COPY_AND_ASSIGN(BrowserWithTestWindowTest);
};

#endif  // CHROME_TEST_BROWSER_WITH_TEST_WINDOW_TEST_H_