Skip to content

Commit

Permalink
test: 改进广播相关测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
guok2046 authored and shifujun committed Dec 11, 2019
1 parent ffa99f7 commit cea18d3
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.tencent.shadow.test.cases.plugin_main;

import android.content.Intent;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.espresso.Espresso;
import androidx.test.espresso.action.ViewActions;
import androidx.test.espresso.matcher.ViewMatchers;

import org.hamcrest.Matchers;
import org.junit.Test;

/**
* 广播测试
*/
public class PluginBroadcastReceiverTest extends PluginMainAppTest {

private final static String INTENT_NORMAL_ACTION = "com.tencent.test.normal.action";
private final static String INTENT_DYNAMIC_ACTION = "com.tencent.test.action.DYNAMIC";

private final static String MSG_NORMAL = "收到测试静态广播发送";
private final static String MSG_DYNAMIC = "收到动态动态广播发送";


@Override
protected Intent getLaunchIntent() {
Intent pluginIntent = new Intent();
String packageName = ApplicationProvider.getApplicationContext().getPackageName();
pluginIntent.setClassName(
packageName,
"com.tencent.shadow.test.plugin.general_cases.lib.usecases.receiver.TestReceiverActivity"
);
return pluginIntent;
}

/**
* 动态广播测试
*/
@Test
public void testDynamicBroadcastReceiver(){
//测试动态广播可以正常收到,并且action,extra,以及context都是正确的
Espresso.onView(ViewMatchers.withTagValue(Matchers.<Object>is("button_dynamic"))).
perform(ViewActions.click());
matchTextWithViewTag("text",
String.format("action:%s msg:%s isShadowContext:%s",INTENT_DYNAMIC_ACTION,
MSG_DYNAMIC,"true"));

//测试动态广播反注册后就收不到广播了
Espresso.onView(ViewMatchers.withTagValue(Matchers.<Object>is("button_unRegister_dynamic"))).
perform(ViewActions.click());
matchTextWithViewTag("text", "");

}
/**
* 静态广播测试
*/
@Test
public void testStaticBroadcastReceiver(){
//测试静态广播可以正常收到,并且action,extra,以及context都是正确的
Espresso.onView(ViewMatchers.withTagValue(Matchers.<Object>is("button_static"))).
perform(ViewActions.click());

matchTextWithViewTag("text",
String.format("action:%s msg:%s isShadowContext:%s",INTENT_NORMAL_ACTION,
MSG_NORMAL,"true"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public List<BroadcastInfo> getBroadcastInfoList(String partKey) {
broadcastInfos.add(
new ComponentManager.BroadcastInfo(
"com.tencent.shadow.test.plugin.general_cases.lib.usecases.receiver.MyReceiver",
new String[]{"com.tencent.test.action"}
new String[]{"com.tencent.test.normal.action"}
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@

<activity android:name=".usecases.receiver.TestReceiverActivity" />

<activity android:name=".usecases.receiver.TestDynamicReceiverActivity" />

<activity android:name=".usecases.fragment.TestDynamicFragmentActivity" />

<activity android:name=".usecases.fragment.TestXmlFragmentActivity" />
Expand All @@ -53,7 +51,7 @@

<receiver android:name=".usecases.receiver.MyReceiver">
<intent-filter>
<action android:name="com.tencent.test.action"/>
<action android:name="com.tencent.test.normal.action"/>
</intent-filter>
</receiver>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.tencent.shadow.test.plugin.general_cases.lib.usecases.receiver;

import android.content.Context;
import android.content.Intent;

public class BroadCastHelper {


private static Notify notify;


public static void setNotify(Notify notify) {
BroadCastHelper.notify = notify;
}

public static void notify(Intent intent, Context context) {
notify.onReceiver(intent, context);
}

interface Notify {
void onReceiver(Intent intent, Context context);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ public class MyReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("msg");
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();

BroadCastHelper.notify(intent, context);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,96 @@
package com.tencent.shadow.test.plugin.general_cases.lib.usecases.receiver;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.tencent.shadow.test.plugin.general_cases.lib.R;
import com.tencent.shadow.test.plugin.general_cases.lib.gallery.util.ToastUtil;
import com.tencent.shadow.test.plugin.general_cases.lib.usecases.WithIdlingResourceActivity;

public class TestReceiverActivity extends Activity {
public class TestReceiverActivity extends WithIdlingResourceActivity {

private final static String INTENT_NORMAL_ACTION = "com.tencent.test.normal.action";
private final static String INTENT_DYNAMIC_ACTION = "com.tencent.test.action.DYNAMIC";

private final static String MSG_NORMAL = "收到测试静态广播发送";
private final static String MSG_DYNAMIC = "收到动态动态广播发送";


private TextView mTextView;

private BroadcastReceiver mBroadcastReceiver;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_receiver);
Button button = findViewById(R.id.button);
button.setText("测试静态广播发送");

button.setOnClickListener(new View.OnClickListener() {
mTextView = findViewById(R.id.content);

BroadCastHelper.setNotify(new BroadCastHelper.Notify() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.tencent.test.action");
intent.putExtra("msg", "收到测试静态广播发送");
sendBroadcast(intent);
public void onReceiver(Intent intent, Context context) {
mIdlingResource.setIdleState(true);

boolean isShadowContext = false;
try {
Class clazz = Class.forName("com.tencent.shadow.core.runtime.ShadowContext");
isShadowContext = clazz.isAssignableFrom(context.getClass());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
mTextView.setText(String.format("action:%s msg:%s isShadowContext:%s", intent.getAction(), intent.getStringExtra("msg"), isShadowContext));
}
});

mBroadcastReceiver = new DynamicBroadcastReceiver();
registerReceiver(mBroadcastReceiver, new IntentFilter(INTENT_DYNAMIC_ACTION));
}

public void TestNormalBraodcast(View view) {
mIdlingResource.setIdleState(false);
Intent intent = new Intent(INTENT_NORMAL_ACTION);
intent.putExtra("msg", MSG_NORMAL);
sendBroadcast(intent);
}

public void TestDynamicBraodcast(View view) {
mIdlingResource.setIdleState(false);
Intent intent = new Intent(INTENT_DYNAMIC_ACTION);
intent.putExtra("msg", MSG_DYNAMIC);
sendBroadcast(intent);
}

public void TestUnregisterDynamicBraodcast(View view){
mTextView.setText("");
unregisterReceiver(mBroadcastReceiver);
TestDynamicBraodcast(view);

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mIdlingResource.setIdleState(true);
}
},2000);
}

private class DynamicBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("msg");
ToastUtil.showToast(context, msg);

BroadCastHelper.notify(intent, context);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,48 @@


<Button
android:id="@+id/button"
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginLeft="16dp"
android:tag="button_static"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:onClick="TestNormalBraodcast"
android:text="测试静态广播发送" />


<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:tag="button_dynamic"
android:layout_marginBottom="8dp"
android:onClick="TestDynamicBraodcast"
android:text="测试动态广播发送" />

<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:tag="button_unRegister_dynamic"
android:layout_marginBottom="8dp"
android:onClick="TestUnregisterDynamicBraodcast"
android:text="测试动态广播注销" />


<TextView
android:id="@+id/content"
android:layout_marginTop="20dp"
android:tag="text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

0 comments on commit cea18d3

Please sign in to comment.