package io.fotoapparat;

import android.app.Activity;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import io.fotoapparat.hardware.provider.CameraProvider;
import io.fotoapparat.log.Logger;
import io.fotoapparat.parameter.LensPosition;
import io.fotoapparat.parameter.Size;
import io.fotoapparat.parameter.selector.SelectorFunction;
import io.fotoapparat.view.CameraRenderer;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;

@RunWith(MockitoJUnitRunner.class)
public class FotoapparatBuilderTest {

	@Mock
	Activity activity;
	@Mock
	CameraProvider cameraProvider;
	@Mock
	CameraRenderer cameraRenderer;
	@Mock
	SelectorFunction<Size> photoSizeSelector;
	@Mock
	SelectorFunction<LensPosition> lensPositionSelector;
	@Mock
	Logger logger;

	@Test
	public void onlyMandatoryParameters() throws Exception {
		// Given
		FotoapparatBuilder builder = builderWithMandatoryArguments();

		// When
		Fotoapparat result = builder.build();

		// Then
		assertNotNull(result);
	}

	@Test
	public void cameraProvider_HasDefault() throws Exception {
		// When
		FotoapparatBuilder builder = builderWithMandatoryArguments();

		// Then
		assertNotNull(builder.cameraProvider);
	}

	@Test
	public void cameraProvider_IsConfigurable() throws Exception {
		// When
		FotoapparatBuilder builder = builderWithMandatoryArguments()
				.cameraProvider(cameraProvider);

		// Then
		assertEquals(
				cameraProvider,
				builder.cameraProvider
		);
	}

	@Test
	public void logger_HasDefault() throws Exception {
		// When
		FotoapparatBuilder builder = builderWithMandatoryArguments();

		// Then
		assertNotNull(builder.logger);
	}

	@Test
	public void logger_IsConfigurable() throws Exception {
		// When
		FotoapparatBuilder builder = builderWithMandatoryArguments()
				.logger(logger);

		// Then
		assertEquals(
				logger,
				builder.logger
		);
	}

	@Test(expected = IllegalStateException.class)
	public void rendererIsMandatory() throws Exception {
		// Given
		FotoapparatBuilder builder = new FotoapparatBuilder(activity)
				.photoSize(photoSizeSelector)
				.lensPosition(lensPositionSelector);

		// When
		builder.build();

		// Then
		// Expect exception
	}

	@Test(expected = IllegalStateException.class)
	public void lensPositionIsMandatory() throws Exception {
		// Given
		FotoapparatBuilder builder = new FotoapparatBuilder(activity)
				.photoSize(photoSizeSelector)
				.into(cameraRenderer);

		// When
		builder.build();

		// Then
		// Expect exception
	}

	private FotoapparatBuilder builderWithMandatoryArguments() {
		return new FotoapparatBuilder(activity)
				.lensPosition(lensPositionSelector)
				.into(cameraRenderer);
	}

}
@@ -37,22 +37,85 @@
 oid.
-app.Activity;%0A
+content.Context;%0Aimport android.view.WindowManager;%0A%0Aimport org.junit.Before;
 %0Aimp
@@ -192,16 +192,44 @@
 o.Mock;%0A
+import org.mockito.Mockito;%0A
 import o
@@ -645,16 +645,60 @@
 NotNull;
+%0Aimport static org.mockito.BDDMockito.given;
 %0A%0A@RunWi
@@ -777,25 +777,23 @@
 ck%0A%09
-Activity activity
+Context context
 ;%0A%09@
@@ -1003,16 +1003,179 @@
 ogger;%0A%0A
+%09@Before%0A%09public void setUp() throws Exception %7B%0A%09%09given(context.getSystemService(Context.WINDOW_SERVICE))%0A%09%09%09%09.willReturn(Mockito.mock(WindowManager.class));%0A%09%7D%0A%0A
 %09@Test%0A%09
@@ -2425,32 +2425,31 @@
 aratBuilder(
-activity
+context
 )%0A%09%09%09%09.photo
@@ -2746,32 +2746,31 @@
 aratBuilder(
-activity
+context
 )%0A%09%09%09%09.photo
@@ -2985,16 +2985,15 @@
 der(
-activity
+context
 )%0A%09%09
@@ -3060,10 +3060,9 @@
 er);%0A%09%7D%0A
-%0A
 %7D

Fix builder test