package ch.epfl.sweng.swissaffinity;

import android.content.Context;
import android.content.res.Resources;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.assertion.ViewAssertions;
import android.support.test.espresso.matcher.ViewMatchers;
import android.test.ActivityInstrumentationTestCase2;

import org.junit.Test;

import static android.support.test.InstrumentationRegistry.getContext;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

/**
 * Created by sahinfurkan on 26/11/15.
 */
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
    public MainActivityTest() {
        super(MainActivity.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
    }

    public void testCanGreetUsers() {
        getActivity();

        if (MainActivity.mUser == null)
            onView(withId(R.id.mainWelcomeText)).check(matches(withText(R.string.welcome_not_registered_text)));
        else {
            String welcomeText = String.format(
                    MainActivity.mContext.getString(R.string.welcome_registered_text),
                    MainActivity.mUser.getUsername());
            onView(withId(R.id.mainWelcomeText)).check(matches(withText(welcomeText)));
        }
    }

    public void testAbout(){
        getActivity();

        onView(withId(R.id.action_about)).perform(click());
        onView(withId(R.id.aboutSwissAffinityText)).check(matches(isDisplayed()));
    }

    public void testSettings(){
        getActivity();


        onView(withId(R.id.action_settings)).perform(click());
        pressBack();
        if(MainActivity.mUser == null)
            onView(withId(R.id.mainWelcomeText)).check(matches(withText(R.string.welcome_not_registered_text)));
        else {
            String welcomeText = String.format(
                    MainActivity.mContext.getString(R.string.welcome_registered_text),
                    MainActivity.mUser.getUsername());
            onView(withId(R.id.mainWelcomeText)).check(matches(withText(welcomeText)));

        }
    }
}

@@ -402,16 +402,82 @@
 .Test;%0A%0A
+import ch.epfl.sweng.swissaffinity.utilities.network.ServerTags;%0A%0A
 import s
@@ -1558,172 +1558,99 @@
     
-if (MainActivity.mUser == null)%0A            onView(withId(R.id.mainWelcomeText)).check(matches(withText(R.string.welcome_not_registered_text)));%0A        else %7B%0A    
+String userName = MainActivity.getSharedPrefs().getString(ServerTags.USERNAME.get(), %22%22);%0A%0A
     
@@ -1669,39 +1669,24 @@
 elcomeText =
- String.format(
 %0A           
@@ -1694,33 +1694,35 @@
     
-    MainActivity.mContext
+String.format(getActivity()
 .get
@@ -1766,68 +1766,20 @@
 xt),
-%0A                    MainActivity.mUser.getUsern
+ userN
 ame
-()
 );%0A
-    
     
@@ -1858,25 +1858,16 @@
 ext)));%0A
-        %7D
 %0A    %7D%0A%0A
@@ -1893,16 +1893,17 @@
 tAbout()
+ 
 %7B%0A      
@@ -2100,16 +2100,17 @@
 ttings()
+ 
 %7B%0A      
@@ -2212,24 +2212,25 @@
 Back();%0A
+%0A
         
 if(MainA
@@ -2225,171 +2225,99 @@
     
-if(MainActivity.mUser == null)%0A            onView(withId(R.id.mainWelcomeText)).check(matches(withText(R.string.welcome_not_registered_text)));%0A        else %7B%0A    
+String userName = MainActivity.getSharedPrefs().getString(ServerTags.USERNAME.get(), %22%22);%0A%0A
     
@@ -2376,33 +2376,21 @@
     
-    MainActivity.mContext
+getActivity()
 .get
@@ -2451,51 +2451,19 @@
     
-    MainActivity.mUser.getUsern
+userN
 ame
-()
 );%0A
-    
     
@@ -2547,17 +2547,8 @@
 );%0A%0A
-        %7D
 %0A   

fix MainActivityTest