/*
** Module   : LogScreenBuffer.java
** Abstract : directive to query the current screen and store it in the report
**
** Copyright (c) 2009-2021, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 GES 20090126 Directive to query the current screen and store it in the report.
** 002 CA  20210827 Refactored to allow other test types, beside terminal (which can be executed from the same 
**                  test suite).  Added SOAP and REST web service tests.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.goldencode.harness.test;

import com.goldencode.harness.*;
import com.goldencode.harness.terminal.*;

/**
 * Directive to query the current screen and store it in the report.
 */
public class LogScreenBuffer
extends Report
implements TerminalTestable
{
   /** Short name for the activity. */
   private static final String NAME = "LOG-SCREEN-BUFFER";
   
   /** Flag to clear the screen buffer after reading. */
   private final boolean clear;
   
   /** Number of milliseconds to pause. */
   private final int millis;
   
   /**
    * Construct an instance.
    *
    * @param    clear
    *           <code>true</code> if the screen buffer must be cleared after
    *           reading the contents.
    * @param    millis
    *           Number of milliseconds to wait for a screen buffer to become
    *           available. Use -1 for an indefinite wait and 0 for no wait.
    */
   public LogScreenBuffer(boolean clear, int millis)
   {
      super(NAME, "");
      this.clear  = clear;
      this.millis = millis;
   }
   
   /**
    * Execute the directive of the test using the given terminal as the data
    * sink and/or source for the operation.
    *
    * @param    term
    *           The terminal that represents the system under test.
    */
   @Override
   public void execute(Terminal term)
   {
      setDescription(term.getScreen(clear, millis));
      setStatus(Status.PASSED, "");
   }
   
   /**
    * Output the given terminal screen to the console.
    *
    * @param    buffer
    *           The terminal screen to display.
    */
   private void setDescription(char[][] buffer)
   {
      if (buffer == null)
      {
         setDescription("No screen buffer available.");
      }
      else
      {
         setDescription(Terminal.screenToString(buffer));
      }
   }
}
