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_mapper.h" |
6 #include "chrome/browser/extensions/extension_apitest.h" | 7 #include "chrome/browser/extensions/extension_apitest.h" |
7 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/extensions/extension_system.h" |
8 #include "chrome/browser/extensions/extension_test_message_listener.h" | 10 #include "chrome/browser/extensions/extension_test_message_listener.h" |
9 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
11 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
12 #include "chrome/test/base/ui_test_utils.h" | 14 #include "chrome/test/base/ui_test_utils.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 |
| 17 using ::testing::MatchesRegex; |
| 18 using ::testing::StrictMock; |
13 | 19 |
14 namespace extensions { | 20 namespace extensions { |
15 | 21 |
| 22 class MockInvalidationMapper : public PushMessagingInvalidationMapper { |
| 23 public: |
| 24 MockInvalidationMapper(); |
| 25 ~MockInvalidationMapper(); |
| 26 |
| 27 MOCK_METHOD1(RegisterExtension, void(const std::string&)); |
| 28 MOCK_METHOD1(UnregisterExtension, void(const std::string&)); |
| 29 }; |
| 30 |
| 31 MockInvalidationMapper::MockInvalidationMapper() {} |
| 32 MockInvalidationMapper::~MockInvalidationMapper() {} |
| 33 |
16 class PushMessagingApiTest : public ExtensionApiTest { | 34 class PushMessagingApiTest : public ExtensionApiTest { |
17 public: | 35 public: |
18 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 36 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
19 ExtensionApiTest::SetUpCommandLine(command_line); | 37 ExtensionApiTest::SetUpCommandLine(command_line); |
20 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); | 38 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); |
21 } | 39 } |
22 }; | 40 }; |
23 | 41 |
24 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, EventDispatch) { | 42 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, EventDispatch) { |
25 ResultCatcher catcher; | 43 ResultCatcher catcher; |
26 catcher.RestrictToProfile(browser()->profile()); | 44 catcher.RestrictToProfile(browser()->profile()); |
27 | 45 |
28 ExtensionTestMessageListener ready("ready", true); | 46 ExtensionTestMessageListener ready("ready", true); |
29 const extensions::Extension* extension = | 47 const extensions::Extension* extension = |
30 LoadExtension(test_data_dir_.AppendASCII("push_messaging")); | 48 LoadExtension(test_data_dir_.AppendASCII("push_messaging")); |
31 ASSERT_TRUE(extension); | 49 ASSERT_TRUE(extension); |
32 GURL page_url = extension->GetResourceURL("event_dispatch.html"); | 50 GURL page_url = extension->GetResourceURL("event_dispatch.html"); |
33 ui_test_utils::NavigateToURL(browser(), page_url); | 51 ui_test_utils::NavigateToURL(browser(), page_url); |
34 EXPECT_TRUE(ready.WaitUntilSatisfied()); | 52 EXPECT_TRUE(ready.WaitUntilSatisfied()); |
35 | 53 |
36 // Trigger a callback. | 54 // Trigger a callback. |
37 browser()->profile()->GetExtensionService()-> | 55 ExtensionSystem::Get(browser()->profile())->extension_service()-> |
38 push_messaging_event_router()->OnMessage( | 56 push_messaging_event_router()->OnMessage( |
39 extension->id(), 1, "payload"); | 57 extension->id(), 1, "payload"); |
40 | 58 |
41 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 59 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
42 } | 60 } |
43 | 61 |
| 62 // Checks that an extension with the pushMessaging permission gets automatically |
| 63 // registered for invalidations when it is loaded. |
| 64 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, AutoRegistration) { |
| 65 // The odd ordering allows us to avoid ugly casts here and when passing |
| 66 // ownership to PushMessagingEventRouter. |
| 67 StrictMock<MockInvalidationMapper>* unsafe_mapper = |
| 68 new StrictMock<MockInvalidationMapper>; |
| 69 scoped_ptr<PushMessagingInvalidationMapper> mapper(unsafe_mapper); |
| 70 // PushMessagingEventRouter owns the mapper now. |
| 71 ExtensionSystem::Get(browser()->profile())->extension_service()-> |
| 72 push_messaging_event_router()->SetMapperForTest(mapper.Pass()); |
| 73 |
| 74 EXPECT_CALL(*unsafe_mapper, RegisterExtension(MatchesRegex("[a-p]{32}"))); |
| 75 const extensions::Extension* extension = |
| 76 LoadExtension(test_data_dir_.AppendASCII("push_messaging")); |
| 77 ASSERT_TRUE(extension); |
| 78 EXPECT_CALL(*unsafe_mapper, UnregisterExtension(extension->id())); |
| 79 UnloadExtension(extension->id()); |
| 80 } |
| 81 |
44 } // namespace extensions | 82 } // namespace extensions |
OLD | NEW |