update unit tests

This commit is contained in:
David Luhmer 2019-04-07 13:47:50 -03:00
parent 26c90c228c
commit 8716119737
12 changed files with 310 additions and 233 deletions

View file

@ -161,8 +161,13 @@
-keep class org.conscrypt.Conscrypt { *; }
# Required for unit tests
# https://stackoverflow.com/a/39777485
# Also, note that this rule should be added to the regular proguard file(the one of listed in proguardFiles) and not the test one(declared as testProguardFile)
# java.lang.NoSuchMethodError: No virtual method getParameter
-keepclasseswithmembers public class com.nextcloud.android.sso.aidl.NextcloudRequest { *; }
-keepclasseswithmembers public class com.nextcloud.android.sso.AccountImporter { *; }
# NewsReaderListActivityTests
-keepclasseswithmembers public class androidx.recyclerview.widget.RecyclerView { *; }

View file

@ -33,16 +33,12 @@
-dontwarn com.squareup.javawriter.JavaWriter
-dontwarn androidx.concurrent.futures.AbstractResolvableFuture
-dontwarn org.conscrypt.Conscrypt
#-dontwarn org.conscrypt.Conscrypt
#com.google.common.util.concurrent.ListenableFuture
-keep interface okhttp3.internal.platform.ConscryptPlatform
-keep class okhttp3.internal.platform.ConscryptPlatform
#-keep interface okhttp3.internal.platform.ConscryptPlatform
#-keep class okhttp3.internal.platform.ConscryptPlatform
-keep class org.conscrypt.Conscrypt { *; }
-keep interface org.conscrypt.Conscrypt { *; }
#org.conscrypt
-dontwarn org.conscrypt.**
#-keep class org.conscrypt.** { *; }
#-keep interface org.conscrypt.** { *; }
#-keep class org.conscrypt.Conscrypt { *; }
#-keep interface org.conscrypt.Conscrypt { *; }

View file

@ -0,0 +1,155 @@
package de.luhmer.owncloudnewsreader;
import android.content.SharedPreferences;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import androidx.annotation.Nullable;
/**
* Mock implementation of shared preference, which just saves data in memory using map.
*/
public class MockSharedPreference implements SharedPreferences {
private final HashMap<String, Object> preferenceMap;
private final MockSharedPreferenceEditor preferenceEditor;
public MockSharedPreference() {
preferenceMap = new HashMap<>();
preferenceEditor = new MockSharedPreferenceEditor(preferenceMap);
}
@Override
public Map<String, ?> getAll() {
return preferenceMap;
}
@Nullable
@Override
public String getString(final String s, @Nullable final String s1) {
Object v = preferenceMap.get(s);
return v != null ? (String) v : s1;
}
@Nullable
@Override
public Set<String> getStringSet(final String s, @Nullable final Set<String> set) {
Object v = preferenceMap.get(s);
return v != null ? (Set<String>) v : set;
}
@Override
public int getInt(final String s, final int i) {
Object v = preferenceMap.get(s);
return v != null ? (int) v : i;
}
@Override
public long getLong(final String s, final long l) {
Object v = preferenceMap.get(s);
return v != null ? (long) v : l;
}
@Override
public float getFloat(final String s, final float f) {
Object v = preferenceMap.get(s);
return v != null ? (float) v : f;
}
@Override
public boolean getBoolean(final String s, final boolean b) {
Object v = preferenceMap.get(s);
return v != null ? (boolean) v : b;
}
@Override
public boolean contains(final String s) {
return preferenceMap.containsKey(s);
}
@Override
public Editor edit() {
return preferenceEditor;
}
@Override
public void registerOnSharedPreferenceChangeListener(final OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
throw new UnsupportedOperationException();
}
@Override
public void unregisterOnSharedPreferenceChangeListener(final OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
throw new UnsupportedOperationException();
}
public static class MockSharedPreferenceEditor implements Editor {
private final HashMap<String, Object> preferenceMap;
public MockSharedPreferenceEditor(final HashMap<String, Object> preferenceMap) {
this.preferenceMap = preferenceMap;
}
@Override
public Editor putString(final String s, @Nullable final String s1) {
preferenceMap.put(s, s1);
return this;
}
@Override
public Editor putStringSet(final String s, @Nullable final Set<String> set) {
preferenceMap.put(s, set);
return this;
}
@Override
public Editor putInt(final String s, final int i) {
preferenceMap.put(s, i);
return this;
}
@Override
public Editor putLong(final String s, final long l) {
preferenceMap.put(s, l);
return this;
}
@Override
public Editor putFloat(final String s, final float v) {
preferenceMap.put(s, v);
return this;
}
@Override
public Editor putBoolean(final String s, final boolean b) {
preferenceMap.put(s, b);
return this;
}
@Override
public Editor remove(final String s) {
preferenceMap.remove(s);
return this;
}
@Override
public Editor clear() {
preferenceMap.clear();
return this;
}
@Override
public boolean commit() {
return true;
}
@Override
public void apply() {
// Nothing to do, everything is saved in memory.
}
}
}

