目录

The following describes how to perform an app test, using headset status awareness as an example.

Testing the Headset Capture API

Run the test code to check whether a headset is plugged in or not. The test code creates a headset capture task and checks whether the task is successful. If the task is successful, the headset status is returned. The return value of getStatus is the headset status. The options are as follows: 1: plugged in; 0: unplugged. If the test fails, the returned value is –1, indicating that the headset status is unknown. Troubleshoot and try again.

1. Run the test code.

  1. Awareness.getCaptureClient(this)
  2.    .getHeadsetStatus()
  3.    .addOnSuccessListener(headsetStatusResponse -> {
  4.        HeadsetStatus headsetStatus = headsetStatusResponse.getHeadsetStatus();
  5.        Log.i(TAG, "headsetStatus.getStatus()>>" + headsetStatus.getStatus());
  6.    });

2. Verify the test result.

    a. Run the test code when no headset is plugged in. If the log is headsetStatus.getStatus()>>0, the test result is success.

    b. Run the test code when a headset is plugged in. If the log is headsetStatus.getStatus()>>1, the test result is success.

    c. If these two test results are success, the function is successfully called. If either test result is not success, troubleshoot and try again.

Testing the Headset Barrier API

The headset status barrier supports three use cases: (1) determines whether the headset state changes from the current time; (2) checks whether the headset is plugged in; (3) checks whether the headset is unplugged. The first use case is used as an example in the test.

1. Run the test code.

  1. final String BARRIER_RECEIVER_ACTION = getApplication().getPackageName() + "HEADSET_BARRIER_RECEIVER_ACTION";
  2. Intent intent = new Intent(BARRIER_RECEIVER_ACTION);
  3. PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  4. // The value 1 is the private request code of the sender.
  5. AwarenessBarrier headsetBarrier = HeadsetBarrier.keeping(HeadsetStatus.CONNECTED);
  6. Awareness.getBarrierClient(this).updateBarriers(new BarrierUpdateRequest.Builder()
  7.    .addBarrier ("HEADSET_BARRIER_KEY", headsetBarrier, mPendingIntent).build())
  8.    .addOnSuccessListener(aVoid -> {
  9.        Log.i(TAG, "Barrier is started successfully!");
  10.    })
  11.    .addOnFailureListener(e -> {
  12.        Log.i(TAG, "Barrier fails to be started!!\n"+ e.getLocalizedMessage());     });

2. Verify the test result.

If the log is Barrier is started successfully!, the barrier has been successfully registered.