OLD | NEW |
(Empty) | |
| 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 "base/command_line.h" |
| 6 #include "chrome/browser/extensions/api/mdns/mdns_api.h" |
| 7 #include "chrome/browser/extensions/extension_apitest.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/common/chrome_switches.h" |
| 10 #include "chrome/common/extensions/api/mdns.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 |
| 13 using extensions::DnsSdRegistry; |
| 14 using ::testing::A; |
| 15 using ::testing::_; |
| 16 |
| 17 namespace api = extensions::api; |
| 18 |
| 19 namespace { |
| 20 |
| 21 class MockDnsSdRegistry : public DnsSdRegistry { |
| 22 public: |
| 23 explicit MockDnsSdRegistry(extensions::MDnsAPI* api) : api_(api) {} |
| 24 virtual ~MockDnsSdRegistry() {} |
| 25 |
| 26 MOCK_METHOD1(AddObserver, void(DnsSdObserver* observer)); |
| 27 MOCK_METHOD1(RemoveObserver, void(DnsSdObserver* observer)); |
| 28 MOCK_METHOD1(RegisterDnsSdListener, void(std::string service_type)); |
| 29 MOCK_METHOD1(UnregisterDnsSdListener, void(std::string service_type)); |
| 30 |
| 31 void DispatchMDnsEvent(const std::string& service_type, |
| 32 const DnsSdServiceList& services) { |
| 33 api_->OnDnsSdEvent(service_type, services); |
| 34 } |
| 35 |
| 36 private: |
| 37 extensions::DnsSdRegistry::DnsSdObserver* api_; |
| 38 }; |
| 39 |
| 40 class MDnsAPITest : public ExtensionApiTest { |
| 41 public: |
| 42 MDnsAPITest() {} |
| 43 |
| 44 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 45 ExtensionApiTest::SetUpCommandLine(command_line); |
| 46 command_line->AppendSwitchASCII( |
| 47 switches::kWhitelistedExtensionID, "ddchlicdkolnonkihahngkmmmjnjlkkf"); |
| 48 } |
| 49 |
| 50 void SetUpTestDnsSdRegistry() { |
| 51 extensions::MDnsAPI* api = extensions::MDnsAPI::Get(profile()); |
| 52 dns_sd_registry_ = new MockDnsSdRegistry(api); |
| 53 // Transfers ownership of the registry instance. |
| 54 api->SetDnsSdRegistryForTesting( |
| 55 make_scoped_ptr<DnsSdRegistry>(dns_sd_registry_).Pass()); |
| 56 } |
| 57 |
| 58 protected: |
| 59 MockDnsSdRegistry* dns_sd_registry_; |
| 60 }; |
| 61 |
| 62 } // namespace |
| 63 |
| 64 // TODO(justinlin): Win Dbg has a workaround that makes RunExtensionSubtest |
| 65 // always return true without actually running the test. Remove when fixed. |
| 66 #if defined(OS_WIN) && !defined(NDEBUG) |
| 67 #define MAYBE_RegisterListener DISABLED_RegisterListener |
| 68 #else |
| 69 #define MAYBE_RegisterListener RegisterListener |
| 70 #endif |
| 71 // Test loading extension, registering an MDNS listener and dispatching events. |
| 72 IN_PROC_BROWSER_TEST_F(MDnsAPITest, MAYBE_RegisterListener) { |
| 73 const std::string& service_type = "_googlecast._tcp.local"; |
| 74 SetUpTestDnsSdRegistry(); |
| 75 |
| 76 EXPECT_CALL(*dns_sd_registry_, RegisterDnsSdListener(service_type)) |
| 77 .Times(1); |
| 78 EXPECT_CALL(*dns_sd_registry_, UnregisterDnsSdListener(service_type)) |
| 79 .Times(1); |
| 80 EXPECT_CALL(*dns_sd_registry_, |
| 81 RemoveObserver(A<extensions::DnsSdRegistry::DnsSdObserver*>())) |
| 82 .Times(1); |
| 83 |
| 84 EXPECT_TRUE(RunExtensionSubtest("mdns/api", "register_listener.html")) |
| 85 << message_; |
| 86 |
| 87 ResultCatcher catcher; |
| 88 // Dispatch 3 events, one of which should not be sent to the test extension. |
| 89 std::vector<std::string> services; |
| 90 services.push_back("test"); |
| 91 |
| 92 dns_sd_registry_->DispatchMDnsEvent(service_type, services); |
| 93 dns_sd_registry_->DispatchMDnsEvent("_uninteresting._tcp.local", services); |
| 94 dns_sd_registry_->DispatchMDnsEvent(service_type, services); |
| 95 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 96 } |
| 97 |
| 98 // TODO(justinlin): Win Dbg has a workaround that makes RunExtensionSubtest |
| 99 // always return true without actually running the test. Remove when fixed. |
| 100 #if defined(OS_WIN) && !defined(NDEBUG) |
| 101 #define MAYBE_RegisterMultipleListeners DISABLED_RegisterMultipleListeners |
| 102 #else |
| 103 #define MAYBE_RegisterMultipleListeners RegisterMultipleListeners |
| 104 #endif |
| 105 // Test loading extension and registering multiple listeners. |
| 106 IN_PROC_BROWSER_TEST_F(MDnsAPITest, MAYBE_RegisterMultipleListeners) { |
| 107 const std::string& service_type = "_googlecast._tcp.local"; |
| 108 const std::string& test_service_type = "_testing._tcp.local"; |
| 109 SetUpTestDnsSdRegistry(); |
| 110 |
| 111 EXPECT_CALL(*dns_sd_registry_, RegisterDnsSdListener(service_type)) |
| 112 .Times(1); |
| 113 EXPECT_CALL(*dns_sd_registry_, UnregisterDnsSdListener(service_type)) |
| 114 .Times(1); |
| 115 EXPECT_CALL(*dns_sd_registry_, RegisterDnsSdListener(test_service_type)) |
| 116 .Times(1); |
| 117 EXPECT_CALL(*dns_sd_registry_, UnregisterDnsSdListener(test_service_type)) |
| 118 .Times(1); |
| 119 EXPECT_CALL(*dns_sd_registry_, |
| 120 RemoveObserver(A<extensions::DnsSdRegistry::DnsSdObserver*>())) |
| 121 .Times(1); |
| 122 |
| 123 EXPECT_TRUE(RunExtensionSubtest("mdns/api", |
| 124 "register_multiple_listeners.html")) |
| 125 << message_; |
| 126 |
| 127 ResultCatcher catcher; |
| 128 std::vector<std::string> services; |
| 129 services.push_back("test"); |
| 130 |
| 131 dns_sd_registry_->DispatchMDnsEvent(service_type, services); |
| 132 dns_sd_registry_->DispatchMDnsEvent(test_service_type, services); |
| 133 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 134 } |
OLD | NEW |