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.SensorTestPlatformException; 21 import android.hardware.cts.helpers.reporting.ISensorTestNode; 22 23 /** 24 * A {@link SensorOperation} that executes a single {@link SensorOperation} a given number of 25 * times. This class can be combined to compose complex {@link SensorOperation}s. 26 */ 27 public class RepeatingSensorOperation extends SensorOperation { 28 public static final String STATS_TAG = "repeating"; 29 30 private final SensorOperation mOperation; 31 private final int mIterations; 32 33 /** 34 * Constructor for {@link RepeatingSensorOperation}. 35 * 36 * @param operation the {@link SensorOperation} to run. 37 * @param iterations the number of iterations to run the operation for. 38 */ RepeatingSensorOperation(SensorOperation operation, int iterations)39 public RepeatingSensorOperation(SensorOperation operation, int iterations) { 40 if (operation == null) { 41 throw new IllegalArgumentException("Arguments cannot be null"); 42 } 43 mOperation = operation; 44 mIterations = iterations; 45 } 46 47 /** 48 * Executes the {@link SensorOperation}s the given number of times. If an exception occurs in 49 * one iterations, it is thrown and all subsequent iterations will not run. 50 */ 51 @Override execute(ISensorTestNode parent)52 public void execute(ISensorTestNode parent) throws InterruptedException { 53 ISensorTestNode currentNode = asTestNode(parent); 54 for(int i = 0; i < mIterations; ++i) { 55 SensorOperation operation = mOperation.clone(); 56 try { 57 operation.execute(new TestNode(currentNode, i)); 58 } catch (AssertionError e) { 59 String msg = String.format("Iteration %d failed: \"%s\"", i, e.getMessage()); 60 getStats().addValue(SensorStats.ERROR, msg); 61 throw new AssertionError(msg, e); 62 } finally { 63 addSensorStats(STATS_TAG, i, operation.getStats()); 64 } 65 } 66 } 67 68 /** 69 * {@inheritDoc} 70 */ 71 @Override clone()72 public RepeatingSensorOperation clone() { 73 return new RepeatingSensorOperation(mOperation.clone(), mIterations); 74 } 75 76 private class TestNode implements ISensorTestNode { 77 private final ISensorTestNode mTestNode; 78 private final int mIteration; 79 TestNode(ISensorTestNode parent, int iteration)80 public TestNode(ISensorTestNode parent, int iteration) { 81 mTestNode = asTestNode(parent); 82 mIteration = iteration; 83 } 84 85 @Override getName()86 public String getName() throws SensorTestPlatformException { 87 return String.format("%s-iteration%d", mTestNode.getName(), mIteration); 88 } 89 } 90 } 91