小编azi*_*ian的帖子

单元测试协程 runBlockingTest:此作业尚未完成

请在下面找到一个使用协程替换回调的函数:

override suspend fun signUp(authentication: Authentication): AuthenticationError {
    return suspendCancellableCoroutine {
        auth.createUserWithEmailAndPassword(authentication.email, authentication.password)
            .addOnCompleteListener(activityLifeCycleService.getActivity()) { task ->
                if (task.isSuccessful) {
                    it.resume(AuthenticationError.SignUpSuccess)
                } else {
                    Log.w(this.javaClass.name, "createUserWithEmail:failure", task.exception)
                    it.resume(AuthenticationError.SignUpFail)
                }
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想对这个函数进行单元测试。我正在使用 Mockk :

  @Test
  fun `signup() must be delegated to createUserWithEmailAndPassword()`() = runBlockingTest {

      val listener = slot<OnCompleteListener<AuthResult>>()
      val authentication = mockk<Authentication> {
        every { email } returns "email"
        every { password } returns "pswd"
      }
      val task = mockk<Task<AuthResult>> {
        every { isSuccessful } returns true
      } …
Run Code Online (Sandbox Code Playgroud)

android unit-testing kotlin mockk kotlin-coroutines

37
推荐指数
6
解决办法
1万
查看次数

使用Android Studio/Gradle从git describe自动版本化Android项目

我已经进行了广泛的搜索,但可能由于Android Studio和Gradle的新功能,我还没有找到任何关于如何执行此操作的说明.我想基本上完成本文中描述的内容,但使用Android Studio,Gradle和Windows而不是Eclipse和Linux.

git android auto-versioning gradle android-build

36
推荐指数
5
解决办法
2万
查看次数

禁用应用程序的Android O自动填充服务

Android O具有支持字段自动填充的功能.有什么办法可以为特定的应用程序禁用它.那是我想强制我的应用程序不使用自动填充服务.

可能吗 ?

要阻止整个活动的自动填充,请在活动的onCreate()中使用:

getWindow()
  .getDecorView()
  .setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
Run Code Online (Sandbox Code Playgroud)

有没有比这更好的方法?

android autofill textview android-8.0-oreo android-autofill-manager

34
推荐指数
4
解决办法
2万
查看次数

左上角的ConstraintLayout视图

每当我创建类似ButtonTextView中的视图时ConstraintLayout,它们都会卡在顶角而不是我放置的位置.

我尝试创建新活动并更改模拟器,但结果仍然相同.

这是正在发生的事情的屏幕截图:

在此输入图像描述

可能是什么问题?

android android-layout android-view view-hierarchy android-constraintlayout

29
推荐指数
2
解决办法
3万
查看次数

Android - Google Play就像标签一样

Google刚刚在Google Play中为其标签添加了新外观.

我知道这可以通过ViewPagerIndicator完成,但我不想在我的应用程序中使用另一个库,并将应用程序的大小提高到另一个MB左右.

我目前正在使用android.support.v4.view.PagerTabStrip(就像在旧的Google Play中一样),我想知道是否也可以使用android支持库实现新外观.

提前致谢.

tabs android google-play

27
推荐指数
2
解决办法
3万
查看次数

ResourcesCompat.getDrawable()vs AppCompatResources.getDrawable()

我对这两个API有点困惑.

ResourcesCompat.getDrawable(Resources res,int id,Resources.Theme theme)

返回与特定资源ID关联的可绘制对象,并为指定的主题设置样式.将根据底层资源返回各种类型的对象 - 例如,纯色,PNG图像,可缩放图像等.

在API级别21之前,不会应用主题,此方法只需调用getDrawable(int).

AppCompatResources.getDrawable(Context context,int resId)

返回与特定资源ID关联的可绘制对象.

此方法支持在没有平台支持的设备上对矢量动画矢量资源进行充气.

  1. 这两个类之间有什么显着差异(除了矢量膨胀)?
  2. 我应该选择哪一个?为什么?

java android android-theme android-resources android-styles

26
推荐指数
3
解决办法
8226
查看次数

从android框架覆盖布局xml

问题

我想重写从Android的命名空间,例如布局文件R.layout.popup_menu_item_layout(这是从代码中引用com.android.internal.R.layout.popup_menu_item_layout).通过说覆盖,我假设xml在项目中声明一个文件,该文件将优先于框架拥有的布局.

请注意,这只是一个示例布局,因此问题涉及sdk/platforms/android-XX/data/res/layout目录中存在的每个布局.

我试过的

工具:覆盖

有一个未记录的 tools:override标签可用,它会覆盖特定的资源.请参阅答案以获取示例,该示例将覆盖设计支持库中的值,而不是来自Android框架的值.

应用于tools:override="true"布局的根标签不会生效.


XML布局参考 - refs.xml

正如在描述这个帖子,声明refs.xml文件/values/与以下内容目录:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="layout" name="activity_main">@layout/activity_second</item>
</resources>
Run Code Online (Sandbox Code Playgroud)

将参考activity_second.xml一次activity_main.xml使用.有一个答案建议使用这种技术来替代Snackbar布局.

这也不会生效.

有没有合法的方法来覆盖/替换android包中的布局文件?

xml android android-layout aapt android-resources

26
推荐指数
2
解决办法
4194
查看次数

更改浮动标签EditText和TextInputLayout的字体

有人试图改变浮标的字体吗?我更改了EditText的源代码,但浮动标签的字体没有改变,我非常感谢那些帮助我的人

码:

               <android.support.design.widget.TextInputLayout
                    android:id="@+id/tilTextoDescricao"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@id/tilValorUnidade"
                    android:layout_marginTop="10dp">

                    <EditText
                        android:id="@+id/etTextoDescricao"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:hint="Descrição"
                        android:textSize="15dp"
                        android:inputType="text" />

                </android.support.design.widget.TextInputLayout>

----------------- 

   etTextoDescricao= (EditText) findViewById(R.id.etTextoDescricao);
  etTextoDescricao.setTypeface(CustomTypeface.getTypefaceMediumDefault(this));
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

android android-edittext android-design-library android-textinputlayout material-components

25
推荐指数
6
解决办法
2万
查看次数

Dagger + Retrofit.在运行时添加auth头

我想知道Dagger是否有办法知道它应该在新数据可用时重新创建一个对象.

我所说的实例是我用于改造的请求标题.在某些时候(当用户登录时),我得到一个令牌,我需要将其添加到改造的标题中以进行经过身份验证的请求.问题是,我留下了相同的未经验证的改造版本.这是我的注射代码:

@Provides
    @Singleton
    OkHttpClient provideOkHttpClient(Cache cache) {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .cache(cache).build();
         client
                .newBuilder()
                .addInterceptor(
                    chain -> {
                        Request original = chain.request();
                        Request.Builder requestBuilder = original.newBuilder()
                                .addHeader("Accept", "Application/JSON");
                        Request request = requestBuilder.build();
                        return chain.proceed(request);
                    }).build();
        return client;
    }

  @Provides
    @Singleton
    Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) { 
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create())
                .baseUrl(mBaseUrl)
                .client(okHttpClient)
                .build();
        return retrofit;
}

@Provides
    @Singleton
    public NetworkService providesNetworkService(Retrofit retrofit) {
        return retrofit.create(NetworkService.class);
    }
Run Code Online (Sandbox Code Playgroud)

关于如何使这项工作的任何想法?

java android dagger dagger-2 retrofit2

25
推荐指数
2
解决办法
9624
查看次数

Espresso如何测试活动是否完成?

我想声明我正在测试的我的Acitivty在执行某些操作时已完成.不幸的是到目前为止,我只是通过在测试结束时添加一些睡眠来断言它.有没有更好的办法 ?

import android.content.Context;
import android.os.Build;
import android.support.test.rule.ActivityTestRule;
import android.test.suitebuilder.annotation.LargeTest;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import static org.junit.Assert.assertTrue;

@SuppressWarnings("unchecked")
@RunWith(JUnit4.class)
@LargeTest
public class MyActivityTest {

    Context context;

    @Rule
    public ActivityTestRule<MyActivity> activityRule
            = new ActivityTestRule(MyActivity.class, true, false);

    @Before
    public void setup() {
        super.setup();
        // ...
    }

    @Test
    public void finishAfterSomethingIsPerformed() throws Exception {

        activityRule.launchActivity(MyActivity.createIntent(context));

        doSomeTesting();

        activityRule.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                fireEventThatResultsInTheActivityToFinishItself();
            }
        });

        Thread.sleep(2000); // this is needed :(

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) …
Run Code Online (Sandbox Code Playgroud)

testing android android-activity android-testing android-espresso

23
推荐指数
4
解决办法
1万
查看次数