service_discovery_client_mac_unittest.mm revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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.
4
5#import <Cocoa/Cocoa.h>
6
7#include "base/bind.h"
8#include "base/mac/scoped_nsobject.h"
9#include "base/message_loop/message_loop.h"
10#include "chrome/common/local_discovery/service_discovery_client.h"
11#include "chrome/browser/local_discovery/service_discovery_client_mac.h"
12#import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
13#include "testing/gtest_mac.h"
14
15@interface TestNSNetService : NSNetService {
16 @private
17  NSData* data_;
18}
19- (id) initWithData:(NSData *)data;
20@end
21
22@implementation TestNSNetService
23
24-(id) initWithData:(NSData *)data {
25  if ((self = [super init])) {
26    data_ = data;
27  }
28  return self;
29}
30
31- (NSArray *)addresses {
32  return [NSMutableArray array];
33}
34
35- (NSData *)TXTRecordData {
36  return data_;
37}
38
39@end
40
41namespace local_discovery {
42
43class ServiceDiscoveryClientMacTest : public CocoaTest {
44 public:
45  ServiceDiscoveryClientMacTest() : num_updates_(0), num_resolves_(0) {
46    client_mac_ = new ServiceDiscoveryClientMac();
47    client_ = client_mac_.get();
48  }
49
50  void OnServiceUpdated(
51      ServiceWatcher::UpdateType update,
52      const std::string& service_name) {
53    last_update_ = update;
54    last_service_name_ = service_name;
55    num_updates_++;
56  }
57
58
59  void OnResolveComplete(
60      ServiceResolver::RequestStatus status,
61      const ServiceDescription& service_description) {
62    last_status_ = status;
63    last_service_description_ = service_description;
64    num_resolves_++;
65  }
66
67 protected:
68  scoped_refptr<ServiceDiscoveryClientMac> client_mac_;
69  ServiceDiscoveryClient* client_;  // weak
70
71  base::MessageLoop message_loop_;
72  ServiceWatcher::UpdateType last_update_;
73  std::string last_service_name_;
74  int num_updates_;
75  ServiceResolver::RequestStatus last_status_;
76  ServiceDescription last_service_description_;
77  int num_resolves_;
78};
79
80TEST_F(ServiceDiscoveryClientMacTest, ServiceWatcher) {
81  const std::string test_service_type = "_testing._tcp.local";
82  const std::string test_service_name = "Test.123";
83
84  scoped_ptr<ServiceWatcher> watcher = client_->CreateServiceWatcher(
85      test_service_type,
86      base::Bind(&ServiceDiscoveryClientMacTest::OnServiceUpdated,
87                 base::Unretained(this)));
88  watcher->Start();
89
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);
102}
103
104TEST_F(ServiceDiscoveryClientMacTest, ServiceResolver) {
105  const std::string test_service_name = "Test.123._testing._tcp.local";
106  scoped_ptr<ServiceResolver> resolver = client_->CreateServiceResolver(
107      test_service_name,
108      base::Bind(&ServiceDiscoveryClientMacTest::OnResolveComplete,
109                 base::Unretained(this)));
110
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)]]);
116
117  ServiceResolverImplMac* resolver_impl =
118      static_cast<ServiceResolverImplMac*>(resolver.get());
119  resolver_impl->GetContainerForTesting()->SetServiceForTesting(test_service);
120  resolver->StartResolving();
121
122  resolver_impl->GetContainerForTesting()->OnResolveUpdate(
123      ServiceResolver::STATUS_SUCCESS);
124
125  base::MessageLoop::current()->RunUntilIdle();
126
127  EXPECT_EQ(1, num_resolves_);
128  EXPECT_EQ(2u, last_service_description_.metadata.size());
129}
130
131}  // namespace local_discovery
132