package me.alb_i986.selenium.tinafw.tasks.runnable;

import java.util.List;

import me.alb_i986.selenium.tinafw.config.Config;
import me.alb_i986.selenium.tinafw.domain.SupportedBrowser;
import me.alb_i986.selenium.tinafw.domain.WebUser;
import me.alb_i986.selenium.tinafw.tasks.CompositeWebTask;
import me.alb_i986.selenium.tinafw.tasks.WebTask;

/**
 * A fully executable <i>single</i> user interaction with the SUT.
 * <p>
 * The only thing required is to inject a {@link WebUser} <i>with a {@link SupportedBrowser}</i>.
 * E.g.:
 * <pre>
 * {@code
 * WebUser user = new WebUser().withBrowserType(SupportedBrowser.CHROME);
 * SingleUserInteraction sui = .. ;
 * sui.setUser(user);
 * new Thread(sui).start();
 * }
 * </pre>
 */
public class SingleUserInteraction extends CompositeWebTask implements RunnableTask {

	public SingleUserInteraction(WebTask... subtasks) {
		super(subtasks);
	}

	public SingleUserInteraction(List<WebTask> subtasks) {
		super(subtasks);
	}

	/**
	 * Open the browser, execute this task, and finally close the browser.
	 */
	@Override
	public void run() {
		if(user == null)
			throw new IllegalStateException("user is null");
		user.openBrowser();
		try {
			user.doTask(this);
		} finally {
			if(!Config.getKeepBrowsersOpen()) {
				user.closeBrowser();
			}
		}
	}

}

@@ -1177,17 +1177,8 @@
 ();%0A
-%09%09try %7B%0A%09
 %09%09us
@@ -1198,95 +1198,8 @@
 s);%0A
-%09%09%7D finally %7B%0A%09%09%09if(!Config.getKeepBrowsersOpen()) %7B%0A%09%09%09%09user.closeBrowser();%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%7D%0A%0A

fix bug: HtmlReporter broken: the browser was closed before BrowserManager did