Home | History | Annotate | Download | only in action
      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 
     35         StubLoader() {
     36             super(null);
     37         }
     38 
     39         public void clear() {
     40             mUriList.clear();
     41         }
     42 
     43         @Override
     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
     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 {
     59         public StubBackgroundWorker() {
     60             super();
     61             mActions = new ArrayList<Action>();
     62         }
     63 
     64         ArrayList<Action> mActions;
     65         public ArrayList<Action> getRequestsMade() {
     66             return mActions;
     67         }
     68 
     69         @Override
     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;
     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;
     97         public ArrayList<StateTransition> getTransitions() {
     98             return mTransitions;
     99         }
    100 
    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
    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 
    114         public void setState(final int state) {
    115             mState = state;
    116         }
    117 
    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 
    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 
    147         public ArrayList<StubActionServiceCallLog> getCalls() {
    148             return mServiceCalls;
    149         }
    150 
    151         @Override
    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
    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
    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 {
    179         public StubConnectivityUtil(final Context context) {
    180             super(context);
    181         }
    182 
    183         @Override
    184         public void registerForSignalStrength() {
    185         }
    186 
    187         @Override
    188         public void unregisterForSignalStrength() {
    189         }
    190     }
    191 }
    192