Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.split.android.client.SplitFactory;

interface SplitFactoryProvider {
public interface SplitFactoryProvider {

SplitFactory getSplitFactory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ class SplitMethodParserImpl implements SplitMethodParser {
private final MethodChannel mMethodChannel;
private final SplitProviderHelper mProviderHelper;

public SplitMethodParserImpl(@NonNull Context context, MethodChannel channel) {
public SplitMethodParserImpl(@NonNull Context context, @NonNull MethodChannel channel, @Nullable SplitFactoryProvider splitFactoryProvider) {
mContext = context;
mArgumentParser = new ArgumentParserImpl();
mMethodChannel = channel;
mProviderHelper = new SplitProviderHelperImpl();
mProviderHelper = new SplitProviderHelperImpl(splitFactoryProvider);
}

@VisibleForTesting
Expand Down
10 changes: 10 additions & 0 deletions android/src/main/java/io/split/splitio/SplitProviderHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,18 @@ SplitFactoryProvider getProvider(

class SplitProviderHelperImpl implements SplitProviderHelper {

@Nullable private final SplitFactoryProvider mSplitFactoryProvider;

SplitProviderHelperImpl(@Nullable SplitFactoryProvider splitFactoryProvider) {
mSplitFactoryProvider = splitFactoryProvider;
}

@Override
public SplitFactoryProvider getProvider(Context context, String apiKey, String matchingKey, @Nullable String bucketingKey, SplitClientConfig splitClientConfig) {
if (mSplitFactoryProvider != null) {
return mSplitFactoryProvider;
}

return new SplitFactoryProviderImpl(context, apiKey, matchingKey, bucketingKey, splitClientConfig);
}
}
20 changes: 14 additions & 6 deletions android/src/main/java/io/split/splitio/SplitioPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import static io.split.splitio.Constants.Error.METHOD_PARSER_NOT_INITIALIZED;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.annotation.Nullable;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
Expand All @@ -16,18 +18,24 @@
* SplitioPlugin
*/
public class SplitioPlugin implements FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private MethodChannel channel;
private SplitMethodParser methodParser;

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "splitio");
channel.setMethodCallHandler(this);
methodParser = new SplitMethodParserImpl(flutterPluginBinding.getApplicationContext(), channel);
SplitFactoryProvider provider = getSplitFactoryProvider(flutterPluginBinding.getApplicationContext());
methodParser = new SplitMethodParserImpl(flutterPluginBinding.getApplicationContext(), channel, provider);
}

@Nullable
private static SplitFactoryProvider getSplitFactoryProvider(Context applicationContext) {
try {
return (SplitFactoryProvider) applicationContext;
} catch (ClassCastException exception) {
return null;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -108,4 +109,44 @@ public void testGetListArgumentFailure() {

assertTrue(listArgument.isEmpty());
}

@Test
public void testGetDoubleArgument() {
Map<String, Object> arguments = new HashMap<>();
arguments.put("doubleArgument", 25.25);

Double doubleArgument = mArgumentParser.getDoubleArgument("doubleArgument", arguments);

assertEquals(new Double(25.25), doubleArgument);
}

@Test
public void testGetDoubleArgumentFailure() {
Map<String, Object> arguments = new HashMap<>();
arguments.put("doubleArgument", "error");

Double doubleArgument = mArgumentParser.getDoubleArgument("doubleArgument", arguments);

assertNull(doubleArgument);
}

@Test
public void testGetObjectArgument() {
Map<String, Object> arguments = new HashMap<>();
arguments.put("objectArgument", "error");

Object objectArgument = mArgumentParser.getObjectArgument("objectArgument", arguments);

assertNotNull(objectArgument);
}

@Test
public void testGetObjectArgumentFailure() {
Map<String, Object> arguments = new HashMap<>();
arguments.put("objectArgument", null);

Object objectArgument = mArgumentParser.getObjectArgument("objectArgument", arguments);

assertNull(objectArgument);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public class SplitMethodParserImplTest {
@Mock
private MethodChannel mMethodChannel;
@Mock
private SplitProviderHelper mProviderFactory;
private SplitProviderHelper mProviderHelper;

@Before
public void setUp() {
MockitoAnnotations.openMocks(this);
mMethodParser = new SplitMethodParserImpl(mSplitWrapper, mArgumentParser, mMethodChannel, mProviderFactory);
mMethodParser = new SplitMethodParserImpl(mSplitWrapper, mArgumentParser, mMethodChannel, mProviderHelper);
}

@Test
Expand All @@ -60,7 +60,7 @@ public void successfulGetClient() {

@Test
public void failingGetClient() {
mMethodParser = new SplitMethodParserImpl(null, mArgumentParser, mMethodChannel, mProviderFactory);
mMethodParser = new SplitMethodParserImpl(null, mArgumentParser, mMethodChannel, mProviderHelper);

Map<String, Object> map = new HashMap<>();
map.put("matchingKey", "user-key");
Expand Down Expand Up @@ -465,11 +465,11 @@ public void initialization() {
when(mArgumentParser.getMapArgument("sdkConfiguration", arguments)).thenReturn(configurationMap);

SplitFactoryProvider splitFactoryProvider = mock(SplitFactoryProvider.class);
when(mProviderFactory.getProvider(any(), any(), any(), any(), any())).thenReturn(splitFactoryProvider);
when(mProviderHelper.getProvider(any(), any(), any(), any(), any())).thenReturn(splitFactoryProvider);

mMethodParser.onMethodCall("init", arguments, mResult);

verify(mProviderFactory).getProvider(any(),
verify(mProviderHelper).getProvider(any(),
eq("key"),
eq("matching-key"),
eq("bucketing-key"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.split.splitio;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;

import org.junit.Test;

public class SplitProviderHelperImplTest {

private SplitProviderHelperImpl mHelper;

@Test
public void helperBuildsItsOwnProviderWhenNoneIsSupplied() {
mHelper = new SplitProviderHelperImpl(null);

SplitFactoryProvider provider = mHelper.getProvider(null, "", "", "", null);

assertNotNull(provider);
}

@Test
public void helperReturnsSuppliedProviderWhenExists() {
SplitFactoryProvider factoryProvider = mock(SplitFactoryProvider.class);
mHelper = new SplitProviderHelperImpl(factoryProvider);
SplitFactoryProvider provider = mHelper.getProvider(null, "", "", "", null);

assertEquals(factoryProvider, provider);
}
}
4 changes: 4 additions & 0 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
C51277632847F3D800B7D1A2 /* SplitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C51277622847F3D800B7D1A2 /* SplitTests.swift */; };
C5538DE328A5483400EE141E /* ExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5538DE228A5483400EE141E /* ExtensionsTests.swift */; };
C56515F82857B47E007D9112 /* SplitClientConfigHelperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C56515F72857B47E007D9112 /* SplitClientConfigHelperTests.swift */; };
C5E9331728BE5433007A9A93 /* SplitProviderHelperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5E9331628BE5433007A9A93 /* SplitProviderHelperTests.swift */; };
C5EAE748284FCC610025614D /* ArgumentParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5EAE747284FCC610025614D /* ArgumentParserTests.swift */; };
C5EAE74C2853A38C0025614D /* SplitMethodParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5EAE74B2853A38C0025614D /* SplitMethodParserTests.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -69,6 +70,7 @@
C51277622847F3D800B7D1A2 /* SplitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplitTests.swift; sourceTree = "<group>"; };
C5538DE228A5483400EE141E /* ExtensionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExtensionsTests.swift; sourceTree = "<group>"; };
C56515F72857B47E007D9112 /* SplitClientConfigHelperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplitClientConfigHelperTests.swift; sourceTree = "<group>"; };
C5E9331628BE5433007A9A93 /* SplitProviderHelperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplitProviderHelperTests.swift; sourceTree = "<group>"; };
C5EAE747284FCC610025614D /* ArgumentParserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArgumentParserTests.swift; sourceTree = "<group>"; };
C5EAE74B2853A38C0025614D /* SplitMethodParserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplitMethodParserTests.swift; sourceTree = "<group>"; };
CDE81357140D4D18CD672621 /* Pods_SplitTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SplitTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -167,6 +169,7 @@
C51277612847F3D800B7D1A2 /* SplitTests */ = {
isa = PBXGroup;
children = (
C5E9331628BE5433007A9A93 /* SplitProviderHelperTests.swift */,
C5538DE228A5483400EE141E /* ExtensionsTests.swift */,
C56515F72857B47E007D9112 /* SplitClientConfigHelperTests.swift */,
C5EAE74B2853A38C0025614D /* SplitMethodParserTests.swift */,
Expand Down Expand Up @@ -405,6 +408,7 @@
buildActionMask = 2147483647;
files = (
C5538DE328A5483400EE141E /* ExtensionsTests.swift in Sources */,
C5E9331728BE5433007A9A93 /* SplitProviderHelperTests.swift in Sources */,
C5EAE74C2853A38C0025614D /* SplitMethodParserTests.swift in Sources */,
C56515F82857B47E007D9112 /* SplitClientConfigHelperTests.swift in Sources */,
C51277632847F3D800B7D1A2 /* SplitTests.swift in Sources */,
Expand Down
23 changes: 23 additions & 0 deletions example/ios/SplitTests/SplitProviderHelperTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import XCTest
@testable import splitio
@testable import Split

class SplitProviderHelperTests: XCTestCase {

private var helper: DefaultSplitProviderHelper?

func testHelperBuildsItsOwnProviderWhenNoneIsSupplied() throws {
helper = DefaultSplitProviderHelper(splitFactoryProvider: nil)
let provider = helper?.getProvider(apiKey: "", matchingKey: "", bucketingKey: "", splitClientConfig: SplitClientConfig())

XCTAssertNotNil(provider)
}

func testHelperReturnsSuppliedProviderWhenExists() throws {
let factoryProvider: SplitFactoryProviderStub? = SplitFactoryProviderStub()
helper = DefaultSplitProviderHelper(splitFactoryProvider: factoryProvider)
let provider: SplitFactoryProviderStub? = helper?.getProvider(apiKey: "", matchingKey: "", bucketingKey: "", splitClientConfig: SplitClientConfig()) as? SplitFactoryProviderStub

XCTAssert(factoryProvider?.uuid == provider?.uuid)
}
}
6 changes: 6 additions & 0 deletions example/ios/SplitTests/SplitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ class SplitTests: XCTestCase {

class SplitFactoryProviderStub: SplitFactoryProvider {

// For testing purposes only
let uuid: Int = Int.random(in: 0..<1000)

var manager: SplitManagerStub?

init(manager: SplitManagerStub?) {
Expand All @@ -206,6 +209,9 @@ class SplitFactoryProviderStub: SplitFactoryProvider {

class SplitFactoryProviderStubWithClient: SplitFactoryProvider {

// For testing purposes only
let uuid: Int = Int.random(in: 0..<1000)

let client: SplitClientStub

init(client: SplitClientStub) {
Expand Down
16 changes: 14 additions & 2 deletions ios/Classes/SplitFactoryProvider.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import Split

protocol SplitFactoryProvider {
public protocol SplitFactoryProvider {

func getFactory() -> SplitFactory?
}
Expand All @@ -13,6 +13,9 @@ protocol SplitProviderHelper {

class DefaultSplitFactoryProvider: SplitFactoryProvider {

// For testing purposes only
let uuid: Int = Int.random(in: 0..<1000)

private let splitFactory: SplitFactory?

init(apiKey: String, matchingKey: String, bucketingKey: String? = nil, splitClientConfig: SplitClientConfig) {
Expand All @@ -30,8 +33,17 @@ class DefaultSplitFactoryProvider: SplitFactoryProvider {

class DefaultSplitProviderHelper: SplitProviderHelper {

let splitFactoryProvider: SplitFactoryProvider?

init(splitFactoryProvider: SplitFactoryProvider?) {
self.splitFactoryProvider = splitFactoryProvider
}

func getProvider(apiKey: String, matchingKey: String, bucketingKey: String? = nil, splitClientConfig: SplitClientConfig) -> SplitFactoryProvider {
guard let provider = splitFactoryProvider else {
return DefaultSplitFactoryProvider(apiKey: apiKey, matchingKey: matchingKey, splitClientConfig: splitClientConfig)
}

return DefaultSplitFactoryProvider(apiKey: apiKey, matchingKey: matchingKey, splitClientConfig: splitClientConfig)
return provider
}
}
5 changes: 3 additions & 2 deletions ios/Classes/SplitMethodParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class DefaultSplitMethodParser: SplitMethodParser {
private var methodChannel: FlutterMethodChannel
private var providerHelper: SplitProviderHelper

init(methodChannel: FlutterMethodChannel) {
init(methodChannel: FlutterMethodChannel, splitFactoryProvider: SplitFactoryProvider?) {
self.argumentParser = DefaultArgumentParser()
self.methodChannel = methodChannel
self.providerHelper = DefaultSplitProviderHelper()
self.providerHelper = DefaultSplitProviderHelper(splitFactoryProvider: splitFactoryProvider)
}

init(splitWrapper: SplitWrapper, argumentParser: ArgumentParser, methodChannel: FlutterMethodChannel, providerHelper: SplitProviderHelper) {
Expand Down Expand Up @@ -145,6 +145,7 @@ class DefaultSplitMethodParser: SplitMethodParser {
matchingKey: matchingKey,
bucketingKey: bucketingKey,
splitClientConfig: SplitClientConfigHelper.fromMap(configurationMap: configurationMap, impressionListener: getImpressionListener(impressionListenerEnabled: SplitClientConfigHelper.impressionListenerEnabled(configurationMap: configurationMap))))

splitWrapper = DefaultSplitWrapper(splitFactoryProvider: factoryProvider)
}

Expand Down
21 changes: 11 additions & 10 deletions ios/Classes/SwiftSplitioPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import UIKit

public class SwiftSplitioPlugin: NSObject, FlutterPlugin {

private var methodParser: SplitMethodParser?
private var methodParser: SplitMethodParser?

public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "splitio", binaryMessenger: registrar.messenger())
let instance = SwiftSplitioPlugin()
instance.methodParser = DefaultSplitMethodParser(methodChannel: channel)
registrar.addMethodCallDelegate(instance, channel: channel)
}
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "splitio", binaryMessenger: registrar.messenger())
let instance = SwiftSplitioPlugin()
let externalProvider: SplitFactoryProvider? = UIApplication.shared.delegate as? SplitFactoryProvider
instance.methodParser = DefaultSplitMethodParser(methodChannel: channel, splitFactoryProvider: externalProvider)
registrar.addMethodCallDelegate(instance, channel: channel)
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
methodParser?.onMethodCall(methodName: call.method, arguments: call.arguments, result: result)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
methodParser?.onMethodCall(methodName: call.method, arguments: call.arguments, result: result)
}
}