Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 46ed57e

Browse files
committed
add tests
1 parent 5566742 commit 46ed57e

File tree

9 files changed

+261
-0
lines changed

9 files changed

+261
-0
lines changed

CodePushDownloadHandler.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
5858

5959
self.progressCallback(self.expectedContentLength, self.receivedContentLength);
6060

61+
// bytesLeft should not be negative.
62+
assert(bytesLeft >= 0);
63+
6164
if (bytesLeft) {
6265
[self.outputFileStream close];
6366
[connection cancel];
@@ -72,6 +75,9 @@ - (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
7275
}
7376

7477
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
78+
// We should have received all of the bytes if this is called.
79+
assert(self.receivedContentLength == self.expectedContentLength);
80+
7581
[self.outputFileStream close];
7682
self.doneCallback();
7783
}

Examples/CodePushDemoApp/CodePushDemoApp.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
5451ACBA1B86A5B600E2A7DF /* QueryUpdateTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5451ACB81B86A5B600E2A7DF /* QueryUpdateTests.m */; };
2626
5451ACEC1B86E40A00E2A7DF /* libRCTTest.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 5451ACEB1B86E34300E2A7DF /* libRCTTest.a */; };
2727
54D774BA1B87DAF800F2ABF8 /* ApplyUpdateTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 54D774B91B87DAF800F2ABF8 /* ApplyUpdateTests.m */; };
28+
54F5F2B41BF6B45D007C3CEA /* DownloadProgressTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 54F5F2B31BF6B45D007C3CEA /* DownloadProgressTests.m */; settings = {ASSET_TAGS = (); }; };
2829
81551E1B1B3B428000F5B9F1 /* libCodePush.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 81551E0F1B3B427200F5B9F1 /* libCodePush.a */; };
2930
/* End PBXBuildFile section */
3031

