Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / chrome / test / base / test_chrome_web_ui_controller_factory_browsertest.cc
blob15ef7f9df07c58746906cd7c782693f82d028778
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"
11 #include "url/gurl.h"
13 using content::WebContents;
14 using content::WebUI;
15 using content::WebUIController;
16 using ::testing::_;
17 using ::testing::Eq;
18 using ::testing::StrictMock;
20 namespace {
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 {
31 public:
32 MOCK_METHOD2(NewWebUI, WebUIController*(content::WebUI* web_ui,
33 const GURL& url));
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 {
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_);
55 virtual void TearDownOnMainThread() override {
56 test_factory_->RemoveFactoryOverride(
57 GURL(kChromeTestChromeWebUIControllerFactory).host());
58 content::WebUIControllerFactory::UnregisterFactoryForTesting(
59 test_factory_.get());
61 test_factory_.reset();
64 protected:
65 StrictMock<MockWebUIProvider> mock_provider_;
66 scoped_ptr<TestChromeWebUIControllerFactory> test_factory_;
69 } // namespace
71 // Test that browsing to our test url causes us to be called once.
72 IN_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);