From 4f7605b302e4deaf6382bc30909af2a559a806fe Mon Sep 17 00:00:00 2001 From: "hui.zhao" Date: Thu, 12 Mar 2020 22:20:05 +0800 Subject: [PATCH] unit test --- .../PageLifecycleMethodEventTypes.java | 48 +++---- .../godeye/core/utils/ProcessUtils.java | 4 - .../hikyson/godeye/core/GodEyeHelperTest.java | 132 ++++++++++++++---- .../core/helper/Test1NotV4Fragment.java | 34 +++++ .../helper/Test2NotV4FragmentActivity.java | 24 ++++ .../PageLifecycleMethodEventTypesTest.java | 56 ++++++++ .../cn/hikyson/godeye/core/utils/LTest.java | 26 ++++ .../godeye/core/utils/ReflectUtilTest.java | 10 ++ 8 files changed, 281 insertions(+), 53 deletions(-) create mode 100644 android-godeye/src/test/java/cn/hikyson/godeye/core/helper/Test1NotV4Fragment.java create mode 100644 android-godeye/src/test/java/cn/hikyson/godeye/core/helper/Test2NotV4FragmentActivity.java create mode 100644 android-godeye/src/test/java/cn/hikyson/godeye/core/internal/modules/pageload/PageLifecycleMethodEventTypesTest.java diff --git a/android-godeye/src/main/java/cn/hikyson/godeye/core/internal/modules/pageload/PageLifecycleMethodEventTypes.java b/android-godeye/src/main/java/cn/hikyson/godeye/core/internal/modules/pageload/PageLifecycleMethodEventTypes.java index 732edba7..28762ee5 100644 --- a/android-godeye/src/main/java/cn/hikyson/godeye/core/internal/modules/pageload/PageLifecycleMethodEventTypes.java +++ b/android-godeye/src/main/java/cn/hikyson/godeye/core/internal/modules/pageload/PageLifecycleMethodEventTypes.java @@ -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; @@ -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 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 sPairsOfLifecycleAndMethods; static { sPairsOfLifecycleAndMethods = new HashMap<>(); diff --git a/android-godeye/src/main/java/cn/hikyson/godeye/core/utils/ProcessUtils.java b/android-godeye/src/main/java/cn/hikyson/godeye/core/utils/ProcessUtils.java index 8b53d9da..ee468666 100644 --- a/android-godeye/src/main/java/cn/hikyson/godeye/core/utils/ProcessUtils.java +++ b/android-godeye/src/main/java/cn/hikyson/godeye/core/utils/ProcessUtils.java @@ -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; diff --git a/android-godeye/src/test/java/cn/hikyson/godeye/core/GodEyeHelperTest.java b/android-godeye/src/test/java/cn/hikyson/godeye/core/GodEyeHelperTest.java index 4b1d5a57..2f4e3a33 100644 --- a/android-godeye/src/test/java/cn/hikyson/godeye/core/GodEyeHelperTest.java +++ b/android-godeye/src/test/java/cn/hikyson/godeye/core/GodEyeHelperTest.java @@ -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; @@ -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().moduleObservable(GodEye.ModuleName.PAGELOAD).test(); - GodEyeHelper.onPageLoaded(new Activity()); - GodEyeHelper.onPageLoaded(new Fragment()); + TestObserver testObserver = GodEye.instance().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() { + @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() { + @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)); } } @@ -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 activityController = Robolectric.buildActivity(Test2NotV4FragmentActivity.class).create().start().resume(); + activity = activityController.get(); + testObserver = GodEye.instance().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 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() { + @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 @@ -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 @@ -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 activityController = Robolectric.buildActivity(Test2Activity.class).create().start().resume(); - Test2Activity activity = activityController.get(); - TestObserver testObserver = GodEye.instance().moduleObservable(GodEye.ModuleName.PAGELOAD).test(); - Test1Fragment fragment = activity.getTest1Fragment(); - + activity = activityController.get(); + testObserver = GodEye.instance().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 testPageEvents = new ArrayList<>(); testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_CREATE, 1)); testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_ATTACH, 1)); @@ -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() { @Override @@ -306,6 +387,7 @@ public boolean test(PageLifecycleEventInfo o) throws Exception { } } + @Test public void onFragmentPageVisibilityChangeWhenIllegal() { try { diff --git a/android-godeye/src/test/java/cn/hikyson/godeye/core/helper/Test1NotV4Fragment.java b/android-godeye/src/test/java/cn/hikyson/godeye/core/helper/Test1NotV4Fragment.java new file mode 100644 index 00000000..1710976f --- /dev/null +++ b/android-godeye/src/test/java/cn/hikyson/godeye/core/helper/Test1NotV4Fragment.java @@ -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(); + } +} diff --git a/android-godeye/src/test/java/cn/hikyson/godeye/core/helper/Test2NotV4FragmentActivity.java b/android-godeye/src/test/java/cn/hikyson/godeye/core/helper/Test2NotV4FragmentActivity.java new file mode 100644 index 00000000..17184ed6 --- /dev/null +++ b/android-godeye/src/test/java/cn/hikyson/godeye/core/helper/Test2NotV4FragmentActivity.java @@ -0,0 +1,24 @@ +package cn.hikyson.godeye.core.helper; + +import android.os.Bundle; + +import androidx.annotation.Nullable; +import androidx.fragment.app.FragmentActivity; + +public class Test2NotV4FragmentActivity extends FragmentActivity { + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + getFragmentManager().beginTransaction().add(new Test1NotV4Fragment(), Test1NotV4Fragment.class.getName()).commit(); + } + + public Test1NotV4Fragment getTest1NotV4Fragment() { + return (Test1NotV4Fragment) getFragmentManager().findFragmentByTag(Test1NotV4Fragment.class.getName()); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + } +} diff --git a/android-godeye/src/test/java/cn/hikyson/godeye/core/internal/modules/pageload/PageLifecycleMethodEventTypesTest.java b/android-godeye/src/test/java/cn/hikyson/godeye/core/internal/modules/pageload/PageLifecycleMethodEventTypesTest.java new file mode 100644 index 00000000..d31997dd --- /dev/null +++ b/android-godeye/src/test/java/cn/hikyson/godeye/core/internal/modules/pageload/PageLifecycleMethodEventTypesTest.java @@ -0,0 +1,56 @@ +package cn.hikyson.godeye.core.internal.modules.pageload; + +import android.os.Build; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +import cn.hikyson.godeye.core.helper.RoboTestApplication; +import cn.hikyson.methodcanary.lib.MethodEvent; + +@RunWith(RobolectricTestRunner.class) +@Config(sdk = Build.VERSION_CODES.LOLLIPOP, application = RoboTestApplication.class) +public class PageLifecycleMethodEventTypesTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void isMatch() { + Assert.assertTrue(PageLifecycleMethodEventTypes.isMatch(ActivityLifecycleEvent.ON_CREATE, ActivityLifecycleEvent.ON_CREATE)); + Assert.assertFalse(PageLifecycleMethodEventTypes.isMatch(FragmentLifecycleEvent.ON_CREATE, ActivityLifecycleEvent.ON_CREATE)); + } + + private MethodEvent mock(String methodName, String methodDesc) { + return new MethodEvent("class0", 1, methodName, methodDesc, true, 0, 0); + } + + @Test + public void convert() { + Assert.assertNull(PageLifecycleMethodEventTypes.convert(PageType.ACTIVITY, mock("class0", "desc0"))); + Assert.assertNull(PageLifecycleMethodEventTypes.convert(PageType.ACTIVITY, mock("onCreate", "(Landroid/os/Bundle;)"))); + Assert.assertEquals(ActivityLifecycleEvent.ON_CREATE, PageLifecycleMethodEventTypes.convert(PageType.ACTIVITY, mock("onCreate", "(Landroid/os/Bundle;)V"))); + Assert.assertEquals(FragmentLifecycleEvent.ON_CREATE, PageLifecycleMethodEventTypes.convert(PageType.FRAGMENT, mock("onCreate", "(Landroid/os/Bundle;)V"))); + } + + @Test + public void equalMethodInfoForLifecycle() { + PageLifecycleMethodEventTypes.MethodInfoForLifecycle methodInfoForLifecycle0 = new PageLifecycleMethodEventTypes.MethodInfoForLifecycle(PageType.ACTIVITY, "class0", "method0"); + PageLifecycleMethodEventTypes.MethodInfoForLifecycle methodInfoForLifecycle1 = new PageLifecycleMethodEventTypes.MethodInfoForLifecycle(PageType.ACTIVITY, "class0", "method0"); + Assert.assertTrue(methodInfoForLifecycle0.equals(methodInfoForLifecycle1)); + Assert.assertTrue(methodInfoForLifecycle0.equals(methodInfoForLifecycle0)); + Assert.assertFalse(methodInfoForLifecycle0.equals(null)); + Assert.assertNotNull(methodInfoForLifecycle0.toString()); + } + +} \ No newline at end of file diff --git a/android-godeye/src/test/java/cn/hikyson/godeye/core/utils/LTest.java b/android-godeye/src/test/java/cn/hikyson/godeye/core/utils/LTest.java index eab148e0..e0425429 100644 --- a/android-godeye/src/test/java/cn/hikyson/godeye/core/utils/LTest.java +++ b/android-godeye/src/test/java/cn/hikyson/godeye/core/utils/LTest.java @@ -124,6 +124,32 @@ public void onRuntimeException(RuntimeException e) { L.e(new Exception("AndroidGodEye-Exception")); } + @Test + public void w() { + L.setProxy(new L.LogProxy() { + @Override + public void d(String msg) { + Assert.fail(); + } + + @Override + public void w(String msg) { + Assert.assertFalse(msg.isEmpty()); + } + + @Override + public void e(String msg) { + Assert.fail(); + } + + @Override + public void onRuntimeException(RuntimeException e) { + Assert.fail(); + } + }); + L.w(new Object()); + } + @Test public void onRuntimeException() { L.setProxy(new L.LogProxy() { diff --git a/android-godeye/src/test/java/cn/hikyson/godeye/core/utils/ReflectUtilTest.java b/android-godeye/src/test/java/cn/hikyson/godeye/core/utils/ReflectUtilTest.java index 64635637..2bf8c7b7 100644 --- a/android-godeye/src/test/java/cn/hikyson/godeye/core/utils/ReflectUtilTest.java +++ b/android-godeye/src/test/java/cn/hikyson/godeye/core/utils/ReflectUtilTest.java @@ -31,6 +31,9 @@ public void invokeStaticMethod() { Object result2 = ReflectUtil.invokeStaticMethod(ReflectUtilTest.class.getName(), "testMethod2", new Class[]{String.class, int.class, boolean.class}, new Object[]{"AndroidGodEye-String", 10, false}); Assert.assertNull(result2); + Object result25 = ReflectUtil.invokeStaticMethod(ReflectUtilTest.class, "testMethod", + new Class[]{String.class, int.class, boolean.class}, new Object[]{"AndroidGodEye-String", "10", false}); + Assert.assertNull(result25); try { Object result3 = ReflectUtil.invokeStaticMethodUnSafe(ReflectUtilTest.class.getName(), "testMethod", new Class[]{String.class, int.class, boolean.class}, new Object[]{"AndroidGodEye-String", 10, false}); @@ -44,5 +47,12 @@ public void invokeStaticMethod() { Assert.fail(); } catch (Exception ignore) { } + try { + Object result4 = ReflectUtil.invokeStaticMethodUnSafe("FakeClass", "testMethod", + new Class[]{String.class, int.class}, new Object[]{"AndroidGodEye-String", 10}); + Assert.fail(); + } catch (Exception ignore) { + } + } } \ No newline at end of file