1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.messaging.datamodel.action;
18 
19 import android.content.Context;
20 import android.database.ContentObserver;
21 import android.net.Uri;
22 import android.os.Bundle;
23 
24 import java.util.ArrayList;
25 import java.util.List;
26 
27 public class ActionTestHelpers {
28     private static final String TAG = "DataModelTestHelpers";
29 
30     static class StubLoader extends ContentObserver {
31         ArrayList<Uri> mUriList = new ArrayList<Uri>();
32 
StubLoader()33         StubLoader() {
34             super(null);
35         }
36 
clear()37         public void clear() {
38             mUriList.clear();
39         }
40 
41         @Override
onChange(final boolean selfChange)42         public void onChange(final boolean selfChange) {
43             // Handle change.
44             mUriList.add(null);
45         }
46 
47         // Implement the onChange(boolean, Uri) method to take advantage of the new Uri argument.
48         // Only supported on platform 16 and above...
49         @Override
onChange(final boolean selfChange, final Uri uri)50         public void onChange(final boolean selfChange, final Uri uri) {
51             // Handle change.
52             mUriList.add(uri);
53         }
54     }
55 
56     static class StubBackgroundWorker extends BackgroundWorker {
StubBackgroundWorker()57         public StubBackgroundWorker() {
58             super();
59             mActions = new ArrayList<Action>();
60         }
61 
62         ArrayList<Action> mActions;
getRequestsMade()63         public ArrayList<Action> getRequestsMade() {
64             return mActions;
65         }
66 
67         @Override
queueBackgroundWork(final List<Action> actions)68         public void queueBackgroundWork(final List<Action> actions) {
69             mActions.addAll(actions);
70 
71             synchronized(this) {
72                 this.notifyAll();
73             }
74         }
75     }
76 
77     static class ResultTracker {
78         public Object executionResult;
79         public Object completionResult;
80     }
81 
82     static class StubChatActionMonitor extends ActionMonitor {
83         static public class StateTransition {
84             Action action;
85             int from;
86             int to;
StateTransition(final Action action, final int from, final int to)87             public StateTransition(final Action action, final int from, final int to) {
88                 this.action = action;
89                 this.from = from;
90                 this.to = to;
91             }
92         }
93 
94         private final ArrayList<StateTransition> mTransitions;
getTransitions()95         public ArrayList<StateTransition> getTransitions() {
96             return mTransitions;
97         }
98 
StubChatActionMonitor(final int initialState, final String actionKey, final Object data)99         protected StubChatActionMonitor(final int initialState, final String actionKey,
100                 final Object data) {
101             super(initialState, actionKey, data);
102             mTransitions =  new ArrayList<StateTransition>();
103         }
104 
105         @Override
updateState(final Action action, final int expectedState, final int state)106         protected void updateState(final Action action, final int expectedState,
107                 final int state) {
108             mTransitions.add(new StateTransition(action, mState, state));
109             super.updateState(action, expectedState, state);
110         }
111 
setState(final int state)112         public void setState(final int state) {
113             mState = state;
114         }
115 
getState()116         public int getState() {
117             return mState;
118         }
119     }
120 
121     public static class StubActionService extends ActionService {
122         public static class StubActionServiceCallLog {
123             public final Action action;
124             public final Action request;
125             public final Bundle response;
126             public final Exception exception;
127             public final Action update;
128 
StubActionServiceCallLog(final Action action, final Action request, final Bundle response, final Exception exception, final Action update)129             public StubActionServiceCallLog(final Action action,
130                     final Action request,
131                     final Bundle response,
132                     final Exception exception,
133                     final Action update) {
134                 this.action = action;
135                 this.request = request;
136                 this.response = response;
137                 this.exception = exception;
138                 this.update = update;
139             }
140         }
141 
142         private final ArrayList<StubActionServiceCallLog> mServiceCalls =
143                 new ArrayList<StubActionServiceCallLog>();
144 
getCalls()145         public ArrayList<StubActionServiceCallLog> getCalls() {
146             return mServiceCalls;
147         }
148 
149         @Override
startAction(final Action action)150         public void startAction(final Action action) {
151             mServiceCalls.add(new StubActionServiceCallLog(action, null, null, null, null));
152             synchronized(this) {
153                 this.notifyAll();
154             }
155         }
156 
157         @Override
handleResponseFromBackgroundWorker(final Action request, final Bundle response)158         public void handleResponseFromBackgroundWorker(final Action request,
159                 final Bundle response) {
160             mServiceCalls.add(new StubActionServiceCallLog(null, request, response, null, null));
161             synchronized(this) {
162                 this.notifyAll();
163             }
164         }
165 
166         @Override
handleFailureFromBackgroundWorker(final Action request, final Exception exception)167         protected void handleFailureFromBackgroundWorker(final Action request,
168                 final Exception exception) {
169             mServiceCalls.add(new StubActionServiceCallLog(null, request, null, exception, null));
170             synchronized(this) {
171                 this.notifyAll();
172             }
173         }
174     }
175 }
176