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.
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"
13 using content::WebContents
;
15 using content::WebUIController
;
18 using ::testing::StrictMock
;
22 // Returns a new WebUI object for the WebContents from |arg0|.
23 ACTION(ReturnNewWebUI
) {
24 return new WebUIController(arg0
);
27 // Mock the TestChromeWebUIControllerFactory::WebUIProvider to prove that we are
28 // called as expected.
29 class MockWebUIProvider
30 : public TestChromeWebUIControllerFactory::WebUIProvider
{
32 MOCK_METHOD2(NewWebUI
, WebUIController
*(content::WebUI
* web_ui
,
36 // Dummy URL location for us to override.
37 const char kChromeTestChromeWebUIControllerFactory
[] =
38 "chrome://ChromeTestChromeWebUIControllerFactory/";
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.
44 class TestChromeWebUIControllerFactoryTest
: public InProcessBrowserTest
{
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_
);
55 virtual void TearDownOnMainThread() override
{
56 test_factory_
->RemoveFactoryOverride(
57 GURL(kChromeTestChromeWebUIControllerFactory
).host());
58 content::WebUIControllerFactory::UnregisterFactoryForTesting(
61 test_factory_
.reset();
65 StrictMock
<MockWebUIProvider
> mock_provider_
;
66 scoped_ptr
<TestChromeWebUIControllerFactory
> test_factory_
;
71 // Test that browsing to our test url causes us to be called once.
72 IN_PROC_BROWSER_TEST_F(TestChromeWebUIControllerFactoryTest
,
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
);