The Android unit test replaces the naked eye with a color comparison.

Source: Internet
Author: User

For unit tests of the Android UI class, some of the previously written code was sleep 10 seconds, and then the naked eye checked. This is not used in automated tests.

Today changes the code, in fact, the visual inspection is to check the color of the pixel, so you can directly get the color of a point of view, and then compare with the expected value of the line.

This is the code that gets the color of a pixel in the view:

     Public Static intGetColor (View view,intXinty) {intW =view.getwidth (); inth =view.getheight (); if(x >= 0 && x < w && y >= 0 && y <h) {Bitmap bmp=Bitmap.createbitmap (W, H, Bitmap.Config.ARGB_8888); Canvas Canvas=NewCanvas (BMP);            View.draw (canvas); returnbmp.getpixel (x, y); }        return0; }

To calculate the distance between four elements (RGBA) of two colors, you can use this method if you only need to have a similar color and not exactly the same:

     Public Static int getcolordistance (intint  color2) {        return (int) MATH.SQRT (                -color.red (Color2), 2)                + Math.pow (Color.green (Color1)-Color.green (Color2), 2)                + Math.pow (Color.Blue (Color1)-Color.Blue (Color2), 2)                + Math.pow (Color.alpha ( Color1)-Color.alpha (Color2), 2));    }

The Android unit test replaces the naked eye with a color comparison.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.