View file

@ -1,29 +1,31 @@
package de.luhmer.owncloudnewsreader.di;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import com.nextcloud.android.sso.AccountImporter;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
import com.nextcloud.android.sso.model.SingleSignOnAccount;
import java.io.IOException;
import de.luhmer.owncloudnewsreader.SettingsActivity;
import de.luhmer.owncloudnewsreader.ssl.MemorizingTrustManager;
import javax.inject.Named;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import dagger.Provides;
import de.luhmer.owncloudnewsreader.MockSharedPreference;
import de.luhmer.owncloudnewsreader.NewsReaderListFragment;
import de.luhmer.owncloudnewsreader.SettingsActivity;
import de.luhmer.owncloudnewsreader.model.UserInfo;
import de.luhmer.owncloudnewsreader.ssl.MemorizingTrustManager;
public class TestApiModule extends ApiModule {
private Application application;
public static String DUMMY_ACCOUNT_AccountName = "test-account";
public static String DUMMY_ACCOUNT_username = "david";
public static String DUMMY_ACCOUNT_token = "abc";
public static String DUMMY_ACCOUNT_server_url = "http://nextcloud.com/";
public TestApiModule(Application application) {
super(application);
this.application = application;
@ -31,43 +33,52 @@ public class TestApiModule extends ApiModule {
@Override
public SharedPreferences providesSharedPreferences() {
SharedPreferences sharedPrefs = mock(SharedPreferences.class);
final Context context = mock(Context.class);
when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs);
// Create dummy account
String prefKey = "PREF_ACCOUNT_STRING" + DUMMY_ACCOUNT_AccountName;
SingleSignOnAccount ssoAccount = new SingleSignOnAccount(DUMMY_ACCOUNT_AccountName, DUMMY_ACCOUNT_username, DUMMY_ACCOUNT_token, DUMMY_ACCOUNT_server_url);
UserInfo userInfo = new UserInfo.Builder()
.setUserId("1")
.setDisplayName(DUMMY_ACCOUNT_username)
.setAvatar(null)
.build();
SharedPreferences sharedPrefs = new MockSharedPreference();
// Turn on Single-Sign-On
when(sharedPrefs.getBoolean(SettingsActivity.SW_USE_SINGLE_SIGN_ON, false)).thenReturn(true);
sharedPrefs.edit().putBoolean(SettingsActivity.SW_USE_SINGLE_SIGN_ON, true).commit();
// Set cache size
when(sharedPrefs.getString(eq(SettingsActivity.SP_MAX_CACHE_SIZE), any())).thenReturn("500");
// Set mock preferences for AccountImporter
AccountImporter.setSharedPreferences(sharedPrefs);
// Add dummy account
String accountName = "test-account";
String username = "david";
String token = "abc";
String server_url = "http://nextcloud.com/";
String prefKey = "PREF_ACCOUNT_STRING" + accountName;
SingleSignOnAccount ssoAccount = new SingleSignOnAccount(accountName, username, token, server_url);
// Return mock login data when requesting the account
try {
AccountImporter.getSharedPreferences(application).edit().putString(prefKey, SingleSignOnAccount.toString(ssoAccount)).commit();
sharedPrefs.edit().putString(prefKey, SingleSignOnAccount.toString(ssoAccount)).commit();
} catch (IOException e) {
e.printStackTrace();
throw new Error(e);
}
//try {
// when(sharedPrefs.getString(eq(prefKey), any())).thenReturn(SingleSignOnAccount.toString(ssoAccount));
//} catch (IOException e) {
// e.printStackTrace();
//}
SingleAccountHelper.setCurrentAccount(application, accountName);
// For userinfo in main activity
sharedPrefs.edit().putString(SettingsActivity.EDT_OWNCLOUDROOTPATH_STRING, DUMMY_ACCOUNT_server_url).commit();
sharedPrefs.edit().putString(SettingsActivity.EDT_USERNAME_STRING, DUMMY_ACCOUNT_username).commit();
sharedPrefs.edit().putString("PREF_CURRENT_ACCOUNT_STRING", DUMMY_ACCOUNT_AccountName).commit();
try {
sharedPrefs.edit().putString("USER_INFO", NewsReaderListFragment.toString(userInfo)).commit();
} catch (IOException e) {
throw new Error(e);
}
return sharedPrefs;
}
@Override
public String providesSharedPreferencesFileName() {
return application.getPackageName() + "_preferences_test";
}
@Override
protected ApiProvider provideAPI(MemorizingTrustManager mtm, SharedPreferences sp) {
ApiProvider apiProvider = new TestApiProvider(mtm, sp, application);

View file

@ -22,6 +22,8 @@ import de.luhmer.owncloudnewsreader.reader.nextcloud.API;
import de.luhmer.owncloudnewsreader.ssl.MemorizingTrustManager;
import retrofit2.NextcloudRetrofitApiBuilder;
import static de.luhmer.owncloudnewsreader.di.TestApiModule.DUMMY_ACCOUNT_AccountName;
import static de.luhmer.owncloudnewsreader.di.TestApiModule.DUMMY_ACCOUNT_username;
import static org.mockito.ArgumentMatchers.any;
public class TestApiProvider extends ApiProvider {
@ -58,7 +60,7 @@ public class TestApiProvider extends ApiProvider {
}
class NewsTestNetworkRequest extends NetworkRequest {
public class NewsTestNetworkRequest extends NetworkRequest {
NewsTestNetworkRequest(NextcloudAPI.ApiConnectedListener callback) {
super(null, null, callback);
@ -82,8 +84,11 @@ public class TestApiProvider extends ApiProvider {
case "/index.php/apps/news/api/v1-2/feeds":
inputStream = handleCreateFeed(request);
break;
case "/index.php/apps/news/api/v1-2/user":
inputStream = handleUser(request);
break;
default:
throw new UnsupportedOperationException("Not implemented yet!");
throw new Error("Not implemented yet!");
}
return inputStream;
}
@ -100,10 +105,21 @@ public class TestApiProvider extends ApiProvider {
case NEW_FEED_FAIL:
throw new NextcloudHttpRequestFailedException(422, new Throwable(NEW_FEED_FAIL_ERROR_MESSAGE));
default:
throw new UnsupportedOperationException("Not implemented yet!");
throw new Error("Not implemented yet!");
}
}
private InputStream handleUser(NextcloudRequest request) throws NextcloudHttpRequestFailedException {
String user = "{\n" +
" \"userId\": \"" + DUMMY_ACCOUNT_AccountName + "\",\n" +
" \"displayName\": \"" + DUMMY_ACCOUNT_username + "\",\n" +
" \"lastLoginTimestamp\": 1241231233, \n" +
" \"avatar\": null" +
"}";
return stringToInputStream(user);
}
private InputStream stringToInputStream(String data) {
return new ByteArrayInputStream(data.getBytes(Charset.forName("UTF-8")));
}

View file

@ -1,13 +1,15 @@
package de.luhmer.owncloudnewsreader.di;
import com.nextcloud.android.sso.api.NewFeedTests;
import javax.inject.Singleton;
import dagger.Component;
import de.luhmer.owncloudnewsreader.tests.NewFeedTests;
import de.luhmer.owncloudnewsreader.tests.NightModeTest;
@Singleton
@Component(modules = { ApiModule.class })
public interface TestComponent extends AppComponent {
void inject(NewFeedTests test);
void inject(NewFeedTests newFeedTest);
void inject(NightModeTest nightModeTest);
}

View file

@ -1,4 +1,4 @@
package com.nextcloud.android.sso.api;
package de.luhmer.owncloudnewsreader.tests;
import com.nextcloud.android.sso.aidl.NextcloudRequest;
@ -123,7 +123,7 @@ public class NewFeedTests {
// Verify that the API was actually called
private void verifyRequest(String feed) throws Exception {
NetworkRequest nr = ((TestApiProvider)mApi).networkRequestSpy;
TestApiProvider.NewsTestNetworkRequest nr = ((TestApiProvider)mApi).networkRequestSpy;
ArgumentCaptor<NextcloudRequest> argument = ArgumentCaptor.forClass(NextcloudRequest.class);
verify(nr, timeout(2000)).performNetworkRequest(argument.capture(), any());
assertEquals("/index.php/apps/news/api/v1-2/feeds", argument.getValue().getUrl());

View file

@ -4,14 +4,6 @@ import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import androidx.test.espresso.Espresso;
import androidx.test.espresso.contrib.RecyclerViewActions;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import org.junit.Before;
import org.junit.Rule;
@ -21,6 +13,14 @@ import org.junit.runner.RunWith;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.test.espresso.Espresso;
import androidx.test.espresso.contrib.RecyclerViewActions;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import de.luhmer.owncloudnewsreader.Constants;
import de.luhmer.owncloudnewsreader.NewsReaderDetailFragment;
import de.luhmer.owncloudnewsreader.NewsReaderListActivity;
@ -73,7 +73,7 @@ public class NewsReaderListActivityUiTests {
onView(isRoot()).perform(OrientationChangeAction.orientationLandscape(getActivity()));
//onView(isRoot()).perform(OrientationChangeAction.orientationPortrait(getActivity()));
sleep(0.5f);
sleep(1.0f);
LinearLayoutManager llm = (LinearLayoutManager) ndf.getRecyclerView().getLayoutManager();
onView(withId(R.id.list)).check(new RecyclerViewAssertions(scrollPosition-(scrollPosition-llm.findFirstVisibleItemPosition())));
@ -84,11 +84,9 @@ public class NewsReaderListActivityUiTests {
@Test
public void testPositionAfterActivityRestart_sameActivity() {
onView(withId(R.id.list)).perform(
RecyclerViewActions.scrollToPosition(scrollPosition));
onView(withId(R.id.list)).perform(RecyclerViewActions.scrollToPosition(scrollPosition));
onView(withId(R.id.list)).perform(
RecyclerViewActions.actionOnItemAtPosition(scrollPosition, click()));
onView(withId(R.id.list)).perform(RecyclerViewActions.actionOnItemAtPosition(scrollPosition, click()));
sleep(2);
@ -102,13 +100,8 @@ public class NewsReaderListActivityUiTests {
assertNotNull(vh);
LinearLayoutManager llm = (LinearLayoutManager) ndf.getRecyclerView().getLayoutManager();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
na.changeReadStateOfItem(vh, false);
}
});
sleep(0.5f);
getActivity().runOnUiThread(() -> na.changeReadStateOfItem(vh, false));
sleep(1.0f);
onView(withId(R.id.list)).check(new RecyclerViewAssertions(scrollPosition-(scrollPosition-llm.findFirstVisibleItemPosition())));
onView(withId(R.id.tv_no_items_available)).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.GONE)));
@ -153,7 +146,7 @@ public class NewsReaderListActivityUiTests {
}
});
getInstrumentation().waitForIdleSync();
sleep(0.5f);
sleep(1.0f);
if(!testFirstPosition) {
onView(withId(com.google.android.material.R.id.snackbar_text)).check(matches(isDisplayed()));

View file

@ -2,10 +2,8 @@ package de.luhmer.owncloudnewsreader.tests;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.Preference;
import android.preference.PreferenceManager;
import org.junit.Before;
import org.junit.Rule;
@ -16,16 +14,18 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.inject.Inject;
import androidx.test.InstrumentationRegistry;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import de.luhmer.owncloudnewsreader.NewsReaderListActivity;
import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.TestApplication;
import de.luhmer.owncloudnewsreader.di.TestComponent;
import de.luhmer.owncloudnewsreader.helper.ThemeChooser;
import static android.preference.PreferenceManager.KEY_HAS_SET_DEFAULT_VALUES;
import static androidx.test.InstrumentationRegistry.getInstrumentation;
import static androidx.test.espresso.Espresso.onData;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu;
@ -36,19 +36,13 @@ import static androidx.test.espresso.matcher.PreferenceMatchers.withSummary;
import static androidx.test.espresso.matcher.PreferenceMatchers.withTitle;
import static androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static de.luhmer.owncloudnewsreader.SettingsActivity.CB_OLED_MODE;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_APP_THEME;
import static helper.CustomMatchers.withBackgroundColor;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.AllOf.allOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@RunWith(AndroidJUnit4.class)
@ -56,36 +50,24 @@ import static org.junit.Assert.fail;
public class NightModeTest {
/**
* NOTE: These tests only works during "daylight".. (this is because there is no way to check
* NOTE: These tests only work during "daylight".. (this is because there is no way to check
* the current state of the android day/night mode)
*/
@Rule
public ActivityTestRule<NewsReaderListActivity> mActivityRule = new ActivityTestRule<>(NewsReaderListActivity.class, true, false);
public ActivityTestRule<NewsReaderListActivity> mActivityRule = new ActivityTestRule<>(NewsReaderListActivity.class);
//public ActivityTestRule<NewsReaderListActivity> mActivityRule = new ActivityTestRule<>(NewsReaderListActivity.class, true, false);
private Activity getActivity() {
return mActivityRule.getActivity();
}
protected @Inject SharedPreferences mPrefs;
@Before
public void resetSharedPrefs() {
Context context = getInstrumentation().getTargetContext();
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
// Reset SharedPrefs
// https://developer.android.com/guide/topics/ui/settings#Defaults
mPrefs.edit()
.remove(CB_OLED_MODE)
.remove(SP_APP_THEME)
.commit();
assertThat(mPrefs.contains(SP_APP_THEME), equalTo(false));
assertThat(mPrefs.contains(CB_OLED_MODE), equalTo(false));
SharedPreferences defaultValueSp = context.getSharedPreferences(KEY_HAS_SET_DEFAULT_VALUES, Context.MODE_PRIVATE);
defaultValueSp.edit().putBoolean(KEY_HAS_SET_DEFAULT_VALUES, false).commit();
TestComponent ac = (TestComponent) ((TestApplication)(getActivity().getApplication())).getAppComponent();
ac.inject(this);
/*
// Set Fixed time
@ -99,17 +81,13 @@ public class NightModeTest {
@Test
public void testBackgroundDaylightTheme() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
launchActivity();
public void testBackgroundDaylightTheme() {
assertFalse(isDarkTheme());
onView(withId(R.id.sliding_layout)).check(matches(withBackgroundColor(android.R.color.white, getActivity())));
//onView(withId(R.id.sliding_layout)).check(matches(withBackgroundColor(android.R.color.white, getActivity())));
}
@Test
public void testOledAutoMode() {
launchActivity();
openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
openSettings();
changeAppTheme(R.string.pref_display_apptheme_auto);
@ -126,8 +104,6 @@ public class NightModeTest {
@Test
public void testLightTheme() {
launchActivity();
openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
openSettings();
@ -143,8 +119,6 @@ public class NightModeTest {
@Test
public void testDarkTheme() {
launchActivity();
openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
openSettings();
@ -152,16 +126,14 @@ public class NightModeTest {
navigateUp();
sleep();
boolean isDarkTheme = isDarkTheme();
assertThat(ThemeChooser.getInstance(getActivity()).isOledMode(getActivity(), false), equalTo(false));
assertThat(isDarkTheme, equalTo(true));
assertFalse(ThemeChooser.getInstance(getActivity()).isOledMode(getActivity(), false));
assertTrue(isDarkTheme);
assertEquals(ThemeChooser.THEME.DARK, getPrivateField("mSelectedTheme"));
//sleep();
}
@Test
public void testDarkOledTheme() {
launchActivity();
openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
openSettings();
@ -218,26 +190,6 @@ public class NightModeTest {
.perform(click());
}
private void launchActivity() {
mActivityRule.launchActivity(new Intent());
/*
NewsReaderApplication nra = (NewsReaderApplication) getActivity().getApplication();
AppComponent appComponent = DaggerAppComponent.builder()
.apiModule(new TestApiModule(nra))
.build();
nra.setAppComponent(appComponent);
*/
sleep();
//assertFalse(ThemeChooser.getInstance(getActivity()).isDarkTheme(getActivity()));
//assertFalse(ThemeChooser.getInstance(getActivity()).isOledMode(getActivity(), true));
}
private boolean isDarkTheme() {
ThemeChooser themeChooser = ThemeChooser.getInstance(getActivity());

View file

@ -1,28 +1,21 @@
package screengrab;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import androidx.core.view.GravityCompat;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.IOException;
import androidx.core.view.GravityCompat;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import de.luhmer.owncloudnewsreader.NewsReaderDetailFragment;
import de.luhmer.owncloudnewsreader.NewsReaderListActivity;
import de.luhmer.owncloudnewsreader.NewsReaderListFragment;
import de.luhmer.owncloudnewsreader.SettingsActivity;
import de.luhmer.owncloudnewsreader.adapter.NewsListRecyclerAdapter;
import de.luhmer.owncloudnewsreader.adapter.ViewHolder;
import de.luhmer.owncloudnewsreader.database.DatabaseConnectionOrm;
import de.luhmer.owncloudnewsreader.model.PodcastItem;
import de.luhmer.owncloudnewsreader.model.UserInfo;
import tools.fastlane.screengrab.Screengrab;
import tools.fastlane.screengrab.locale.LocaleTestRule;
@ -51,22 +44,6 @@ public class ScreenshotTest {
activity = mActivityRule.getActivity();
nrlf = mActivityRule.getActivity().getSlidingListFragment();
nrdf = mActivityRule.getActivity().getNewsReaderDetailFragment();
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(mActivityRule.getActivity());
UserInfo userInfo = new UserInfo.Builder()
.setUserId("1")
.setDisplayName("David")
.setAvatar(null)
.build();
try {
mPrefs.edit().putString("USER_INFO", NewsReaderListFragment.toString(userInfo)).commit();
mPrefs.edit().putBoolean(SettingsActivity.CB_SKIP_DETAILVIEW_AND_OPEN_BROWSER_DIRECTLY_STRING, false).commit();
} catch (IOException e) {
e.printStackTrace();
}
}
@ -75,14 +52,9 @@ public class ScreenshotTest {
public void testTakeScreenshots() {
Screengrab.screenshot("startup");
activity.runOnUiThread(new Runnable() {
public void run() {
//Set url to mock
nrlf.bindUserInfoToUI(true);
activity.runOnUiThread(() -> {
openDrawer();
nrlf.getListView().expandGroup(podcastGroupPosition);
}
});
try {
@ -94,8 +66,7 @@ public class ScreenshotTest {
Screengrab.screenshot("slider_open");
activity.runOnUiThread(new Runnable() {
public void run() {
activity.runOnUiThread(() -> {
closeDrawer();
try {
@ -104,10 +75,8 @@ public class ScreenshotTest {
e.printStackTrace();
}
activity.onClick(null, itemPos); //Select item
}
});
try {
@ -118,13 +87,10 @@ public class ScreenshotTest {
Screengrab.screenshot("detail_activity");
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.runOnUiThread(() -> {
NewsListRecyclerAdapter na = (NewsListRecyclerAdapter) nrdf.getRecyclerView().getAdapter();
ViewHolder vh = (ViewHolder) nrdf.getRecyclerView().getChildViewHolder(nrdf.getRecyclerView().getLayoutManager().findViewByPosition(itemPos));
na.changeReadStateOfItem(vh, false);
}
});
}
@ -133,15 +99,10 @@ public class ScreenshotTest {
@Test
public void testPodcast() {
activity.runOnUiThread(new Runnable() {
public void run() {
//Set url to mock
nrlf.bindUserInfoToUI(true);
activity.runOnUiThread(() -> {
openDrawer();
nrlf.getListView().expandGroup(podcastGroupPosition);
openFeed(podcastGroupPosition, 0);
}
});
try {
@ -152,12 +113,10 @@ public class ScreenshotTest {
Screengrab.screenshot("podcast_list");
activity.runOnUiThread(new Runnable() {
public void run() {
activity.runOnUiThread(() -> {
ViewHolder vh = (ViewHolder) nrdf.getRecyclerView().getChildViewHolder(nrdf.getRecyclerView().getLayoutManager().findViewByPosition(0));
PodcastItem podcastItem = DatabaseConnectionOrm.ParsePodcastItemFromRssItem(activity, vh.getRssItem());
activity.openMediaItem(podcastItem);
}
});
@ -170,11 +129,7 @@ public class ScreenshotTest {
Screengrab.screenshot("podcast_running");
activity.runOnUiThread(new Runnable() {
public void run() {
activity.pausePodcast();
}
});
activity.runOnUiThread(() -> activity.pausePodcast());
try {
@ -188,14 +143,12 @@ public class ScreenshotTest {
@Test
public void testVideoPodcast() {
activity.runOnUiThread(new Runnable() {
public void run() {
activity.runOnUiThread(() -> {
//Set url to mock
nrlf.bindUserInfoToUI(true);
nrlf.bindUserInfoToUI();
openDrawer();
openFeed(0, 13); //Click on ARD Podcast
}
});
try {
@ -204,12 +157,10 @@ public class ScreenshotTest {
e.printStackTrace();
}
activity.runOnUiThread(new Runnable() {
public void run() {
activity.runOnUiThread(() -> {
ViewHolder vh = (ViewHolder) nrdf.getRecyclerView().getChildViewHolder(nrdf.getRecyclerView().getLayoutManager().findViewByPosition(1));
PodcastItem podcastItem = DatabaseConnectionOrm.ParsePodcastItemFromRssItem(activity, vh.getRssItem());
activity.openMediaItem(podcastItem);
}
});
@ -222,11 +173,7 @@ public class ScreenshotTest {
Screengrab.screenshot("video_podcast_running");
activity.runOnUiThread(new Runnable() {
public void run() {
activity.pausePodcast();
}
});
activity.runOnUiThread(() -> activity.pausePodcast());
try {

View file

@ -32,8 +32,7 @@ public class DatabaseHelperOrm {
private volatile static DaoSession daoSession;
public static DaoSession getDaoSession(Context context)
{
public static DaoSession getDaoSession(Context context) {
if(daoSession == null) {
synchronized (DatabaseHelperOrm.class) {
if(daoSession == null) {

View file

@ -2,7 +2,6 @@ package de.luhmer.owncloudnewsreader.di;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.NonNull;
import android.util.Log;
import com.nextcloud.android.sso.api.NextcloudAPI;
@ -14,6 +13,7 @@ import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import de.luhmer.owncloudnewsreader.SettingsActivity;
import de.luhmer.owncloudnewsreader.helper.GsonConfig;
@ -103,7 +103,8 @@ public class ApiProvider {
private void initImageLoader(SharedPreferences mPrefs, OkHttpClient okHttpClient, Context context) {
int diskCacheSize = Integer.parseInt(mPrefs.getString(SettingsActivity.SP_MAX_CACHE_SIZE,"500"))*1024*1024;
String cacheSize = mPrefs.getString(SettingsActivity.SP_MAX_CACHE_SIZE,"500");
int diskCacheSize = Integer.parseInt(cacheSize)*1024*1024;
if(ImageLoader.getInstance().isInited()) {
ImageLoader.getInstance().destroy();
}