简体   繁体   English

使用 FragmentScenario 时的样式问题

[英]Styling issue when using FragmentScenario

When using FragmentScenario from androidx.fragment:fragment-testing to test fragment UI, not all styles are applied correctly.使用androidx.fragment:fragment-testing中的FragmentScenario测试片段 UI 时,并非所有 styles 都正确应用。

As an example, there's very simple application that just presents fragment with following layout:例如,有一个非常简单的应用程序,它只显示具有以下布局的片段:

fragment_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

with default theme generated by Android Studio (4.1.2) that looks like this when running the app:由 Android Studio (4.1.2) 生成的默认主题在运行应用程序时如下所示:

在此处输入图像描述

doesn't look the same when run as a FragmentScenario test:作为FragmentScenario测试运行时看起来不一样:

在此处输入图像描述

I can understand that ActionBar is not shown (as it is a part of host Activity not a Fragment ).我可以理解ActionBar没有显示(因为它是主机Activity的一部分而不是Fragment )。 But why Button is not styled correctly ...??但是为什么Button的样式不正确...??

As others have commented FragmentScenario will launch your Fragment in a EmptyFragmentActivity (defined within the FragmentScenario class) by default this uses the R.style.FragmentScenarioEmptyFragmentActivityTheme .正如其他人评论的那样,默认情况下, FragmentScenario将在EmptyFragmentActivity (在FragmentScenario类中定义)中启动您的Fragment ,这使用R.style.FragmentScenarioEmptyFragmentActivityTheme

To ensure our tests use the correct theme we supply our theme Theme_App to the themeResId param (as well as any Arguments) and it renders correctly.为了确保我们的测试使用正确的主题,我们将主题Theme_App提供给themeResId参数(以及任何参数)并正确呈现。

val scenario: FragmentScenario<MyFragment> = launchFragmentInContainer(
            themeResId = style.Theme_App,
            fragmentArgs = args
        )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 必须使用父活动引用时,如何使用“FragmentScenario”单独测试 Fragment - How to test Fragment in isolation using `FragmentScenario` when you have to use parent activity reference 使用 FragmentScenario 测试片段时如何测试与菜单的交互 - How to test interactions with menu when testing fragments with FragmentScenario Robolectric 测试无法使用 FragmentScenario 和数据绑定找到框架布局 ID - Robolectric test fails to find frame layout id using FragmentScenario and Databinding 有没有一种简单的方法可以在使用FragmentScenario的测试中注入匕首模拟? - Is there a simple way to inject dagger mocks in tests using FragmentScenario? 如何在使用 FragmentScenario 的 Android 仪器测试中预期错误? - How can I expect errors in my Android instrumentation test using FragmentScenario? DialogFragment 的 FragmentScenario,未调用 onCreateDialog - FragmentScenario of DialogFragment, onCreateDialog not called FragmentScenario 必须实现 OnFragmentInteractionListener - FragmentScenario must implement OnFragmentInteractionListener FragmentController 与 FragmentScenario - FragmentController vs FragmentScenario 从 FragmentController 移动到 FragmentScenario - Moving from FragmentController to FragmentScenario 如何使用 FragmentScenario 测试 DaggerFragment? - How to test DaggerFragment with FragmentScenario?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM