package pl.srw.billcalculator;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

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

import javax.inject.Inject;

import pl.srw.billcalculator.di.ApplicationModule;
import pl.srw.billcalculator.di.DaggerTestApplicationComponent;
import pl.srw.billcalculator.di.TestApplicationComponent;
import pl.srw.billcalculator.history.DrawerActivity;
import pl.srw.billcalculator.settings.global.SettingsRepo;
import pl.srw.billcalculator.tester.SettingsTester;

@RunWith(AndroidJUnit4.class)
public class ProviderSettingsAndroidTest {

    @Inject SettingsRepo settingsRepo;

    @Rule
    public ActivityTestRule<DrawerActivity> mActivityTestRule = new ActivityTestRule<>(DrawerActivity.class, false, false);

    @Before
    public void setUp() throws Exception {
        Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

        TestApplicationComponent component = DaggerTestApplicationComponent.builder()
                .applicationModule(new ApplicationModule(context))
                .build();
        component.inject(this);

        settingsRepo.markFirstLaunch();

        mActivityTestRule.launchActivity(null);
    }

    @Test
    public void providerSettingsTariffChangeCauseScreenChangeDisplayedValues() {
        final SettingsTester tester = new SettingsTester();
        tester
                .openSettings()
                .pickProvider(SettingsTester.PGE)
                .getPreferenceLineAt(0)
                .pickOption("Taryfa dwustrefowa (G12)");
        tester
                .getPreferenceLineAt(1)
                .hasTitle("za energię czynną (strefa dzienna)");
        tester
                .getPreferenceLineAt(0)
                .pickOption("Taryfa całodobowa (G11)");
        tester
                .getPreferenceLineAt(1)
                .hasTitle("za energię czynną");
    }

    @Test
    public void settingPreferenceValueUpdatedSummary() {
        final SettingsTester tester = new SettingsTester();
        tester
                .openSettings()
                .pickProvider(SettingsTester.PGNIG)
                .getPreferenceLineAt(0)
                .changeValueTo("1.234");

        tester.getPreferenceLineAt(0)
                .hasSummary("1.234");
    }

    @Test
    public void restoreSettingsSetDefaultValues() {
        final SettingsTester tester = new SettingsTester();
        tester
                .openSettings()
                .pickProvider(SettingsTester.TAURON)
                .getPreferenceLineAt(0)
                .pickOption("Taryfa dwustrefowa (G12)");
        tester
                .restoreDefault()
                .getPreferenceLineAt(0)
                .hasSummary("Taryfa całodobowa (G11)");
    }
}

@@ -762,48 +762,8 @@
  %7B%0A%0A
-    @Inject SettingsRepo settingsRepo;%0A%0A
     
@@ -889,24 +889,115 @@
 e, false);%0A%0A
+    @Inject SettingsRepo settingsRepo;%0A%0A    SettingsTester tester = new SettingsTester();%0A%0A
     @Before%0A
@@ -1534,68 +1534,8 @@
 ) %7B%0A
-        final SettingsTester tester = new SettingsTester();%0A
     
@@ -2136,68 +2136,8 @@
 ) %7B%0A
-        final SettingsTester tester = new SettingsTester();%0A
     
@@ -2426,103 +2426,374 @@
 oid 
-restoreSettingsSetDefaultValues() %7B%0A        final SettingsTester tester = new SettingsTester();
+settingPreferenceEmptyValueFillZeroSummary() %7B%0A        tester%0A                .openSettings()%0A                .pickProvider(SettingsTester.PGNIG)%0A                .getPreferenceLineAt(0)%0A                .changeValueTo(%22%22);%0A%0A        tester.getPreferenceLineAt(0)%0A                .hasSummary(%220.00%22);%0A    %7D%0A%0A    @Test%0A    public void restoreSettingsSetDefaultValues() %7B
 %0A   

Add ui test: preferences set default value when empty