Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / local_discovery / service_discovery_client_mac_unittest.mm
blobb09b5d8cc8866cee9dca0826dfe8e621bf89c13c
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>
7 #include "base/bind.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "chrome/common/local_discovery/service_discovery_client.h"
10 #include "chrome/browser/local_discovery/service_discovery_client_mac.h"
11 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
12 #include "testing/gtest_mac.h"
14 @interface TestNSNetService : NSNetService {
15  @private
16   NSData* data_;
18 - (id) initWithData:(NSData *)data;
19 @end
21 @implementation TestNSNetService
23 -(id) initWithData:(NSData *)data {
24   if ((self = [super init])) {
25     data_ = data;
26   }
27   return self;
30 - (NSArray *)addresses {
31   return [NSMutableArray array];
34 - (NSData *)TXTRecordData {
35   return data_;
38 @end
40 namespace local_discovery {
42 class ServiceDiscoveryClientMacTest : public CocoaTest {
43  public:
44   ServiceDiscoveryClientMacTest() : num_updates_(0), num_resolves_(0) {
45     client_mac_ = new ServiceDiscoveryClientMac();
46     client_ = client_mac_;
47   }
49   void OnServiceUpdated(
50       ServiceWatcher::UpdateType update,
51       const std::string& service_name) {
52     last_update_ = update;
53     last_service_name_ = service_name;
54     num_updates_++;
55   }
58   void OnResolveComplete(
59       ServiceResolver::RequestStatus status,
60       const ServiceDescription& service_description) {
61     last_status_ = status;
62     last_service_description_ = service_description;
63     num_resolves_++;
64   }
66  protected:
67   scoped_refptr<ServiceDiscoveryClientMac> client_mac_;
68   ServiceDiscoveryClient* client_;  // weak
70   ServiceWatcher::UpdateType last_update_;
71   std::string last_service_name_;
72   int num_updates_;
73   ServiceResolver::RequestStatus last_status_;
74   ServiceDescription last_service_description_;
75   int num_resolves_;
78 TEST_F(ServiceDiscoveryClientMacTest, ServiceWatcher) {
79   const std::string test_service_type = "_testing._tcp.local";
80   const std::string test_service_name = "Test.123";
82   scoped_ptr<ServiceWatcher> watcher = client_->CreateServiceWatcher(
83       test_service_type,
84       base::Bind(&ServiceDiscoveryClientMacTest::OnServiceUpdated,
85                  base::Unretained(this)));
86   watcher->Start();
88   // Weak pointer to implementation class.
89   ServiceWatcherImplMac* watcher_impl =
90       static_cast<ServiceWatcherImplMac*>(watcher.get());
91   // Simulate service update events.
92   watcher_impl->OnServicesUpdate(
93       ServiceWatcher::UPDATE_ADDED, test_service_name);
94   watcher_impl->OnServicesUpdate(
95       ServiceWatcher::UPDATE_CHANGED, test_service_name);
96   watcher_impl->OnServicesUpdate(
97       ServiceWatcher::UPDATE_REMOVED, test_service_name);
98   EXPECT_EQ(last_service_name_, test_service_name + "." + test_service_type);
99   EXPECT_EQ(num_updates_, 3);
102 TEST_F(ServiceDiscoveryClientMacTest, ServiceResolver) {
103   const std::string test_service_name = "Test.123._testing._tcp.local";
104   scoped_ptr<ServiceResolver> resolver = client_->CreateServiceResolver(
105       test_service_name,
106       base::Bind(&ServiceDiscoveryClientMacTest::OnResolveComplete,
107                  base::Unretained(this)));
108   resolver->StartResolving();
110   const uint8 record_bytes[] = { 2, 'a', 'b', 3, 'd', '=', 'e' };
111   base::scoped_nsobject<NSNetService> test_service(
112       [[TestNSNetService alloc] initWithData:
113           [[NSData alloc] initWithBytes:record_bytes
114                           length:arraysize(record_bytes)]]);
116   // Weak pointer to implementation class.
117   ServiceResolverImplMac* resolver_impl =
118       static_cast<ServiceResolverImplMac*>(resolver.get());
119   resolver_impl->SetServiceForTesting(test_service);
120   resolver_impl->OnResolveUpdate(ServiceResolver::STATUS_SUCCESS);
122   EXPECT_EQ(num_resolves_, 1);
123   EXPECT_EQ(last_service_description_.metadata.size(), 2u);
126 }  // namespace local_discovery