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