@@ -151,6 +152,7 @@
151152
5451ACB81B86A5B600E2A7DF /* QueryUpdateTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QueryUpdateTests.m; sourceTree = "<group>"; };
152153
5451ACE61B86E34300E2A7DF /* RCTTest.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTTest.xcodeproj; path = "node_modules/react-native/Libraries/RCTTest/RCTTest.xcodeproj"; sourceTree = "<group>"; };
153154
54D774B91B87DAF800F2ABF8 /* ApplyUpdateTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ApplyUpdateTests.m; sourceTree = "<group>"; };
155+
54F5F2B31BF6B45D007C3CEA /* DownloadProgressTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DownloadProgressTests.m; sourceTree = "<group>"; };
154156
78C398B01ACF4ADC00677621 /* RCTLinking.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTLinking.xcodeproj; path = "node_modules/react-native/Libraries/LinkingIOS/RCTLinking.xcodeproj"; sourceTree = "<group>"; };
155157
81551E0A1B3B427200F5B9F1 /* CodePush.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = CodePush.xcodeproj; path = ../../CodePush.xcodeproj; sourceTree = "<group>"; };
156158
832341B01AAA6A8300B99B32 /* RCTText.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTText.xcodeproj; path = "node_modules/react-native/Libraries/Text/RCTText.xcodeproj"; sourceTree = "<group>"; };
@@ -238,6 +240,7 @@
238240
00E356EF1AD99517003FC87E /* CodePushDemoAppTests */ = {
239241
isa = PBXGroup;
240242
children = (
243+
54F5F2B31BF6B45D007C3CEA /* DownloadProgressTests.m */,
241244
5451ACB81B86A5B600E2A7DF /* QueryUpdateTests.m */,
242245
54D774B91B87DAF800F2ABF8 /* ApplyUpdateTests.m */,
243246
00E356F01AD99517003FC87E /* Supporting Files */,
@@ -608,6 +611,7 @@
608611
buildActionMask = 2147483647;
609612
files = (
610613
5451ACBA1B86A5B600E2A7DF /* QueryUpdateTests.m in Sources */,
614+
54F5F2B41BF6B45D007C3CEA /* DownloadProgressTests.m in Sources */,
611615
54D774BA1B87DAF800F2ABF8 /* ApplyUpdateTests.m in Sources */,
612616
);
613617
runOnlyForDeploymentPostprocessing = 0;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#import <UIKit/UIKit.h>
2+
#import <XCTest/XCTest.h>
3+
#import <RCTTest/RCTTestRunner.h>
4+
5+
#import "RCTAssert.h"
6+
7+
#define FB_REFERENCE_IMAGE_DIR "\"$(SOURCE_ROOT)/$(PROJECT_NAME)Tests/ReferenceImages\""
8+
9+
@interface DownloadProgressTests : XCTestCase
10+
11+
@end
12+
13+
@implementation DownloadProgressTests
14+
{
15+
RCTTestRunner *_runner;
16+
}
17+
18+
- (void)setUp
19+
{
20+
#if __LP64__
21+
RCTAssert(false, @"Tests should be run on 32-bit device simulators (e.g. iPhone 5)");
22+
#endif
23+
24+
NSOperatingSystemVersion version = [[NSProcessInfo processInfo] operatingSystemVersion];
25+
RCTAssert(version.majorVersion == 8 || version.minorVersion == 3, @"Tests should be run on iOS 8.3, found %zd.%zd.%zd", version.majorVersion, version.minorVersion, version.patchVersion);
26+
_runner = RCTInitRunnerForApp(@"CodePushDemoAppTests/DownloadProgressTests/DownloadProgressTestApp.ios", nil);
27+
}
28+
29+
#pragma mark Logic Tests
30+
- (void)testDownloadProgress
31+
{
32+
33+
[_runner runTest:_cmd module:@"DownloadProgressTest"];
34+
}
35+
36+
@end
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
'use strict';
2+
3+
var RCTTestModule = require('NativeModules').TestModule;
4+
var React = require('react-native');
5+
var CodePushSdk = require('react-native-code-push');
6+
var NativeBridge = require('react-native').NativeModules.CodePush;
7+
var { NativeAppEventEmitter } = require("react-native");
8+
9+
var {
10+
Text,
11+
View,
12+
} = React;
13+
14+
var DownloadProgressTest = React.createClass({
15+
propTypes: {
16+
shouldThrow: React.PropTypes.bool,
17+
waitOneFrame: React.PropTypes.bool,
18+
},
19+
20+
getInitialState() {
21+
return {
22+
done: false,
23+
};
24+
},
25+
26+
componentDidMount() {
27+
if (this.props.waitOneFrame) {
28+
requestAnimationFrame(this.runTest);
29+
} else {
30+
this.runTest();
31+
}
32+
},
33+
34+
checkReceivedAndExpectedBytesEqual() {
35+
if (this.state.progress.receivedBytes !== this.state.progress.totalBytes) {
36+
throw new Error("Bytes do not tally: Received bytes=" + this.state.progress.receivedBytes + " Total bytes=" + this.state.progress.totalBytes);
37+
}
38+
},
39+
40+
runTest() {
41+
var downloadProgressSubscription = NativeAppEventEmitter.addListener(
42+
"CodePushDownloadProgress",
43+
(progress) => {
44+
this.setState({
45+
progress:progress,
46+
done: false,
47+
});
48+
}
49+
);
50+
51+
var updates = require("./TestPackages");
52+
NativeBridge.downloadUpdate(updates.smallPackage)
53+
.then((smallPackage) => {
54+
if (smallPackage) {
55+
this.checkReceivedAndExpectedBytesEqual();
56+
return NativeBridge.downloadUpdate(updates.mediumPackage);
57+
} else {
58+
throw new Error("Small package download failed.");
59+
}
60+
})
61+
.then((mediumPackage) => {
62+
if (mediumPackage) {
63+
this.checkReceivedAndExpectedBytesEqual();
64+
return NativeBridge.downloadUpdate(updates.largePackage);
65+
} else {
66+
throw new Error("Medium package download failed.");
67+
}
68+
})
69+
.done((largePackage) => {
70+
if (largePackage) {
71+
this.checkReceivedAndExpectedBytesEqual();
72+
this.setState({done: true}, RCTTestModule.markTestCompleted);
73+
} else {
74+
throw new Error("Large package download failed.");
75+
}
76+
});
77+
},
78+
79+
render() {
80+
var progressView;
81+
if (this.state.progress) {
82+
progressView = (
83+
<Text>{this.state.progress.receivedBytes} of {this.state.progress.totalBytes} bytes received</Text>
84+
);
85+
}
86+
87+
return (
88+
<View style={{backgroundColor: 'white', padding: 40}}>
89+
<Text>
90+
{this.constructor.displayName + ': '}
91+
{this.state.done ? 'Done' : 'Testing...'}
92+
</Text>
93+
{progressView}
94+
</View>
95+
);
96+
}
97+
});
98+
99+
DownloadProgressTest.displayName = 'DownloadProgressTest';
100+
101+
module.exports = DownloadProgressTest;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'use strict';
2+
3+
var React = require('react-native');
4+
5+
var {
6+
AppRegistry,
7+
ScrollView,
8+
StyleSheet,
9+
Text,
10+
TouchableOpacity,
11+
View,
12+
} = React;
13+
14+
var TESTS = [
15+
require('./DownloadProgressTest')
16+
];
17+
18+
TESTS.forEach(
19+
(test) => AppRegistry.registerComponent(test.displayName, () => test)
20+
);
21+
22+
var DownloadProgressTestApp = React.createClass({
23+
getInitialState: function() {
24+
return {
25+
test: null,
26+
};
27+
},
28+
render: function() {
29+
if (this.state.test) {
30+
return (
31+
<ScrollView>
32+
<this.state.test />
33+
</ScrollView>
34+
);
35+
}
36+
return (
37+
<View style={styles.container}>
38+
<Text style={styles.row}>
39+
Click on a test to run it in this shell for easier debugging and
40+
development. Run all tests in the testing environment with cmd+U in
41+
Xcode.
42+
</Text>
43+
<View style={styles.separator} />
44+
<ScrollView>
45+
{TESTS.map((test) => [
46+
<TouchableOpacity
47+
onPress={() => this.setState({test})}
48+
style={styles.row}>
49+
<Text style={styles.testName}>
50+
{test.displayName}
51+
</Text>
52+
</TouchableOpacity>,
53+
<View style={styles.separator} />
54+
])}
55+
</ScrollView>
56+
</View>
57+
);
58+
}
59+
});
60+
61+
var styles = StyleSheet.create({
62+
container: {
63+
backgroundColor: 'white',
64+
marginTop: 40,
65+
margin: 15,
66+
},
67+
row: {
68+
padding: 10,
69+
},
70+
testName: {
71+
fontWeight: '500',
72+
},
73+
separator: {
74+
height: 1,
75+
backgroundColor: '#bbbbbb',
76+
}
77+
});
78+
79+
AppRegistry.registerComponent('DownloadProgressTestApp', () => DownloadProgressTestApp);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module.exports = {
2+
smallPackage: {
3+
downloadUrl: "http://localhost:8081/CodePushDemoAppTests/DownloadProgressTests/smallFile",
4+
description: "Angry flappy birds",
5+
appVersion: "1.5.0",
6+
label: "2.4.0",
7+
isMandatory: false,
8+
isAvailable: true,
9+
updateAppVersion: false,
10+
packageHash: "hash240",
11+
packageSize: 1024
12+
},
13+
mediumPackage: {
14+
downloadUrl: "http://localhost:8081/CodePushDemoAppTests/DownloadProgressTests/mediumFile",
15+
description: "Angry flappy birds",
16+
appVersion: "1.5.0",
17+
label: "2.4.0",
18+
isMandatory: false,
19+
isAvailable: true,
20+
updateAppVersion: false,
21+
packageHash: "hash240",
22+
packageSize: 1024
23+
},
24+
largePackage: {
25+
downloadUrl: "http://localhost:8081/CodePushDemoAppTests/DownloadProgressTests/largeFile",
26+
description: "Angry flappy birds",
27+
appVersion: "1.5.0",
28+
label: "2.4.0",
29+
isMandatory: false,
30+
isAvailable: true,
31+
updateAppVersion: false,
32+
packageHash: "hash240",
33+
packageSize: 1024
34+
}
35+
};
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)