daffa_usman/TestLoginSuccess.java
2024-12-31 09:45:13 +07:00

120 lines
4.5 KiB
Java

package org.aplas.myshop;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import androidx.lifecycle.Lifecycle;
import androidx.test.core.app.ActivityScenario;
import org.aplas.myshop.model.User;
import org.aplas.myshop.sql.Database;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;
import com.google.android.material.snackbar.Snackbar;
@RunWith(RobolectricTestRunner.class)
@Config(manifest= Config.NONE)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestLoginSuccess extends ViewTest{
ActivityScenario<LoginActivity> scenario;
private EditText nama, password;
private Database database;
private Button loginButton;
@Before
public void setUp() {
scenario = ActivityScenario.launch(LoginActivity.class);
scenario.moveToState(Lifecycle.State.CREATED);
scenario.onActivity(activity -> {
nama = activity.findViewById(R.id.textInputEditTextUsername);
password = activity.findViewById(R.id.textInputEditTextPassword);
loginButton = activity.findViewById(R.id.appCompatButtonLogin);
database = new Database(activity);
});
}
@After
public void tearDown() {
scenario.close();
}
@Test
public void test01Seller() {
scenario.onActivity(activity -> {
User user = new User();
user.setName("daffa1@test.com");
user.setPassword("password");
user.setRole("Seller");
database.addUser(user);
nama.setText("daffa1@test.com");
password.setText("password");
loginButton.performClick();
Intent expectedIntent = new Intent(activity, SellerHomeActivity.class);
expectedIntent.putExtra("Username", "daffa1@test.com");
Intent actualIntent = shadowOf(activity).getNextStartedActivity();
assertNotNull("No intents detected, please check your login button on click function inside LoginActivity", actualIntent);
testItem(expectedIntent.getComponent(), actualIntent.getComponent(), "Intents do not match, please check your login button on click function inside LoginActivity", 1);
database.close();
});
}
@Test
public void test02Buyer() {
scenario.onActivity(activity -> {
User user = new User();
user.setName("daffa2@test.com");
user.setPassword("password");
user.setRole("Buyer");
database.addUser(user);
nama.setText("daffa2@test.com");
password.setText("password");
loginButton.performClick();
Intent expectedIntent = new Intent(activity, BuyerHomeActivity.class);
expectedIntent.putExtra("Username", "daffa2@test.com");
Intent actualIntent = shadowOf(activity).getNextStartedActivity();
assertNotNull("No intents detected, please check your login button on click function inside LoginActivity", actualIntent);
testItem(expectedIntent.getComponent(), actualIntent.getComponent(), "Intents do not match, please check your login button on click function inside LoginActivity", 1);
database.close();
});
}
@Test
public void test03invalid() {
scenario.onActivity(activity -> {
Database database = mock(Database.class);
activity.setDatabase(database);
nama.setText("daffa3@test.com");
password.setText("password");
when(database.checkUser("daffa3@test.com", "password")).thenReturn(false);
loginButton.performClick();
Snackbar snackbar = Snackbar.make(activity.findViewById(R.id.nestedScrollViewProduct),activity.getString(R.string.error_valid_email_password), Snackbar.LENGTH_LONG);
snackbar.show();
// assertTrue(snackbar.isShown());
testItem(true, snackbar.isShown(), "Your login function doesn't seem working, please check your login button on click function inside LoginActivity", 3);
database.close();
});
}
}