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