chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//Copyright 2012-2019 Keysight Technologies
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
using System;
using System.ComponentModel;
 
namespace OpenTap.Plugins.PluginDevelopment.GUI
{
    public enum WaitForInputResult
    {
        // The number assigned, determines the order in which the buttons are shown in the dialog.
        Cancel = 2, Ok = 1
    }
 
    // This describes a dialog that asks the user to reset the DUT.
    class ResetDutDialog: IDisplayAnnotation
    {
        public ResetDutDialog(Dut dutObj)
        {
            Dut = dutObj;
        }
 
        private Dut Dut { get; set; }
        // Name is handled specially to create the title of the dialog window.
        public string Name { get { return "Reset " + Dut.Name; } }
 
        [Layout(LayoutMode.FullRow)] // Set the layout of the property to fill the entire row.
        [Browsable(true)] // Show it event though it is read-only.
        public string Message { get { return "Please reset the DUT. Click OK to confirm."; } }
 
        [Layout(LayoutMode.FloatBottom | LayoutMode.FullRow)] // Show the button selection at the bottom of the window.
        [Submit] // When the button is clicked the result is 'submitted', so the dialog is closed.
        public WaitForInputResult Response { get; set; }
 
        string IDisplayAnnotation.Description => string.Empty;
 
        string[] IDisplayAnnotation.Group => Array.Empty<string>();
 
        double IDisplayAnnotation.Order => -10000;
 
        bool IDisplayAnnotation.Collapsed => false;
    }
 
    // This describes a dialog that asks the user to enter the serial number.
    [Display("Please enter serial number")]
    class EnterSNDialog
    {
        // Serial number to be entered by the user.
        [Display("Serial Number")]
        public string SerialNumber { get; set; }
    }
 
    // This example shows how to use UserInput to get user input. 
    // In TAP GUI it appears as a dialog box. In TAP CLI the user is prompted.
    [Display("User Input Example", Groups: new[] { "Examples", "Plugin Development", "GUI" },
        Description: "This example shows how to use UserInput.")]
    public class UserInputExample : TestStep
    {
        [Display("Use Timeout", "Enabling this will make the dialog close after an amount of time.", Group: "Timeout", Order: 1)]
        public bool UseTimeout { get; set; }
 
        [EnabledIf("UseTimeout", true)]
        [Unit("s")]
        [Display("Timeout", "The dialog closes after this time.", Group: "Timeout", Order: 2)]
        public double Timeout { get; set; }
 
        public UserInputExample()
        {
            UseTimeout = true;
            Timeout = 5;
            Rules.Add(() => Timeout > 0, "Timeout must > 0s.", "Timeout");
        }
 
        public override void Run()
        {
            try
            {
                var timeout = UseTimeout ? TimeSpan.FromSeconds(Timeout) : TimeSpan.Zero;
 
                // Dialog/prompt where user has option to select "OK" or "Cancel".
                // The message/query is set by the Message property. Selection options are defined by WaitForInputResult.
                var dut = new SimpleDut() { Name = "My Simple DUT" };
                var dialog = new ResetDutDialog(dut);
                UserInput.Request(dialog, timeout);
 
                // Response from the user.
                if (dialog.Response == WaitForInputResult.Cancel)
                {
                    Log.Info("User clicked Cancel.");
                    return;
                }
                Log.Info("User clicked OK. Now we prompt for DUT S/N.");
 
                var snDialog = new EnterSNDialog();
                // Dialog/prompt where user has option to enter a string as DUT S/N
                UserInput.Request(snDialog, timeout);
                
                Log.Info("DUT S/N: {0}", snDialog.SerialNumber);
            }
            catch (TimeoutException)
            {
                Log.Info("User did not click. Are they sleeping?");
            }
        }
    }
 
}