Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d21f1c1e authored by Guang Zhu's avatar Guang Zhu
Browse files

fixes for connectivity manager test suite

* reduced visibility of most util functions in
  ConnectivityManagerTestBase
* reimplemented various waitFor... util function in base class:
  instead of relying on received broadcast and synchronization
  mechanism, we just poll for what we want with a fixed interval
  and a max timeout
* minor style fixes
* removed state transition checks in all test cases of
  ConnectivityManagerMobileTest: since exact state transitions
  are not that interesting, we just care about if we reach a
  particular state and if all network interfaces are in their
  corresponding states
* clarified in comment what each test cases are testing, since
  some of the test case names are not very straightforward
* any wait for DISCONNECTED checkes with ConnectivityManager are
  temporarily commented out

Change-Id: I99cca95c33c6060909e0df684969b75c2dca23a9
parent 5ba69067
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -66,7 +66,6 @@ import java.util.List;
 *      networkprefixlength.
 */
public class AccessPointParserHelper {
    private static final String TAG = "AccessPointParserHelper";
    static final int NONE = 0;
    static final int WEP = 1;
    static final int PSK = 2;
+43 −15
Original line number Diff line number Diff line
@@ -35,14 +35,14 @@ import junit.framework.TestSuite;
 */

public class ConnectivityManagerStressTestRunner extends InstrumentationTestRunner {
    public int mSoftapIterations = 100;
    public int mScanIterations = 100;
    public int mReconnectIterations = 100;
    private int mSoftApIterations = 100;
    private int mScanIterations = 100;
    private int mReconnectIterations = 100;
    // sleep time before restart wifi, default is set to 2 minutes
    public int mSleepTime = 2 * 60 * 1000;
    public String mReconnectSsid = "securenetdhcp";
    public String mReconnectPassword = "androidwifi";
    public boolean mWifiOnlyFlag = false;
    private long mSleepTime = 2 * 60 * 1000;
    private String mReconnectSsid = null;
    private String mReconnectPassword = null;
    private boolean mWifiOnlyFlag = false;

    @Override
    public TestSuite getAllTests() {
@@ -60,15 +60,15 @@ public class ConnectivityManagerStressTestRunner extends InstrumentationTestRunn
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        String valueStr = (String) icicle.get("softap_iterations");
        String valueStr = icicle.getString("softap_iterations");
        if (valueStr != null) {
            int iteration = Integer.parseInt(valueStr);
            if (iteration > 0) {
                mSoftapIterations = iteration;
                mSoftApIterations = iteration;
            }
        }

        String scanIterationStr = (String) icicle.get("scan_iterations");
        String scanIterationStr = icicle.getString("scan_iterations");
        if (scanIterationStr != null) {
            int scanIteration = Integer.parseInt(scanIterationStr);
            if (scanIteration > 0) {
@@ -76,17 +76,17 @@ public class ConnectivityManagerStressTestRunner extends InstrumentationTestRunn
            }
        }

        String ssidStr= (String) icicle.get("reconnect_ssid");
        String ssidStr= icicle.getString("reconnect_ssid");
        if (ssidStr != null) {
            mReconnectSsid = ssidStr;
        }

        String passwordStr = (String) icicle.get("reconnect_password");
        String passwordStr = icicle.getString("reconnect_password");
        if (passwordStr != null) {
            mReconnectPassword = passwordStr;
        }

        String reconnectStr = (String) icicle.get("reconnect_iterations");
        String reconnectStr = icicle.getString("reconnect_iterations");
        if (reconnectStr != null) {
            int iteration = Integer.parseInt(reconnectStr);
            if (iteration > 0) {
@@ -94,7 +94,7 @@ public class ConnectivityManagerStressTestRunner extends InstrumentationTestRunn
            }
        }

        String sleepTimeStr = (String) icicle.get("sleep_time");
        String sleepTimeStr = icicle.getString("sleep_time");
        if (sleepTimeStr != null) {
            int sleepTime = Integer.parseInt(sleepTimeStr);
            if (sleepTime > 0) {
@@ -102,9 +102,37 @@ public class ConnectivityManagerStressTestRunner extends InstrumentationTestRunn
            }
        }

        String wifiOnlyFlag = (String) icicle.get("wifi-only");
        String wifiOnlyFlag = icicle.getString("wifi-only");
        if (wifiOnlyFlag != null) {
            mWifiOnlyFlag = true;
        }
    }

