OSDN Git Service

Enable button only when scrollview reached the bottom
authorjackqdyulei <jackqdyulei@google.com>
Wed, 14 Dec 2016 01:07:08 +0000 (17:07 -0800)
committerjackqdyulei <jackqdyulei@google.com>
Fri, 16 Dec 2016 22:58:43 +0000 (14:58 -0800)
Bug: 28410288
Test: make RunSettingsRoboTests
Change-Id: I04653c377f7d419b6701b5589ff25961e0f5c077

res/layout/master_clear.xml
src/com/android/settings/MasterClear.java
tests/robotests/src/com/android/settings/MasterClearTest.java [new file with mode: 0644]

index 5d844b1..662b7db 100644 (file)
@@ -21,6 +21,7 @@
     >
 
     <ScrollView
+            android:id="@+id/master_clear_scrollview"
             android:layout_width="match_parent"
             android:layout_height="0dip"
             android:layout_marginStart="12dp"
index b03b583..4ed38d1 100644 (file)
@@ -31,14 +31,18 @@ import android.os.Environment;
 import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.os.UserManager;
+import android.support.annotation.VisibleForTesting;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
+import android.view.View.OnScrollChangeListener;
 import android.view.ViewGroup;
+import android.view.ViewTreeObserver.OnGlobalLayoutListener;
 import android.widget.Button;
 import android.widget.CheckBox;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
+import android.widget.ScrollView;
 import android.widget.TextView;
 
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
@@ -69,6 +73,15 @@ public class MasterClear extends OptionsMenuFragment {
     private Button mInitiateButton;
     private View mExternalStorageContainer;
     private CheckBox mExternalStorage;
+    private ScrollView mScrollView;
+
+    private final OnGlobalLayoutListener mOnGlobalLayoutListener = new OnGlobalLayoutListener() {
+        @Override
+        public void onGlobalLayout() {
+            mScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(mOnGlobalLayoutListener);
+            mInitiateButton.setEnabled(hasReachedBottom(mScrollView));
+        }
+    };
 
     /**
      * Keyguard validation is run using the standard {@link ConfirmLockPattern}
@@ -137,6 +150,7 @@ public class MasterClear extends OptionsMenuFragment {
         mInitiateButton.setOnClickListener(mInitiateListener);
         mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
         mExternalStorage = (CheckBox) mContentView.findViewById(R.id.erase_external);
+        mScrollView = (ScrollView) mContentView.findViewById(R.id.master_clear_scrollview);
 
         /*
          * If the external storage is emulated, it will be erased with a factory
@@ -175,6 +189,32 @@ public class MasterClear extends OptionsMenuFragment {
         View masterClearContainer = mContentView.findViewById(R.id.master_clear_container);
         getContentDescription(masterClearContainer, contentDescription);
         masterClearContainer.setContentDescription(contentDescription);
+
+        // Set the status of initiateButton based on scrollview
+        mScrollView.setOnScrollChangeListener(new OnScrollChangeListener() {
+            @Override
+            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX,
+                int oldScrollY) {
+                if (v instanceof ScrollView && hasReachedBottom((ScrollView) v)) {
+                    mInitiateButton.setEnabled(true);
+                }
+            }
+        });
+
+        // Set the initial state of the initiateButton
+        mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
+    }
+
+    @VisibleForTesting
+    boolean hasReachedBottom(final ScrollView scrollView) {
+        if (scrollView.getChildCount() < 1) {
+            return true;
+        }
+
+        final View view = scrollView.getChildAt(0);
+        final int diff = view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY());
+
+        return diff <= 0;
     }
 
     private void getContentDescription(View v, StringBuffer description) {
diff --git a/tests/robotests/src/com/android/settings/MasterClearTest.java b/tests/robotests/src/com/android/settings/MasterClearTest.java
new file mode 100644 (file)
index 0000000..721d0e0
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * 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 com.android.settings;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Mockito.when;
+
+import android.widget.LinearLayout;
+import android.widget.ScrollView;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.annotation.Config;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class MasterClearTest {
+    private MasterClear mMasterClear;
+    @Mock
+    private ScrollView mScrollView;
+    @Mock
+    private LinearLayout mLinearLayout;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mMasterClear = new MasterClear();
+
+        // Make scrollView only have one child
+        when(mScrollView.getChildAt(0)).thenReturn(mLinearLayout);
+        when(mScrollView.getChildCount()).thenReturn(1);
+    }
+
+    @Test
+    public void testHasReachedBottom_NotScrollDown_returnFalse() {
+        initScrollView(100, 0, 200);
+
+        assertThat(mMasterClear.hasReachedBottom(mScrollView)).isFalse();
+    }
+
+    @Test
+    public void testHasReachedBottom_CanNotScroll_returnTrue() {
+        initScrollView(100, 0, 80);
+
+        assertThat(mMasterClear.hasReachedBottom(mScrollView)).isTrue();
+    }
+
+    @Test
+    public void testHasReachedBottom_ScrollToBottom_returnTrue() {
+        initScrollView(100, 100, 200);
+
+        assertThat(mMasterClear.hasReachedBottom(mScrollView)).isTrue();
+    }
+
+    private void initScrollView(int height, int scrollY, int childBottom) {
+        when(mScrollView.getHeight()).thenReturn(height);
+        when(mScrollView.getScrollY()).thenReturn(scrollY);
+        when(mLinearLayout.getBottom()).thenReturn(childBottom);
+    }
+}