1 // Copyright 2014 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 #if !defined(__has_feature) || !__has_feature(objc_arc)
6 #error "This file requires ARC support."
9 #import "remoting/ios/host_refresh.h"
11 #import "base/compiler_specific.h"
12 #import "testing/gtest_mac.h"
14 #import "remoting/ios/host.h"
15 #import "remoting/ios/host_refresh_test_helper.h"
17 @interface HostRefreshDelegateTester : NSObject<HostRefreshDelegate>
19 @property(nonatomic) NSArray* hostList;
20 @property(nonatomic) NSString* errorMessage;
24 @implementation HostRefreshDelegateTester
26 @synthesize hostList = _hostList;
27 @synthesize errorMessage = _errorMessage;
29 - (void)hostListRefresh:(NSArray*)hostList
30 errorMessage:(NSString*)errorMessage {
32 _errorMessage = errorMessage;
35 - (bool)receivedHosts {
36 return (_hostList.count > 0);
39 - (bool)receivedErrorMessage {
40 return (_errorMessage != nil);
49 NSString* kErrorMessageTest = @"TestErrorMessage";
53 class HostRefreshTest : public ::testing::Test {
55 virtual void SetUp() OVERRIDE {
56 hostRefreshProcessor_ = [[HostRefresh allocWithZone:nil] init];
57 delegateTester_ = [[HostRefreshDelegateTester alloc] init];
58 [hostRefreshProcessor_ setDelegate:delegateTester_];
61 void CreateHostList(int numHosts) {
62 [hostRefreshProcessor_
63 setJsonData:HostRefreshTestHelper::GetHostList(numHosts)];
66 NSError* CreateErrorFromString(NSString* message) {
67 NSDictionary* errorDictionary = nil;
70 errorDictionary = @{NSLocalizedDescriptionKey : message};
73 return [[NSError alloc] initWithDomain:@"HostRefreshTest"
75 userInfo:errorDictionary];
78 HostRefresh* hostRefreshProcessor_;
79 HostRefreshDelegateTester* delegateTester_;
82 TEST_F(HostRefreshTest, ErrorFormatter) {
83 [hostRefreshProcessor_ connection:nil
84 didFailWithError:CreateErrorFromString(nil)];
85 ASSERT_FALSE(hostRefreshProcessor_.errorMessage == nil);
87 [hostRefreshProcessor_ connection:nil
88 didFailWithError:CreateErrorFromString(@"")];
89 ASSERT_FALSE(hostRefreshProcessor_.errorMessage == nil);
91 [hostRefreshProcessor_ connection:nil
92 didFailWithError:CreateErrorFromString(kErrorMessageTest)];
93 ASSERT_TRUE([hostRefreshProcessor_.errorMessage
94 rangeOfString:kErrorMessageTest].location != NSNotFound);
97 TEST_F(HostRefreshTest, JSONParsing) {
98 // There were no hosts returned
100 [hostRefreshProcessor_ connectionDidFinishLoading:nil];
101 ASSERT_TRUE(delegateTester_.hostList.count == 0);
104 [hostRefreshProcessor_ connectionDidFinishLoading:nil];
105 ASSERT_TRUE(delegateTester_.hostList.count == 1);
107 Host* host = static_cast<Host*>([delegateTester_.hostList objectAtIndex:0]);
108 ASSERT_NSEQ(HostRefreshTestHelper::CreatedTimeTest, host.createdTime);
109 ASSERT_NSEQ(HostRefreshTestHelper::HostIdTest, host.hostId);
110 ASSERT_NSEQ(HostRefreshTestHelper::HostNameTest, host.hostName);
111 ASSERT_NSEQ(HostRefreshTestHelper::HostVersionTest, host.hostVersion);
112 ASSERT_NSEQ(HostRefreshTestHelper::KindTest, host.kind);
113 ASSERT_NSEQ(HostRefreshTestHelper::JabberIdTest, host.jabberId);
114 ASSERT_NSEQ(HostRefreshTestHelper::PublicKeyTest, host.publicKey);
115 ASSERT_NSEQ(HostRefreshTestHelper::StatusTest, host.status);
116 ASSERT_NSEQ(HostRefreshTestHelper::UpdatedTimeTest, host.updatedTime);
119 [hostRefreshProcessor_ connectionDidFinishLoading:nil];
120 ASSERT_TRUE(delegateTester_.hostList.count == 11);
122 // An error in parsing returns no hosts
123 [hostRefreshProcessor_
127 [@"{\"dataaaaaafa\":{\"kiffffnd\":\"chromoting#hostList\"}}"
128 dataUsingEncoding:NSUTF8StringEncoding]]];
129 [hostRefreshProcessor_ connectionDidFinishLoading:nil];
130 ASSERT_TRUE(delegateTester_.hostList.count == 0);
133 TEST_F(HostRefreshTest, HostListDelegateNoList) {
134 // Hosts were not processed at all
135 [hostRefreshProcessor_ connectionDidFinishLoading:nil];
136 ASSERT_FALSE([delegateTester_ receivedHosts]);
137 ASSERT_TRUE([delegateTester_ receivedErrorMessage]);
139 // There were no hosts returned
141 [hostRefreshProcessor_ connectionDidFinishLoading:nil];
142 ASSERT_FALSE([delegateTester_ receivedHosts]);
143 ASSERT_TRUE([delegateTester_ receivedErrorMessage]);
146 TEST_F(HostRefreshTest, HostListDelegateHasList) {
148 [hostRefreshProcessor_ connectionDidFinishLoading:nil];
149 ASSERT_TRUE([delegateTester_ receivedHosts]);
150 ASSERT_FALSE([delegateTester_ receivedErrorMessage]);
153 TEST_F(HostRefreshTest, HostListDelegateHasListWithError) {
156 [hostRefreshProcessor_ connection:nil
157 didFailWithError:CreateErrorFromString(kErrorMessageTest)];
159 [hostRefreshProcessor_ connectionDidFinishLoading:nil];
160 ASSERT_TRUE([delegateTester_ receivedHosts]);
161 ASSERT_TRUE([delegateTester_ receivedErrorMessage]);
164 TEST_F(HostRefreshTest, ConnectionFailed) {
165 [hostRefreshProcessor_ connection:nil didFailWithError:nil];
166 ASSERT_FALSE([delegateTester_ receivedHosts]);
167 ASSERT_TRUE([delegateTester_ receivedErrorMessage]);
170 } // namespace remoting