测试开始前未调用OnGlobalLayoutListener

时间:2019-05-06 11:30:50

标签: android unit-testing layout setcontentview android-viewtreeobserver

我有一些junit andorid测试代码:

  @Rule
  public final ActivityScenarioRule<AppCompatActivity> activityScenarioRule =
      new ActivityScenarioRule<>(AppCompatActivity.class);

  StandAloneAccountMenuView<FakeAccount> standAloneSupportSuperG = createAccountMenuView();
int mySize;

    @Before
      public void setUp() {
        activityScenarioRule
            .getScenario()
            .onActivity(
                activity -> activity.setContentView(myView));

        myView
            .getViewTreeObserver()
            .addOnGlobalLayoutListener(
                new OnGlobalLayoutListener() {
                  @Override
                  public void onGlobalLayout() {
                    mySize = myView.getHeaderView().getHeight();
                    //removeOnGlobalLayoutListener
                  }
                });
        activityScenarioRule.getScenario().moveToState(State.RESUMED);
      }

但是我的测试代码在执行mySize = myView.getHeaderView().getHeight();之前运行

在测试运行之前如何获得myView.getHeaderView().getHeight();

0 个答案:

没有答案