Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-muszel committed Dec 2, 2019
1 parent c522e9d commit 8b606a2
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package ar.com.wolox.wolmo.core.fragment

import android.os.Bundle
import android.view.View
import androidx.annotation.LayoutRes

/**
Expand Down Expand Up @@ -53,6 +54,12 @@ interface IWolmoFragment {
/** Populates the view elements of the fragment. */
fun populate()

/**
* Associates variables to views inflated from the XML resource
* provided in [IWolmoFragment.layout]
*/
fun setUi(view: View?)

/**
* Sets the listeners for the views of the fragment
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,9 @@ import javax.inject.Inject

/**
* Base implementation for [IWolmoFragment] for dialog fragments. This is in charge of
* inflating the view returned by [.layout].
* The presenter is created on [.onCreate] if [.handleArguments] returns
* true. This class defines default implementations for most of the methods on [ ].
*
* @param <T> Presenter for this fragment. It should extend [BasePresenter]
</T> */
* inflating the view returned by [layout].
* The presenter is created on [onCreate] if [handleArguments] returns true.
*/
abstract class WolmoDialogFragment<V : Any, P : BasePresenter<V>> : DaggerAppCompatDialogFragment(),
IWolmoFragment {

Expand Down Expand Up @@ -77,23 +74,20 @@ abstract class WolmoDialogFragment<V : Any, P : BasePresenter<V>> : DaggerAppCom
* Returns a [Drawable] for use as background in the window.
* If you want to disable the background drawable return null.
* By default this method returns the color #01FFFFFF
*
* @return Background drawable
*/
protected val backgroundDrawable: Drawable
open val backgroundDrawable: Drawable
get() = ColorDrawable(Color.argb(1, 255, 255, 255))

/**
* Sets a custom [android.content.DialogInterface.OnKeyListener] for the
* [Dialog] returned by [.getDialog] that calls [.onBackPressed]
* if the key pressed is the back key.
*
*
* Beware that, when clicking a key, the [android.content.DialogInterface.OnKeyListener]
* is called before delegating the event to other structures. For example, the back is handled
* here before sending it to an [Activity].
*/
private fun setOnBackPressedListener() {
open fun setOnBackPressedListener() {
dialog?.setOnKeyListener { _: DialogInterface?, keyCode: Int, _: KeyEvent? ->
keyCode == KeyEvent.KEYCODE_BACK && onBackPressed()
}
Expand All @@ -114,7 +108,7 @@ abstract class WolmoDialogFragment<V : Any, P : BasePresenter<V>> : DaggerAppCom
@CallSuper
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
fragmentHandler.onViewCreated()
fragmentHandler.onViewCreated(view)
}

@CallSuper
Expand Down Expand Up @@ -149,9 +143,7 @@ abstract class WolmoDialogFragment<V : Any, P : BasePresenter<V>> : DaggerAppCom
* @return true if arguments were read successfully, false otherwise.
* Default implementation returns true.
*/
override fun handleArguments(arguments: Bundle?): Boolean {
return true
}
override fun handleArguments(arguments: Bundle?) = true

/**
* Associates variables to views inflated from the XML resource
Expand Down Expand Up @@ -200,6 +192,10 @@ abstract class WolmoDialogFragment<V : Any, P : BasePresenter<V>> : DaggerAppCom
return false
}

/**
* Tries to return a non null instance of the presenter [P] for this fragment.
* If the presenter is null this will throw a NullPointerException.
*/
fun requirePresenter() = fragmentHandler.requirePresenter()

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ abstract class WolmoFragment<V : Any, P : BasePresenter<V>> : DaggerFragment(),
@CallSuper
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
fragmentHandler.onViewCreated()
fragmentHandler.onViewCreated(view)
}

@CallSuper
Expand Down Expand Up @@ -102,9 +102,14 @@ abstract class WolmoFragment<V : Any, P : BasePresenter<V>> : DaggerFragment(),
*
* Default implementation returns true.
*/
override fun handleArguments(arguments: Bundle?): Boolean {
return true
}
override fun handleArguments(arguments: Bundle?) = true

/**
* Associates variables to views inflated from the XML resource
* provided in [IWolmoFragment.layout]
* Override if needed.
*/
override fun setUi(view: View?) {}

/**
* Sets the listeners for the views of the fragment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ class WolmoFragmentHandler<V : Any, P : BasePresenter<V>> @Inject @JvmOverloads
* Method called from [WolmoFragment.onViewCreated]. It attaches the
* fragment to the [BasePresenter] calling [BasePresenter.onViewAttached].
*/
fun onViewCreated() {
fun onViewCreated(view: View) {
created = true
presenter?.attachView(wolmoView)
with(wolmoFragment) {
setUi(view)
init()
populate()
setListeners()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import org.junit.Before;
import org.junit.Test;

import ar.com.wolox.wolmo.core.fragment.WolmoDialogFragment;
import ar.com.wolox.wolmo.core.fragment.WolmoFragmentHandler;
import ar.com.wolox.wolmo.core.permission.PermissionManager;
import ar.com.wolox.wolmo.core.presenter.BasePresenter;

Expand Down Expand Up @@ -76,7 +74,7 @@ public void onViewCreatedShouldDelegateCall() {
View viewMock = mock(View.class);

mWolmoDialogFragmentSpy.onViewCreated(viewMock, null);
verify(mWolmoFragmentHandlerMock, times(1)).onViewCreated();
verify(mWolmoFragmentHandlerMock, times(1)).onViewCreated(viewMock);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
import org.mockito.InOrder;

import ar.com.wolox.wolmo.core.R;
import ar.com.wolox.wolmo.core.fragment.IWolmoFragment;
import ar.com.wolox.wolmo.core.fragment.WolmoFragment;
import ar.com.wolox.wolmo.core.fragment.WolmoFragmentHandler;
import ar.com.wolox.wolmo.core.presenter.BasePresenter;
import ar.com.wolox.wolmo.core.util.Logger;
import ar.com.wolox.wolmo.core.util.ToastFactory;
Expand Down Expand Up @@ -102,7 +99,7 @@ public void onViewCreatedShouldCallAttachView() {
when(mWolmoFragmentMock.handleArguments(isNull())).thenReturn(true);
mWolmoFragmentHandler.onCreate(testFragment);

mWolmoFragmentHandler.onViewCreated();
mWolmoFragmentHandler.onViewCreated(mock(View.class));
verify(testPresenterSpy, times(1)).attachView(eq(testFragment));
}

Expand All @@ -112,7 +109,7 @@ public void onViewCreatedCallsWolmoFragmentMethods() {
when(mWolmoFragmentMock.handleArguments(nullable(Bundle.class))).thenReturn(true);

mWolmoFragmentHandler.onCreate(mWolmoFragmentMock);
mWolmoFragmentHandler.onViewCreated();
mWolmoFragmentHandler.onViewCreated(mockView);

// Verify that the methods in wolmoFragment are called in order
InOrder inOrder = inOrder(mWolmoFragmentMock);
Expand All @@ -131,7 +128,7 @@ public void notifyFragmentOnVisibilityChanged() {
when(mWolmoFragmentMock.handleArguments(nullable(Bundle.class))).thenReturn(true);

mWolmoFragmentHandler.onCreate(mWolmoFragmentMock);
mWolmoFragmentHandler.onViewCreated();
mWolmoFragmentHandler.onViewCreated(mock(View.class));
mWolmoFragmentHandler.onResume();
verify(mWolmoFragmentMock, times(1)).onVisible();
verify(mWolmoFragmentMock, times(0)).onHide();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import org.junit.Before;
import org.junit.Test;

import ar.com.wolox.wolmo.core.fragment.WolmoFragment;
import ar.com.wolox.wolmo.core.fragment.WolmoFragmentHandler;
import ar.com.wolox.wolmo.core.permission.PermissionManager;
import ar.com.wolox.wolmo.core.presenter.BasePresenter;

Expand Down Expand Up @@ -76,7 +74,7 @@ public void onViewCreatedShouldDelegateCall() {
View viewMock = mock(View.class);

mWolmoFragmentSpy.onViewCreated(viewMock, null);
verify(mWolmoFragmentHandlerMock, times(1)).onViewCreated();
verify(mWolmoFragmentHandlerMock, times(1)).onViewCreated(viewMock);
}

@Test
Expand Down

0 comments on commit 8b606a2

Please sign in to comment.