    public int getSoftApInterations() {
        return mSoftApIterations;
    }

    public int getScanIterations() {
        return mScanIterations;
    }

    public int getReconnectIterations() {
        return mReconnectIterations;
    }

    public boolean isWifiOnly() {
        return mWifiOnlyFlag;
    }

    public long getSleepTime() {
        return mSleepTime;
    }

    public String getReconnectSsid() {
        return mReconnectSsid;
    }

    public String getReconnectPassword() {
        return mReconnectPassword;
    }
}
+198 −341

File changed.

Preview size limit exceeded, changes collapsed.

+0 −203
Original line number Diff line number Diff line
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.connectivitymanagertest;

import android.net.NetworkInfo.State;
import android.util.Log;

import java.util.List;
import java.util.ArrayList;

public class NetworkState {
    public static final int TO_DISCONNECTION = 0; // transition to disconnection
    public static final int TO_CONNECTION = 1; // transition to connection
    public static final int DO_NOTHING = -1;   // no state change
    private final String LOG_TAG = "NetworkState";
    private List<State> mStateDepository;
    private State mTransitionTarget;
    private int mTransitionDirection;
    private String mReason = null;         // record mReason of state transition failure

    public NetworkState() {
        mStateDepository = new ArrayList<State>();
        mTransitionDirection = DO_NOTHING;
        mTransitionTarget = State.UNKNOWN;
    }

    public NetworkState(State currentState) {
        mStateDepository = new ArrayList<State>();
        mStateDepository.add(currentState);
        mTransitionDirection = DO_NOTHING;
        mTransitionTarget = State.UNKNOWN;
    }

    // Reinitialize the network state
    public void resetNetworkState() {
        mStateDepository.clear();
        mTransitionDirection = DO_NOTHING;
        mTransitionTarget = State.UNKNOWN;
    }

    // set the transition criteria, transitionDir could be:
    // DO_NOTHING, TO_CONNECTION, TO_DISCONNECTION
    public void setStateTransitionCriteria(State initState, int transitionDir, State targetState) {
        if (!mStateDepository.isEmpty()) {
            mStateDepository.clear();
        }
        mStateDepository.add(initState);
        mTransitionDirection = transitionDir;
        mTransitionTarget = targetState;
        Log.v(LOG_TAG, "setStateTransitionCriteria: " + printStates());
    }

    public void recordState(State currentState) {
        mStateDepository.add(currentState);
    }

    // Verify state transition
    public boolean validateStateTransition() {
        Log.v(LOG_TAG, "print state depository: " + printStates());
        if (mTransitionDirection == DO_NOTHING) {
            if (mStateDepository.isEmpty()) {
                Log.v(LOG_TAG, "no state is recorded");
                mReason = "no state is recorded.";
                return false;
            } else if (mStateDepository.size() > 1) {
                for (int i = 0; i < mStateDepository.size(); i++) {
                    if (mStateDepository.get(i) != mTransitionTarget) {
                        Log.v(LOG_TAG, "state changed.");
                        mReason = "Unexpected state change";
                        return false;
                    }
                }
            } else if (mStateDepository.get(0) != mTransitionTarget) {
                Log.v(LOG_TAG, mTransitionTarget + " is expected, but it is " +
                        mStateDepository.get(0));
                mReason = mTransitionTarget + " is expected, but it is " + mStateDepository.get(0);
                return false;
            }
            return true;
        } else if (mTransitionDirection == TO_CONNECTION) {
            Log.v(LOG_TAG, "transition to CONNECTED");
            return transitToConnection();
        } else {
            Log.v(LOG_TAG, "transition to DISCONNECTED");
            return transitToDisconnection();
        }
    }

