1

我正在使用 JUnit4 测试运行程序测试我的应用程序,似乎导航抽屉切换被无缘无故地按下,因为我可以看到波纹出现。

我编写了一个测试,将视图绘制为位图并将其保存到存储中。这是图像的外观:http: //imgur.com/EURrE2R

如果将其显示在深色背景上,您会在图标周围看到一个波纹圆圈。

这是我在活动中运行相同代码时得到的结果:http: //imgur.com/KPz50nF

是什么原因造成的,我该如何摆脱它?

测试代码:

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

    private MainActivity activity;

    @Before
    public void setUp() throws Exception {
        activity = testRule.getActivity();

    }

    @Test
    public void testDrawerToggle()
            throws Exception {
        Toolbar toolbar = (Toolbar) activity.findViewById(R.id.toolbar);

        ArrayList<View> views = new ArrayList<>();
        toolbar.findViewsWithText(views, "Navigate up", View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);

        assertEquals(1, views.size());

        final View view = views.get(0);

        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
                Bitmap.Config.ARGB_8888);

        bitmap.setDensity(view.getResources().getDisplayMetrics().densityDpi);

        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);

        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/saved_images");
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-" + n + ".png";
        File file = new File(myDir, fname);
        if (file.exists()) file.delete();
        try {
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

活动代码:

public class MainActivity extends AppCompatActivity {

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        if(toolbar != null) {
            setSupportActionBar(toolbar);
        }

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_black);
    }
}

活动布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:minHeight="56dp"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/Theme.AppCompat.Light"/>

</RelativeLayout>
4

0 回答 0