Skip to content

Instantly share code, notes, and snippets.

@parahall

parahall/Description.txt

Last active Sep 20, 2016
Embed
What would you like to do?
Imagine that you, and your new Pixel device, have gone back in time to 1980.
Lucky for you, your friend Marty mcFly, the developer of "WatchStarWars" application, and you're just about to watch "Episode V", you're about to watch it for the 1st time.
WatchStarWars app has a single activity, WatchStarwarsActivity.
It's an activity with one button "Say Something", that when clicked, logs an interesting fact about the movie to the log, a fact that, if you haven't watched Episode V, you might find to be a spoiler.
In its OnCreate method, WatchStarwarsActivity posts-delayed a "Finished Watching Starwars" log message for x milliseconds.
Marty loves technology. He uses Timber for injecting a testable log into the activity with Dagger. He is also a very pragmatic developer - he tests his app with Espresso, with this scenario:
Open the activity
Click the button
Inspect the logs.
Marty put X as delay and expects the log result to be similar to this output:
1: TAG: StarWarsNewbie MSG: Finished Watching Starwars
2: TAG: StarWarsNewbie MSG: OMG! [Insert Spoiler Here]
but sadly, he gets it the other way around:
1: TAG: StarWarsNewbie MSG: OMG! [Insert Spoiler Here]
2: TAG: StarWarsNewbie MSG: Finished Watching Starwars (Spoiled)
Question is Why, and how to solve it?
Or, slightly hinted question:
How long should the movie be so that Marty doesn't spoil it? And how to solve it if the movie is longer?
Or, Very hinted question:
What value of x (the postDelayed's delayMillis) causes this behavior, and how to avoid it, given that we can't control George Lucas?
package com.example.felix.droidconil.droidconilriddle;
import ...;
public class WatchStarwarsActivity extends AppCompatActivity {
private static final String TAG = "Star Wars Newbie";
private boolean isSpoiled = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_watch_starwars);
long delay = ...;
Handler handler = new Handler();
Runnable watchTheMovie =new Runnable(){
public void run(){
try {
if (isSpoiled) {
Timber.i(TAG, "Finished Watching Starwars.");
} else {
Timber.e(TAG, "Finished Watching Starwars, Spoiled.");
}
}
catch (Exception e) {
// We don' really care, we'e watching StarWars!
}
}
};
handler.postDelayed(watchTheMovie, delay);
}
public void saySomething(View view) {
Timber.i(TAG, "OMG, So that guy is actually... OMG!");
isSpoiled = true;
}
}
The Test:
@Test
public void watchStarwarsActivityTest(){
Espresso.onView(withText("Watch Starwars")).perform(ViewActions.click());
// Inspect Logs Here.
}
@bouhady

This comment has been minimized.

Copy link

@bouhady bouhady commented Sep 20, 2016

Runnable watchTheMovie =new Runnable(){
public void run(){
try {
if (WatchStarwarsActivity.this.isSpoiled) {
Timber.i(TAG, "Finished Watching Starwars.");
} else {
Timber.e(TAG, "Finished Watching Starwars, Spoiled.");
}
}
catch (Exception e) {
// We don' really care, we'e watching StarWars!
}
}
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment