• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.os.Bundle;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 import android.test.suitebuilder.annotation.MediumTest;
23 import android.test.suitebuilder.annotation.SmallTest;
24 
25 import com.android.messaging.BugleTestCase;
26 import com.android.messaging.FakeFactory;
27 import com.android.messaging.datamodel.DataModelImpl;
28 import com.android.messaging.datamodel.action.ActionTestHelpers.StubChatActionMonitor;
29 
30 import java.util.ArrayList;
31 
32 @MediumTest
33 public class ActionTest extends BugleTestCase {
34     private static final String parameter = "parameter";
35     private static final Object executeActionResult = "executeActionResult";
36     private static final Object processResponseResult = "processResponseResult";
37     private static final Object processFailureResult = "processFailureResult";
38 
39     private static final String mActionKey = "TheActionKey";
40     private static final Object mData = "PrivateData";
41     private StubChatActionMonitor mMonitor;
42     private TestChatAction mAction;
43 
44     private ArrayList<StubChatActionMonitor.StateTransition> mTransitions;
45 
46     @Override
setUp()47     public void setUp() throws Exception {
48         super.setUp();
49         FakeFactory.register(getTestContext())
50                 .withDataModel(new DataModelImpl(getContext()));
51 
52         mMonitor = new StubChatActionMonitor(ActionMonitor.STATE_CREATED, mActionKey,
53                 mData);
54         mAction = new TestChatAction(mActionKey, parameter);
55         mTransitions = mMonitor.getTransitions();
56     }
57 
verifyState(final int count, final int from, final int to)58     private void verifyState(final int count, final int from, final int to) {
59         assertEquals(to, mMonitor.getState());
60         assertEquals(mTransitions.size(), count);
61         verifyTransition(count-1, from , to);
62     }
63 
verifyTransition(final int index, final int from, final int to)64     private void verifyTransition(final int index, final int from, final int to) {
65         assertTrue(mTransitions.size() > index);
66         assertEquals(mAction, mTransitions.get(index).action);
67         assertEquals(from, mTransitions.get(index).from);
68         assertEquals(to, mTransitions.get(index).to);
69     }
70 
71     @SmallTest
testActionStartTransitionsCorrectly()72     public void testActionStartTransitionsCorrectly() {
73         mMonitor.setState(ActionMonitor.STATE_CREATED);
74 
75         ActionMonitor.registerActionMonitor(mAction.actionKey, mMonitor);
76         assertTrue(ActionMonitor.sActionMonitors.containsKey(mAction.actionKey));
77         assertTrue(ActionMonitor.sActionMonitors.containsValue(mMonitor));
78         assertEquals(ActionMonitor.sActionMonitors.get(mAction.actionKey), mMonitor);
79 
80         mAction.markStart();
81         assertEquals("After start state: STATE_QUEUED", ActionMonitor.STATE_QUEUED,
82                 mMonitor.getState());
83         verifyState(1, ActionMonitor.STATE_CREATED, ActionMonitor.STATE_QUEUED);
84 
85         ActionMonitor.unregisterActionMonitor(mAction.actionKey, mMonitor);
86 
87         assertFalse(ActionMonitor.sActionMonitors.containsKey(mAction.actionKey));
88         assertFalse(ActionMonitor.sActionMonitors.containsValue(mMonitor));
89     }
90 
91     @SmallTest
testActionStartAssertsFromIncorrectState()92     public void testActionStartAssertsFromIncorrectState() {
93         mMonitor.setState(ActionMonitor.STATE_UNDEFINED);
94 
95         ActionMonitor.registerActionMonitor(mAction.actionKey, mMonitor);
96         assertTrue(ActionMonitor.sActionMonitors.containsKey(mAction.actionKey));
97         assertTrue(ActionMonitor.sActionMonitors.containsValue(mMonitor));
98         assertEquals(ActionMonitor.sActionMonitors.get(mAction.actionKey), mMonitor);
99 
100         try {
101             mAction.markStart();
102             fail("Expect assertion when starting from STATE_UNDEFINED");
103         } catch (final IllegalStateException ex){
104         }
105         ActionMonitor.unregisterActionMonitor(mAction.actionKey, mMonitor);
106 
107         assertFalse(ActionMonitor.sActionMonitors.containsKey(mAction.actionKey));
108         assertFalse(ActionMonitor.sActionMonitors.containsValue(mMonitor));
109     }
110 
testActionTransitionsEndToEndWithRequests()111     public void testActionTransitionsEndToEndWithRequests() {
112         assertEquals("Start state: STATE_CREATED", ActionMonitor.STATE_CREATED,
113                 mMonitor.getState());
114 
115         ActionMonitor.registerActionMonitor(mAction.actionKey, mMonitor);
116         assertTrue(ActionMonitor.sActionMonitors.containsKey(mAction.actionKey));
117         assertTrue(ActionMonitor.sActionMonitors.containsValue(mMonitor));
118         assertEquals(ActionMonitor.sActionMonitors.get(mAction.actionKey), mMonitor);
119 
120         mAction.markStart();
121 
122         verifyState(1, ActionMonitor.STATE_CREATED, ActionMonitor.STATE_QUEUED);
123 
124         mAction.markBeginExecute();
125 
126         verifyState(2, ActionMonitor.STATE_QUEUED, ActionMonitor.STATE_EXECUTING);
127 
128         final Object result = mAction.executeAction();
129         mAction.requestBackgroundWork();
130 
131         assertEquals("Check executeAction result", result, executeActionResult);
132 
133         mAction.markEndExecute(result);
134 
135         verifyState(3, ActionMonitor.STATE_EXECUTING,
136                 ActionMonitor.STATE_BACKGROUND_ACTIONS_QUEUED);
137 
138         mAction.markBackgroundWorkStarting();
139 
140         verifyState(4, ActionMonitor.STATE_BACKGROUND_ACTIONS_QUEUED,
141                 ActionMonitor.STATE_EXECUTING_BACKGROUND_ACTION);
142 
143         mAction.markBackgroundWorkQueued();
144 
145         verifyState(5, ActionMonitor.STATE_EXECUTING_BACKGROUND_ACTION,
146                 ActionMonitor.STATE_BACKGROUND_ACTIONS_QUEUED);
147 
148         mAction.markBackgroundWorkStarting();
149 
150         verifyState(6, ActionMonitor.STATE_BACKGROUND_ACTIONS_QUEUED,
151                 ActionMonitor.STATE_EXECUTING_BACKGROUND_ACTION);
152 
153         final Bundle response = null;
154 
155         mAction.markBackgroundCompletionQueued();
156 
157         verifyState(7, ActionMonitor.STATE_EXECUTING_BACKGROUND_ACTION,
158                 ActionMonitor.STATE_BACKGROUND_COMPLETION_QUEUED);
159 
160         assertTrue(ActionMonitor.sActionMonitors.containsKey(mAction.actionKey));
161         assertTrue(ActionMonitor.sActionMonitors.containsValue(mMonitor));
162         assertEquals(ActionMonitor.sActionMonitors.get(mAction.actionKey), mMonitor);
163 
164         mAction.processBackgroundWorkResponse(response);
165 
166         verifyTransition(7, ActionMonitor.STATE_BACKGROUND_COMPLETION_QUEUED,
167                 ActionMonitor.STATE_PROCESSING_BACKGROUND_RESPONSE);
168 
169         verifyState(9, ActionMonitor.STATE_PROCESSING_BACKGROUND_RESPONSE,
170                 ActionMonitor.STATE_COMPLETE);
171 
172         assertFalse(ActionMonitor.sActionMonitors.containsKey(mAction.actionKey));
173         assertFalse(ActionMonitor.sActionMonitors.containsValue(mMonitor));
174     }
175 
testActionTransitionsEndToEndFailsRequests()176     public void testActionTransitionsEndToEndFailsRequests() {
177         assertEquals("Start state: STATE_CREATED", ActionMonitor.STATE_CREATED,
178                 mMonitor.getState());
179 
180         ActionMonitor.registerActionMonitor(mAction.actionKey, mMonitor);
181         assertTrue(ActionMonitor.sActionMonitors.containsKey(mAction.actionKey));
182         assertTrue(ActionMonitor.sActionMonitors.containsValue(mMonitor));
183         assertEquals(ActionMonitor.sActionMonitors.get(mAction.actionKey), mMonitor);
184 
185         mAction.markStart();
186 
187         verifyState(1, ActionMonitor.STATE_CREATED, ActionMonitor.STATE_QUEUED);
188 
189         mAction.markBeginExecute();
190 
191         verifyState(2, ActionMonitor.STATE_QUEUED, ActionMonitor.STATE_EXECUTING);
192 
193         final Object result = mAction.executeAction();
194         mAction.requestBackgroundWork();
195 
196         assertEquals("Check executeAction result", result, executeActionResult);
197 
198         mAction.markEndExecute(result);
199 
200         verifyState(3, ActionMonitor.STATE_EXECUTING,
201                 ActionMonitor.STATE_BACKGROUND_ACTIONS_QUEUED);
202 
203         mAction.markBackgroundWorkStarting();
204 
205         verifyState(4, ActionMonitor.STATE_BACKGROUND_ACTIONS_QUEUED,
206                 ActionMonitor.STATE_EXECUTING_BACKGROUND_ACTION);
207 
208         mAction.markBackgroundWorkQueued();
209 
210         verifyState(5, ActionMonitor.STATE_EXECUTING_BACKGROUND_ACTION,
211                 ActionMonitor.STATE_BACKGROUND_ACTIONS_QUEUED);
212 
213         mAction.markBackgroundWorkStarting();
214 
215         verifyState(6, ActionMonitor.STATE_BACKGROUND_ACTIONS_QUEUED,
216                 ActionMonitor.STATE_EXECUTING_BACKGROUND_ACTION);
217 
218         assertTrue(ActionMonitor.sActionMonitors.containsKey(mAction.actionKey));
219         assertTrue(ActionMonitor.sActionMonitors.containsValue(mMonitor));
220         assertEquals(ActionMonitor.sActionMonitors.get(mAction.actionKey), mMonitor);
221 
222         mAction.processBackgroundWorkFailure();
223 
224         verifyState(7, ActionMonitor.STATE_EXECUTING_BACKGROUND_ACTION,
225                 ActionMonitor.STATE_COMPLETE);
226 
227         assertFalse(ActionMonitor.sActionMonitors.containsKey(mAction.actionKey));
228         assertFalse(ActionMonitor.sActionMonitors.containsValue(mMonitor));
229     }
230 
testActionTransitionsEndToEndNoRequests()231     public void testActionTransitionsEndToEndNoRequests() {
232         assertEquals("Start state: STATE_CREATED", ActionMonitor.STATE_CREATED,
233                 mMonitor.getState());
234 
235         ActionMonitor.registerActionMonitor(mAction.actionKey, mMonitor);
236         assertTrue(ActionMonitor.sActionMonitors.containsKey(mAction.actionKey));
237         assertTrue(ActionMonitor.sActionMonitors.containsValue(mMonitor));
238         assertEquals(ActionMonitor.sActionMonitors.get(mAction.actionKey), mMonitor);
239 
240         mAction.markStart();
241 
242         verifyState(1, ActionMonitor.STATE_CREATED, ActionMonitor.STATE_QUEUED);
243 
244         mAction.markBeginExecute();
245 
246         verifyState(2, ActionMonitor.STATE_QUEUED, ActionMonitor.STATE_EXECUTING);
247 
248         final Object result = mAction.executeAction();
249 
250         assertEquals("Check executeAction result", result, executeActionResult);
251 
252         assertTrue(ActionMonitor.sActionMonitors.containsKey(mAction.actionKey));
253         assertTrue(ActionMonitor.sActionMonitors.containsValue(mMonitor));
254         assertEquals(ActionMonitor.sActionMonitors.get(mAction.actionKey), mMonitor);
255 
256         mAction.markEndExecute(result);
257 
258         verifyState(3, ActionMonitor.STATE_EXECUTING,
259                 ActionMonitor.STATE_COMPLETE);
260 
261         assertFalse(ActionMonitor.sActionMonitors.containsKey(mAction.actionKey));
262         assertFalse(ActionMonitor.sActionMonitors.containsValue(mMonitor));
263     }
264 
265     public static class TestChatAction extends Action implements Parcelable {
TestChatAction(final String key, final String parameter)266         protected TestChatAction(final String key, final String parameter) {
267             super(key);
268             this.parameter = parameter;
269         }
270 
271         public final String parameter;
272 
273         /**
274          * Process the action locally - runs on service thread
275          */
276         @Override
executeAction()277         protected Object executeAction() {
278             assertEquals("Check parameter", parameter, ActionTest.parameter);
279             return executeActionResult;
280         }
281 
282         /**
283          * Process the response from the server - runs on service thread
284          */
285         @Override
processBackgroundResponse(final Bundle response)286         protected Object processBackgroundResponse(final Bundle response) {
287             assertEquals("Check parameter", parameter, ActionTest.parameter);
288             return processResponseResult;
289         }
290 
291         /**
292          * Called in case of failures when sending requests - runs on service thread
293          */
294         @Override
processBackgroundFailure()295         protected Object processBackgroundFailure() {
296             assertEquals("Check parameter", parameter, ActionTest.parameter);
297             return processFailureResult;
298         }
299 
TestChatAction(final Parcel in)300         private TestChatAction(final Parcel in) {
301             super(in);
302             parameter = in.readString();
303         }
304 
305         public static final Parcelable.Creator<TestChatAction> CREATOR
306                 = new Parcelable.Creator<TestChatAction>() {
307             @Override
308             public TestChatAction createFromParcel(final Parcel in) {
309                 return new TestChatAction(in);
310             }
311 
312             @Override
313             public TestChatAction[] newArray(final int size) {
314                 return new TestChatAction[size];
315             }
316         };
317 
318         @Override
writeToParcel(final Parcel parcel, final int flags)319         public void writeToParcel(final Parcel parcel, final int flags) {
320             writeActionToParcel(parcel, flags);
321             parcel.writeString(parameter);
322         }
323     }
324 }
325