    /*
     * Verifies state transition from CONNECTED->...-> DISCONNECTED.
     *
     * returns false if initial state or target state is not correct, or if there is
     * any transition from DISCONNECTING/DISCONNECTED -> CONNECTED.
     */
    public boolean transitToDisconnection () {
        mReason = "states: " + printStates();
        if (mStateDepository.get(0) != State.CONNECTED) {
            mReason += " initial state should be CONNECTED, but it is " +
                    mStateDepository.get(0) + ".";
            return false;
        }
        State lastState = mStateDepository.get(mStateDepository.size() - 1);
        if ( lastState != mTransitionTarget) {
            mReason += " the last state should be DISCONNECTED, but it is " + lastState;
            return false;
        }
        for (int i = 1; i < mStateDepository.size() - 1; i++) {
            State preState = mStateDepository.get(i-1);
            State curState = mStateDepository.get(i);
            if (preState == curState) {
                continue;
            } else if ((preState == State.CONNECTED) && ((curState == State.DISCONNECTING) ||
                    (curState == State.DISCONNECTED))) {
                continue;
            } else if ((preState == State.DISCONNECTING) && (curState == State.DISCONNECTED)) {
                continue;
            } else {
                mReason += " Transition state from " + preState.toString() + " to " +
                        curState.toString() + " is not valid.";
                return false;
            }
        }
        return true;
    }

    /*
     * Verifies state transition from DISCONNECTED->...-> CONNECTED.
     *
     * returns false if initial state or target state is not correct, or if there is
     * any transition from CONNECED -> DISCONNECTED.
     */
    public boolean transitToConnection() {
        mReason = "states: " + printStates();
        if (mStateDepository.get(0) != State.DISCONNECTED) {
            mReason += " initial state should be DISCONNECTED, but it is " +
                    mStateDepository.get(0) + ".";
            return false;
        }
        State lastState = mStateDepository.get(mStateDepository.size() - 1);
        if ( lastState != mTransitionTarget) {
            mReason += "The last state should be " + mTransitionTarget + ", but it is " + lastState;
            return false;
        }
        for (int i = 1; i < mStateDepository.size(); i++) {
            State preState = mStateDepository.get(i-1);
            State curState = mStateDepository.get(i);
            if (preState == curState) {
                continue;
            }
            if ((preState == State.DISCONNECTED) && ((curState == State.CONNECTING) ||
                    (curState == State.CONNECTED))) {
                continue;
             } else if ((preState == State.CONNECTING) && (curState == State.CONNECTED)) {
                 continue;
             } else {
                mReason += " Transition state from " + preState.toString() + " to " +
                        curState.toString() + " is not valid.";
                return false;
            }
        }
        return true;
    }

    public List<State> getTransitionStates() {
        return mStateDepository;
    }

    // return state failure mReason
    public String getReason() {
        return mReason;
    }

    public String printStates() {
        StringBuilder stateBuilder = new StringBuilder("");
        for (int i = 0; i < mStateDepository.size(); i++) {
            stateBuilder.append(" ").append(mStateDepository.get(i).toString()).append("->");
        }
        return stateBuilder.toString();
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder(" ");
        builder.append("mTransitionDirection: ").append(Integer.toString(mTransitionDirection)).
                append("; ").append("states:").
                append(printStates()).append("; ");
        return builder.toString();
    }
}
+1 −2
Original line number Diff line number Diff line
@@ -57,8 +57,7 @@ public class WifiAssociationTestRunner extends InstrumentationTestRunner {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        Bundle arguments = icicle;
        String mFrequencyBand = arguments.getString("frequency-band");
        String mFrequencyBand = icicle.getString("frequency-band");
        if (mFrequencyBand != null) {
            setFrequencyBand(mFrequencyBand);
        }
Loading