gpt4 book ai didi

android - 空闲资源在 android 的第二个 Activity 中不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 08:22:41 26 4
gpt4 key购买 nike

我有两个Activity A 和B。我正在运行 500 毫秒的后台线程并更新 TextViewTextView 更新后,我单击 TextView 转到 B Activity。在 Activity B 中,我有另一个运行 500 毫秒的后台线程,并在 B Activity 中更新一个 TextView。我正在使用 Espresso 测试此流程。我等待 后台线程 使用 Idling Resource 完成执行。我在 B Activity 中使用 Idling Resource 时遇到问题。我把我的代码写在下面:

MainActivityTest.java:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> mainActivityActivityTestRule = new ActivityTestRule<>(MainActivity.class);

private IdlingResource mIdlingResource;

@Before
public void registerIdlingResource(){
mIdlingResource = mainActivityActivityTestRule.getActivity().getIdlingResource();
// To prove that the test fails, omit this call:
// Espresso.registerIdlingResources(mIdlingResource);
IdlingRegistry.getInstance().register(mIdlingResource);
}

@Test
public void mainScreenLoads(){
onView(withId(R.id.my_text)).check(matches(ViewMatchers.withText("Boom")));
onView(withId(R.id.my_text)).perform(click());
onView(withId(R.id.second_text)).check(matches(ViewMatchers.withText("Boom")));
}

@After
public void unregisterIdlingResource() {
if (mIdlingResource != null) {
// Espresso.unregisterIdlingResources(mIdlingResource);
IdlingRegistry.getInstance().unregister(mIdlingResource);
}
}
}

Activity :

public class MainActivity extends AppCompatActivity {

CountingIdlingResource countingIdlingResource = new CountingIdlingResource("DATA_LOADER");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
protected void onResume() {
super.onResume();

countingIdlingResource.increment();
new Thread(new Runnable() {
@Override
public void run() {
try{
Thread.sleep(500);
}catch (InterruptedException e){
e.printStackTrace();
}

runOnUiThread(new Runnable() {
@Override
public void run() {
((TextView)findViewById(R.id.my_text)).setText("Boom");
((TextView)findViewById(R.id.my_text)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
countingIdlingResource.decrement();
}
});
}
}).start();
}

/**
* Only called from test, creates and returns a new {@link SimpleIdlingResource}.
*/
@VisibleForTesting
@NonNull
public IdlingResource getIdlingResource() {
return countingIdlingResource;
}
}

B Activity :

public class SecondActivity extends AppCompatActivity {

CountingIdlingResource countingIdlingResource = new CountingIdlingResource("DATA_LOADER");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}

@Override
protected void onResume() {
super.onResume();
countingIdlingResource.increment();
new Thread(new Runnable() {
@Override
public void run() {
try{
Thread.sleep(500);
}catch (InterruptedException e){
e.printStackTrace();
}

runOnUiThread(new Runnable() {
@Override
public void run() {
((TextView)findViewById(R.id.second_text)).setText("Boom");
// if (mIdlingResource != null) {
// mIdlingResource.setIdleState(true);
// }
countingIdlingResource.decrement();
}
});
}
}).start();
}
}

我遇到以下错误:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "Boom"' doesn't match the selected view.
Expected: with text: is "Boom"
Got: "AppCompatTextView{id=2131165282, res-name=second_text, visibility=VISIBLE, width=246, height=51, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@94bfcec, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Second Screen, input-type=0, ime-target=false, has-links=false}"

at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:1566)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:90)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:52)
at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:314)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:291)
at com.example.sagarsuri.rxjavademo.MainActivityTest.mainScreenLoads(MainActivityTest.java:47)

最佳答案

您可以使用单例类并从那里控制空闲资源。

class EspressoKIdlingResource {
companion object {
val countingIdlingResource = CountingIdlingResource("data_loaded")

fun getInstance() : CountingIdlingResource{
return countingIdlingResource
}

fun increment(){
countingIdlingResource.increment()
}

fun decrement(){
countingIdlingResource.decrement()
}
}

关于android - 空闲资源在 android 的第二个 Activity 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48899952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com