Coder Social home page Coder Social logo

Comments (5)

gpeal avatar gpeal commented on May 5, 2024 1

I've gotten this sort of working by creating these helper methods:

    public void setConfigurationToNexus5() {
        hasSetConfiguration = true;
        Configuration config = getActivity().getResources().getConfiguration();
        DisplayMetrics dm = getActivity().getResources().getDisplayMetrics();
        dm.widthPixels = 1080;
        dm.heightPixels = 1920;
        dm.density = 3f;
        dm.scaledDensity = 3f;
        dm.densityDpi = 480;
        dm.xdpi = 480;
        dm.ydpi = 480;
        setNoncompatSizeFields(dm);
        config.orientation = Configuration.ORIENTATION_PORTRAIT;
        config.densityDpi = 480;
        config.screenWidthDp = 360;
        config.screenHeightDp = 567;
        config.smallestScreenWidthDp = 360;
        config.screenLayout = 268435538;

        getActivity().getResources().updateConfiguration(config, dm);
    }

    public void setConfigurationToNexus9() {

        hasSetConfiguration = true;
        Resources res = getActivity().getResources();
        Configuration config = res.getConfiguration();
        DisplayMetrics dm = res.getDisplayMetrics();
        dm.widthPixels = 2048;
        dm.heightPixels = 1440;
        dm.density = 2f;
        dm.scaledDensity = 2f;
        dm.densityDpi = 320;
        dm.xdpi = 320;
        dm.ydpi = 320;
        setNoncompatSizeFields(dm);
        config.orientation = Configuration.ORIENTATION_PORTRAIT;
        config.densityDpi = 320;
        config.screenWidthDp = 1024;
        config.screenHeightDp = 695;
        config.smallestScreenWidthDp = 768;
        config.screenLayout = 268435540;
        res.updateConfiguration(config, dm);
    }

    public void setConfigurationToNexus9Landscape() {
        setConfigurationToNexus9();
        Resources res = getActivity().getResources();
        Configuration config = res.getConfiguration();
        config.orientation = Configuration.ORIENTATION_LANDSCAPE;
        res.updateConfiguration(config, res.getDisplayMetrics());
    }

However, it seems like some things like bitmaps and drawables are cached by the asset manager so there are slight differences when running on different devices and what these test renders vs their respective devices.

from screenshot-tests-for-android.

phil-lopreiato avatar phil-lopreiato commented on May 5, 2024 1

I've gotten testing with multiple devices to work with some gradle hacks. I modified recordDir to have a suffix specific to the current device attached in adb (device codename + API version), that way recorded screenshots from different devices won't conflict with each other. It's pretty hacky and basic, but is decent enough for my use.

project.afterEvaluate {
    def pullScreenshots = project.tasks.getByName("pullScreenshots")
    pullScreenshots.dependsOn updateScreenshotDirs
}

task updateScreenshotDirs << {
    def codeNameOut = new ByteArrayOutputStream()
    exec {
        commandLine 'adb', 'shell', 'getprop', 'ro.product.name'
        standardOutput codeNameOut
    }

    def sdkOut = new ByteArrayOutputStream()
    exec {
        commandLine 'adb', 'shell', 'getprop', 'ro.build.version.sdk'
        standardOutput sdkOut
    }
    def deviceName = codeNameOut.toString().trim()
    def deviceSdk = sdkOut.toString().trim()
    def outputDir = project.screenshots.recordDir + "/" + deviceName + "_api" + deviceSdk
    project.screenshots.recordDir = outputDir
    System.out.println("New screenshot output dierctory: " + outputDir)
}

from screenshot-tests-for-android.

tdrhq avatar tdrhq commented on May 5, 2024

We're trying to figure out the best way to do this internally too. In a record-verify style environment, multiple devices can have a lot of extra load on developers to keep the screenshots in sync. (Not so much of an issue with automatic recording on the server which we do internally at Facebook but haven't realeased the tooling for yet.)

In the meantime, it's possible to simulate some parts of a tablet configuration in a non-tablet run using this method: http://developer.android.com/reference/android/content/res/Resources.html#updateConfiguration(android.content.res.Configuration,%20android.util.DisplayMetrics)

We don't really use this for this purpose, although we do use this for switching Locales within a single test run, so I do know this is pretty powerful. Just make sure to clean up any configuration in your tearDown because it sets things globally as opposed to just the current Context.

Let me know if this helps, and I'll update this task once I have a better story for multiple devices.

from screenshot-tests-for-android.

tdrhq avatar tdrhq commented on May 5, 2024

Do you know where/what these caches are in AssetManager? I'm sure it should be technically possible to invalidate those caches with the hammer of reflection

from screenshot-tests-for-android.

xiphirx avatar xiphirx commented on May 5, 2024

Merging into a more up-to-date issue #112

from screenshot-tests-for-android.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.