summaryrefslogtreecommitdiffstats
path: root/chrome/browser/local_discovery/service_discovery_client_mac_unittest.mm
blob: 4cf4557ea065a6b2e7cfee27add3e524a41f0441 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <Cocoa/Cocoa.h>

#include "base/bind.h"
#include "base/mac/scoped_nsobject.h"
#include "base/message_loop/message_loop.h"
#include "chrome/common/local_discovery/service_discovery_client.h"
#include "chrome/browser/local_discovery/service_discovery_client_mac.h"
#import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
#include "testing/gtest_mac.h"

@interface TestNSNetService : NSNetService {
 @private
  NSData* data_;
}
- (id) initWithData:(NSData *)data;
@end

@implementation TestNSNetService

-(id) initWithData:(NSData *)data {
  if ((self = [super init])) {
    data_ = data;
  }
  return self;
}

- (NSArray *)addresses {
  return [NSMutableArray array];
}

- (NSData *)TXTRecordData {
  return data_;
}

@end

namespace local_discovery {

class ServiceDiscoveryClientMacTest : public CocoaTest {
 public:
  ServiceDiscoveryClientMacTest() : num_updates_(0), num_resolves_(0) {
    client_mac_ = new ServiceDiscoveryClientMac();
    client_ = client_mac_.get();
  }

  void OnServiceUpdated(
      ServiceWatcher::UpdateType update,
      const std::string& service_name) {
    last_update_ = update;
    last_service_name_ = service_name;
    num_updates_++;
  }


  void OnResolveComplete(
      ServiceResolver::RequestStatus status,
      const ServiceDescription& service_description) {
    last_status_ = status;
    last_service_description_ = service_description;
    num_resolves_++;
  }

 protected:
  scoped_refptr<ServiceDiscoveryClientMac> client_mac_;
  ServiceDiscoveryClient* client_;  // weak

  base::MessageLoop message_loop_;
  ServiceWatcher::UpdateType last_update_;
  std::string last_service_name_;
  int num_updates_;
  ServiceResolver::RequestStatus last_status_;
  ServiceDescription last_service_description_;
  int num_resolves_;
};

TEST_F(ServiceDiscoveryClientMacTest, ServiceWatcher) {
  const std::string test_service_type = "_testing._tcp.local";
  const std::string test_service_name = "Test.123";

  scoped_ptr<ServiceWatcher> watcher = client_->CreateServiceWatcher(
      test_service_type,
      base::Bind(&ServiceDiscoveryClientMacTest::OnServiceUpdated,
                 base::Unretained(this)));
  watcher->Start();

  // Weak pointer to implementation class.
  ServiceWatcherImplMac* watcher_impl =
      static_cast<ServiceWatcherImplMac*>(watcher.get());
  // Simulate service update events.
  watcher_impl->OnServicesUpdate(
      ServiceWatcher::UPDATE_ADDED, test_service_name);
  watcher_impl->OnServicesUpdate(
      ServiceWatcher::UPDATE_CHANGED, test_service_name);
  watcher_impl->OnServicesUpdate(
      ServiceWatcher::UPDATE_REMOVED, test_service_name);
  EXPECT_EQ(last_service_name_, test_service_name + "." + test_service_type);
  EXPECT_EQ(num_updates_, 3);
}

TEST_F(ServiceDiscoveryClientMacTest, ServiceResolver) {
  const std::string test_service_name = "Test.123._testing._tcp.local";
  scoped_ptr<ServiceResolver> resolver = client_->CreateServiceResolver(
      test_service_name,
      base::Bind(&ServiceDiscoveryClientMacTest::OnResolveComplete,
                 base::Unretained(this)));

  const uint8 record_bytes[] = { 2, 'a', 'b', 3, 'd', '=', 'e' };
  base::scoped_nsobject<NSNetService> test_service(
      [[TestNSNetService alloc] initWithData:
          [[NSData alloc] initWithBytes:record_bytes
                          length:arraysize(record_bytes)]]);

  ServiceResolverImplMac* resolver_impl =
      static_cast<ServiceResolverImplMac*>(resolver.get());
  resolver_impl->GetContainerForTesting()->SetServiceForTesting(test_service);
  resolver->StartResolving();

  resolver_impl->GetContainerForTesting()->OnResolveUpdate(
      ServiceResolver::STATUS_SUCCESS);

  base::MessageLoop::current()->RunUntilIdle();

  EXPECT_EQ(1, num_resolves_);
  EXPECT_EQ(2u, last_service_description_.metadata.size());
}

}  // namespace local_discovery