hello all I am starting to learn espresso testing as well as all kinds of testing and ive made a test as follows to test the navigation menu. when the activity starts it loads a fragment with a compass and reads the accelerometer and turns the compass and it is just stuck on that fragment until I open another fragment from the navigation menu and then the test executes. what I would like is for the test to execute right when the app starts. I will also add activity.
import android.support.test.espresso.contrib.DrawerActions;
import android.support.test.espresso.contrib.NavigationViewActions;
import android.support.test.espresso.intent.rule.IntentsTestRule;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public IntentsTestRule<MainActivity> activityRule = new IntentsTestRule<>(MainActivity.class);
@Test
public void test_navigation_view() throws InterruptedException {
//Thread.sleep(2000);
//intended(hasComponent(MainActivity.class.getName()));
onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
onView(withId(R.id.nav_view)).perform(NavigationViewActions.navigateTo(R.id.nav_slideshow));
Thread.sleep(1000);
onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
onView(withId(R.id.nav_view)).perform(NavigationViewActions.navigateTo(R.id.nav_send));
Thread.sleep(1000);
//onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
//onView(withId(R.id.nav_view)).perform(NavigationViewActions.navigateTo(R.id.nav_gallery));
//Thread.sleep(1000);
}
}
MainActivity.java
package navigation.celestial.apps.jacs.celestialnavigation;
import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ProgressBar;
import org.apache.commons.io.IOUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
String pre = PreferenceManager.getDefaultSharedPreferences(this).getString("transfer", "default");
if(pre.equals("default")){
new TransferFiles().execute();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Compas fragment = new Compas();
Bundle args = new Bundle();
fragment.setArguments(args);
// Insert the fragment by replacing any existing fragment
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.twobodyfix) {
fab.setVisibility(View.INVISIBLE);
TwoBodyFix fragment = new TwoBodyFix();
Bundle args = new Bundle();
fragment.setArguments(args);
// Insert the fragment by replacing any existing fragment
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
} else if (id == R.id.compassfrag) {
fab.setVisibility(View.VISIBLE);
Compas fragment = new Compas();
Bundle args = new Bundle();
fragment.setArguments(args);
// Insert the fragment by replacing any existing fragment
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
AlertDialog b;
AlertDialog.Builder dialogBuilder;
View dialogView;
public void showProgressDialog() {
dialogBuilder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
dialogView = inflater.inflate(R.layout.progress_dialog_layout, null);
dialogBuilder.setView(dialogView);
dialogBuilder.setCancelable(false);
b = dialogBuilder.create();
b.show();
}
public void hideProgressDialog(){
b.dismiss();
}
private class TransferFiles extends AsyncTask<String, Integer, Integer> {
ProgressBar progress;
@Override
protected void onPreExecute() {
super.onPreExecute();
showProgressDialog();
progress = (ProgressBar)dialogView.findViewById(R.id.progressBar2);
}
@Override
protected Integer doInBackground(String... params) {
Field[] fields = R.raw.class.getFields();
// loop for every file in raw folder
for(int count=0; count < fields.length; count++){
int rid = 0;
try {
rid = fields[count].getInt(fields[count]);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
// Use that if you just need the file name
String filename = fields[count].getName();
// Use this to load the file
try {
Resources res = getResources();
InputStream in = res.openRawResource(rid);
File file = new File(getFilesDir() + File.separator+ filename + ".xls");
OutputStream out = new FileOutputStream(file);
IOUtils.copy(in,out);
in.close();
out.close();
publishProgress(((count + 1)/fields.length)*100);
} catch (Exception e) {
// log error
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
progress.setProgress(values[0]);
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit().putString("transfer", "done").apply();
hideProgressDialog();
}
}
}
so to be clear when I run the test the app starts and nothing happens, if I back out of the app the test fails and says the no activity was started and if I switch to another fragment through the navigation menu it will then do the test and pass but not until then. what im trying to do is run the test the activity starts and goes through the test without interaction. thank you for your time
Aucun commentaire:
Enregistrer un commentaire