Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Building, Testing and reversing

Android Building, Testing and reversing

Slides of the Talk at the Romobos 2013, Cluj Napoca

Enrique López Mañas

November 21, 2013
Tweet

More Decks by Enrique López Mañas

Other Decks in Programming

Transcript

  1. Agenda: -Intro -Android Testing -Creating a project -JUnit Testing -Mock

    Objects + UI Testing -Integration and acceptance test -Performance and system testing -Advance assertions -TDD -Real App Dev !
  2. Ego slide Mobile Developer @ Sixt M. Sc. UCM/RWTH CS

    Teacher at Alcalá University ! ! ! +EnriqueLópezMañas @eenriquelopez
  3. What you need • Java SE Version 1.6 • Android

    SDK Tools • Eclipse IDE for Java Developers • ADT • (Android Bundle)
  4. Is about productivity! $59.5 billion annually ! 1/3 could be

    saved ! (National Institute of Standards and Technology (USA))
  5. Why, what, how, when During, before ! Continuous Integration !

    Classes, methods, lines, basic blocks..
  6. Elements of a test The fixture Method setUp() Method tearDown()

    Test preconditions (retrospection vs. annotations)
  7. Mock objects Mimic objects • MockApplication • MockContentProvider • MockContentResolver

    • MockContext • MockCursor • MockDialogInterface • MockPackageManager • MockResources
  8. Android Testing Best practice: test should live in a separate

    correlated project • Stripped (not included in main) • Easier to run in Emulator • Takes less time • Encourages code reusability
  9. AVD

  10. Running from the command line • All • From an

    specific test case • Specific test by name • By category • (create custom annotations)
  11. Summary • Created first Android project • Followed best practice

    creating companion project • Simple test class • Eclipse • Command line options
  12. UI Testing • Android SDK Tools, 21+ • API 16+

    • uiaumatorviewer • uiautomator Tools
  13. Performance tests Performance behavior System test • GUI Test •

    Smoke Tests • Performance • Installation
  14. Benchmarking • Traditional logging statement methods • Creating Android performance

    tests • Using profiling tools • Microbenchmarks using Caliper
  15. Benchmarking /* (non-Javadoc) * @see android.text.TextWatcher#onTextChanged( * java.lang.CharSequence, int, int,

    int) */ public void onTextChanged(CharSequence s, int start, int before, int count) { if (!mDest.hasWindowFocus() || mDest.hasFocus() || s == null ) { return; } final String str = s.toString(); if ( "".equals(str) ) { mDest.setText(""); return; } final long t0; if ( BENCHMARK_TEMPERATURE_CONVERSION ) { t0 = System.currentTimeMillis(); } !
  16. Benchmarking try { final double temp = Double.parseDouble(str); final double

    result = (mOp == OP.C2F) ? TemperatureConverter.celsiusToFarenheit(temp); TemperatureConverter.fahrenheitToCelsius(temp); final String resultString = String.format("%.2f", result); mDest.setNumber(result); mDest.setSelection(resultString.length()); } catch (NumberFormatException e) { // WARNING // this is generated while a number is entered, // for example just a '-' // so we don't want to show the error } catch (Exception e) { mSource.setError("ERROR: " + e.getLocalizedMessage()); } if ( BENCHMARK_TEMPERATURE_CONVERSION ) { long t = System.currentTimeMillis() - t0; Log.i(TAG, "TemperatureConversion took " + t + " ms to complete."); } }
  17. Assertions in depth • assertEquals • assertFalse • assertNotNull •

    assertNotSame • assertNull • assertSame • assertTrue • assertFail
  18. Custom messages public void testMax() {
 final int a =

    1;
 final int b = 2;
 final int expected = b;
 final int actual = Math.max(a, b);
 assertEquals("Expection " + expected + " but was " + actual, expected, actual); }
  19. Static imports public void testAlignment() { final int margin =

    0; ... android.test.ViewAsserts.assertRightAligned( mMessage, mCapitalize, margin); ! }
  20. Example public void testUserInterfaceLayout() {
 final int margin = 0;


    final View origin = mActivity.getWindow().getDecorView(); assertOnScreen(origin, mMessage); assertOnScreen(origin, mCapitalize); assertRightAligned(mMessage, mCapitalize, margin); }
  21. Example @UiThreadTest public void testNoErrorInCapitalization() { final String msg =

    "this is a sample"; mMessage.setText(msg); mCapitalize.performClick(); final String actual = mMessage.getText().toString(); final String notExpectedRegexp = “(?i:ERROR)"; assertNotContainsRegex("Capitalization found error:", notExpectedRegexp, actual); }
  22. assertActivityRequiresPermission public void testActivityPermission() { final String PKG = "com.example.aatg.myfirstproject";

    final String ACTIVITY = PKG + ".MyFirstProjectActivity"; final String PERMISSION = android.Manifest.permission.WRITE_EXTERNAL_STORAGE; assertActivityRequiresPermission(PKG, ACTIVITY, PERMISSION); }
  23. assertReadingContentUriRequiresPermission public void testReadingContacts() { final Uri URI = ContactsContract.AUTHORITY_URI;

    final String PERMISSION = android.Manifest.permission.READ_CONTACTS; assertReadingContentUriRequiresPermission(URI, PERMISSION); }
  24. assertWritingContentUriRequiresPermission public void testWritingContacts() { final Uri URI = ContactsContract.AUTHORITY_URI;

    final String PERMISSION = android.Manifest.permission.WRITE_CONTACTS; assertWritingContentUriRequiresPermission(URI, PERMISSION); }
  25. ActivityMonitor public void testFollowLink() { final Instrumentation inst = getInstrumentation();

    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_VIEW); intentFilter.addDataScheme("http"); intentFilter.addCategory(Intent.CATEGORY_BROWSABLE); ActivityMonitor monitor = inst.addMonitor(intentFilter, null, false); assertEquals(0, monitor.getHits()); TouchUtils.clickView(this, mLink); monitor.waitForActivityWithTimeout(5000); assertEquals(1, monitor.getHits()); inst.removeMonitor(monitor); }
  26. Requirements: • Application convert from Celsius to F. • …

    and viceversa • Two fields to input data • When temperature is entered, other field updates • Error displayed • Some space reserved keyboard • Entry fields start empty • Digits right aligned • Last entered values retained after onPause()