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/test/base/in_process_browser_test.h"
6#include "chrome/test/base/test_chrome_web_ui_controller_factory.h"
7#include "chrome/test/base/ui_test_utils.h"
8#include "content/public/browser/web_ui_controller.h"
9#include "testing/gmock/include/gmock/gmock.h"
10#include "testing/gtest/include/gtest/gtest.h"
11#include "url/gurl.h"
12
13using content::WebContents;
14using content::WebUI;
15using content::WebUIController;
16using ::testing::_;
17using ::testing::Eq;
18using ::testing::StrictMock;
19
20namespace {
21
22// Returns a new WebUI object for the WebContents from |arg0|.
23ACTION(ReturnNewWebUI) {
24  return new WebUIController(arg0);
25}
26
27// Mock the TestChromeWebUIControllerFactory::WebUIProvider to prove that we are
28// called as expected.
29class MockWebUIProvider
30    : public TestChromeWebUIControllerFactory::WebUIProvider {
31 public:
32  MOCK_METHOD2(NewWebUI, WebUIController*(content::WebUI* web_ui,
33                                          const GURL& url));
34};
35
36// Dummy URL location for us to override.
37const char kChromeTestChromeWebUIControllerFactory[] =
38    "chrome://ChromeTestChromeWebUIControllerFactory/";
39
40// Sets up and tears down the factory override for our url's host. It is
41// necessary to do this here, rather than in the test declaration, which is too
42// late to catch the possibility of an initial browse to about:blank mistakenly
43// going to this handler.
44class TestChromeWebUIControllerFactoryTest : public InProcessBrowserTest {
45 public:
46  virtual void SetUpOnMainThread() OVERRIDE {
47    content::WebUIControllerFactory::UnregisterFactoryForTesting(
48        ChromeWebUIControllerFactory::GetInstance());
49    test_factory_.reset(new TestChromeWebUIControllerFactory);
50    content::WebUIControllerFactory::RegisterFactory(test_factory_.get());
51    test_factory_->AddFactoryOverride(
52        GURL(kChromeTestChromeWebUIControllerFactory).host(), &mock_provider_);
53  }
54
55  virtual void TearDownOnMainThread() OVERRIDE {
56    test_factory_->RemoveFactoryOverride(
57        GURL(kChromeTestChromeWebUIControllerFactory).host());
58    content::WebUIControllerFactory::UnregisterFactoryForTesting(
59        test_factory_.get());
60
61    test_factory_.reset();
62  }
63
64 protected:
65  StrictMock<MockWebUIProvider> mock_provider_;
66  scoped_ptr<TestChromeWebUIControllerFactory> test_factory_;
67};
68
69}  // namespace
70
71// Test that browsing to our test url causes us to be called once.
72IN_PROC_BROWSER_TEST_F(TestChromeWebUIControllerFactoryTest,
73                       TestWebUIProvider) {
74  const GURL kChromeTestChromeWebUIControllerFactoryURL(
75      kChromeTestChromeWebUIControllerFactory);
76  EXPECT_CALL(mock_provider_,
77              NewWebUI(_, Eq(kChromeTestChromeWebUIControllerFactoryURL)))
78      .WillOnce(ReturnNewWebUI());
79  ui_test_utils::NavigateToURL(browser(),
80                               kChromeTestChromeWebUIControllerFactoryURL);
81}
82