1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.deskclock.timer;
18 
19 import android.content.Context;
20 import android.view.View;
21 import android.view.ViewGroup;
22 
23 import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
24 import androidx.test.platform.app.InstrumentationRegistry;
25 import androidx.test.rule.ActivityTestRule;
26 import androidx.viewpager.widget.ViewPager;
27 
28 import com.android.deskclock.DeskClock;
29 import com.android.deskclock.R;
30 import com.android.deskclock.data.DataModel;
31 import com.android.deskclock.data.Timer;
32 
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.assertNotNull;
39 
40 /**
41  * Exercise the user interface that shows current timers.
42  */
43 @RunWith(AndroidJUnit4ClassRunner.class)
44 public class TimerItemFragmentTest {
45 
46     @Rule
47     public ActivityTestRule<DeskClock> rule = new ActivityTestRule<>(DeskClock.class, true);
48 
49     @Test
ensureTimerIsHeldSuccessfully_whenOneTimerIsRunning()50     public void ensureTimerIsHeldSuccessfully_whenOneTimerIsRunning() {
51         Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
52         final TimerFragment timerFragment = new TimerFragment();
53         rule.getActivity().getFragmentManager()
54                 .beginTransaction().add(timerFragment, null).commit();
55         Runnable selectTabRunnable = () -> {
56             timerFragment.selectTab();
57             Timer timer = DataModel.getDataModel().addTimer(5000L, "", false);
58 
59             // Get the view held by the TimerFragment
60             final View view = timerFragment.getView();
61             assertNotNull(view);
62 
63             // Get the TimerPagerAdapter associated with this view
64             ViewPager viewPager = (ViewPager) view.findViewById(R.id.vertical_view_pager);
65             TimerPagerAdapter adapter = (TimerPagerAdapter) viewPager.getAdapter();
66             ViewGroup viewGroup = view.findViewById(R.id.timer_view);
67 
68             // Retrieve the TimerItemFragment from the adapter
69             TimerItemFragment timerItemFragment =
70                     (TimerItemFragment) adapter.instantiateItem(viewGroup, 0);
71 
72             // Assert that the correct timer is set
73             assertEquals(timerItemFragment.getTimer(), timer);
74             DataModel.getDataModel().removeTimer(timer);
75         };
76         InstrumentationRegistry.getInstrumentation().runOnMainSync(selectTabRunnable);
77     }
78 }
79