Skip to content

Commit

Permalink
Merge pull request #151 from deepueg/add-tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
deepueg authored Sep 22, 2020
2 parents a52a065 + 5591e00 commit ce6fe32
Show file tree
Hide file tree
Showing 9 changed files with 484 additions and 56 deletions.
12 changes: 9 additions & 3 deletions android/lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ android {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
multiDexEnabled true
}
}
lintOptions {
abortOnError false
Expand All @@ -27,7 +30,10 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
api ('com.walmartlabs.ern:container-movies-reloaded:1.0.0') {changing=true}
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
androidTestImplementation 'androidx.multidex:multidex:2.0.0'
androidTestImplementation 'androidx.test:core:1.3.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'com.google.truth:truth:1.0.1'
}
9 changes: 9 additions & 0 deletions android/lib/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.walmartlabs.ern.navigation.sample">

<application>
<activity android:name="com.ern.api.impl.BasicNavigationApiTest$SampleActivity" android:theme="@style/Theme.AppCompat"/>

</application>

</manifest>

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sample_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/holo_purple">

</LinearLayout>
4 changes: 4 additions & 0 deletions android/lib/src/androidTest/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="root_page_title">page 0</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ public void onDestroyView() {
mElectrodeReactFragmentDelegate.onDestroyView();
super.onDestroyView();
}

