Skip to content

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
hui.zhao committed Mar 12, 2020
1 parent 04b4f26 commit 4f7605b
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

import cn.hikyson.methodcanary.lib.MethodEvent;

public class PageLifecycleMethodEventTypes {
class PageLifecycleMethodEventTypes {

@Keep
public static class MethodInfoForLifecycle implements Serializable {
static class MethodInfoForLifecycle implements Serializable {
PageType pageType;
String methodName;
String methodDesc;

public MethodInfoForLifecycle(PageType pageType, String methodName, String methodDesc) {
MethodInfoForLifecycle(PageType pageType, String methodName, String methodDesc) {
this.pageType = pageType;
this.methodName = methodName;
this.methodDesc = methodDesc;
}

public MethodInfoForLifecycle(PageType pageType, MethodEvent methodEvent) {
MethodInfoForLifecycle(PageType pageType, MethodEvent methodEvent) {
this.pageType = pageType;
this.methodName = methodEvent.methodName;
this.methodDesc = methodEvent.methodDesc;
Expand Down Expand Up @@ -60,26 +60,26 @@ public int hashCode() {
}


static MethodInfoForLifecycle ON_ACTIVITY_CREATE = new MethodInfoForLifecycle(PageType.ACTIVITY, "onCreate", "(Landroid/os/Bundle;)V");
static MethodInfoForLifecycle ON_ACTIVITY_START = new MethodInfoForLifecycle(PageType.ACTIVITY, "onStart", "()V");
static MethodInfoForLifecycle ON_ACTIVITY_RESUME = new MethodInfoForLifecycle(PageType.ACTIVITY, "onResume", "()V");
static MethodInfoForLifecycle ON_ACTIVITY_PAUSE = new MethodInfoForLifecycle(PageType.ACTIVITY, "onPause", "()V");
static MethodInfoForLifecycle ON_ACTIVITY_STOP = new MethodInfoForLifecycle(PageType.ACTIVITY, "onStop", "()V");
static MethodInfoForLifecycle ON_ACTIVITY_SAVE_INSTANCE_STATE = new MethodInfoForLifecycle(PageType.ACTIVITY, "onSaveInstanceState", "(Landroid/os/Bundle;)V");
static MethodInfoForLifecycle ON_ACTIVITY_DESTORY = new MethodInfoForLifecycle(PageType.ACTIVITY, "onDestroy", "()V");
static MethodInfoForLifecycle ON_FRAGMENT_ATTACH = new MethodInfoForLifecycle(PageType.FRAGMENT, "onAttach", "(Landroid/app/Activity;)V");
static MethodInfoForLifecycle ON_FRAGMENT_CREATE = new MethodInfoForLifecycle(PageType.FRAGMENT, "onCreate", "(Landroid/os/Bundle;)V");
static MethodInfoForLifecycle ON_FRAGMENT_VIEW_CREATE = new MethodInfoForLifecycle(PageType.FRAGMENT, "onCreateView", "(Landroid/view/LayoutInflater;Landroid/view/ViewGroup;Landroid/os/Bundle;)Landroid/view/View;");
static MethodInfoForLifecycle ON_FRAGMENT_START = new MethodInfoForLifecycle(PageType.FRAGMENT, "onStart", "()V");
static MethodInfoForLifecycle ON_FRAGMENT_RESUME = new MethodInfoForLifecycle(PageType.FRAGMENT, "onResume", "()V");
static MethodInfoForLifecycle ON_FRAGMENT_PAUSE = new MethodInfoForLifecycle(PageType.FRAGMENT, "onPause", "()V");
static MethodInfoForLifecycle ON_FRAGMENT_STOP = new MethodInfoForLifecycle(PageType.FRAGMENT, "onStop", "()V");
static MethodInfoForLifecycle ON_FRAGMENT_SAVE_INSTANCE_STATE = new MethodInfoForLifecycle(PageType.FRAGMENT, "onSaveInstanceState", "(Landroid/os/Bundle;)V");
static MethodInfoForLifecycle ON_FRAGMENT_VIEW_DESTROY = new MethodInfoForLifecycle(PageType.FRAGMENT, "onDestroyView", "()V");
static MethodInfoForLifecycle ON_FRAGMENT_DESTROY = new MethodInfoForLifecycle(PageType.FRAGMENT, "onDestory", "()V");
static MethodInfoForLifecycle ON_FRAGMENT_DETACH = new MethodInfoForLifecycle(PageType.FRAGMENT, "onDetach", "()V");

static Map<MethodInfoForLifecycle, LifecycleEvent> sPairsOfLifecycleAndMethods;
private static MethodInfoForLifecycle ON_ACTIVITY_CREATE = new MethodInfoForLifecycle(PageType.ACTIVITY, "onCreate", "(Landroid/os/Bundle;)V");
private static MethodInfoForLifecycle ON_ACTIVITY_START = new MethodInfoForLifecycle(PageType.ACTIVITY, "onStart", "()V");
private static MethodInfoForLifecycle ON_ACTIVITY_RESUME = new MethodInfoForLifecycle(PageType.ACTIVITY, "onResume", "()V");
private static MethodInfoForLifecycle ON_ACTIVITY_PAUSE = new MethodInfoForLifecycle(PageType.ACTIVITY, "onPause", "()V");
private static MethodInfoForLifecycle ON_ACTIVITY_STOP = new MethodInfoForLifecycle(PageType.ACTIVITY, "onStop", "()V");
private static MethodInfoForLifecycle ON_ACTIVITY_SAVE_INSTANCE_STATE = new MethodInfoForLifecycle(PageType.ACTIVITY, "onSaveInstanceState", "(Landroid/os/Bundle;)V");
private static MethodInfoForLifecycle ON_ACTIVITY_DESTORY = new MethodInfoForLifecycle(PageType.ACTIVITY, "onDestroy", "()V");
private static MethodInfoForLifecycle ON_FRAGMENT_ATTACH = new MethodInfoForLifecycle(PageType.FRAGMENT, "onAttach", "(Landroid/app/Activity;)V");
private static MethodInfoForLifecycle ON_FRAGMENT_CREATE = new MethodInfoForLifecycle(PageType.FRAGMENT, "onCreate", "(Landroid/os/Bundle;)V");
private static MethodInfoForLifecycle ON_FRAGMENT_VIEW_CREATE = new MethodInfoForLifecycle(PageType.FRAGMENT, "onCreateView", "(Landroid/view/LayoutInflater;Landroid/view/ViewGroup;Landroid/os/Bundle;)Landroid/view/View;");
private static MethodInfoForLifecycle ON_FRAGMENT_START = new MethodInfoForLifecycle(PageType.FRAGMENT, "onStart", "()V");
private static MethodInfoForLifecycle ON_FRAGMENT_RESUME = new MethodInfoForLifecycle(PageType.FRAGMENT, "onResume", "()V");
private static MethodInfoForLifecycle ON_FRAGMENT_PAUSE = new MethodInfoForLifecycle(PageType.FRAGMENT, "onPause", "()V");
private static MethodInfoForLifecycle ON_FRAGMENT_STOP = new MethodInfoForLifecycle(PageType.FRAGMENT, "onStop", "()V");
private static MethodInfoForLifecycle ON_FRAGMENT_SAVE_INSTANCE_STATE = new MethodInfoForLifecycle(PageType.FRAGMENT, "onSaveInstanceState", "(Landroid/os/Bundle;)V");
private static MethodInfoForLifecycle ON_FRAGMENT_VIEW_DESTROY = new MethodInfoForLifecycle(PageType.FRAGMENT, "onDestroyView", "()V");
private static MethodInfoForLifecycle ON_FRAGMENT_DESTROY = new MethodInfoForLifecycle(PageType.FRAGMENT, "onDestory", "()V");
private static MethodInfoForLifecycle ON_FRAGMENT_DETACH = new MethodInfoForLifecycle(PageType.FRAGMENT, "onDetach", "()V");

private static Map<MethodInfoForLifecycle, LifecycleEvent> sPairsOfLifecycleAndMethods;

static {
sPairsOfLifecycleAndMethods = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ public class ProcessUtils {
private static volatile String sProcessName;
private final static Object sNameLock = new Object();

private ProcessUtils() {
throw new InstantiationError("Must not instantiate this class");
}

public static String myProcessName(Context context) {
if (sProcessName != null) {
return sProcessName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import cn.hikyson.godeye.core.helper.Test1Activity;
import cn.hikyson.godeye.core.helper.Test1Fragment;
import cn.hikyson.godeye.core.helper.Test2Activity;
import cn.hikyson.godeye.core.helper.Test2NotV4FragmentActivity;
import cn.hikyson.godeye.core.helper.TestPageEvent;
import cn.hikyson.godeye.core.helper.ThreadHelper;
import cn.hikyson.godeye.core.internal.modules.methodcanary.MethodCanaryConfig;
Expand Down Expand Up @@ -96,23 +97,34 @@ public void onPageLoadedNotInstalled() {
}

@Test
public void onPageLoadedNoResultWhenNotCreated() {
public void onPageLoadedWhenNotCreated() {
GodEye.instance().uninstall();
GodEye.instance().install(GodEyeConfig.noneConfigBuilder().withPageloadConfig(new PageloadConfig()).build());
try {
TestObserver testObserver = GodEye.instance().<Pageload, PageLifecycleEventInfo>moduleObservable(GodEye.ModuleName.PAGELOAD).test();
GodEyeHelper.onPageLoaded(new Activity());
GodEyeHelper.onPageLoaded(new Fragment());
TestObserver<PageLifecycleEventInfo> testObserver = GodEye.instance().<Pageload, PageLifecycleEventInfo>moduleObservable(GodEye.ModuleName.PAGELOAD).test();
Activity activity = new Activity();
GodEyeHelper.onPageLoaded(activity);
Fragment fragment = new Fragment();
GodEyeHelper.onPageLoaded(fragment);
Shadows.shadowOf(ThreadUtil.obtainHandler("godeye-pageload").getLooper()).getScheduler().advanceToNextPostedRunnable();
testObserver.await(1, TimeUnit.SECONDS);
testObserver.assertNoValues();
fail();
testObserver.assertValueAt(0, new Predicate<PageLifecycleEventInfo>() {
@Override
public boolean test(PageLifecycleEventInfo o) throws Exception {
return activity.hashCode() == o.pageInfo.pageHashCode
&& ActivityLifecycleEvent.ON_LOAD.equals(o.currentEvent.lifecycleEvent);
}
}).assertValueAt(1, new Predicate<PageLifecycleEventInfo>() {
@Override
public boolean test(PageLifecycleEventInfo o) throws Exception {
return fragment.hashCode() == o.pageInfo.pageHashCode
&& FragmentLifecycleEvent.ON_LOAD.equals(o.currentEvent.lifecycleEvent);
}
});
} catch (UninstallException e) {
fail();
} catch (InterruptedException e) {
fail();
} catch (IllegalStateException ignore) {
// will throw new IllegalStateException(String.format("Page [%s] Lifecycle [%s] must start with ActivityLifecycleEvent.ON_CREATE or FragmentLifecycleEvent.ON_ATTACH", pageInfo, pageLifecycleEventLine.lifecycleEvent));
}
}

Expand Down Expand Up @@ -227,6 +239,69 @@ public boolean test(PageLifecycleEventInfo o) throws Exception {
}
}

/**
* Test2NotV4FragmentActivityON_CREATE
* Test2NotV4FragmentActivityON_START
* Test2NotV4FragmentActivityON_DRAW
* Test2NotV4FragmentActivityON_RESUME
* Test1NotV4FragmentON_SHOW
* Test1NotV4FragmentON_HIDE
* Test2NotV4FragmentActivityON_LOAD
* Test1NotV4FragmentON_LOAD
* Test2NotV4FragmentActivityON_PAUSE
* Test2NotV4FragmentActivityON_STOP
* Test2NotV4FragmentActivityON_DESTROY
*/
@Test
public void onFragmentPageVisibilityChangeSuccess() {
try {
GodEye.instance().uninstall();
GodEye.instance().install(GodEyeConfig.noneConfigBuilder().withPageloadConfig(new PageloadConfig()).build());
Activity activity = null;
Object fragment = null;
TestObserver testObserver = null;
ActivityController<Test2NotV4FragmentActivity> activityController = Robolectric.buildActivity(Test2NotV4FragmentActivity.class).create().start().resume();
activity = activityController.get();
testObserver = GodEye.instance().<Pageload, PageLifecycleEventInfo>moduleObservable(GodEye.ModuleName.PAGELOAD).test();
fragment = ((Test2NotV4FragmentActivity) activity).getTest1NotV4Fragment();
GodEyeHelper.onFragmentPageVisibilityChange(fragment, true);
Thread.sleep(100);
GodEyeHelper.onFragmentPageVisibilityChange(fragment, false);
GodEyeHelper.onPageLoaded(activity);
GodEyeHelper.onPageLoaded(fragment);
activityController.pause().stop().destroy();
Shadows.shadowOf(ThreadUtil.obtainHandler("godeye-pageload").getLooper()).getScheduler().advanceToNextPostedRunnable();
List<TestPageEvent> testPageEvents = new ArrayList<>();
testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_CREATE, 1));
testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_START, 2));
testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_DRAW, 3));
testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_RESUME, 4));
testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_SHOW, 1));
testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_HIDE, 2));
testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_LOAD, 5));
testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_LOAD, 3));
testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_PAUSE, 6));
testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_STOP, 7));
testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_DESTROY, 8));
testObserver.assertValueCount(11);
for (int i = 0; i < 11; i++) {
int finalI = i;
testObserver.assertValueAt(i, new Predicate<PageLifecycleEventInfo>() {
@Override
public boolean test(PageLifecycleEventInfo o) throws Exception {
return testPageEvents.get(finalI).pageHashCode == o.pageInfo.pageHashCode
&& testPageEvents.get(finalI).allEventSize == o.allEvents.size()
&& testPageEvents.get(finalI).lifecycleEvent.equals(o.currentEvent.lifecycleEvent);
}
});
}
} catch (UninstallException e) {
fail();
} catch (InterruptedException e) {
fail();
}
}

