Skip to content

Commit

Permalink
Merge pull request #14 from Mofazzal874/Amit
Browse files Browse the repository at this point in the history
Amit
  • Loading branch information
superXnova21 committed May 24, 2024
2 parents 01afe4c + 0d1b196 commit 3576d07
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 30 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ dependencies {

//dependencies for ui_test
androidTestImplementation("androidx.test:rules:1.5.0")
androidTestImplementation("org.mockito:mockito-core:3.12.4")
androidTestImplementation("org.mockito:mockito-android:3.12.4")
androidTestImplementation("androidx.test.espresso:espresso-intents:3.4.0")
// AndroidX Test - Instrumentation Testing
androidTestImplementation("androidx.test:runner:1.5.2")
androidTestImplementation("androidx.test:rules:1.5.0")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.example.projecto.activities;

import androidx.test.espresso.intent.Intents;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.example.projecto.R;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.intent.Intents.intended;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent;
import static androidx.test.espresso.matcher.ViewMatchers.withId;

@RunWith(AndroidJUnit4.class)
public class AddressActivityTest {

@Rule
public ActivityScenarioRule<AddressActivity> activityRule =
new ActivityScenarioRule<>(AddressActivity.class);

@Rule
public FirebaseAuthRule firebaseAuthRule = new FirebaseAuthRule();

@Before
public void setUp() {
Intents.init();
}

@After
public void tearDown() {
Intents.release();
}

@Test
public void testPaymentButton_opensPaymentActivity() {
// Perform click on paymentBtn
onView(withId(R.id.payment_btn)).perform(click());

// Verify that the PaymentActivity is launched
intended(hasComponent(PaymentActivity.class.getName()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.projecto.activities;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class FirebaseAuthRule implements TestRule {

private final FirebaseAuth mockAuth;
private final FirebaseUser mockUser;

public FirebaseAuthRule() {
mockAuth = mock(FirebaseAuth.class);
mockUser = mock(FirebaseUser.class);
when(mockAuth.getCurrentUser()).thenReturn(mockUser);
when(mockUser.getUid()).thenReturn("testUid");
}

@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
// Inject the mock FirebaseAuth instance
AddressActivity.setFirebaseAuth(mockAuth);

// Execute the test
base.evaluate();

// Clean up after test execution
AddressActivity.setFirebaseAuth(null);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.example.projecto.activities;

import static android.app.PendingIntent.getActivity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
Expand All @@ -28,22 +26,20 @@

public class AddressActivity extends AppCompatActivity implements AddressAdapter.SelectedAddress {

Button addAddress,paymentBtn;

Button addAddress, paymentBtn;
RecyclerView recyclerView;

private List<AddressModel> addressModelList;
private AddressAdapter addressAdapter;

FirebaseFirestore firestore;
FirebaseAuth auth;

private static FirebaseAuth auth;
Toolbar toolbar;

String mAddress = "";

double receivedTotalAmount;

// Static method to allow setting FirebaseAuth from tests
public static void setFirebaseAuth(FirebaseAuth firebaseAuth) {
auth = firebaseAuth;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -61,41 +57,41 @@ public void onClick(View v) {
}
});


Intent intent = getIntent();
if (intent != null) {
receivedTotalAmount = intent.getDoubleExtra("TOTAL_AMOUNT_KEY", 0.0);
// Now 'receivedTotalAmount' holds your total amount as a double, use it as needed
}

if (auth == null) {
auth = FirebaseAuth.getInstance();
}

auth = FirebaseAuth.getInstance();
firestore = FirebaseFirestore.getInstance();

recyclerView = findViewById(R.id.address_recycler);

addAddress = findViewById(R.id.add_address_btn);
paymentBtn = findViewById(R.id.payment_btn);

recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

addressModelList = new ArrayList<>();
addressAdapter = new AddressAdapter(getApplicationContext(),addressModelList,this) ;
addressAdapter = new AddressAdapter(getApplicationContext(), addressModelList, this);
recyclerView.setAdapter(addressAdapter);

firestore.collection("CurrentUser").document(auth.getCurrentUser().getUid())
.collection("Address").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){
for (DocumentSnapshot doc: task.getResult().getDocuments()){
AddressModel addressModel = doc.toObject(AddressModel.class);
addressModelList.add(addressModel);
addressAdapter.notifyDataSetChanged();
if (auth.getCurrentUser() != null) {
firestore.collection("CurrentUser").document(auth.getCurrentUser().getUid())
.collection("Address").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot doc : task.getResult().getDocuments()) {
AddressModel addressModel = doc.toObject(AddressModel.class);
addressModelList.add(addressModel);
addressAdapter.notifyDataSetChanged();
}
}
}
}
});
});
}

paymentBtn.setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -112,11 +108,10 @@ public void onClick(View v) {
startActivity(new Intent(AddressActivity.this, AddAddressActivity.class));
}
});

}

@Override
public void setAddress(String address) {
mAddress = address;
}
}
}
3 changes: 2 additions & 1 deletion app/src/main/res/layout/activity_payment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".activities.PaymentActivity">
tools:context=".activities.PaymentActivity"
android:id="@+id/payment_layer_id">

<androidx.appcompat.widget.Toolbar
android:id="@+id/payment_toolbar"
Expand Down

0 comments on commit 3576d07

Please sign in to comment.