OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/push_messaging/push_messaging_api.h" | 5 #include "chrome/browser/extensions/api/push_messaging/push_messaging_api.h" |
| 6 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_handler.h" |
| 7 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_mapper.h" |
6 #include "chrome/browser/extensions/extension_apitest.h" | 8 #include "chrome/browser/extensions/extension_apitest.h" |
7 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/extensions/extension_system.h" |
8 #include "chrome/browser/extensions/extension_test_message_listener.h" | 11 #include "chrome/browser/extensions/extension_test_message_listener.h" |
9 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
11 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
12 #include "chrome/test/base/ui_test_utils.h" | 15 #include "chrome/test/base/ui_test_utils.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 |
| 18 using ::testing::_; |
| 19 using ::testing::SaveArg; |
| 20 using ::testing::StrictMock; |
13 | 21 |
14 namespace extensions { | 22 namespace extensions { |
15 | 23 |
| 24 class MockInvalidationMapper : public PushMessagingInvalidationMapper { |
| 25 public: |
| 26 MockInvalidationMapper(); |
| 27 ~MockInvalidationMapper(); |
| 28 |
| 29 MOCK_METHOD1(RegisterExtension, void(const std::string&)); |
| 30 MOCK_METHOD1(UnregisterExtension, void(const std::string&)); |
| 31 }; |
| 32 |
| 33 MockInvalidationMapper::MockInvalidationMapper() {} |
| 34 MockInvalidationMapper::~MockInvalidationMapper() {} |
| 35 |
16 class PushMessagingApiTest : public ExtensionApiTest { | 36 class PushMessagingApiTest : public ExtensionApiTest { |
17 public: | 37 public: |
18 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 38 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
19 ExtensionApiTest::SetUpCommandLine(command_line); | 39 ExtensionApiTest::SetUpCommandLine(command_line); |
20 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); | 40 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); |
21 } | 41 } |
| 42 |
| 43 PushMessagingEventRouter* GetEventRouter() { |
| 44 return ExtensionSystem::Get(browser()->profile())->extension_service()-> |
| 45 push_messaging_event_router(); |
| 46 } |
22 }; | 47 }; |
23 | 48 |
24 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, EventDispatch) { | 49 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, EventDispatch) { |
25 ResultCatcher catcher; | 50 ResultCatcher catcher; |
26 catcher.RestrictToProfile(browser()->profile()); | 51 catcher.RestrictToProfile(browser()->profile()); |
27 | 52 |
28 ExtensionTestMessageListener ready("ready", true); | 53 ExtensionTestMessageListener ready("ready", true); |
29 const extensions::Extension* extension = | 54 const extensions::Extension* extension = |
30 LoadExtension(test_data_dir_.AppendASCII("push_messaging")); | 55 LoadExtension(test_data_dir_.AppendASCII("push_messaging")); |
31 ASSERT_TRUE(extension); | 56 ASSERT_TRUE(extension); |
32 GURL page_url = extension->GetResourceURL("event_dispatch.html"); | 57 GURL page_url = extension->GetResourceURL("event_dispatch.html"); |
33 ui_test_utils::NavigateToURL(browser(), page_url); | 58 ui_test_utils::NavigateToURL(browser(), page_url); |
34 EXPECT_TRUE(ready.WaitUntilSatisfied()); | 59 EXPECT_TRUE(ready.WaitUntilSatisfied()); |
35 | 60 |
36 // Trigger a callback. | 61 GetEventRouter()->TriggerMessageForTest(extension->id(), 1, "payload"); |
37 browser()->profile()->GetExtensionService()-> | |
38 push_messaging_event_router()->OnMessage( | |
39 extension->id(), 1, "payload"); | |
40 | 62 |
41 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 63 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
42 } | 64 } |
43 | 65 |
| 66 // Checks that an extension with the pushMessaging permission gets automatically |
| 67 // registered for invalidations when it is loaded. |
| 68 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, AutoRegistration) { |
| 69 scoped_ptr<StrictMock<MockInvalidationMapper> > mapper( |
| 70 new StrictMock<MockInvalidationMapper>); |
| 71 StrictMock<MockInvalidationMapper>* unsafe_mapper = mapper.get(); |
| 72 // PushMessagingEventRouter owns the mapper now. |
| 73 GetEventRouter()->SetMapperForTest( |
| 74 mapper.PassAs<PushMessagingInvalidationMapper>()); |
| 75 |
| 76 std::string extension_id; |
| 77 EXPECT_CALL(*unsafe_mapper, RegisterExtension(_)) |
| 78 .WillOnce(SaveArg<0>(&extension_id)); |
| 79 const extensions::Extension* extension = |
| 80 LoadExtension(test_data_dir_.AppendASCII("push_messaging")); |
| 81 ASSERT_TRUE(extension); |
| 82 EXPECT_EQ(extension->id(), extension_id); |
| 83 EXPECT_CALL(*unsafe_mapper, UnregisterExtension(extension->id())); |
| 84 UnloadExtension(extension->id()); |
| 85 } |
| 86 |
| 87 // Tests that we re-register for invalidations on restart for extensions that |
| 88 // are already installed. |
| 89 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, PRE_Restart) { |
| 90 PushMessagingInvalidationHandler* handler = |
| 91 static_cast<PushMessagingInvalidationHandler*>( |
| 92 GetEventRouter()->GetMapperForTest()); |
| 93 EXPECT_TRUE(handler->GetRegisteredExtensionsForTest().empty()); |
| 94 ASSERT_TRUE(InstallExtension(test_data_dir_.AppendASCII("push_messaging"), |
| 95 1 /* new install */)); |
| 96 } |
| 97 |
| 98 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, Restart) { |
| 99 PushMessagingInvalidationHandler* handler = |
| 100 static_cast<PushMessagingInvalidationHandler*>( |
| 101 GetEventRouter()->GetMapperForTest()); |
| 102 EXPECT_EQ(1U, handler->GetRegisteredExtensionsForTest().size()); |
| 103 } |
| 104 |
44 } // namespace extensions | 105 } // namespace extensions |
OLD | NEW |