/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. // GTMReadMonitorInputStreamTest.m
  17. //
  18. #import <XCTest/XCTest.h>
  19. #import "GTMReadMonitorInputStream.h"
  20. @interface GTMReadMonitorInputStreamTest : XCTestCase {
  21. NSMutableData *monitoredData_;
  22. }
  23. @end
  24. @implementation GTMReadMonitorInputStreamTest
  25. - (void)setUp {
  26. monitoredData_ = [[NSMutableData alloc] init];
  27. }
  28. - (void)tearDown {
  29. [monitoredData_ release];
  30. monitoredData_ = nil;
  31. }
  32. - (void)testGTMReadMonitorInputStream {
  33. // Make some data with lotsa bytes
  34. NSMutableData *testData = [NSMutableData data];
  35. for (int idx = 0; idx < 100; idx++) {
  36. const char *str = "abcdefghijklmnopqrstuvwxyz ";
  37. [testData appendBytes:str length:strlen(str)];
  38. }
  39. // Make a stream for the data
  40. NSInputStream *dataStream = [NSInputStream inputStreamWithData:testData];
  41. // Make a monitor stream, with self as the delegate
  42. GTMReadMonitorInputStream *monitorStream;
  43. monitorStream = [GTMReadMonitorInputStream inputStreamWithStream:dataStream];
  44. SEL sel = @selector(inputStream:readIntoBuffer:length:);
  45. monitorStream.readDelegate = self;
  46. monitorStream.readSelector = sel;
  47. // Now read random size chunks of data and append them to a mutable NSData
  48. NSMutableData *readData = [NSMutableData data];
  49. [monitorStream open];
  50. while ([monitorStream hasBytesAvailable]) {
  51. unsigned char buffer[101];
  52. NSUInteger numBytesToRead = (arc4random() % 100) + 1;
  53. NSInteger numRead = [monitorStream read:buffer maxLength:numBytesToRead];
  54. if (numRead == 0) break;
  55. // Append the read chunk to our buffer
  56. [readData appendBytes:buffer length:numRead];
  57. }
  58. [monitorStream close];
  59. // Verify we read all the data
  60. XCTAssertEqualObjects(readData, testData,
  61. @"read data doesn't match stream data");
  62. // Verify the callback saw the same data
  63. XCTAssertEqualObjects(monitoredData_, testData,
  64. @"callback progress doesn't match actual progress");
  65. }
  66. - (void)inputStream:(GTMReadMonitorInputStream *)stream
  67. readIntoBuffer:(uint8_t *)buffer
  68. length:(NSUInteger)length {
  69. [monitoredData_ appendBytes:buffer
  70. length:length];
  71. }
  72. @end