/**
* ACTIVITY,Test2Activity,ON_CREATE
* FRAGMENT,Test1Fragment,ON_ATTACH
Expand All @@ -240,6 +315,8 @@ public boolean test(PageLifecycleEventInfo o) throws Exception {
* FRAGMENT,Test1Fragment,ON_RESUME
* FRAGMENT,Test1Fragment,ON_SHOW
* FRAGMENT,Test1Fragment,ON_HIDE
* ACTIVITY,Test2Activity,ON_LOAD
* FRAGMENT,Test1Fragment,ON_LOAD
* ACTIVITY,Test2Activity,ON_PAUSE
* FRAGMENT,Test1Fragment,ON_PAUSE
* ACTIVITY,Test2Activity,ON_STOP
Expand All @@ -250,22 +327,24 @@ public boolean test(PageLifecycleEventInfo o) throws Exception {
* FRAGMENT,Test1Fragment,ON_DETACH
*/
@Test
public void onFragmentPageVisibilityChangeSuccess() {
public void onFragmentPageVisibilityChangeSuccessV4() {
try {
GodEye.instance().uninstall();
GodEye.instance().install(GodEyeConfig.noneConfigBuilder().withPageloadConfig(new PageloadConfig()).build());
Activity activity = null;
Object fragment = null;
TestObserver testObserver = null;
ActivityController<Test2Activity> activityController = Robolectric.buildActivity(Test2Activity.class).create().start().resume();
Test2Activity activity = activityController.get();
TestObserver testObserver = GodEye.instance().<Pageload, PageLifecycleEventInfo>moduleObservable(GodEye.ModuleName.PAGELOAD).test();
Test1Fragment fragment = activity.getTest1Fragment();

activity = activityController.get();
testObserver = GodEye.instance().<Pageload, PageLifecycleEventInfo>moduleObservable(GodEye.ModuleName.PAGELOAD).test();
fragment = ((Test2Activity) activity).getTest1Fragment();
GodEyeHelper.onFragmentPageVisibilityChange(fragment, true);
Thread.sleep(100);
GodEyeHelper.onFragmentPageVisibilityChange(fragment, false);

GodEyeHelper.onPageLoaded(activity);
GodEyeHelper.onPageLoaded(fragment);
activityController.pause().stop().destroy();
Shadows.shadowOf(ThreadUtil.obtainHandler("godeye-pageload").getLooper()).getScheduler().advanceToNextPostedRunnable();

List<TestPageEvent> testPageEvents = new ArrayList<>();
testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_CREATE, 1));
testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_ATTACH, 1));
Expand All @@ -279,16 +358,18 @@ public void onFragmentPageVisibilityChangeSuccess() {
testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_RESUME, 6));
testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_SHOW, 7));
testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_HIDE, 8));
testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_PAUSE, 5));
testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_PAUSE, 9));
testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_STOP, 6));
testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_STOP, 10));
testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_DESTROY, 7));
testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_VIEW_DESTROY, 11));
testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_DESTROY, 12));
testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_DETACH, 13));
testObserver.assertValueCount(20);
for (int i = 0; i < 20; i++) {
testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_LOAD, 5));
testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_LOAD, 9));
testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_PAUSE, 6));
testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_PAUSE, 10));
testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_STOP, 7));
testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_STOP, 11));
testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_DESTROY, 8));
testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_VIEW_DESTROY, 12));
testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_DESTROY, 13));
testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_DETACH, 14));
testObserver.assertValueCount(22);
for (int i = 0; i < 22; i++) {
int finalI = i;
testObserver.assertValueAt(i, new Predicate<PageLifecycleEventInfo>() {
@Override
Expand All @@ -306,6 +387,7 @@ public boolean test(PageLifecycleEventInfo o) throws Exception {
}
}


@Test
public void onFragmentPageVisibilityChangeWhenIllegal() {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cn.hikyson.godeye.core.helper;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

public class Test1NotV4Fragment extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = new View(inflater.getContext());
return view;
}

@Override
public void onDestroyView() {
super.onDestroyView();
}

@Override
public void onDestroy() {
super.onDestroy();
}
}
Loading

0 comments on commit 4f7605b

Please sign in to comment.