vendredi 3 juillet 2020

Espresso: test if activity is started

So, as from the title, I tried testing if an android activity is started. So far, in some different ways I found here on SO, namely:

None of which worked for me, the test runs for a while, then fails. It's important to note that:

  • The intent I want to intercept actually comes from a Fragment(HomeFragment) within MainActivity.
  • The Activity that is opened (the 'target' activity), contains a com.google.android.gms.maps.SupportMapFragment.
  • When I click the button (R.id.btnSearch) in the Fragment, the activity starts, it's just that the test won't pass.
  • By using a ActivityLifecycleMonitorRegistry, I can see that MainActivity, goes all the way to Stage.RESUMED, while MapsActivity hangs on Stage.ON_PRE_CREATE.
  • I have a Service doing background network operations and ShopsDatabase also does (on another Thread).

I did set up an IntentsTestRule:

@Rule
public IntentsTestRule<MainActivity> activityRule = new IntentsTestRule<>(MainActivity.class);

This is the test I'm trying to execute:

@Test
public void testStartsMapsActivity() {
    onView(withId(R.id.btnSearch)).perform(click());
    intended(hasComponent(MapsActivity.class.getName()));
}

I made sure that I'm using the correct dependencies (app/build.gradle):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }

}

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android.defaultConfig.vectorDrawables.useSupportLibrary = true

dependencies {
    // FirebaseUI for Firebase Realtime Database
    implementation 'com.firebaseui:firebase-ui-database:6.2.1'
    // FirebaseUI for Cloud Firestore
    implementation 'com.firebaseui:firebase-ui-firestore:6.2.1'
    // FirebaseUI for Firebase Auth
    implementation 'com.firebaseui:firebase-ui-auth:6.2.1'
    // FirebaseUI for Cloud Storage
    implementation 'com.firebaseui:firebase-ui-storage:6.2.1'
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'com.google.android.gms:play-services-auth:18.0.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'org.jetbrains:annotations:15.0'
    implementation 'com.google.android.material:material:1.3.0-alpha01'
    implementation 'com.google.firebase:firebase-auth:19.3.1'
    implementation 'com.google.firebase:firebase-database:19.3.1'
    implementation 'com.google.firebase:firebase-firestore:21.4.3'
    implementation 'com.google.firebase:firebase-analytics:17.4.3'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test:rules:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
    implementation 'org.jetbrains:annotations:15.0'
    implementation 'androidx.annotation:annotation:1.1.0'
    implementation 'org.jetbrains:annotations:15.0'
}

This is the Fragment that starts the target Activity:

    public class HomeFragment extends Fragment {

    private Spinner mSpinner;
    private Button btnSearch;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }
    
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mSpinner = getView().findViewById(R.id.mySpinner);

        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(), R.array.planets_array, android.R.layout.simple_spinner_item);

        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // Apply the adapter to the spinner
        mSpinner.setAdapter(adapter);

        btnSearch = view.findViewById(R.id.btnSearch);
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Model.getInstance().setCurrentItem((String) adapter.getItem(mSpinner.getSelectedItemPosition()));
                showOnMap();
            }
        });
    }

    public void showOnMap() {
        Objects.requireNonNull(getContext()).
                startService(new Intent(getContext(), DataPollService.class));

        Intent sendDataToMapIntent = new Intent(getActivity(), MapsActivity.class);
        startActivity(sendDataToMapIntent);
    }
}

This is the target Activity:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    private GoogleMap mMap;

    private List<Shop> shops = new ArrayList<>();
    private LocationManager locationManager;
    private LocationListener locationListener;
    private LatLng userPosition;

    private Model m = Model.getInstance();
    private Map<Integer, Marker> shopsMarkers = new HashMap<>();

    private String productID = m.getCurrentSelection();
    private ShopsDatabase shopsDB = new ShopsDatabase();

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            }
        }
    }

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

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);

        assert mapFragment != null;
        mapFragment.getMapAsync(this);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    }

    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                userPosition = new LatLng(location.getLatitude(), location.getLongitude());
                shopsDB.update();
                shops = shopsDB.filterFor(productID);

                mMap.clear();
                mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(MapsActivity.this));

  
                for (int i = 0; i < shops.size(); i++){
                    Marker shopMarker = createMarker(shops.get(i).getLat(), shops.get(i).getLng());
                    
                    shopsMarkers.put(Integer.parseInt(shops.get(i).getId()), shopMarker);
                }

                mMap.addMarker(new MarkerOptions().position(userPosition).title(getResources().getString(R.string.marker_here)));
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userPosition, 13));
                locationManager.removeUpdates(this);
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };

        if(Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            } else {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            }
        } else {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        }
    }

    protected Marker createMarker(double latitude, double longitude) {
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(new LatLng(latitude, longitude));

        markerOptions.icon(bitmapDescriptorFromVector(getApplicationContext(), R.drawable.shop));
        return mMap.addMarker(markerOptions);
    }

     /**
     * Changing marker Icon
     */
    private BitmapDescriptor bitmapDescriptorFromVector(Context context, @DrawableRes  int vectorDrawableResourceId) {
        Drawable background = ContextCompat.getDrawable(context, vectorDrawableResourceId);
        background.setBounds(0, 0, background.getIntrinsicWidth(), background.getIntrinsicHeight());
        Drawable vectorDrawable = ContextCompat.getDrawable(context, vectorDrawableResourceId);
        vectorDrawable.setBounds(40, 20, vectorDrawable.getIntrinsicWidth() + 40, vectorDrawable.getIntrinsicHeight() + 20);
        Bitmap bitmap = Bitmap.createBitmap(background.getIntrinsicWidth(), background.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        background.draw(canvas);
        vectorDrawable.draw(canvas);
        return BitmapDescriptorFactory.fromBitmap(bitmap);
    }

}

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire