-2
是Parse.initialize()一個即忘即用的異步方法嗎?是Parse.initialize()異步嗎?
考慮下面的代碼:
應用類:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Initialize Crash Reporting.
ParseCrashReporting.enable(this);
// Enable Local Datastore.
Parse.enableLocalDatastore(this);
// Add your initialization code here
Parse.initialize(this);
ParseACL defaultACL = new ParseACL();
// Optionally enable public read access.
// defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
}
}
測試用例:
import android.test.AndroidTestCase;
import com.parse.DeleteCallback;
import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import com.parse.SignUpCallback;
import java.util.concurrent.CountDownLatch;
/**
* Created by alex on 31/03/15.
*/
public class UserTest extends AndroidTestCase {
public UserTest() {
super();
}
@Override
protected void setUp() {
}
interface SignupAndLoginCallback {
public void done(ParseUser user, ParseException e);
}
public void signupAndLogin(final SignupAndLoginCallback callback) {
ParseUser user = new ParseUser();
user.setUsername("olatest2");
user.setPassword("kari4ever");
user.setEmail("[email protected]");
user.put("phone", "650-253-0000");
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
ParseUser.logInInBackground("olatest2", "kari4ever", new LogInCallback() {
public void done(ParseUser user, ParseException e) {
callback.done(user, e);
}
});
}
});
}
public void testSignupLoginDestroy() throws InterruptedException {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ParseUser user = new ParseUser();
user.setUsername("olatest");
user.setPassword("kari4ever");
user.setEmail("[email protected]");
user.put("phone", "650-253-0000");
final CountDownLatch latch = new CountDownLatch(1);
class Spy {
ParseUser user;
ParseException signupException;
ParseException loginException;
ParseException deleteException;
}
final Spy spy = new Spy();
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
spy.signupException = e;
ParseUser.logInInBackground("olatest", "kari4ever", new LogInCallback() {
public void done(ParseUser user, ParseException e) {
spy.loginException = e;
spy.user = user;
if (user == null || e != null) {
latch.countDown();
} else {
user.deleteEventually(new DeleteCallback() {
@Override
public void done(ParseException e) {
spy.deleteException = e;
latch.countDown();
}
});
}
}
});
}
});
latch.await();
assertNull(spy.signupException);
assertNull(spy.loginException);
assertNull(spy.deleteException);
assertNotNull(spy.user);
}
public void testCreateObject() throws InterruptedException {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
final CountDownLatch latch = new CountDownLatch(1);
class Spy {
ParseException loginException;
ParseException deleteException;
ParseException saveException;
ParseUser user;
}
final Spy spy = new Spy();
signupAndLogin(new SignupAndLoginCallback() {
@Override
public void done(ParseUser user, ParseException e) {
spy.loginException = e;
spy.user = user;
if (e == null && user != null) {
ParseObject gameScore = new ParseObject("GameScore");
gameScore.put("score", 1337);
gameScore.put("playerName", "Sean Plott");
gameScore.put("cheatMode", false);
gameScore.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
spy.saveException = e;
latch.countDown();
}
});
} else {
latch.countDown();
}
}
});
latch.await();
assertNull(spy.loginException);
assertNull(spy.deleteException);
assertNull(spy.saveException);
assertNotNull(spy.user);
}
}
如果Thread.sleep(1000);
調用從測試的情況下,有時測試炸燬與刪除例外:
java.lang.IllegalArgumentException: You must register this ParseObject subclass before instantiating it.
at com.parse.ParseObject.<init>(ParseObject.java:166)
at com.parse.ParseObject.<init>(ParseObject.java:127)
at com.parse.ParseUser.<init>(ParseUser.java:89)
at agens.no.gjensidigeparseandroidclient.UserTest.signupAndLogin(UserTest.java:35)
at agens.no.gjensidigeparseandroidclient.UserTest.testCreateObject(UserTest.java:122)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
我懷疑這是因爲代碼寄存器ParseUser
尚未運行。
如果是這樣,可以解析添加一個回調到Parse.initialize()
?
鍵是使用''標籤'AndroidManifest.xml'增加。這是設置ID和密鑰的另一種支持方式,它工作正常。如果'Parse.initialize()'不是遺忘和異常的異步,如果你在調用'initialize()'後直接嘗試使用'Parse'對象,你怎麼解釋'IllegalArgumentException'?如果測試用例線程睡幾百微秒,它就可以正常工作。我懷疑'Parse.initialize()'踢了一個沒有回調的異步任務。 –
alexbirkett
2015-04-01 04:36:07
我對測試用例並不熟悉,但是您在哪裏初始化測試代碼中的Parse? – xnagyg 2015-04-01 08:07:47
解析由MyApplication初始化,MyApplication在運行測試之前由測試框架新建 – alexbirkett 2015-04-07 09:26:43