package me.alb_i986.selenium.tinafw.domain;

import static org.junit.Assert.*;
import me.alb_i986.selenium.tinafw.pages.Page;
import me.alb_i986.selenium.tinafw.tasks.BaseWebTask;
import me.alb_i986.selenium.tinafw.tasks.WebTask;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class UserTest {

	private User user;

	@Before
	public void before() {
		this.user = new User();
	}
	

	/**
	 * Testing post conditions of {@link User#doTask(WebTask)}:
	 * <ol>
	 * <li>Given a User with no initial page</li>
	 * <li>When user does a task that lands to some not null page</li>
	 * <li>Then {@link User#getCurrentPage()} should return not null</li>
	 * </ol>
	 */
	@Test
	public void doTask_landingToNotNullPage() {
		// open a browser by injecting a WebDriver in the Browser instance
		user.getBrowser().setDriver(new HtmlUnitDriver());
		assertNull(user.getCurrentPage());
		WebTask taskThatReturnsANotNullPage = new BaseWebTask() {
			
			@Override
			public Page run(Page initialPage) {
				return new Page() {
					
					@Override
					public String getTitle() {
						return "stub";
					}
					
					@Override
					public String getCurrentUrl() {
						return "stub";
					}
				};
			}
		};
		user.doTask(taskThatReturnsANotNullPage);
		assertNotNull(user.getCurrentPage());
	}

	/**
	 * Testing post conditions of {@link User#doTask(WebTask)}:
	 * <ol>
	 * <li>Given a User with no initial page</li>
	 * <li>When user does a task that lands to some null page</li>
	 * <li>Then {@link User#getCurrentPage()} should return null</li>
	 * </ol>
	 */
	@Test
	public void doTask_landingToNullPage() {
		// open a browser by injecting a WebDriver in the Browser instance
		user.getBrowser().setDriver(new HtmlUnitDriver());
		WebTask taskThatReturnsANullPage = new BaseWebTask() {
			
			@Override
			public Page run(Page initialPage) {
				return null;
			}
		};
		user.doTask(taskThatReturnsANullPage);
		assertNull(user.getCurrentPage());
	}
	
	/**
	 * Testing {@link User#User()}:
	 * <ol>
	 * <li>Given a new user with no initial page</li>
	 * <li>Then {@link User#getCurrentPage()} should return null</li>
	 * </ol>
	 */
	@Test
	public void givenNewUserThenCurrentPageShouldBeNull() {
		assertNull(user.getCurrentPage());
	}


	@After
	public void after() {
	}
	
}

@@ -2306,16 +2306,823 @@
 ));%0A%09%7D%0A%0A
+%09@Test%0A%09public void equalsToNonUser() %7B%0A%09%09Object o = new Object();%0A%09%09assertFalse(user.equals(o));%0A%09%7D%0A%0A%09@Test%0A%09public void equalsToUserWithSameUsername() %7B%0A%09%09User userWithSameUsername = new User().withUsername(user.getUsername());%0A%09%09assertTrue(user.equals(userWithSameUsername));%0A%09%7D%0A%0A%09@Test%0A%09public void equalsToUserWithDifferentUsername() %7B%0A%09%09User userWithNullUsername = new User().withUsername(%22asdasd%22);%0A%09%09// make sure the two usernames are different%0A%09%09assertFalse(user.getUsername().equals(userWithNullUsername.getUsername()));%0A%09%09assertFalse(user.equals(userWithNullUsername));%0A%09%7D%0A%0A%09@Test%0A%09public void equalsToNullObject() %7B%0A%09%09Object nullObject = null;%0A%09%09assertFalse(user.equals(nullObject));%0A%09%7D%0A%0A%09@Test%0A%09public void equalsToNullUser() %7B%0A%09%09User nullUser = null;%0A%09%09assertFalse(user.equals(nullUser));%0A%09%7D%0A%0A
 %0A%09@After

add unit tests for User#equals