1/*
2 * Copyright (C) 2017 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
17package android.support.wear.widget;
18
19import static junit.framework.Assert.assertEquals;
20
21import android.app.Activity;
22import android.support.test.InstrumentationRegistry;
23import android.support.test.filters.MediumTest;
24import android.support.test.rule.ActivityTestRule;
25import android.support.test.runner.AndroidJUnit4;
26import android.support.wear.test.R;
27import android.support.wear.widget.util.WakeLockRule;
28import android.view.View;
29import android.widget.FrameLayout;
30
31import org.junit.Before;
32import org.junit.Rule;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35
36import java.util.concurrent.atomic.AtomicReference;
37
38@MediumTest
39@RunWith(AndroidJUnit4.class)
40public class WearableLinearLayoutManagerTest {
41
42    @Rule
43    public final WakeLockRule wakeLock = new WakeLockRule();
44
45    @Rule
46    public final ActivityTestRule<WearableRecyclerViewTestActivity> mActivityRule =
47            new ActivityTestRule<>(WearableRecyclerViewTestActivity.class, true, true);
48
49    WearableLinearLayoutManager mWearableLinearLayoutManagerUnderTest;
50
51    @Before
52    public void setUp() throws Throwable {
53        Activity activity = mActivityRule.getActivity();
54        CurvingLayoutCallback mCurvingCallback = new CurvingLayoutCallback(activity);
55        mCurvingCallback.setOffset(10);
56        mWearableLinearLayoutManagerUnderTest =
57                new WearableLinearLayoutManager(mActivityRule.getActivity(), mCurvingCallback);
58    }
59
60    @Test
61    public void testRoundOffsetting() throws Throwable {
62        ((CurvingLayoutCallback) mWearableLinearLayoutManagerUnderTest.getLayoutCallback())
63                .setRound(true);
64        final AtomicReference<WearableRecyclerView> wrvReference = new AtomicReference<>();
65        mActivityRule.runOnUiThread(new Runnable() {
66            @Override
67            public void run() {
68                WearableRecyclerView wrv =
69                        (WearableRecyclerView) mActivityRule.getActivity().findViewById(R.id.wrv);
70                // Set a fixed layout so that the test adapts to different device screens.
71                wrv.setLayoutParams(new FrameLayout.LayoutParams(390, 390));
72            }
73        });
74
75        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
76
77        mActivityRule.runOnUiThread(new Runnable() {
78            @Override
79            public void run() {
80                WearableRecyclerView wrv =
81                        (WearableRecyclerView) mActivityRule.getActivity().findViewById(R.id.wrv);
82                wrv.setLayoutManager(mWearableLinearLayoutManagerUnderTest);
83                wrvReference.set(wrv);
84            }
85        });
86
87        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
88        WearableRecyclerView wrv = wrvReference.get();
89
90        View child1 = wrv.getChildAt(0);
91        View child2 = wrv.getChildAt(1);
92        View child3 = wrv.getChildAt(2);
93        View child4 = wrv.getChildAt(3);
94        View child5 = wrv.getChildAt(4);
95
96        // The left position and the translation of the child is modified if the screen is round.
97        // Check if the 5th child is not null as some devices will not be able to display 5 views.
98        assertEquals(136, child1.getLeft());
99        assertEquals(-6.3, child1.getTranslationY(), 0.1);
100
101        assertEquals(91, child2.getLeft(), 1);
102        assertEquals(-15.21, child2.getTranslationY(), 0.1);
103
104        assertEquals(58, child3.getLeft(), 1);
105        assertEquals(-13.5, child3.getTranslationY(), 0.1);
106
107        assertEquals(42, child4.getLeft(), 1);
108        assertEquals(-4.5, child4.getTranslationY(), 0.1);
109
110        if (child5 != null) {
111            assertEquals(43, child5.getLeft(), 1);
112            assertEquals(6.7, child5.getTranslationY(), 0.1);
113        }
114    }
115
116    @Test
117    public void testStraightOffsetting() throws Throwable {
118        ((CurvingLayoutCallback) mWearableLinearLayoutManagerUnderTest.getLayoutCallback())
119                .setRound(
120                false);
121        final AtomicReference<WearableRecyclerView> wrvReference = new AtomicReference<>();
122        mActivityRule.runOnUiThread(new Runnable() {
123            @Override
124            public void run() {
125                WearableRecyclerView wrv =
126                        (WearableRecyclerView) mActivityRule.getActivity().findViewById(R.id.wrv);
127                wrv.setLayoutManager(mWearableLinearLayoutManagerUnderTest);
128                wrvReference.set(wrv);
129            }
130        });
131        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
132        WearableRecyclerView wrv = wrvReference.get();
133
134        View child1 = wrv.getChildAt(0);
135        View child2 = wrv.getChildAt(1);
136        View child3 = wrv.getChildAt(2);
137        View child4 = wrv.getChildAt(3);
138        View child5 = wrv.getChildAt(4);
139
140        // The left position and the translation of the child is not modified if the screen is
141        // straight. Check if the 5th child is not null as some devices will not be able to display
142        // 5 views.
143        assertEquals(0, child1.getLeft());
144        assertEquals(0.0f, child1.getTranslationY(), 0);
145
146        assertEquals(0, child2.getLeft());
147        assertEquals(0.0f, child2.getTranslationY(), 0);
148
149        assertEquals(0, child3.getLeft());
150        assertEquals(0.0f, child3.getTranslationY(), 0);
151
152        assertEquals(0, child4.getLeft());
153        assertEquals(0.0f, child4.getTranslationY(), 0);
154
155        if (child5 != null) {
156            assertEquals(0, child5.getLeft());
157            assertEquals(0.0f, child5.getTranslationY(), 0);
158        }
159    }
160}
161