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 com.android.messaging.util.ConnectivityUtil;
25 
26 import java.util.ArrayList;
27 import java.util.List;
28 
29 public class ActionTestHelpers {
30     private static final String TAG = "DataModelTestHelpers";
31 
32     static class StubLoader extends ContentObserver {
33         ArrayList<Uri> mUriList = new ArrayList<Uri>();
34 
StubLoader()35         StubLoader() {
36             super(null);
37         }
38 
clear()39         public void clear() {
40             mUriList.clear();
41         }
42 
43         @Override
onChange(final boolean selfChange)44         public void onChange(final boolean selfChange) {
45             // Handle change.
46             mUriList.add(null);
47         }
48 
49         // Implement the onChange(boolean, Uri) method to take advantage of the new Uri argument.
50         // Only supported on platform 16 and above...
51         @Override
onChange(final boolean selfChange, final Uri uri)52         public void onChange(final boolean selfChange, final Uri uri) {
53             // Handle change.
54             mUriList.add(uri);
55         }
56     }
57 
58     static class StubBackgroundWorker extends BackgroundWorker {
StubBackgroundWorker()59         public StubBackgroundWorker() {
60             super();
61             mActions = new ArrayList<Action>();
62         }
63 
64         ArrayList<Action> mActions;
getRequestsMade()65         public ArrayList<Action> getRequestsMade() {
66             return mActions;
67         }
68 
69         @Override
queueBackgroundWork(final List<Action> actions)70         public void queueBackgroundWork(final List<Action> actions) {
71             mActions.addAll(actions);
72 
73             synchronized(this) {
74                 this.notifyAll();
75             }
76         }
77     }
78 
79     static class ResultTracker {
80         public Object executionResult;
81         public Object completionResult;
82     }
83 
84     static class StubChatActionMonitor extends ActionMonitor {
85         static public class StateTransition {
86             Action action;
87             int from;
88             int to;
StateTransition(final Action action, final int from, final int to)89             public StateTransition(final Action action, final int from, final int to) {
90                 this.action = action;
91                 this.from = from;
92                 this.to = to;
93             }
94         }
95 
96         private final ArrayList<StateTransition> mTransitions;
getTransitions()97         public ArrayList<StateTransition> getTransitions() {
98             return mTransitions;
99         }
100 
StubChatActionMonitor(final int initialState, final String actionKey, final Object data)101         protected StubChatActionMonitor(final int initialState, final String actionKey,
102                 final Object data) {
103             super(initialState, actionKey, data);
104             mTransitions =  new ArrayList<StateTransition>();
105         }
106 
107         @Override
updateState(final Action action, final int expectedState, final int state)108         protected void updateState(final Action action, final int expectedState,
109                 final int state) {
110             mTransitions.add(new StateTransition(action, mState, state));
111             super.updateState(action, expectedState, state);
112         }
113 
setState(final int state)114         public void setState(final int state) {
115             mState = state;
116         }
117 
getState()118         public int getState() {
119             return mState;
120         }
121     }
122 
123     public static class StubActionService extends ActionService {
124         public static class StubActionServiceCallLog {
125             public final Action action;
126             public final Action request;
127             public final Bundle response;
128             public final Exception exception;
129             public final Action update;
130 
StubActionServiceCallLog(final Action action, final Action request, final Bundle response, final Exception exception, final Action update)131             public StubActionServiceCallLog(final Action action,
132                     final Action request,
133                     final Bundle response,
134                     final Exception exception,
135                     final Action update) {
136                 this.action = action;
137                 this.request = request;
138                 this.response = response;
139                 this.exception = exception;
140                 this.update = update;
141             }
142         }
143 
144         private final ArrayList<StubActionServiceCallLog> mServiceCalls =
145                 new ArrayList<StubActionServiceCallLog>();
146 
getCalls()147         public ArrayList<StubActionServiceCallLog> getCalls() {
148             return mServiceCalls;
149         }
150 
151         @Override
startAction(final Action action)152         public void startAction(final Action action) {
153             mServiceCalls.add(new StubActionServiceCallLog(action, null, null, null, null));
154             synchronized(this) {
155                 this.notifyAll();
156             }
157         }
158 
159         @Override
handleResponseFromBackgroundWorker(final Action request, final Bundle response)160         public void handleResponseFromBackgroundWorker(final Action request,
161                 final Bundle response) {
162             mServiceCalls.add(new StubActionServiceCallLog(null, request, response, null, null));
163             synchronized(this) {
164                 this.notifyAll();
165             }
166         }
167 
168         @Override
handleFailureFromBackgroundWorker(final Action request, final Exception exception)169         protected void handleFailureFromBackgroundWorker(final Action request,
170                 final Exception exception) {
171             mServiceCalls.add(new StubActionServiceCallLog(null, request, null, exception, null));
172             synchronized(this) {
173                 this.notifyAll();
174             }
175         }
176     }
177 
178     public static class StubConnectivityUtil extends ConnectivityUtil {
StubConnectivityUtil(final Context context)179         public StubConnectivityUtil(final Context context) {
180             super(context);
181         }
182 
183         @Override
registerForSignalStrength()184         public void registerForSignalStrength() {
185         }
186 
187         @Override
unregisterForSignalStrength()188         public void unregisterForSignalStrength() {
189         }
190     }
191 }
192