|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 package org.mozilla.gecko.background.sync.helpers; |
|
5 |
|
6 import static junit.framework.Assert.assertNotNull; |
|
7 |
|
8 import org.mozilla.gecko.background.common.log.Logger; |
|
9 import org.mozilla.gecko.background.testhelpers.WaitHelper; |
|
10 import org.mozilla.gecko.sync.repositories.InvalidSessionTransitionException; |
|
11 import org.mozilla.gecko.sync.repositories.Repository; |
|
12 import org.mozilla.gecko.sync.repositories.RepositorySession; |
|
13 |
|
14 import android.content.Context; |
|
15 |
|
16 public class SessionTestHelper { |
|
17 |
|
18 protected static RepositorySession prepareRepositorySession( |
|
19 final Context context, |
|
20 final boolean begin, |
|
21 final Repository repository) { |
|
22 |
|
23 final WaitHelper testWaiter = WaitHelper.getTestWaiter(); |
|
24 |
|
25 final String logTag = "prepareRepositorySession"; |
|
26 class CreationDelegate extends DefaultSessionCreationDelegate { |
|
27 private RepositorySession session; |
|
28 synchronized void setSession(RepositorySession session) { |
|
29 this.session = session; |
|
30 } |
|
31 synchronized RepositorySession getSession() { |
|
32 return this.session; |
|
33 } |
|
34 |
|
35 @Override |
|
36 public void onSessionCreated(final RepositorySession session) { |
|
37 assertNotNull(session); |
|
38 Logger.info(logTag, "Setting session to " + session); |
|
39 setSession(session); |
|
40 if (begin) { |
|
41 Logger.info(logTag, "Calling session.begin on new session."); |
|
42 // The begin callbacks will notify. |
|
43 try { |
|
44 session.begin(new ExpectBeginDelegate()); |
|
45 } catch (InvalidSessionTransitionException e) { |
|
46 testWaiter.performNotify(e); |
|
47 } |
|
48 } else { |
|
49 Logger.info(logTag, "Notifying after setting new session."); |
|
50 testWaiter.performNotify(); |
|
51 } |
|
52 } |
|
53 } |
|
54 |
|
55 final CreationDelegate delegate = new CreationDelegate(); |
|
56 try { |
|
57 Runnable runnable = new Runnable() { |
|
58 @Override |
|
59 public void run() { |
|
60 repository.createSession(delegate, context); |
|
61 } |
|
62 }; |
|
63 testWaiter.performWait(runnable); |
|
64 } catch (IllegalArgumentException ex) { |
|
65 Logger.warn(logTag, "Caught IllegalArgumentException."); |
|
66 } |
|
67 |
|
68 Logger.info(logTag, "Retrieving new session."); |
|
69 final RepositorySession session = delegate.getSession(); |
|
70 assertNotNull(session); |
|
71 |
|
72 return session; |
|
73 } |
|
74 |
|
75 public static RepositorySession createSession(final Context context, final Repository repository) { |
|
76 return prepareRepositorySession(context, false, repository); |
|
77 } |
|
78 |
|
79 public static RepositorySession createAndBeginSession(Context context, Repository repository) { |
|
80 return prepareRepositorySession(context, true, repository); |
|
81 } |
|
82 } |