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

Commit 35182ca

Browse files
authored
[Tests] Update project template for tests (#1773)
1 parent 950702e commit 35182ca

File tree

5 files changed

+152
-109
lines changed

5 files changed

+152
-109
lines changed

test/template/android/app/src/main/java/com/microsoft/codepush/test/MainActivity.java

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.testcodepush;
2+
3+
import android.app.Application;
4+
import com.microsoft.codepush.react.CodePush;
5+
import android.content.Context;
6+
import com.facebook.react.PackageList;
7+
import com.facebook.react.ReactApplication;
8+
import com.facebook.react.ReactNativeHost;
9+
import com.facebook.react.ReactPackage;
10+
import com.facebook.soloader.SoLoader;
11+
import java.lang.reflect.InvocationTargetException;
12+
import java.util.List;
13+
14+
public class MainApplication extends Application implements ReactApplication {
15+
16+
private final ReactNativeHost mReactNativeHost =
17+
new ReactNativeHost(this) {
18+
@Override
19+
public boolean getUseDeveloperSupport() {
20+
return BuildConfig.DEBUG;
21+
}
22+
23+
@Override
24+
protected String getJSBundleFile() {
25+
return CodePush.getJSBundleFile();
26+
}
27+
28+
@Override
29+
protected List<ReactPackage> getPackages() {
30+
@SuppressWarnings("UnnecessaryLocalVariable")
31+
List<ReactPackage> packages = new PackageList(this).getPackages();
32+
// Packages that cannot be autolinked yet can be added manually here, for example:
33+
// packages.add(new MyReactNativePackage());
34+
return packages;
35+
}
36+
37+
@Override
38+
protected String getJSMainModuleName() {
39+
return "index";
40+
}
41+
};
42+
43+
@Override
44+
public ReactNativeHost getReactNativeHost() {
45+
return mReactNativeHost;
46+
}
47+
48+
@Override
49+
public void onCreate() {
50+
super.onCreate();
51+
SoLoader.init(this, /* native exopackage */ false);
52+
initializeFlipper(this); // Remove this line if you don't want Flipper enabled
53+
}
54+
55+
/**
56+
* Loads Flipper in React Native templates.
57+
*
58+
* @param context
59+
*/
60+
private static void initializeFlipper(Context context) {
61+
if (BuildConfig.DEBUG) {
62+
try {
63+
/*
64+
We use reflection here to pick up the class that initializes Flipper,
65+
since Flipper library is not available in release mode
66+
*/
67+
Class<?> aClass = Class.forName("com.facebook.flipper.ReactNativeFlipper");
68+
aClass.getMethod("initializeFlipper", Context.class).invoke(null, context);
69+
} catch (ClassNotFoundException e) {
70+
e.printStackTrace();
71+
} catch (NoSuchMethodException e) {
72+
e.printStackTrace();
73+
} catch (IllegalAccessException e) {
74+
e.printStackTrace();
75+
} catch (InvocationTargetException e) {
76+
e.printStackTrace();
77+
}
78+
}
79+
}
80+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<resources>
2+
<string name="app_name">TestCodePush</string>
3+
<string moduleConfig="true" name="CodePushDeploymentKey">CODE_PUSH_ANDROID_DEPLOYMENT_KEY</string>
4+
<string moduleConfig="true" name="CodePushServerUrl">CODE_PUSH_SERVER_URL</string>
5+
</resources>

test/template/index.js

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
*/
44

55
import React, {
6-
Component
6+
Component
77
} from 'react';
88

99
import {
10-
AppRegistry,
11-
StyleSheet,
12-
Text,
13-
View
10+
AppRegistry,
11+
StyleSheet,
12+
Text,
13+
View
1414
} from 'react-native';
1515

1616
import CodePush from "react-native-code-push";
@@ -20,9 +20,12 @@ var testScenario = require("./CODE_PUSH_INDEX_JS_PATH");
2020
/** A promise that maintains synchronous sending of the test messages. */
2121
var testMessageQueue;
2222

23-
var CODE_PUSH_TEST_APP_NAME = React.createClass({
23+
class CODE_PUSH_TEST_APP_NAME extends Component {
2424
// CodePush API Callbacks
25-
25+
constructor() {
26+
super();
27+
this.state = { message: '' };
28+
}
2629
// checkForUpdate
2730
checkUpdateSuccess(remotePackage) {
2831
if (remotePackage) {
@@ -34,42 +37,42 @@ var CODE_PUSH_TEST_APP_NAME = React.createClass({
3437
} else {
3538
return this.setStateAndSendMessage("The application is up to date.", "CHECK_UP_TO_DATE");
3639
}
37-
},
40+
}
3841
checkUpdateError(error) {
3942
return this.setStateAndSendMessage("An error occured while checking for updates:\n" + error, "CHECK_ERROR");
40-
},
41-
43+
}
44+
4245
// remotePackage.download
4346
downloadSuccess(localPackage) {
4447
return this.setStateAndSendMessage("Download succeeded.", "DOWNLOAD_SUCCEEDED", [localPackage]);
45-
},
48+
}
4649
downloadError(error) {
4750
return this.setStateAndSendMessage("Download error:\n" + error, "DOWNLOAD_ERROR");
48-
},
49-
51+
}
52+
5053
// localPackage.install
5154
installSuccess() {
5255
return this.setStateAndSendMessage("Update installed.", "UPDATE_INSTALLED");
53-
},
56+
}
5457
installError() {
5558
return this.setStateAndSendMessage("Install error.", "INSTALL_ERROR");
56-
},
57-
59+
}
60+
5861
// sync
5962
onSyncStatus(status) {
6063
return this.setStateAndSendMessage("Sync status " + status + " received.", "SYNC_STATUS", [status]);
61-
},
64+
}
6265
onSyncError(error) {
6366
return this.setStateAndSendMessage("Sync error " + error + " received.", "SYNC_STATUS", [CodePush.SyncStatus.UNKNOWN_ERROR]);
64-
},
65-
66-
67+
}
68+
69+
6770
// Test Output Methods
68-
71+
6972
readyAfterUpdate(callback) {
7073
return this.setStateAndSendMessage("Ready after update.", "DEVICE_READY_AFTER_UPDATE", undefined, callback);
71-
},
72-
74+
}
75+
7376
sendCurrentAndPendingPackage() {
7477
return CodePush.getUpdateMetadata(CodePush.UpdateState.PENDING)
7578
.then((pendingPackage) => {
@@ -79,69 +82,69 @@ var CODE_PUSH_TEST_APP_NAME = React.createClass({
7982
.then((currentPackage) => {
8083
return this.setStateAndSendMessage("Current package: " + currentPackage, "CURRENT_PACKAGE", [currentPackage ? currentPackage.packageHash : null]);
8184
});
82-
},
83-
85+
}
86+
8487
setStateAndSendMessage(message, testMessage, args, callback) {
8588
this.setState({
8689
message: this.state.message + "\n...\n" + message
8790
});
8891
return this.sendTestMessage(testMessage, args, callback);
89-
},
90-
92+
}
93+
9194
sendTestMessage(message, args, callback) {
9295
function makeNetworkCall() {
93-
return new Promise(function(resolve, reject) {
96+
return new Promise(function (resolve, reject) {
9497
var xhr = new XMLHttpRequest();
95-
98+
9699
xhr.onreadystatechange = function () {
97100
if (xhr.readyState == 4 && xhr.status == 200) {
98101
callback && callback(xhr.response);
99102
resolve();
100103
}
101104
};
102-
105+
103106
xhr.open("POST", "CODE_PUSH_SERVER_URL/reportTestMessage", true);
104-
var body = JSON.stringify({ message: message, args: args});
107+
var body = JSON.stringify({ message: message, args: args });
105108
console.log("Sending test message body: " + body);
106109

107110
xhr.setRequestHeader("Content-type", "application/json");
108-
111+
109112
xhr.send(body);
110113
});
111114
}
112-
115+
113116
if (!testMessageQueue) testMessageQueue = makeNetworkCall();
114117
else testMessageQueue = testMessageQueue.then(makeNetworkCall);
115-
118+
116119
return testMessageQueue;
117-
},
118-
119-
120+
}
121+
122+
120123
// Test Setup Methods
121-
124+
122125
componentDidMount() {
123126
testScenario.startTest(this);
124-
},
125-
127+
}
128+
126129
getInitialState() {
127130
return {
128131
message: ""
129132
};
130-
},
133+
}
131134

132135
render() {
133136
return (
134-
<View style={styles.container}>
135-
<Text style={styles.welcome}>
136-
CodePush React-Native Plugin Tests
137-
</Text>
138-
<Text style={styles.instructions}>
139-
{testScenario.getScenarioName()}{this.state.message}
137+
<View style={styles.container}>
138+
<Text style={styles.welcome}>
139+
CodePush React-Native Plugin Tests
140140
</Text>
141-
</View>
141+
<Text style={styles.instructions}>
142+
{testScenario.getScenarioName()}{this.state.message}
143+
</Text>
144+
</View>
142145
);
143146
}
144-
});
147+
};
145148

