mirror of https://github.com/iSoron/uhabits.git
parent
3e558be4d4
commit
94c70485b7
@ -1,199 +0,0 @@
|
||||
/*
|
||||
* 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.androidbase.activities;
|
||||
|
||||
import android.content.*;
|
||||
import android.os.*;
|
||||
import android.support.v4.app.*;
|
||||
import android.support.v7.app.*;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.view.*;
|
||||
import android.widget.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.activities.common.dialogs.*;
|
||||
import org.junit.*;
|
||||
import org.junit.runner.*;
|
||||
import org.robolectric.*;
|
||||
import org.robolectric.annotation.*;
|
||||
import org.robolectric.shadows.*;
|
||||
|
||||
import static org.hamcrest.core.IsEqual.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.robolectric.Robolectric.*;
|
||||
import static org.robolectric.Shadows.*;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(sdk = BuildConfig.roboSdk, constants = BuildConfig.class)
|
||||
public class BaseActivityTest
|
||||
{
|
||||
private static boolean hasCrashed = false;
|
||||
|
||||
@Test
|
||||
public void activityResultTest()
|
||||
{
|
||||
ScreenActivity activity = spy(setupActivity(ScreenActivity.class));
|
||||
activity.onActivityResult(0, 0, null);
|
||||
verify(activity.screen).onResult(0, 0, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void componentTest()
|
||||
{
|
||||
EmptyActivity activity = setupActivity(EmptyActivity.class);
|
||||
ActivityComponent component = activity.getComponent();
|
||||
assertThat(component.getActivity(), equalTo(activity));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dialogFragmentTest()
|
||||
{
|
||||
EmptyActivity activity = setupActivity(EmptyActivity.class);
|
||||
FragmentManager manager = activity.getSupportFragmentManager();
|
||||
ColorPickerDialog d = new ColorPickerDialogFactory(activity).create(0);
|
||||
|
||||
activity.showDialog(d, "picker");
|
||||
assertTrue(d.getDialog().isShowing());
|
||||
assertThat(d, equalTo(manager.findFragmentByTag("picker")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dialogTest()
|
||||
{
|
||||
EmptyActivity activity = setupActivity(EmptyActivity.class);
|
||||
AlertDialog dialog =
|
||||
new AlertDialog.Builder(activity).setTitle("Hello world").create();
|
||||
|
||||
activity.showDialog(dialog);
|
||||
assertTrue(dialog.isShowing());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void restartTest() throws Exception
|
||||
{
|
||||
EmptyActivity activity = setupActivity(EmptyActivity.class);
|
||||
|
||||
activity.restartWithFade(EmptyActivity.class);
|
||||
ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
|
||||
|
||||
Intent nextStartedActivity = shadowOf(activity).getNextStartedActivity();
|
||||
assertNotNull(nextStartedActivity);
|
||||
|
||||
assertTrue(activity.isFinishing());
|
||||
assertThat(shadowOf(nextStartedActivity).getIntentClass(),
|
||||
equalTo(EmptyActivity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exceptionHandlerTest() throws InterruptedException
|
||||
{
|
||||
assertFalse(hasCrashed);
|
||||
|
||||
Thread crashThread = new Thread()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
Looper.prepare();
|
||||
CrashActivity activity = setupActivity(CrashActivity.class);
|
||||
activity.crash();
|
||||
}
|
||||
};
|
||||
|
||||
crashThread.start();
|
||||
crashThread.join();
|
||||
assertTrue(hasCrashed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void menuTest()
|
||||
{
|
||||
MenuActivity activity = setupActivity(MenuActivity.class);
|
||||
verify(activity.baseMenu).onCreate(eq(activity.getMenuInflater()),
|
||||
any());
|
||||
|
||||
Menu menu = activity.toolbar.getMenu();
|
||||
MenuItem item = menu.getItem(0);
|
||||
activity.onMenuItemSelected(0, item);
|
||||
verify(activity.baseMenu).onItemSelected(item);
|
||||
}
|
||||
|
||||
static class CrashActivity extends BaseActivity
|
||||
{
|
||||
public void crash()
|
||||
{
|
||||
throw new RuntimeException("crash!");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Thread.UncaughtExceptionHandler getExceptionHandler()
|
||||
{
|
||||
return (t, e) -> hasCrashed = true;
|
||||
}
|
||||
}
|
||||
|
||||
static class EmptyActivity extends BaseActivity
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static class MenuActivity extends BaseActivity
|
||||
{
|
||||
public BaseMenu baseMenu;
|
||||
|
||||
public Toolbar toolbar;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState)
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
toolbar = new Toolbar(this);
|
||||
LinearLayout layout = new LinearLayout(this);
|
||||
layout.addView(toolbar);
|
||||
setContentView(layout);
|
||||
setSupportActionBar(toolbar);
|
||||
|
||||
baseMenu = spy(new BaseMenu(this)
|
||||
{
|
||||
@Override
|
||||
protected int getMenuResourceId()
|
||||
{
|
||||
return R.menu.list_habits;
|
||||
}
|
||||
});
|
||||
|
||||
setBaseMenu(baseMenu);
|
||||
}
|
||||
}
|
||||
|
||||
static class ScreenActivity extends BaseActivity
|
||||
{
|
||||
private BaseScreen screen;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState)
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
screen = spy(new BaseScreen(this));
|
||||
setScreen(screen);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,141 +0,0 @@
|
||||
/*
|
||||
* 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.androidbase.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.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.robolectric.Robolectric.*;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(sdk = BuildConfig.roboSdk, 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…
Reference in new issue