Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use interceptor to mock any network request #6299

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ ext {
daggerVersion = "2.27"
markwonVersion = "4.4.0"
prismVersion = "2.0.0"
androidLibraryVersion = "master-SNAPSHOT"
androidLibraryVersion = "interceptor-SNAPSHOT"

travisBuild = System.getenv("TRAVIS") == "true"

Expand Down Expand Up @@ -368,6 +368,8 @@ dependencies {
testImplementation 'org.json:json:20200518'
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
testImplementation "androidx.arch.core:core-testing:2.1.0"
kaptAndroidTest "com.google.dagger:dagger-compiler:$daggerVersion"
kaptTest "com.google.dagger:dagger-compiler:$daggerVersion"

// dependencies for instrumented tests
// JUnit4 Rules
Expand Down
2 changes: 2 additions & 0 deletions src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@
android:sharedUserId="${applicationId}.uid">

<uses-sdk tools:overrideLibrary="android_libs.ub_uiautomator, tools.fastlane.screengrab" />

<application android:name="com.owncloud.android.TestApp"></application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@

package com.nextcloud.client;

import android.app.Application;
import android.content.Context;
import android.os.Bundle;

import com.facebook.testing.screenshot.ScreenshotRunner;
import com.owncloud.android.TestApp;

import androidx.test.runner.AndroidJUnitRunner;

public class ScreenshotTestRunner extends AndroidJUnitRunner {

@Override
public Application newApplication(ClassLoader cl, String className, Context context) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
return super.newApplication(cl, TestApp.class.getName(), context);
}

@Override
public void onCreate(Bundle args) {
super.onCreate(args);
Expand Down
44 changes: 44 additions & 0 deletions src/androidTest/java/com/owncloud/android/MockInterceptor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2020 Tobias Kaminsky
* Copyright (C) 2020 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.owncloud.android

import okhttp3.Interceptor
import okhttp3.MediaType
import okhttp3.Protocol
import okhttp3.Response
import okhttp3.ResponseBody

class MockInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
return chain.proceed(chain.request())
.newBuilder()
.code(200)
.protocol(Protocol.HTTP_2)
.message("123")
.body(ResponseBody.create(MediaType.parse("application/json"),
"responseString".toByteArray()))
.addHeader("content-type", "application/json")
.build()
}
}

68 changes: 68 additions & 0 deletions src/androidTest/java/com/owncloud/android/MockedNetworkModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2020 Tobias Kaminsky
* Copyright (C) 2020 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.owncloud.android;

import android.content.Context;
import android.net.ConnectivityManager;

import com.nextcloud.client.account.UserAccountManager;
import com.nextcloud.client.logger.Logger;
import com.nextcloud.client.network.ClientFactory;
import com.nextcloud.client.network.ClientFactoryImpl;
import com.nextcloud.client.network.ConnectivityService;
import com.nextcloud.client.network.ConnectivityServiceImpl;
import com.owncloud.android.lib.common.utils.Log_OC;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;

@Module
public class MockedNetworkModule {

@Provides
ConnectivityService connectivityService(ConnectivityManager connectivityManager,
UserAccountManager accountManager,
ClientFactory clientFactory,
Logger logger) {
return new ConnectivityServiceImpl(connectivityManager,
accountManager,
clientFactory,
new ConnectivityServiceImpl.GetRequestBuilder(),
logger);
}

@Provides
@Singleton
ClientFactory clientFactory(Context context) {
Log_OC.d("MOCK", "c");
return new ClientFactoryImpl(context, new MockInterceptor());
}

@Provides
@Singleton
ConnectivityManager connectivityManager(Context context) {
return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
}
}
33 changes: 33 additions & 0 deletions src/androidTest/java/com/owncloud/android/TestApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2020 Tobias Kaminsky
* Copyright (C) 2020 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.owncloud.android


class TestApp : MainApp() {
override fun setupDagger() {
DaggerTestAppComponent.builder()
.application(this)
.build()
.inject(this);
}
}
70 changes: 70 additions & 0 deletions src/androidTest/java/com/owncloud/android/TestAppComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2020 Tobias Kaminsky
* Copyright (C) 2020 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.owncloud.android;

import android.app.Application;

import com.nextcloud.client.appinfo.AppInfoModule;
import com.nextcloud.client.device.DeviceModule;
import com.nextcloud.client.di.AppModule;
import com.nextcloud.client.di.ViewModelModule;
import com.nextcloud.client.jobs.JobsModule;
import com.nextcloud.client.onboarding.OnboardingModule;
import com.nextcloud.client.preferences.PreferencesModule;

import javax.inject.Singleton;

import dagger.BindsInstance;
import dagger.Component;
import dagger.android.support.AndroidSupportInjectionModule;

@Singleton
@Component(modules = {
AndroidSupportInjectionModule.class,
AppModule.class,
PreferencesModule.class,
AppInfoModule.class,
MockedNetworkModule.class,
DeviceModule.class,
OnboardingModule.class,
ViewModelModule.class,
JobsModule.class
})

public interface TestAppComponent {

void inject(TestApp app);

@Component.Builder
interface Builder {
@BindsInstance
Builder application(Application application);

TestAppComponent build();
}

//@Component.Factory
//interface Factory {
// TestAppComponent create(@BindsInstance Application application);
//}
}
2 changes: 1 addition & 1 deletion src/main/java/com/nextcloud/client/di/AppModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
import dagger.Provides;

@Module(includes = {ComponentsModule.class, VariantComponentsModule.class})
class AppModule {
public class AppModule {

@Provides
AccountManager accountManager(Application application) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@

import java.io.IOException;

class ClientFactoryImpl implements ClientFactory {
import okhttp3.Interceptor;

public class ClientFactoryImpl implements ClientFactory {

private Context context;
private Interceptor interceptor;

ClientFactoryImpl(Context context) {
public ClientFactoryImpl(Context context, Interceptor interceptor) {
this.context = context;
this.interceptor = interceptor;
}

@Override
Expand All @@ -58,7 +62,7 @@ public OwnCloudClient create(User user) throws CreationException {
@Override
public NextcloudClient createNextcloudClient(User user) throws CreationException {
try {
return OwnCloudClientFactory.createNextcloudClient(user.toPlatformAccount(), context);
return OwnCloudClientFactory.createNextcloudClient(user.toPlatformAccount(), context, interceptor);
} catch (OperationCanceledException |
AuthenticatorException |
IOException |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import androidx.core.net.ConnectivityManagerCompat;
import kotlin.jvm.functions.Function1;

class ConnectivityServiceImpl implements ConnectivityService {
public class ConnectivityServiceImpl implements ConnectivityService {

private final static String TAG = ConnectivityServiceImpl.class.getName();

Expand All @@ -48,18 +48,18 @@ class ConnectivityServiceImpl implements ConnectivityService {
private final GetRequestBuilder requestBuilder;
private final Logger logger;

static class GetRequestBuilder implements Function1<String, GetMethod> {
public static class GetRequestBuilder implements Function1<String, GetMethod> {
@Override
public GetMethod invoke(String url) {
return new GetMethod(url);
}
}

ConnectivityServiceImpl(ConnectivityManager platformConnectivityManager,
UserAccountManager accountManager,
ClientFactory clientFactory,
GetRequestBuilder requestBuilder,
Logger logger) {
public ConnectivityServiceImpl(ConnectivityManager platformConnectivityManager,
UserAccountManager accountManager,
ClientFactory clientFactory,
GetRequestBuilder requestBuilder,
Logger logger) {
this.platformConnectivityManager = platformConnectivityManager;
this.accountManager = accountManager;
this.clientFactory = clientFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ConnectivityService connectivityService(ConnectivityManager connectivityManager,
@Provides
@Singleton
ClientFactory clientFactory(Context context) {
return new ClientFactoryImpl(context);
return new ClientFactoryImpl(context, null);
}

@Provides
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/owncloud/android/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@

import androidx.annotation.RequiresApi;
import androidx.annotation.StringRes;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.util.Pair;
Expand Down Expand Up @@ -222,10 +223,7 @@ protected void attachBaseContext(Context base) {
super.attachBaseContext(base);

initGlobalContext(this);
DaggerAppComponent.builder()
.application(this)
.build()
.inject(this);
setupDagger();

// we don't want to handle crashes occurring inside crash reporter activity/process;
// let the platform deal with those
Expand All @@ -240,6 +238,14 @@ protected void attachBaseContext(Context base) {
}
}

@VisibleForTesting
public void setupDagger() {
DaggerAppComponent.builder()
.application(this)
.build()
.inject(this);
}

@SuppressFBWarnings("ST")
@Override
public void onCreate() {
Expand Down
Loading