1 // Copyright 2013 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 #import <Cocoa/Cocoa.h>
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/local_discovery/service_discovery_client_mac.h"
11 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
12 #include "chrome/common/local_discovery/service_discovery_client.h"
13 #include "testing/gtest_mac.h"
15 @interface TestNSNetService : NSNetService {
19 - (id) initWithData:(NSData *)data;
22 @implementation TestNSNetService
24 -(id) initWithData:(NSData *)data {
25 if ((self = [super init])) {
31 - (NSArray *)addresses {
32 return [NSMutableArray array];
35 - (NSData *)TXTRecordData {
41 namespace local_discovery {
43 class ServiceDiscoveryClientMacTest : public CocoaTest {
45 ServiceDiscoveryClientMacTest() : num_updates_(0), num_resolves_(0) {
46 client_mac_ = new ServiceDiscoveryClientMac();
47 client_ = client_mac_.get();
50 void OnServiceUpdated(
51 ServiceWatcher::UpdateType update,
52 const std::string& service_name) {
53 last_update_ = update;
54 last_service_name_ = service_name;
59 void OnResolveComplete(
60 ServiceResolver::RequestStatus status,
61 const ServiceDescription& service_description) {
62 last_status_ = status;
63 last_service_description_ = service_description;
68 scoped_refptr<ServiceDiscoveryClientMac> client_mac_;
69 ServiceDiscoveryClient* client_; // weak
71 base::MessageLoop message_loop_;
72 ServiceWatcher::UpdateType last_update_;
73 std::string last_service_name_;
75 ServiceResolver::RequestStatus last_status_;
76 ServiceDescription last_service_description_;
80 TEST_F(ServiceDiscoveryClientMacTest, ServiceWatcher) {
81 const std::string test_service_type = "_testing._tcp.local";
82 const std::string test_service_name = "Test.123";
84 scoped_ptr<ServiceWatcher> watcher = client_->CreateServiceWatcher(
86 base::Bind(&ServiceDiscoveryClientMacTest::OnServiceUpdated,
87 base::Unretained(this)));
90 // Weak pointer to implementation class.
91 ServiceWatcherImplMac* watcher_impl =
92 static_cast<ServiceWatcherImplMac*>(watcher.get());
93 // Simulate service update events.
94 watcher_impl->OnServicesUpdate(
95 ServiceWatcher::UPDATE_ADDED, test_service_name);
96 watcher_impl->OnServicesUpdate(
97 ServiceWatcher::UPDATE_CHANGED, test_service_name);
98 watcher_impl->OnServicesUpdate(
99 ServiceWatcher::UPDATE_REMOVED, test_service_name);
100 EXPECT_EQ(last_service_name_, test_service_name + "." + test_service_type);
101 EXPECT_EQ(num_updates_, 3);
104 TEST_F(ServiceDiscoveryClientMacTest, ServiceResolver) {
105 const std::string test_service_name = "Test.123._testing._tcp.local";
106 scoped_ptr<ServiceResolver> resolver = client_->CreateServiceResolver(
108 base::Bind(&ServiceDiscoveryClientMacTest::OnResolveComplete,
109 base::Unretained(this)));
111 const uint8 record_bytes[] = { 2, 'a', 'b', 3, 'd', '=', 'e' };
112 base::scoped_nsobject<NSNetService> test_service(
113 [[TestNSNetService alloc] initWithData:
114 [[NSData alloc] initWithBytes:record_bytes
115 length:arraysize(record_bytes)]]);
117 ServiceResolverImplMac* resolver_impl =
118 static_cast<ServiceResolverImplMac*>(resolver.get());
119 resolver_impl->GetContainerForTesting()->SetServiceForTesting(test_service);
120 resolver->StartResolving();
122 resolver_impl->GetContainerForTesting()->OnResolveUpdate(
123 ServiceResolver::STATUS_SUCCESS);
125 base::MessageLoop::current()->RunUntilIdle();
127 EXPECT_EQ(1, num_resolves_);
128 EXPECT_EQ(2u, last_service_description_.metadata.size());
131 } // namespace local_discovery