1page.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
21are the mechanism by which the Developer, Integrator, and Test Runner can work together without
22having to duplicate each-other's work.  Put simply, our implementation of option handling allows the
23Developer to mark a Java class member as being configurable, at which point the value of that member
24may be augmented or overridden by the Integrator, and may be subsequently augmented or overridden by
25the 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
29interfaces 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 -->
36They specify (at a minimum) the <code>name</code> and <code>description</code> values, which
37specify the argument name associated with that Option, and the description that will be displayed on
38the 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
41phone numbers, and will expect to receive a sequence of DTMF tones from each number after it
42connects.</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
56test.  They could then go off and use <code>mWaitTime</code> and <code>mCalls</code> as normal,
57without 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
60or perform some kind of filtering on <code>Map</code> and <code>Collection</code> fields, which are
61otherwise 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
65allows the Integrator to set (or append) a value for any <code>@Option</code> field.  For instance,
66suppose the Integrator wanted to define a lower-latency test that calls the default number, as well
67as a long-running test that calls a variety of numbers.  They could create a pair of configurations
68that 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.
88First 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).
90Beyond that, they can specify any list of arguments are part of the command, which may replace or
91append 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
94could 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
98for the <code>many-numbers</code> test:</p>
99<code><pre>tf >run many-numbers.xml --timeout 5000</code></pre>
100
101