<commit_before>package org.jboss.reddeer.eclipse.ui.browser;

import org.jboss.reddeer.swt.api.Browser;
import org.jboss.reddeer.swt.condition.WaitCondition;
import org.jboss.reddeer.swt.impl.browser.InternalBrowser;
import org.jboss.reddeer.swt.wait.TimePeriod;
import org.jboss.reddeer.swt.wait.WaitUntil;
import org.jboss.reddeer.swt.wait.WaitWhile;
import org.jboss.reddeer.workbench.exception.ViewNotFoundException;
import org.jboss.reddeer.workbench.view.impl.WorkbenchView;

/**
 * Represents Internal Browser view in Eclipse
 * 
 * @author psuchy
 * 
 */
public class BrowserView extends WorkbenchView {

	private Browser browser;
	private static final TimePeriod TIMEOUT = TimePeriod.LONG;

	/**
	 * Constructor of BrowserView Class
	 * Check if Internal Web Browser view exists
	 * 
	 * @throws ViewNotFoundException if the view does not exist
	 * @see WorkbenchView
	 */
	public BrowserView() {
		super("Internal Web Browser");
	}

	/**
	 * Opens Internal Web Browser view
	 */
	@Override
	public void open() {
		super.open();
		browser = new InternalBrowser();
	};

	/**
	 * Opens page with given URL in browser
	 * 
	 * @param url
	 */
	public void openPageURL(String url) {
		browser.setURL(url);
		new WaitUntil(new PageIsLoaded(browser), TIMEOUT);
	}

	/**
	 * Refreshes currently opened page in browser
	 */
	public void refreshPage() {
		browser.setURL(browser.getURL());
		new WaitUntil(new PageIsLoaded(browser), TIMEOUT);
	}

	/**
	 * Go to the previous page in browser
	 */
	public void back() {
		String prevUrl = browser.getURL();
		browser.back();
		new WaitWhile(new BrowserHasURL(browser, prevUrl), TIMEOUT);
		new WaitUntil(new PageIsLoaded(browser), TIMEOUT);
	}

	/**
	 * Go to the next page in browser
	 */
	public void forward() {
		String prevUrl = browser.getURL();
		browser.forward();
		new WaitWhile(new BrowserHasURL(browser, prevUrl), TIMEOUT);
		new WaitUntil(new PageIsLoaded(browser), TIMEOUT);
	}

	/**
	 * Gets URL of the currently opened page
	 * 
	 * @return String URL of the current page
	 */
	public String getPageURL() {
		return browser.getURL();
	}
	
	/**
	 * Gets Text of the currently opened page
	 * 
	 * @return String Text of the current page
	 */
	public String getText() {
		return browser.getText();
	}

	/**
	 * WaitCondition to test whether page is already loaded into browser
	 */
	private class PageIsLoaded implements WaitCondition {

		private Browser browser;<commit_msg>Provides ability to use existing BrowserView without opening the new one<commit_after>package org.jboss.reddeer.eclipse.ui.browser;

import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.jboss.reddeer.swt.api.Browser;
import org.jboss.reddeer.swt.condition.WaitCondition;
import org.jboss.reddeer.swt.impl.browser.InternalBrowser;
import org.jboss.reddeer.swt.wait.TimePeriod;
import org.jboss.reddeer.swt.wait.WaitUntil;
import org.jboss.reddeer.swt.wait.WaitWhile;
import org.jboss.reddeer.workbench.exception.ViewNotFoundException;
import org.jboss.reddeer.workbench.view.impl.WorkbenchView;

/**
 * Represents Internal Browser view in Eclipse
 * 
 * @author psuchy
 * 
 */
public class BrowserView extends WorkbenchView {

	private Browser browser;
	private static final TimePeriod TIMEOUT = TimePeriod.LONG;

	/**
	 * Constructor of BrowserView Class
	 * Check if Internal Web Browser view exists
	 * 
	 * @throws ViewNotFoundException if the view does not exist
	 * @see WorkbenchView
	 */
	public BrowserView() {
		super("Internal Web Browser");
	}

	/**
	 * Opens Internal Web Browser view
	 */
	@Override
	public void open() {
		if(!isOpen()){
			super.open();
		}
		browser = new InternalBrowser();
	};

	/**
	 * Opens page with given URL in browser
	 * 
	 * @param url
	 */
	public void openPageURL(String url) {
		browser.setURL(url);
		new WaitUntil(new PageIsLoaded(browser), TIMEOUT);
	}

	/**
	 * Refreshes currently opened page in browser
	 */
	public void refreshPage() {
		browser.setURL(browser.getURL());
		new WaitUntil(new PageIsLoaded(browser), TIMEOUT);
	}

	/**
	 * Go to the previous page in browser
	 */
	public void back() {
		String prevUrl = browser.getURL();
		browser.back();
		new WaitWhile(new BrowserHasURL(browser, prevUrl), TIMEOUT);
		new WaitUntil(new PageIsLoaded(browser), TIMEOUT);
	}

	/**
	 * Go to the next page in browser
	 */
	public void forward() {
		String prevUrl = browser.getURL();
		browser.forward();
		new WaitWhile(new BrowserHasURL(browser, prevUrl), TIMEOUT);
		new WaitUntil(new PageIsLoaded(browser), TIMEOUT);
	}

	/**
	 * Gets URL of the currently opened page
	 * 
	 * @return String URL of the current page
	 */
	public String getPageURL() {
		return browser.getURL();
	}
	
	/**
	 * Gets Text of the currently opened page
	 * 
	 * @return String Text of the current page
	 */
	public String getText() {
		return browser.getText();
	}
	
	/**
	 * Checks if browser is already open
	 */
	public boolean isOpen() {
		try{
			new InternalBrowser(); 
			return true;  // browser is already opened
		}catch(WidgetNotFoundException ex){
			return false;
		}
	};

	/**
	 * WaitCondition to test whether page is already loaded into browser
	 */
	private class PageIsLoaded implements WaitCondition {

		private Browser browser;
{'fraction_non_alphanumeric': 0.10895238095238095, 'fraction_numerical': 0.0, 'mean_word_length': 3.830726770929163, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2280822', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1575, 'n_words': 460}