Nama : SAFIRA AINI
NIM : 1202154315
Kelas : SI-39-03
Lesson 3 : Testing, Debugging and using Support Library
3.1 : Menggunakan Debugger
Tugas 1. Membuat Proyek dan Aplikasi SimpleCalc
1.1 Unduh dan buka proyek SimpleCalc
1.2 Menjelajahi Layout
Hasil :
1.3 Jelajahi kode aplikasi
Tugas 2. Menjalankan SimpleCalc di Debugger
2.1 Memulai dan Menjalankan aplikasi dalam mode debug
2.2 Men-debug aplikasi yang berjalan
Tugas 3: Menjelajahi Fitur Debugger
3.1 Menyusuri eksekusi aplikasi
3.2 Bekerja dengan Breakpoint
3.3 Memeriksa dan memodifikasi variabel
3.2: Menguji Aplikasi dengan Pengujian Unit
Tugas 1. Menjelajahi dan menjalankan SimpleCalc di Android Studio
1.1 Menjelajahi rangkaian sumber dan SimpleCalc
package com.example.android.SimpleCalc;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import android.test.suitebuilder.annotation.SmallTest;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.closeTo;
import static org.junit.Assert.assertThat;
/**
* JUnit4 unit tests for the calculator logic. These are local unit tests; no device needed
*/
@RunWith(JUnit4.class)
@SmallTest
public class CalculatorTest {
private Calculator mCalculator;
/**
* Set up the environment for testing
*/
@Before
public void setUp() {
mCalculator = new Calculator();
}
/**
* Test for simple addition
*/
@Test
public void addTwoNumbers() {
double resultAdd = mCalculator.add(1d, 1d);
assertThat(resultAdd, is(equalTo(2d)));
}
}
1.2 Menjalankan pengujian dalam Android Studio
Tugas 2. Menambahkan lebih banyak pengujian unit ke CalculatorTest
2.1 Menambahkan lebih banyak pengujian untuk metode add()
2.2 Menambahkan pengujian unit untuk metode penghitungan lain
/* * Copyright 2016, Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.android.SimpleCalc; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import android.test.suitebuilder.annotation.SmallTest; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.closeTo; import static org.junit.Assert.assertThat; /** * JUnit4 unit tests for the calculator logic. These are local unit tests; no device needed */ @RunWith(JUnit4.class) @SmallTest public class CalculatorTest { private Calculator mCalculator; /** * Set up the environment for testing */ @Before public void setUp() { mCalculator = new Calculator(); } /** * Test for simple addition */ @Test public void addTwoNumbers() { double resultAdd = mCalculator.add(1d, 1d); assertThat(resultAdd, is(equalTo(2d))); } @Test public void addTwoNumbersNegative() { double resultAdd = mCalculator.add(-1d, 2d); assertThat(resultAdd, is(equalTo(1d))); } @Test public void addTwoNumbersFloats() { double resultAdd = mCalculator.add(1.111f, 1.111d); assertThat(resultAdd, is(closeTo(2.222, 0.01))); } @Test public void subTwoNumbers() { double resultSub = mCalculator.sub(1d, 1d); assertThat(resultSub, is(equalTo(0d))); } @Test public void subWorksWithNegativeResult() { double resultSub = mCalculator.sub(1d, 17d); assertThat(resultSub, is(equalTo(-16d))); } @Test public void mulTwoNumbers() { double resultMul = mCalculator.mul(32d, 2d); assertThat(resultMul, is(equalTo(64d))); } @Test public void divTwoNumbers() { double resultDiv = mCalculator.div(32d,2d); assertThat(resultDiv, is(equalTo(16d))); } @Test public void divTwoNumbersZero() { double resultDiv = mCalculator.div(32d,0); assertThat(resultDiv, is(equalTo(Double.POSITIVE_INFINITY))); } }
3.3: Menggunakan Pustaka Dukungan Android
Tugas 1. Menyiapkan proyek
1.1 Memverifikasi bahwa Pustaka Dukungan Android tersedia
1.2 Menyiapkan Proyek dan memeriksa build.gradle
1.3 Menambahkan layout dan warna
1.4 Menambahkan perilaku ke MainActivity
Tugas 2. Mengimplementasikan perilaku tombol
2.1 Tambahkan handler onClick changeButton()
2.2 Implementasikan tindakan tombol
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="@dimen/ptop" android:paddingLeft="@dimen/pleft" android:paddingRight="@dimen/pright" android:paddingBottom="@dimen/pbottom" tools:context="com.example.safiraaini.hellocompat.MainActivity"> <TextView android:id="@+id/hello_textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/hello_text_string" android:gravity="center" android:textSize="100sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/color_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="34dp" android:onClick="changeColor" android:text="@string/button_label" /> </RelativeLayout>
MainActivity.java
package com.example.safiraaini.hellocompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import java.util.Random; public class MainActivity extends AppCompatActivity { private TextView mHelloTextView; private String[] mColorArray = {"red", "pink", "purple", "deep_purple", "indigo", "blue", "light_blue", "cyan", "teal", "green", "light_green", "lime", "yellow", "amber", "orange", "deep_orange", "brown", "grey", "blue_grey", "black" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mHelloTextView = (TextView) findViewById(R.id.hello_textview); if (savedInstanceState != null) { mHelloTextView.setTextColor(savedInstanceState.getInt("color")); } } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // save the current text color outState.putInt("color", mHelloTextView.getCurrentTextColor()); } public void changeColor(View view) { Random random = new Random(); String colorName = mColorArray[random.nextInt(20)]; int colorResourceName = getResources().getIdentifier(colorName, "color", getApplicationContext().getPackageName()); int colorRes = ContextCompat.getColor(this, colorResourceName); mHelloTextView.setTextColor(colorRes); } }
colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="red">#F44336</color> <color name="pink">#E91E63</color> <color name="purple">#9C27B0</color> <color name="deep_purple">#673AB7</color> <color name="indigo">#3F51B5</color> <color name="blue">#2196F3</color> <color name="light_blue">#03A9F4</color> <color name="cyan">#00BCD4</color> <color name="teal">#009688</color> <color name="green">#4CAF50</color> <color name="light_green">#8BC34A</color> <color name="lime">#CDDC39</color> <color name="yellow">#FFEB3B</color> <color name="amber">#FFC107</color> <color name="orange">#FF9800</color> <color name="deep_orange">#FF5722</color> <color name="brown">#795548</color> <color name="grey">#9E9E9E</color> <color name="blue_grey">#607D8B</color> <color name="black">#000000</color> </resources>
dimens.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
<dimen name=”ptop”>16sp</dimen>
<dimen name=”pleft”>16sp</dimen>
<dimen name=”pright”>16sp</dimen>
<dimen name=”pbottom”>16sp</dimen>
</resources>
Hasil:
Leave a Reply