public String getReactComponentName() {
return mElectrodeReactFragmentDelegate.getReactComponentName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ElectrodeBaseFragmentDelegate<T extends ElectrodeBaseFragmentDelega
protected final C mFragmentConfig;

@Nullable
private ReactRootView mMiniAppView;
private View mMiniAppView;

private String mMiniAppComponentName = "NAME_NOT_SET_YET";

Expand Down Expand Up @@ -100,7 +100,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c

if (mMiniAppView == null) {
if (!TextUtils.isEmpty(mMiniAppComponentName)) {
mMiniAppView = (ReactRootView) mElectrodeActivityListener.createReactNativeView(mMiniAppComponentName, initialProps(savedInstanceState != null));
mMiniAppView = mElectrodeActivityListener.createReactNativeView(mMiniAppComponentName, initialProps(savedInstanceState != null));
} else {
Logger.i(TAG, "Missing miniAppComponentName inside arguments, will not create a MiniApp view.");
}
Expand Down Expand Up @@ -172,13 +172,13 @@ private Bundle initialProps(boolean isFragmentBeingReconstructed) {
}

public void refresh(@Nullable Bundle data) {
if (mMiniAppView != null) {
if (mMiniAppView instanceof ReactRootView) {
Bundle props = getDefaultProps();
addGlobalProps(props);
if (data != null) {
props.putAll(data);
}
mMiniAppView.setAppProperties(props);
((ReactRootView) mMiniAppView).setAppProperties(props);
} else {
Logger.w(TAG, "Refresh called on a null mMiniAppView. Should never reach here");
}
Expand Down Expand Up @@ -255,7 +255,7 @@ public void onDestroyView() {
View parentView = rootView.findViewById(mFragmentConfig.mReactViewContainerId);
if (parentView != null && mMiniAppView.getParent() == parentView) {
((ViewGroup) parentView).removeView(mMiniAppView);
Logger.v(TAG, "Removed MiniApp(%s) view from parent view", getReactComponentName());
Logger.v(TAG, "Removed MiniApp(%s) view from parent view", getReactComponentName());
}
}
}
Expand All @@ -265,8 +265,8 @@ public void onDestroyView() {
@CallSuper
public void onDestroy() {
Logger.v(TAG, "onDestroy(): " + getReactComponentName());
if (mMiniAppView != null) {
mElectrodeActivityListener.removeReactNativeView(mMiniAppComponentName, mMiniAppView);
if (mMiniAppView instanceof ReactRootView) {
mElectrodeActivityListener.removeReactNativeView(mMiniAppComponentName, (ReactRootView) mMiniAppView);
mMiniAppView = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,13 @@ private boolean updateNavBar(@Nullable Bundle arguments) {
NavigationBar navigationBar = NavUtils.getNavBar(arguments);
if (navigationBar != null) {
updateNavBar(navigationBar);
return true;
} else {
return false;
ActionBar actionBar = getSupportActionBar();
if(actionBar != null) {
applyDefaultUpNavigation(actionBar);
}
}
return true;
}
return false;
}
Expand Down Expand Up @@ -379,7 +382,7 @@ public interface OnOptionsMenuUpdatedListener {
void onOptionsMenuUpdated(@NonNull Menu menu, @NonNull MenuInflater inflater);
}

private void updateNavBar(@NonNull NavigationBar navigationBar) {
private void updateNavBar(@Nullable NavigationBar navigationBar) {
Logger.d(TAG, "Updating nav bar: %s", navigationBar);
ActionBar actionBar = getSupportActionBar();
if (actionBar == null) {
Expand Down Expand Up @@ -430,6 +433,10 @@ private void updateHomeAsUpIndicator(@Nullable NavigationBarLeftButton leftButto
}

//Default action
applyDefaultUpNavigation(actionBar);
}

private void applyDefaultUpNavigation(@NonNull ActionBar actionBar) {
if (mFragment.getArguments() != null) {
if (mFragment.getArguments().getBoolean(ActivityDelegateConstants.KEY_MINI_APP_FRAGMENT_SHOW_UP_ENABLED)) {
Logger.d(TAG, "Enabling up indicator for component: %s", getReactComponentName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,9 @@ public void onRequest(@Nullable final ErnNavRoute ernRoute, @NonNull final Elect
log("onRequest: NAVIGATE");
if (!validate(ernRoute, responseListener)) return;

if (validateLiveDataObservers()) {
final Bundle bundle = ernRoute.toBundle();
bundle.putString(KEY_NAV_TYPE, Type.NAVIGATE.toString());
post(bundle, responseListener);
} else {
throwNoFragmentOrActivityListenerError(ernRoute, responseListener);
}
final Bundle bundle = ernRoute.toBundle();
bundle.putString(KEY_NAV_TYPE, Type.NAVIGATE.toString());
post(bundle, responseListener);
}
};

Expand All @@ -67,48 +63,44 @@ public void onRequest(@Nullable final ErnNavRoute ernRoute, @NonNull final Elect
public void onRequest(@Nullable ErnNavRoute ernRoute, @NonNull final ElectrodeBridgeResponseListener<None> responseListener) {
log("onRequest: UPDATE");
if (!validate(ernRoute, responseListener)) return;
if (validateLiveDataObservers()) {
final Bundle bundle = ernRoute.toBundle();
bundle.putString(KEY_NAV_TYPE, Type.UPDATE.toString());
post(bundle, responseListener);
} else {
throwNoFragmentOrActivityListenerError(ernRoute, responseListener);
}
final Bundle bundle = ernRoute.toBundle();
bundle.putString(KEY_NAV_TYPE, Type.UPDATE.toString());
post(bundle, responseListener);
}
};

private final ElectrodeBridgeRequestHandler<ErnNavRoute, None> backRequestHandler = new ElectrodeBridgeRequestHandler<ErnNavRoute, None>() {
@Override
public void onRequest(@Nullable ErnNavRoute ernRoute, @NonNull final ElectrodeBridgeResponseListener<None> responseListener) {
log("onRequest: BACK");
if (validateLiveDataObservers()) {
final Bundle bundle = ernRoute != null ? ernRoute.toBundle() : new Bundle();
bundle.putString(KEY_NAV_TYPE, Type.BACK.toString());
post(bundle, responseListener);
} else {
throwNoFragmentOrActivityListenerError(ernRoute, responseListener);
}
final Bundle bundle = ernRoute != null ? ernRoute.toBundle() : new Bundle();
bundle.putString(KEY_NAV_TYPE, Type.BACK.toString());
post(bundle, responseListener);
}
};

private final ElectrodeBridgeRequestHandler<String, None> finishRequestHandler = new ElectrodeBridgeRequestHandler<String, None>() {
@Override
public void onRequest(@Nullable String payload, @NonNull final ElectrodeBridgeResponseListener<None> responseListener) {
log("onRequest: FINISH");
if (validateLiveDataObservers()) {
final Bundle bundle = new Bundle();
if (payload != null) {
bundle.putString("jsonPayload", payload);
}
bundle.putString(KEY_NAV_TYPE, Type.FINISH.toString());
post(bundle, responseListener);
} else {
throwNoFragmentOrActivityListenerError(null, responseListener);
final Bundle bundle = new Bundle();
if (payload != null) {
bundle.putString("jsonPayload", payload);
}
bundle.putString(KEY_NAV_TYPE, Type.FINISH.toString());
post(bundle, responseListener);
}
};

private void post(final Bundle bundle, @NonNull final ElectrodeBridgeResponseListener<None> responseListener) {
if (!routeLiveData.hasActiveObservers()) {
// No active observers indicates that the fragment or activity holding this view model instance is not in `RESUMED` state.
// We send a successful response in this case to avoid the bridge triggering a failure with a timeout.
// One drawback of this approach is that later when the fragment resumes and the request actually fails there is no way to notify the caller.
// In that case this response becomes a false positive.
responseListener.onSuccess(None.NONE);
}

Route route = new Route.Builder(bundle)
.routingNotifier(new RoutingNotifier() {
@Override
Expand All @@ -133,19 +125,6 @@ private boolean validate(@Nullable ErnNavRoute ernRoute, @NonNull ElectrodeBridg
return true;
}

/**
* Check to see if there is a live data observer currently active before posting a request.
*
* @return True | False
*/
private boolean validateLiveDataObservers() {
return routeLiveData.hasActiveObservers();
}

private void throwNoFragmentOrActivityListenerError(@Nullable ErnNavRoute ernRoute, @NonNull ElectrodeBridgeResponseListener<None> responseListener) {
responseListener.onFailure(BridgeFailureMessage.create("NAVIGATION_FAILED", "No activity or fragment is currently handling this navigation request: " + (ernRoute != null ? ernRoute.getPath() : "")));
}

private RequestHandlerHandle requestHandle;
private RequestHandlerHandle updateRequestHandle;
private RequestHandlerHandle backRequestHandle;
Expand Down

0 comments on commit ce6fe32

Please sign in to comment.