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