PageRenderTime 1177ms CodeModel.GetById 1066ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/externals/GData/Source/HTTPFetcher/Tests/GTMReadMonitorInputStreamTest.m

http://google-email-uploader-mac.googlecode.com/
Objective C | 93 lines | 48 code | 21 blank | 24 comment | 4 complexity | 574223d5e847cff107d72136083e857c MD5 | raw file
 1/* Copyright (c) 2011 Google Inc.
 2 *
 3 * Licensed under the Apache License, Version 2.0 (the "License");
 4 * you may not use this file except in compliance with the License.
 5 * You may obtain a copy of the License at
 6 *
 7 *     http://www.apache.org/licenses/LICENSE-2.0
 8 *
 9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16//
17//  GTMReadMonitorInputStreamTest.m
18//
19
20#import <XCTest/XCTest.h>
21
22#import "GTMReadMonitorInputStream.h"
23
24@interface GTMReadMonitorInputStreamTest : XCTestCase {
25  NSMutableData *monitoredData_;
26}
27@end
28
29@implementation GTMReadMonitorInputStreamTest
30
31- (void)setUp {
32  monitoredData_ = [[NSMutableData alloc] init];
33}
34
35- (void)tearDown {
36  [monitoredData_ release];
37  monitoredData_ = nil;
38}
39
40- (void)testGTMReadMonitorInputStream {
41
42  // Make some data with lotsa bytes
43  NSMutableData *testData = [NSMutableData data];
44  for (int idx = 0; idx < 100; idx++) {
45    const char *str = "abcdefghijklmnopqrstuvwxyz ";
46    [testData appendBytes:str length:strlen(str)];
47  }
48
49  // Make a stream for the data
50  NSInputStream *dataStream = [NSInputStream inputStreamWithData:testData];
51
52  // Make a monitor stream, with self as the delegate
53  GTMReadMonitorInputStream *monitorStream;
54  monitorStream = [GTMReadMonitorInputStream inputStreamWithStream:dataStream];
55  SEL sel = @selector(inputStream:readIntoBuffer:length:);
56
57  monitorStream.readDelegate = self;
58  monitorStream.readSelector = sel;
59
60  // Now read random size chunks of data and append them to a mutable NSData
61  NSMutableData *readData = [NSMutableData data];
62
63  [monitorStream open];
64  while ([monitorStream hasBytesAvailable]) {
65
66    unsigned char buffer[101];
67    NSUInteger numBytesToRead = (arc4random() % 100) + 1;
68
69    NSInteger numRead = [monitorStream read:buffer maxLength:numBytesToRead];
70    if (numRead == 0) break;
71
72    // Append the read chunk to our buffer
73    [readData appendBytes:buffer length:numRead];
74  }
75  [monitorStream close];
76
77  // Verify we read all the data
78  XCTAssertEqualObjects(readData, testData,
79                       @"read data doesn't match stream data");
80
81  // Verify the callback saw the same data
82  XCTAssertEqualObjects(monitoredData_, testData,
83                       @"callback progress doesn't match actual progress");
84}
85
86
87- (void)inputStream:(GTMReadMonitorInputStream *)stream
88     readIntoBuffer:(uint8_t *)buffer
89             length:(NSUInteger)length {
90  [monitoredData_ appendBytes:buffer
91                       length:length];
92}
93@end