Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,40 @@ public class ConvertActivityTest {
public ActivityTestRule<ConvertActivity> mActivityRule = new ActivityTestRule<>(
ConvertActivity.class);

@Before
public void initValidString() {
// Specify a valid string.
mStringToBetyped = "5";
}

@Test
public void ConvertActivity_toFahrenheitButton_Test() {
mStringToBetyped = "5";
// Type text and then press the button.
onView(withId(R.id.celsiusValue))
onView(withId(R.id.value))
.perform(typeText(mStringToBetyped), closeSoftKeyboard());
onView(withId(R.id.toFahrenheitButton)).perform(click());

// Check that the text was changed.
onView(withId(R.id.celsiusValue))
onView(withId(R.id.value))
.check(matches(withText(mStringToBetyped)));

onView(withId(R.id.fahrenheitValue))
onView(withId(R.id.result))
.check(matches(withText("41.00"))); // формат вывода два знака после запятой

}

@Test
public void ConvertActivity_toMetersPerSecondButton_Test() {
mStringToBetyped = "36";
// Type text and then press the button.
onView(withId(R.id.value))
.perform(typeText(mStringToBetyped), closeSoftKeyboard());
onView(withId(R.id.toMetersPerSecondButton)).perform(click());

// Check that the text was changed.
onView(withId(R.id.value))
.check(matches(withText(mStringToBetyped)));

onView(withId(R.id.result))
.check(matches(withText("10.00"))); // формат вывода два знака после запятой

}

@Test
public void AppContext_Test() throws Exception {
// Context of the app under test.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,28 @@ public class ConvertActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_convert);
sourceText = (EditText) findViewById(R.id.celsiusValue);
destText = (EditText) findViewById(R.id.fahrenheitValue);
sourceText = (EditText) findViewById(R.id.value);
destText = (EditText) findViewById(R.id.result);
}

// обработка нажатия
public void onToFahrenheitClick(View view) {
public void onButtonClick(View view) {
// получить входное значение
float sourceValue = Float.parseFloat(sourceText.getText().toString());
// инстанцировать конвертер
Converter converter = new Converter(sourceValue);
// преобразовать, обратите внимание на параметр ConvertToFahrenheit
float destValue = converter.Convert(new ConvertToFahrenheit()).GetResult();
// записать результат в элемент
destText.setText(String.format("%.02f", destValue));
Float destValue = null;
switch (view.getId()) {
case R.id.toFahrenheitButton:
// преобразовать, обратите внимание на параметр ConvertToFahrenheit
destValue = converter.Convert(new ConvertToFahrenheit()).GetResult();
break;
case R.id.toMetersPerSecondButton:
destValue = converter.Convert(new ConvertToMetersPerSecond()).GetResult();
break;
}
if (destValue != null) {
destText.setText(String.format("%.02f", destValue));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.geekbrains.converter;

class ConvertToMetersPerSecond implements ConvertTo {

@Override public float Do(float sourceValue) {
return sourceValue * 1000 / 3600;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
tools:showIn="@layout/activity_convert">

<EditText
android:id="@+id/celsiusValue"
android:id="@+id/value"
android:layout_width="215dp"
android:layout_height="43dp"
android:ems="10"
Expand All @@ -20,21 +20,21 @@
app:layout_constraintRight_toRightOf="parent" />

<EditText
android:id="@+id/fahrenheitValue"
android:id="@+id/result"
android:layout_width="215dp"
android:layout_height="43dp"
android:ems="10"
android:inputType="numberDecimal"
android:layout_marginTop="67dp"
app:layout_constraintTop_toBottomOf="@+id/celsiusValue"
app:layout_constraintTop_toBottomOf="@+id/value"
android:layout_marginRight="16dp"
app:layout_constraintRight_toRightOf="parent" />

<TextView
android:id="@+id/celsiusLabel"
android:id="@+id/valueLabel"
android:layout_width="56dp"
android:layout_height="17dp"
android:text="@string/celsius"
android:text="@string/value"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="25dp"
android:layout_marginLeft="16dp"
Expand All @@ -44,11 +44,11 @@
android:id="@+id/fahrenheitLabel"
android:layout_width="78dp"
android:layout_height="17dp"
android:text="@string/fahrenheit"
android:text="@string/result"
android:layout_marginLeft="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="99dp"
app:layout_constraintTop_toBottomOf="@+id/celsiusLabel" />
app:layout_constraintTop_toBottomOf="@+id/valueLabel" />

<Button
android:id="@+id/toFahrenheitButton"
Expand All @@ -57,8 +57,18 @@
android:layout_marginLeft="72dp"
android:layout_marginTop="11dp"
android:text="@string/to_fahrenheit"
android:onClick="onToFahrenheitClick"
android:onClick="onButtonClick"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/celsiusValue" />
app:layout_constraintTop_toBottomOf="@+id/value" />

<Button
android:id="@+id/toMetersPerSecondButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:onClick="onButtonClick"
android:text="@string/to_meters_per_second"
app:layout_constraintStart_toEndOf="@+id/toFahrenheitButton"
app:layout_constraintTop_toTopOf="@+id/toFahrenheitButton" />

</android.support.constraint.ConstraintLayout>
7 changes: 4 additions & 3 deletions Lesson1/Task1/Converter/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<resources>
<string name="app_name">Converter</string>
<string name="action_settings">Settings</string>
<string name="celsius">Цельсия</string>
<string name="fahrenheit">Фаренгейта</string>
<string name="to_fahrenheit">В фаренгейты</string>
<string name="value">Value</string>
<string name="result">Result</string>
<string name="to_fahrenheit">Celsius to Fahrenheit</string>
<string name="to_meters_per_second">km/h to m/s</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,10 @@ public void ConvertToMile_Do_UnitTest() throws Exception{
assertThat(actual, is(1.24274f));
}

@Test
public void ConvertToMetersPerSecond_Do_UnitTest() throws Exception {
ConvertTo convertTo = new ConvertToMetersPerSecond();
float actual = convertTo.Do(36);
assertThat(actual, is(10f));
}
}