1/*
2 * Copyright 2018 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 androidx.swiperefreshlayout.widget;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertTrue;
21import static org.mockito.Matchers.any;
22import static org.mockito.Matchers.anyBoolean;
23import static org.mockito.Matchers.anyFloat;
24import static org.mockito.Mockito.mock;
25import static org.mockito.Mockito.verify;
26
27import android.content.Context;
28import android.graphics.Canvas;
29import android.graphics.Paint;
30import android.graphics.Rect;
31import android.graphics.RectF;
32import android.support.test.filters.SmallTest;
33import android.support.test.rule.ActivityTestRule;
34import android.support.test.runner.AndroidJUnit4;
35
36import org.junit.Before;
37import org.junit.Rule;
38import org.junit.Test;
39import org.junit.runner.RunWith;
40import org.mockito.ArgumentCaptor;
41import org.mockito.Mock;
42
43/**
44 * Tests for CircularProgressDrawable
45 */
46@RunWith(AndroidJUnit4.class)
47public class CircularProgressDrawableTest {
48    @Rule
49    public final ActivityTestRule<CircularProgressDrawableActivity> mActivityTestRule =
50            new ActivityTestRule<>(CircularProgressDrawableActivity.class);
51
52    private CircularProgressDrawable mDrawableUnderTest;
53
54    @Mock
55    Canvas mMockCanvas;
56
57    @Before
58    public void setUp() {
59        Context context = mActivityTestRule.getActivity().getApplicationContext();
60        mMockCanvas = mock(Canvas.class);
61        mDrawableUnderTest = new CircularProgressDrawable(context);
62    }
63
64    @Test
65    @SmallTest
66    public void sizeIsSquareBasedOnSmallerEdgeWithNoCenterRadius() {
67        int width = 100;
68        int height = 50;
69        mDrawableUnderTest.setBounds(new Rect(0, 0, width, height));
70        mDrawableUnderTest.draw(mMockCanvas);
71
72        ArgumentCaptor<RectF> captor = ArgumentCaptor.forClass(RectF.class);
73        verify(mMockCanvas).drawArc(captor.capture(), anyFloat(), anyFloat(), anyBoolean(),
74                any(Paint.class));
75
76        assertTrue(captor.getValue().width() == captor.getValue().height());
77        assertTrue(captor.getValue().width() <= width);
78        assertTrue(captor.getValue().width() <= height);
79    }
80
81    @Test
82    @SmallTest
83    public void setCenterRadiusFixesSize() {
84        float radius = 10f;
85        float strokeWidth = 4f;
86        mDrawableUnderTest.setCenterRadius(radius);
87        mDrawableUnderTest.setStrokeWidth(strokeWidth);
88        mDrawableUnderTest.setBounds(new Rect(0, 0, 100, 50));
89        mDrawableUnderTest.draw(mMockCanvas);
90
91        ArgumentCaptor<RectF> boundsCaptor = ArgumentCaptor.forClass(RectF.class);
92        verify(mMockCanvas).drawArc(boundsCaptor.capture(), anyFloat(), anyFloat(), anyBoolean(),
93                any(Paint.class));
94
95        assertEquals((radius + strokeWidth / 2f) * 2, boundsCaptor.getValue().width(), 0.5);
96        assertEquals((radius + strokeWidth / 2f) * 2, boundsCaptor.getValue().height(), 0.5);
97    }
98}
99