Home | History | Annotate | Download | only in sensoroperations
      1 /*
      2  * Copyright (C) 2013 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 android.hardware.cts.helpers.sensoroperations;
     18 
     19 import android.hardware.cts.helpers.SensorStats;
     20 import android.hardware.cts.helpers.reporting.ISensorTestNode;
     21 
     22 import java.util.ArrayList;
     23 
     24 /**
     25  * A {@link SensorOperation} that executes a set of children {@link SensorOperation}s in a
     26  * sequence. The children are executed in the order they are added. This class can be combined to
     27  * compose complex {@link SensorOperation}s.
     28  */
     29 public class SequentialSensorOperation extends SensorOperation {
     30     public static final String STATS_TAG = "sequential";
     31 
     32     private final ArrayList<SensorOperation> mOperations = new ArrayList<>();
     33 
     34     /**
     35      * Add a set of {@link SensorOperation}s.
     36      */
     37     public void add(SensorOperation ... operations) {
     38         for (SensorOperation operation : operations) {
     39             if (operation == null) {
     40                 throw new IllegalArgumentException("Arguments cannot be null");
     41             }
     42             mOperations.add(operation);
     43         }
     44     }
     45 
     46     /**
     47      * Executes the {@link SensorOperation}s in the order they were added. If an exception occurs
     48      * in one operation, it is thrown and all subsequent operations will not run.
     49      */
     50     @Override
     51     public void execute(ISensorTestNode parent) throws Exception {
     52         ISensorTestNode currentNode = asTestNode(parent);
     53         for (int i = 0; i < mOperations.size(); i++) {
     54             SensorOperation operation = mOperations.get(i);
     55             try {
     56                 operation.execute(currentNode);
     57             } catch (AssertionError e) {
     58                 String msg = String.format("Operation %d failed: \"%s\"", i, e.getMessage());
     59                 getStats().addValue(SensorStats.ERROR, msg);
     60                 throw new AssertionError(msg, e);
     61             } finally {
     62                 addSensorStats(STATS_TAG, i, operation.getStats());
     63             }
     64         }
     65     }
     66 
     67     /**
     68      * {@inheritDoc}
     69      */
     70     @Override
     71     public SequentialSensorOperation clone() {
     72         SequentialSensorOperation operation = new SequentialSensorOperation();
     73         for (SensorOperation subOperation : mOperations) {
     74             operation.add(subOperation.clone());
     75         }
     76         return operation;
     77     }
     78 }
     79