146149
const styles = StyleSheet.create({
147150
container: {
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
#import "AppDelegate.h"
22

3-
#import "RCTRootView.h"
3+
#import <CodePush/CodePush.h>
44

5-
#import "CodePush.h"
6-
7-
#import "RCTLog.h"
5+
#import <React/RCTBridge.h>
6+
#import <React/RCTBundleURLProvider.h>
7+
#import <React/RCTRootView.h>
88

99
@implementation AppDelegate
1010

1111
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
1212
{
13-
RCTSetLogThreshold(RCTLogLevelInfo);
14-
15-
NSURL *jsCodeLocation;
1613

17-
jsCodeLocation = [CodePush bundleURL];
14+
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
15+
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
16+
moduleName:@"CODE_PUSH_TEST_APP_NAME"
17+
initialProperties:nil];
1818

19-
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
20-
moduleName:@"CODE_PUSH_TEST_APP_NAME"
21-
initialProperties:nil
22-
launchOptions:launchOptions];
19+
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
2320

2421
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
25-
UIViewController *rootViewController = [[UIViewController alloc] init];
22+
UIViewController *rootViewController = [UIViewController new];
2623
rootViewController.view = rootView;
2724
self.window.rootViewController = rootViewController;
2825
[self.window makeKeyAndVisible];
2926
return YES;
3027
}
3128

29+
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
30+
{
31+
return [CodePush bundleURL];
32+
}
33+
3234
@end

0 commit comments

Comments
 (0)