Implement tests for BaseScreen

pull/231/merge
Alinson S. Xavier 8 years ago
parent 1832ea639b
commit d0e76d3d55

@ -42,17 +42,16 @@ import static android.os.Build.VERSION_CODES.*;
*/
public abstract class BaseRootView extends FrameLayout
{
@NonNull
private final Context context;
private final BaseActivity activity;
private final ThemeSwitcher themeSwitcher;
public BaseRootView(Context context)
public BaseRootView(@NonNull Context context)
{
super(context);
this.context = context;
activity = (BaseActivity) context;
BaseActivity activity = (BaseActivity) context;
themeSwitcher = activity.getComponent().getThemeSwitcher();
}

@ -61,7 +61,7 @@ public class BaseScreen
@Nullable
private BaseSelectionMenu selectionMenu;
private Snackbar snackbar;
protected Snackbar snackbar;
public BaseScreen(@NonNull BaseActivity activity)
{

@ -59,16 +59,16 @@ public abstract class BaseSelectionMenu
/**
* Called when the menu is first displayed.
* <p>
* This method cannot be overridden. The application should override the
* methods onCreate(Menu) and getMenuResourceId instead.
* This method should not be overridden. The application should override
* the methods onCreate(Menu) and getMenuResourceId instead.
*
* @param inflater a menu inflater, for creating the menu
* @param mode the action mode associated with this menu.
* @param menu the menu that is being created.
*/
public final void onCreate(@NonNull MenuInflater inflater,
@NonNull ActionMode mode,
@NonNull Menu menu)
public void onCreate(@NonNull MenuInflater inflater,
@NonNull ActionMode mode,
@NonNull Menu menu)
{
this.actionMode = mode;
inflater.inflate(getResourceId(), menu);

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*

@ -0,0 +1,140 @@
/*
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.activities;
import android.content.*;
import android.support.annotation.*;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.*;
import android.view.*;
import org.isoron.uhabits.*;
import org.junit.*;
import org.junit.runner.*;
import org.robolectric.*;
import org.robolectric.annotation.*;
import java.util.*;
import static android.view.View.*;
import static junit.framework.Assert.assertNotNull;
import static org.hamcrest.core.IsEqual.*;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
import static org.robolectric.Robolectric.*;
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
public class BaseScreenTest
{
@Test
public void selectionMenuTest()
{
BaseSelectionMenu selectionMenu = spy(new BaseSelectionMenu()
{
@Override
protected int getResourceId()
{
return R.menu.list_habits_selection;
}
});
ActionModeActivity activity = setupActivity(ActionModeActivity.class);
BaseScreen screen = new BaseScreen(activity);
screen.setSelectionMenu(selectionMenu);
activity.setScreen(screen);
screen.startSelection();
assertNotNull(activity.callback);
verify(selectionMenu).onCreate(any(), any(), any());
verify(selectionMenu).onPrepare(any());
ActionMode mode = mock(ActionMode.class);
MenuItem item = mock(MenuItem.class);
activity.callback.onActionItemClicked(mode, item);
verify(selectionMenu).onItemClicked(item);
activity.callback.onDestroyActionMode(mode);
verify(selectionMenu).onFinish();
}
@Test
public void showMessageTest()
{
EmptyActivity activity = setupActivity(EmptyActivity.class);
ConcreteRootView rootView = new ConcreteRootView(activity);
View decor = activity.getWindow().getDecorView();
BaseScreen screen = new BaseScreen(activity);
screen.setRootView(rootView);
activity.setScreen(screen);
ArrayList<View> matches = new ArrayList<>();
screen.showMessage(R.string.checkmark);
decor.findViewsWithText(matches, "Checkmark", FIND_VIEWS_WITH_TEXT);
assertThat(matches.size(), equalTo(1));
assertTrue(matches.get(0).isShown());
screen.showMessage(R.string.frequency);
decor.findViewsWithText(matches, "Frequency", FIND_VIEWS_WITH_TEXT);
assertThat(matches.size(), equalTo(1));
assertTrue(matches.get(0).isShown());
}
static class ActionModeActivity extends BaseActivity
{
private ActionMode.Callback callback;
@Nullable
@Override
public ActionMode startSupportActionMode(
@NonNull ActionMode.Callback callback)
{
this.callback = callback;
return super.startSupportActionMode(this.callback);
}
}
static class ConcreteRootView extends BaseRootView
{
private final Toolbar toolbar;
public ConcreteRootView(@NonNull Context context)
{
super(context);
toolbar = new Toolbar(context);
addView(toolbar);
}
@NonNull
@Override
public Toolbar getToolbar()
{
return toolbar;
}
}
static class EmptyActivity extends BaseActivity
{
}
}
Loading…
Cancel
Save