Home | History | Annotate | Download | only in fundamentals
      1 page.title=Option Handling
      2 @jd:body
      3 
      4 <!--
      5     Copyright 2013 The Android Open Source Project
      6 
      7     Licensed under the Apache License, Version 2.0 (the "License");
      8     you may not use this file except in compliance with the License.
      9     You may obtain a copy of the License at
     10 
     11         http://www.apache.org/licenses/LICENSE-2.0
     12 
     13     Unless required by applicable law or agreed to in writing, software
     14     distributed under the License is distributed on an "AS IS" BASIS,
     15     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16     See the License for the specific language governing permissions and
     17     limitations under the License.
     18 -->
     19 
     20 <p>Option handling lies at the heart of Trade Federation's modular approach.  In particular, options
     21 are the mechanism by which the Developer, Integrator, and Test Runner can work together without
     22 having to duplicate each-other's work.  Put simply, our implementation of option handling allows the
     23 Developer to mark a Java class member as being configurable, at which point the value of that member
     24 may be augmented or overridden by the Integrator, and may be subsequently augmented or overridden by
     25 the Test Runner.  This mechanism works for all Java intrinsic types, as well as for any
     26 <code>Map</code>s or <code>Collection</code>s of intrinsic types.</p>
     27 
     28 <p><em>Note:</em> the option-handling mechanism only works for classes implementing one of the
     29 interfaces included in the <a href="lifecycle.html">Test Lifecycle</a>, and only when that class is
     30 <em>instantiated</em> by the lifecycle machinery.</p>
     31 
     32 <h2>Developer</h2>
     33 <p>To start off, the developer marks a member with the
     34 <code><a href="https://android.googlesource.com/platform/tools/tradefederation/+/master/src/com/android/tradefed/config/Option.java"
     35 >@Option</a></code> annotation.  <!-- note: javadoc for the Option class is broken -->
     36 They specify (at a minimum) the <code>name</code> and <code>description</code> values, which
     37 specify the argument name associated with that Option, and the description that will be displayed on
     38 the TF console when the command is run with <code>--help</code> or <code>--help-all</code>.</p>
     39 
     40 <p>As an example, let's say we want to build a functional phone test which will dial a variety of
     41 phone numbers, and will expect to receive a sequence of DTMF tones from each number after it
     42 connects.</p>
     43 <code><pre>public class PhoneCallFuncTest extends IRemoteTest {
     44     &#64;Option(name = "timeout", description = "How long to wait for connection, in millis")
     45     private long mWaitTime = 30 * 1000;  // 30 seconds
     46 
     47     &#64;Option(name = "call", description = "Key: Phone number to attempt.  " +
     48             "Value: DTMF to expect.  May be repeated.")
     49     private Map&lt;String, String&gt; mCalls = new HashMap&lt;String, String&gt;;
     50 
     51     public PhoneCallFuncTest() {
     52         mCalls.add("123-456-7890", "01134");  // default
     53     }</pre></code>
     54 
     55 <p>That's all that's required for the Developer to set up two points of configuration for that
     56 test.  They could then go off and use <code>mWaitTime</code> and <code>mCalls</code> as normal,
     57 without paying much attention to the fact that they're configurable.  Because the
     58 <code>@Option</code> fields are set after the class is instantiated, but before the
     59 <code>run</code> method is called, that provides an easy way for implementors to set up defaults for
     60 or perform some kind of filtering on <code>Map</code> and <code>Collection</code> fields, which are
     61 otherwise append-only.</p>
     62 
     63 <h2>Integrator</h2>
     64 <p>The Integrator works in the world of Configurations, which are written in XML.  The config format
     65 allows the Integrator to set (or append) a value for any <code>@Option</code> field.  For instance,
     66 suppose the Integrator wanted to define a lower-latency test that calls the default number, as well
     67 as a long-running test that calls a variety of numbers.  They could create a pair of configurations
     68 that might look like the following:</p>
     69 
     70 <code><pre>&lt;?xml version="1.0" encoding="utf-8"?&gt;
     71 &lt;configuration description="low-latency default test; low-latency.xml"&gt;
     72     &lt;test class="com.example.PhoneCallFuncTest"&gt;
     73         &lt;option name="timeout" value="5000" /&gt;
     74     &lt;/test&gt;
     75 &lt;/configuration&gt;</pre></code>
     76 
     77 <code><pre>&lt;?xml version="1.0" encoding="utf-8"?&gt;
     78 &lt;configuration description="call a bunch of numbers; many-numbers.xml"&gt;
     79     &lt;test class="com.example.PhoneCallFuncTest"&gt;
     80         &lt;option name="call" key="111-111-1111" value="#*#*TEST1*#*#" /&gt;
     81         &lt;option name="call" key="222-222-2222" value="#*#*TEST2*#*#" /&gt;
     82         &lt;!-- ... --&gt;
     83     &lt;/test&gt;
     84 &lt;/configuration&gt;</pre></code>
     85 
     86 <h2>Test Runner</h2>
     87 <p>The Test Runner also has access to these configuration points via the Trade Federation console.
     88 First and foremost, they will run a Command (that is, a config and all of its arguments) with the
     89 <code>run command &lt;name&gt;</code> instruction (or <code>run &lt;name&gt;</code> for short).
     90 Beyond that, they can specify any list of arguments are part of the command, which may replace or
     91 append to fields specified by Lifecycle Objects within each config.</p>
     92 
     93 <p>To run the low-latency test with the <code>many-numbers</code> phone numbers, the Test Runner
     94 could execute:</p>
     95 <code><pre>tf >run low-latency.xml --call 111-111-1111 #*#*TEST1*#*# --call 222-222-2222 #*#*TEST2*#*#</pre></code>
     96 
     97 <p>Or, to get a similar effect from the opposite direction, the Test Runner could reduce the wait time
     98 for the <code>many-numbers</code> test:</p>
     99 <code><pre>tf >run many-numbers.xml --timeout 5000</code></pre>
    100 
    101