1+ package test .com .nihility ;
2+
3+ import static com .xiaomi .push .service .MIPushEventProcessor .postProcessMIPushMessage ;
4+ import static com .xiaomi .push .service .PullAllApplicationDataFromServerJob .getPullAction ;
5+ import static org .mockito .Mockito .verify ;
6+
7+ import android .content .Context ;
8+ import android .content .Intent ;
9+
10+ import androidx .test .core .app .ApplicationProvider ;
11+
12+ import com .elvishew .xlog .XLog ;
13+ import com .nihility .MethodHooker ;
14+ import com .nihility .MiPushEventListener ;
15+ import com .nihility .XMPushUtils ;
16+ import com .nihility .utils .MockMIPushMessage ;
17+ import com .xiaomi .channel .commonutils .reflect .JavaCalls ;
18+ import com .xiaomi .push .service .MIPushEventProcessor ;
19+ import com .xiaomi .push .service .MIPushNotificationHelper ;
20+ import com .xiaomi .push .service .XMPushService ;
21+ import com .xiaomi .xmpush .thrift .PushMetaInfo ;
22+ import com .xiaomi .xmpush .thrift .XmPushActionContainer ;
23+
24+ import org .aspectj .lang .ProceedingJoinPoint ;
25+ import org .junit .After ;
26+ import org .junit .Before ;
27+ import org .junit .Test ;
28+ import org .junit .runner .RunWith ;
29+ import org .mockito .Mock ;
30+ import org .mockito .junit .MockitoJUnitRunner ;
31+
32+ @ RunWith (MockitoJUnitRunner .class )
33+ public class MiPushEventListenerTest {
34+ @ Mock
35+ MiPushEventListener eventListener ;
36+ XmPushActionContainer container = XMPushUtils .packToContainer (getPullAction ("appId" ), "" );
37+
38+ @ Before
39+ public void setup () {
40+ XLog .init ();
41+ MiPushEventListener .setInstance (eventListener );
42+ MethodHooker .setInstance (new MethodHooker () {
43+ @ Override
44+ public boolean shouldSendBroadcast (ProceedingJoinPoint joinPoint , XMPushService pushService , String packageName , XmPushActionContainer container , PushMetaInfo metaInfo ) throws Throwable {
45+ return true ;
46+ }
47+ });
48+ }
49+
50+ @ After
51+ public void teardown () {
52+ MiPushEventListener .setInstance (null );
53+ MethodHooker .setInstance (null );
54+ }
55+
56+ @ Test
57+ public void triggerReceiveFromServerAtProcessMIPushMessage () {
58+ try {
59+ MockMIPushMessage .invokeProcessMiPushMessage (null , container );
60+ } catch (Throwable ignored ) {
61+ }
62+
63+ verify (eventListener ).receiveFromServer (container );
64+ }
65+
66+ @ Test
67+ public void triggerTransferToApplicationAtPostProcessMIPushMessageSendBroadcast () {
68+ XMPushService xmPushService = new XMPushService () {
69+ @ Override
70+ public Context getApplicationContext () {
71+ return ApplicationProvider .getApplicationContext ();
72+ }
73+
74+ @ Override
75+ public Context getBaseContext () {
76+ return ApplicationProvider .getApplicationContext ();
77+ }
78+ };
79+ byte [] payload = XMPushUtils .packToBytes (container );
80+ Intent intent = MIPushEventProcessor .buildIntent (payload , 0 );
81+
82+ postProcessMIPushMessage (xmPushService , null , payload , intent );
83+
84+ verify (eventListener ).transferToApplication (container );
85+ }
86+
87+ @ Test
88+ public void triggerTransferToApplicationAtNotifyPushMessage () {
89+ try {
90+ MIPushNotificationHelper .notifyPushMessage (null , container , null );
91+ } catch (Throwable ignored ) {
92+ }
93+
94+ verify (eventListener ).transferToApplication (container );
95+ }
96+
97+ @ Test
98+ public void triggerReceiveFromApplicationAtXMPushServiceOnStart () {
99+ XMPushService xmPushService = new XMPushService () {
100+ @ Override
101+ public void executeJob (Job job ) {
102+ }
103+ };
104+ Intent intent = new Intent ("test" );
105+
106+ xmPushService .onStart (intent , 1 );
107+
108+ verify (eventListener ).receiveFromApplication (intent );
109+ }
110+
111+ @ Test
112+ public void triggerTransferToServerAtSendMessage () {
113+ XMPushService xmPushService = new XMPushService ();
114+
115+ {
116+ Intent intent = new Intent ("test" );
117+
118+ JavaCalls .callMethod (xmPushService , "sendMessage" , intent );
119+
120+ verify (eventListener ).transferToServer (intent );
121+ }
122+ {
123+ Intent intent = new Intent ("qwe" );
124+
125+ JavaCalls .callMethod (xmPushService , "sendMessages" , intent );
126+
127+ verify (eventListener ).transferToServer (intent );
128+ }
129+ }
130+
131+ }
0 commit comments