/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.deskclock.timer;
import android.content.Context;
import android.content.Intent;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.test.InstrumentationRegistry;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
import androidx.test.rule.ActivityTestRule;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.android.deskclock.DeskClock;
import com.android.deskclock.R;
import com.android.deskclock.data.DataModel;
import com.android.deskclock.data.Timer;
import com.android.deskclock.widget.MockFabContainer;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
@RunWith(AndroidJUnit4ClassRunner.class)
public class TimerFragmentTest {
private static int LIGHT;
private static int DARK;
private static int TOP;
private static int BOTTOM;
private static final int GONE = 0;
private TimerFragment fragment;
private View timersView;
private View timerSetupView;
private ViewPager viewPager;
private TimerPagerAdapter adapter;
private ImageView fab;
private Button leftButton;
private Button rightButton;
@Rule
public ActivityTestRule<DeskClock> rule = new ActivityTestRule<>(DeskClock.class, true);
@BeforeClass
public static void staticSetUp() {
LIGHT = R.drawable.ic_swipe_circle_light;
DARK = R.drawable.ic_swipe_circle_dark;
TOP = R.drawable.ic_swipe_circle_top;
BOTTOM = R.drawable.ic_swipe_circle_bottom;
}
private void setUpSingleTimer() {
Runnable addTimerRunnable = () -> {
DataModel.getDataModel().addTimer(60000L, null, false);
};
InstrumentationRegistry.getInstrumentation().runOnMainSync(addTimerRunnable);
setUpFragment();
}
private void setUpTwoTimers() {
Runnable addTimerRunnable = () -> {
DataModel.getDataModel().addTimer(60000L, null, false);
DataModel.getDataModel().addTimer(90000L, null, false);
};
InstrumentationRegistry.getInstrumentation().runOnMainSync(addTimerRunnable);
setUpFragment();
}
private void setUpFragment() {
Runnable setUpFragmentRunnable = () -> {
ViewPager deskClockPager =
(ViewPager) rule.getActivity().findViewById(R.id.desk_clock_pager);
PagerAdapter tabPagerAdapter = (PagerAdapter) deskClockPager.getAdapter();
fragment = (TimerFragment) tabPagerAdapter.instantiateItem(deskClockPager, 2);
fragment.onStart();
fragment.selectTab();
final MockFabContainer fabContainer =
new MockFabContainer(fragment, ApplicationProvider.getApplicationContext());
fragment.setFabContainer(fabContainer);
final View view = fragment.getView();
assertNotNull(view);
timersView = view.findViewById(R.id.timer_view);
timerSetupView = view.findViewById(R.id.timer_setup);
viewPager = view.findViewById(R.id.vertical_view_pager);
adapter = (TimerPagerAdapter) viewPager.getAdapter();
fab = fabContainer.getFab();
leftButton = fabContainer.getLeftButton();
rightButton = fabContainer.getRightButton();
};
InstrumentationRegistry.getInstrumentation().runOnMainSync(setUpFragmentRunnable);
}
@After
public void tearDown() {
clearTimers();
fragment = null;
fab = null;
timerSetupView = null;
timersView = null;
adapter = null;
viewPager = null;
leftButton = null;
rightButton = null;
}
private void clearTimers() {
Runnable clearTimersRunnable = () -> {
final List<Timer> timers = new ArrayList<>(DataModel.getDataModel().getTimers());
for (Timer timer : timers) {
DataModel.getDataModel().removeTimer(timer);
}
};
InstrumentationRegistry.getInstrumentation().runOnMainSync(clearTimersRunnable);
}
@Test
public void initialStateNoTimers() {
setUpFragment();
assertEquals(View.VISIBLE, timerSetupView.getVisibility());
assertEquals(View.GONE, timersView.getVisibility());
assertAdapter(0);
}
@Test
public void initialStateOneTimer() {
setUpSingleTimer();
assertEquals(View.VISIBLE, timersView.getVisibility());
assertEquals(View.GONE, timerSetupView.getVisibility());
assertAdapter(1);
}
@Test
public void initialStateTwoTimers() {
setUpTwoTimers();
assertEquals(View.VISIBLE, timersView.getVisibility());
assertEquals(View.GONE, timerSetupView.getVisibility());
assertAdapter(2);
}
@Test
public void timeClick_startsTimer() {
setUpSingleTimer();
setCurrentItem(0);
final TimerItem timerItem = (TimerItem) viewPager.getChildAt(0);
final TextView timeText = timerItem.findViewById(R.id.timer_time_text);
assertStateEquals(Timer.State.RESET, 0);
clickView(timeText);
assertStateEquals(Timer.State.RUNNING, 0);
}
@Test
public void timeClick_startsSecondTimer() {
setUpTwoTimers();
setCurrentItem(1);
final TimerItem timerItem = (TimerItem) viewPager.getChildAt(1);
final TextView timeText = timerItem.findViewById(R.id.timer_time_text);
assertStateEquals(Timer.State.RESET, 1);
assertStateEquals(Timer.State.RESET, 0);
clickView(timeText);
assertStateEquals(Timer.State.RUNNING, 1);
assertStateEquals(Timer.State.RESET, 0);
}
@Test
public void timeClick_pausesTimer() {
setUpSingleTimer();
setCurrentItem(0);
final TimerItem timerItem = (TimerItem) viewPager.getChildAt(0);
final TextView timeText = timerItem.findViewById(R.id.timer_time_text);
assertStateEquals(Timer.State.RESET, 0);
clickView(timeText);
assertStateEquals(Timer.State.RUNNING, 0);
clickView(timeText);
assertStateEquals(Timer.State.PAUSED, 0);
}
@Test
public void timeClick_pausesSecondTimer() {
setUpTwoTimers();
setCurrentItem(1);
final TimerItem timerItem = (TimerItem) viewPager.getChildAt(1);
final TextView timeText = timerItem.findViewById(R.id.timer_time_text);
assertStateEquals(Timer.State.RESET, 1);
assertStateEquals(Timer.State.RESET, 0);
clickView(timeText);
assertStateEquals(Timer.State.RUNNING, 1);
assertStateEquals(Timer.State.RESET, 0);
clickView(timeText);
assertStateEquals(Timer.State.PAUSED, 1);
assertStateEquals(Timer.State.RESET, 0);
}
@Test
public void timeClick_restar