First pass for CTS tests for CalendarView.

Basic structure + testing date APIs

Change-Id: I1c378a5e89c0d01f46a93f881356663e1e0cc0fe
diff --git a/tests/tests/widget/AndroidManifest.xml b/tests/tests/widget/AndroidManifest.xml
index 11057a5..dcfe4c1 100644
--- a/tests/tests/widget/AndroidManifest.xml
+++ b/tests/tests/widget/AndroidManifest.xml
@@ -360,6 +360,14 @@
             </intent-filter>
         </activity>
 
+        <activity android:name="android.widget.cts.CalendarViewCtsActivity"
+                  android:label="CalendarViewCtsActivity">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST" />
+            </intent-filter>
+        </activity>
+
         <activity android:name="android.app.ActivityGroup"
             android:label="ActivityGroup" />
 
diff --git a/tests/tests/widget/res/layout/calendarview_layout.xml b/tests/tests/widget/res/layout/calendarview_layout.xml
new file mode 100644
index 0000000..987a177
--- /dev/null
+++ b/tests/tests/widget/res/layout/calendarview_layout.xml
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2016 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
+  -->
+
+<ScrollView
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:fillViewport="true">
+
+    <LinearLayout
+        android:id="@+id/container"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="vertical">
+
+        <CalendarView
+            android:id="@+id/calendar_view_material"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content" />
+
+        <!-- CalendarView in Holo Light style for testing attributes and APIs that control
+             the deprecated visual aspects of this widget. -->
+        <CalendarView
+            style="@android:style/Widget.Holo.Light.CalendarView"
+            android:id="@+id/calendar_view_holoyolo"
+            android:layout_width="match_parent"
+            android:layout_height="320dip" />
+
+    </LinearLayout>
+
+</ScrollView>
diff --git a/tests/tests/widget/src/android/widget/cts/CalendarViewCtsActivity.java b/tests/tests/widget/src/android/widget/cts/CalendarViewCtsActivity.java
new file mode 100644
index 0000000..b0a15bf
--- /dev/null
+++ b/tests/tests/widget/src/android/widget/cts/CalendarViewCtsActivity.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2016 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 android.widget.cts;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.widget.CalendarView;
+import android.widget.Toolbar;
+
+/**
+ * A minimal application for {@link CalendarView} test.
+ */
+public class CalendarViewCtsActivity extends Activity {
+    /**
+     * Called with the activity is first created.
+     */
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.calendarview_layout);
+    }
+}
+
diff --git a/tests/tests/widget/src/android/widget/cts/CalendarViewTest.java b/tests/tests/widget/src/android/widget/cts/CalendarViewTest.java
new file mode 100644
index 0000000..c4d523d
--- /dev/null
+++ b/tests/tests/widget/src/android/widget/cts/CalendarViewTest.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2016 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 android.widget.cts;
+
+import android.app.Instrumentation;
+import android.test.ActivityInstrumentationTestCase2;
+import android.test.suitebuilder.annotation.MediumTest;
+import android.widget.CalendarView;
+
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+
+@MediumTest
+public class CalendarViewTest extends ActivityInstrumentationTestCase2<CalendarViewCtsActivity> {
+    private CalendarViewCtsActivity mActivity;
+    private CalendarView mCalendarViewMaterial;
+    private CalendarView mCalendarViewHolo;
+
+    public CalendarViewTest() {
+        super("android.widget.cts", CalendarViewCtsActivity.class);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mActivity = getActivity();
+        mCalendarViewMaterial = (CalendarView) mActivity.findViewById(R.id.calendar_view_material);
+        mCalendarViewHolo = (CalendarView) mActivity.findViewById(R.id.calendar_view_holoyolo);
+
+        // Initialize both calendar views to the current date
+        final long currentDate = new GregorianCalendar().getTime().getTime();
+        getInstrumentation().runOnMainSync(() -> {
+            mCalendarViewMaterial.setDate(currentDate);
+            mCalendarViewHolo.setDate(currentDate);
+        });
+    }
+
+    public void testConstructor() {
+        new CalendarView(mActivity);
+
+        new CalendarView(mActivity, null);
+
+        new CalendarView(mActivity, null, android.R.attr.calendarViewStyle);
+
+        new CalendarView(mActivity, null, 0, android.R.style.Widget_Material_Light_CalendarView);
+    }
+
+    public void testAccessDate() {
+        final Instrumentation instrumentation = getInstrumentation();
+
+        // Go back one year
+        final Calendar newCalendar = new GregorianCalendar();
+        newCalendar.set(Calendar.YEAR, newCalendar.get(Calendar.YEAR) - 1);
+        final long yearAgoDate = newCalendar.getTime().getTime();
+
+        instrumentation.runOnMainSync(
+                () -> mCalendarViewMaterial.setDate(yearAgoDate));
+        assertEquals(yearAgoDate, mCalendarViewMaterial.getDate());
+
+        // Go forward two years (one year from current date in aggregate)
+        newCalendar.set(Calendar.YEAR, newCalendar.get(Calendar.YEAR) + 2);
+        final long yearHenceDate = newCalendar.getTime().getTime();
+
+        instrumentation.runOnMainSync(
+                () -> mCalendarViewMaterial.setDate(yearHenceDate, true, false));
+        assertEquals(yearHenceDate, mCalendarViewMaterial.getDate());
+    }
+
+    public void testAccessMinMaxDate() {
+        final Instrumentation instrumentation = getInstrumentation();
+
+        // Use a range of minus/plus one year as min/max dates
+        final Calendar minCalendar = new GregorianCalendar();
+        minCalendar.set(Calendar.YEAR, minCalendar.get(Calendar.YEAR) - 1);
+        final Calendar maxCalendar = new GregorianCalendar();
+        maxCalendar.set(Calendar.YEAR, maxCalendar.get(Calendar.YEAR) + 1);
+
+        final long minDate = minCalendar.getTime().getTime();
+        final long maxDate = maxCalendar.getTime().getTime();
+
+        instrumentation.runOnMainSync(() -> {
+            mCalendarViewMaterial.setMinDate(minDate);
+            mCalendarViewMaterial.setMaxDate(maxDate);
+        });
+
+        assertEquals(mCalendarViewMaterial.getMinDate(), minDate);
+        assertEquals(mCalendarViewMaterial.getMaxDate(), maxDate);
+    }
+}