Giter Club home page Giter Club logo

testlocationmanager3's Introduction

Play with how Robolectric works concerned with shadows has type from higher API running in lower sdk

Setup

I intentionally used super low compileSdk, targetSdk

android {
  namespace = "com.example.testlocationmanager3"
  compileSdk = 22                 # intentional super low compile sdk

  defaultConfig {
    applicationId = "com.example.testlocationmanager3"
    minSdk = 21 
    targetSdk = 22                # intentional super low target sdk
    versionCode = 1
    versionName = "1.0"
  }
  ...
}

MainActivity

public class MainActivity extends android.app.Activity {

  LocationManager locationManager;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  }
}

MainActivityTest

@RunWith(RobolectricTestRunner.class)
public class MainActivityTest {

  @Test
  public void startup() {
    System.out.println("here packageName = " + RuntimeEnvironment.application.getPackageName());
    System.out.println("here apiLevel = " + RuntimeEnvironment.getApiLevel()); // output is "21"
    MainActivity activity = Robolectric.setupActivity(MainActivity.class);

    assertNotNull(activity.locationManager);
  }

  @Config(sdk = {22})
  @Test
  public void startup_config22() {
    System.out.println("here packageName = " + RuntimeEnvironment.application.getPackageName());
    System.out.println("here apiLevel = " + RuntimeEnvironment.getApiLevel()); // output is "22"
    MainActivity activity = Robolectric.setupActivity(MainActivity.class);

    assertNotNull(activity.locationManager);
  }

  @Config(sdk = {23})
  @Test
  public void startup_config23() {
    System.out.println("here packageName = " + RuntimeEnvironment.application.getPackageName());
    System.out.println("here apiLevel = " + RuntimeEnvironment.getApiLevel());  // output is "23"
    MainActivity activity = Robolectric.setupActivity(MainActivity.class);

    assertNotNull(activity.locationManager);
  }

  @Config(sdk = {26})
  @Test
  public void startup_config26() {
    System.out.println("here packageName = " + RuntimeEnvironment.application.getPackageName());
    System.out.println("here apiLevel = " + RuntimeEnvironment.getApiLevel());  // output is "26"
    MainActivity activity = Robolectric.setupActivity(MainActivity.class);

    assertNotNull(activity.locationManager);
  }
}

Run

In order to make the app build successfully, I removed dependencies on androidx.XXXXX (because androidx requires higher compileSdk), and the app successfully ran on my Pixel Android 12, API 31.

Pixel Android 12, API 31

Running on Android 12, API 31

MainActivityTest

I wrote several test functions here.

MainActivityTest#startup

api 21

MainActivityTest#startup_config22

api 22

MainActivityTest#startup_config23

api 23

MainActivityTest#startup_config26

api 26

Why PictureInPictureParams error in API < 26?

Because PictureInPictureParams is added in API 26, and ShadowActivity referenced PictureInPictureParams link

However, if I change compileSdk to 34, then all test functions in MainActivityTest will succeed when run one by one

android {
  namespace = "com.example.testlocationmanager3"
  compileSdk = 34                 # high API

  defaultConfig {
    applicationId = "com.example.testlocationmanager3"
    minSdk = 21 
    targetSdk = 22                # intentional super low target sdk
    versionCode = 1
    versionName = "1.0"
  }
  ...
}

Change targetSdk to high API test(s) will still fail

android {
  namespace = "com.example.testlocationmanager3"
  compileSdk = 22                 # intentional super low target sdk

  defaultConfig {
    applicationId = "com.example.testlocationmanager3"
    minSdk = 21 
    targetSdk = 34                # high API
    versionCode = 1
    versionName = "1.0"
  }
  ...
}

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.