Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add robolectric basic sample #146

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

fbertault
Copy link

Add example tests using robolectric. Copied the espresso BasicSample as is, with some string substitutions.

This project uses the Gradle build system. You don't need an IDE to build and execute it but Android Studio is recommended.

1. Download the project code, preferably using `git clone`.
1. Open the Android SDK Manager (*Tools* Menu | *Android*) and make sure you have installed the *Android Support Repository* under *Extras*. (For more Information click [here](http://developer.android.com/tools/testing-support-library/index.html#setup))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This step is not necessary for Robolectric... it might not be necessary for instrumentation tests now since we're in gMaven @slinzner ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

compileSdkVersion 26
buildToolsVersion rootProject.buildToolsVersion
defaultConfig {
applicationId "com.example.android.testing.robolectric.BasicSample"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think application id's usually have capital letters in do they? Is this the pattern in the other samples?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it matches the other examples (it matches the package name which is uppercase).

@Test
public void createActivityFromIntent() {
Intent intent = new Intent(RuntimeEnvironment.application, MainActivity.class);
Robolectric.buildActivity(MainActivity.class, intent).setup().get();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really asserting anything, for this to be valuable you really want to make some assertions, like, have the activity render some data in the test fields that you specified in the intent for example.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.


@Test
public void pauseResumeActivity() {
ActivityController<MainActivity> controller = Robolectric.buildActivity(MainActivity.class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like this kind of test as it is not demonstrating any behaviour.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

public void setUp() {
Intent intent = new Intent(RuntimeEnvironment.application, ShowTextActivity.class);
intent.putExtra(ShowTextActivity.KEY_EXTRA_MESSAGE, MESSAGE);
activity = Robolectric.buildActivity(ShowTextActivity.class, intent).setup().get();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Robolectric.setupActivity()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it works with an intent does it?

Copy link
Contributor

@slinzner slinzner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM. Please add licenses to all the files and make sure you use the correct date (2017) on the license.

@@ -0,0 +1,69 @@
/*
* Copyright 2015, The Android Open Source Project
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2017

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2015 The Android Open Source Project
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2017

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

-->
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light"/>
</resources>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add newline

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -0,0 +1,37 @@
package com.example.android.testing.robolectric.BasicSample;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

license missing

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

ext {
buildToolsVersion = "25.0.0"
supportLibVersion = "25.4.0"
runnerVersion = "1.0.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

projects.conf Outdated
@@ -1,5 +1,6 @@
integration/ServiceTestRuleSample
runner/AndroidJunitRunnerSample
ui/robolectric/BasicSample
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be under unit/ not ui/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to unit/BasicRobolectricSample.

@slinzner slinzner requested a review from JoseAlcerreca October 2, 2017 18:56
lintOptions {
abortOnError false
}
testOptions {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment to indicate that this is required in order for Robolectric to work

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

ext {
buildToolsVersion = "25.0.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update this to the latest version, which I guess should be 26.0.0

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

@slinzner slinzner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the changes. Please run the ./test_all.sh script before submitting to make sure everything still works.

@fbertault
Copy link
Author

Checked ./test_all.sh : ALL TESTS PASS

@mrk-han
Copy link
Contributor

mrk-han commented Nov 14, 2018

Curious if all of the tests passed on Oct 6, 2017, why was this never merged in?

@codingjeremy codingjeremy changed the base branch from master to main September 29, 2020 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants