コード例 #1
0
    /// host is the form under test.
    /// lane is some sort of a timeline where changes and SE are reflected.
    public AquaCmds(Form host, Lane lane, bool quiet)
    {
        Text = "Aqua";
        // Attach
        // Owner = host;
        // Commands
        Button btnRec = null, btnRep = null, btnStop, btnNotes, btnSave;        // btnOpen
        Action record, replay, stop, save, takeNote, reset;

        takeNote = () => Write("TODO: Add notes.\n");

        reset = () => {
            btnRep.Text    = REPLAY;
            btnRec.Text    = RECORD;
            btnRep.Enabled = true;
            btnRec.Enabled = true;

            lane.IsRecording = false;
        };

        replay = () => {
            if (lane.IsRecording)
            {
                MessageBox.Show("Can't replay while recording.");
                return;
            }

            var script = OpenScript();
            btnRec.Enabled = false;
            Replay(script, host, quiet);
        };

        record = () => {
            // TODO: Warn the user when there is an unsaved session. i.e:
            //       Unsaved changes 'll be lost. Wanna save before
            //       start a new session? (or something like that).
            //
            //Each record session have to start fresh.
            // lane.Clear();
            lane.IsRecording = true;
            // =======================================
            btnRec.Text    = RECORDING;
            btnRep.Enabled = false;
        };

        stop = () => {
            reset();
        };

        save = () => {
            if (lane.MovesCount == 0)
            {
                MessageBox.Show("No moves.");
                return;
            }

            var src = lane.CreateScript();

            using (var sfd = new SaveFileDialog()) {
                sfd.InitialDirectory = GetInitDir();

                sfd.Filter = "Quality assurance test files (*.qat)|*.qat";
                if (sfd.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                Stream strm = null;
                if ((strm = sfd.OpenFile()) != null)
                {
                    using (var sw = new StreamWriter(strm)) {
                        sw.Write(src);
                        sw.Close();
                    }
                    strm.Close();
                }
            }
            reset();
        };

        btnRec   = CreateBtn(RECORD, record, null);
        btnRep   = CreateBtn("Replay", replay, btnRec);
        btnStop  = CreateBtn("Stop", stop, btnRep);
        btnNotes = CreateBtn("Notes", takeNote, btnStop);
        // btnOpen  = CreateBtn("Open",   open, btnNotes);
        btnSave = CreateBtn("Save", save, btnNotes);

        Controls.Add(btnRec);
        Controls.Add(btnRep);
        Controls.Add(btnStop);
        Controls.Add(btnNotes);
        // Controls.Add(btnOpen);
        Controls.Add(btnSave);

        // Position
        FormBorderStyle = FormBorderStyle.None;
        SetPositionOnScreen(host);
    }