Cleanup: Remove unneeded #include from ui/base/l10n/l10n_util.h.
[chromium-blink-merge.git] / remoting / ios / host_refresh_unittest.mm
blob54f2ee7c265c034c67aea1060b7278b63a316f78
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."
7 #endif
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;
22 @end
24 @implementation HostRefreshDelegateTester
26 @synthesize hostList = _hostList;
27 @synthesize errorMessage = _errorMessage;
29 - (void)hostListRefresh:(NSArray*)hostList
30            errorMessage:(NSString*)errorMessage {
31   _hostList = hostList;
32   _errorMessage = errorMessage;
35 - (bool)receivedHosts {
36   return (_hostList.count > 0);
39 - (bool)receivedErrorMessage {
40   return (_errorMessage != nil);
43 @end
45 namespace remoting {
47 namespace {
49 NSString* kErrorMessageTest = @"TestErrorMessage";
51 }  // namespace
53 class HostRefreshTest : public ::testing::Test {
54  protected:
55   virtual void SetUp() OVERRIDE {
56     hostRefreshProcessor_ = [[HostRefresh allocWithZone:nil] init];
57     delegateTester_ = [[HostRefreshDelegateTester alloc] init];
58     [hostRefreshProcessor_ setDelegate:delegateTester_];
59   }
61   void CreateHostList(int numHosts) {
62     [hostRefreshProcessor_
63         setJsonData:HostRefreshTestHelper::GetHostList(numHosts)];
64   }
66   NSError* CreateErrorFromString(NSString* message) {
67     NSDictionary* errorDictionary = nil;
69     if (message != nil) {
70       errorDictionary = @{NSLocalizedDescriptionKey : message};
71     }
73     return [[NSError alloc] initWithDomain:@"HostRefreshTest"
74                                       code:EPERM
75                                   userInfo:errorDictionary];
76   }
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
99   CreateHostList(0);
100   [hostRefreshProcessor_ connectionDidFinishLoading:nil];
101   ASSERT_TRUE(delegateTester_.hostList.count == 0);
103   CreateHostList(1);
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);
118   CreateHostList(11);
119   [hostRefreshProcessor_ connectionDidFinishLoading:nil];
120   ASSERT_TRUE(delegateTester_.hostList.count == 11);
122   // An error in parsing returns no hosts
123   [hostRefreshProcessor_
124       setJsonData:
125           [NSMutableData
126               dataWithData:
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
140   CreateHostList(0);
141   [hostRefreshProcessor_ connectionDidFinishLoading:nil];
142   ASSERT_FALSE([delegateTester_ receivedHosts]);
143   ASSERT_TRUE([delegateTester_ receivedErrorMessage]);
146 TEST_F(HostRefreshTest, HostListDelegateHasList) {
147   CreateHostList(1);
148   [hostRefreshProcessor_ connectionDidFinishLoading:nil];
149   ASSERT_TRUE([delegateTester_ receivedHosts]);
150   ASSERT_FALSE([delegateTester_ receivedErrorMessage]);
153 TEST_F(HostRefreshTest, HostListDelegateHasListWithError) {
154   CreateHostList(1);
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