Add test case for multiple row horizontal grid view

Add test case for update dataset at last row with empty space.

b/18631342
b/18718926

Change-Id: I3c745a41b0486cf293fc6a5a595e50f0a14ee0d0
diff --git a/samples/SupportLeanbackDemos/AndroidManifest.xml b/samples/SupportLeanbackDemos/AndroidManifest.xml
index 7e5f6a6..391bcb7 100644
--- a/samples/SupportLeanbackDemos/AndroidManifest.xml
+++ b/samples/SupportLeanbackDemos/AndroidManifest.xml
@@ -46,5 +46,7 @@
         <activity android:name="BrowseErrorActivity"
                   android:exported="true" />
 
+        <activity android:name="HorizontalGridTestActivity"
+            android:exported="true" />
     </application>
 </manifest>
diff --git a/samples/SupportLeanbackDemos/res/layout/horizontal_grid.xml b/samples/SupportLeanbackDemos/res/layout/horizontal_grid.xml
new file mode 100644
index 0000000..7faea03
--- /dev/null
+++ b/samples/SupportLeanbackDemos/res/layout/horizontal_grid.xml
@@ -0,0 +1,23 @@
+<RelativeLayout 
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:lb="http://schemas.android.com/apk/res-auto"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    >
+  <android.support.v17.leanback.widget.HorizontalGridView
+      android:id="@+id/gridview"
+      android:layout_width="match_parent"
+      android:layout_height="match_parent"
+      android:clipToPadding="false"
+      android:focusable="true"
+      android:focusableInTouchMode="true"
+      android:background="#00ffff"
+      lb:horizontalMargin="12dip"
+      lb:verticalMargin="24dip"
+      lb:numberOfRows="3"
+      lb:rowHeight="150dip"
+      android:paddingBottom="12dip"
+      android:paddingLeft="12dip"
+      android:paddingRight="12dip"
+      android:paddingTop="12dip" />
+</RelativeLayout>
diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/HorizontalGridTestActivity.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/HorizontalGridTestActivity.java
new file mode 100644
index 0000000..59155af
--- /dev/null
+++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/HorizontalGridTestActivity.java
@@ -0,0 +1,172 @@
+/*
+ * Copyright (C) 2013 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.example.android.leanback;
+
+import android.support.v7.widget.RecyclerView;
+import android.support.v17.leanback.widget.HorizontalGridView;
+import android.support.v17.leanback.widget.OnChildSelectedListener;
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.graphics.BitmapFactory;
+import android.graphics.Color;
+import android.graphics.drawable.ColorDrawable;
+import android.graphics.drawable.Drawable;
+import android.net.Uri;
+import android.os.Bundle;
+import android.util.Log;
+import android.util.SparseArray;
+import android.view.MotionEvent;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.view.View.OnFocusChangeListener;
+import android.view.ViewGroup;
+import android.widget.ImageView;
+import android.widget.TextView;
+
+import java.io.File;
+
+public class HorizontalGridTestActivity extends Activity {
+    private static final String TAG = "HorizontalGridTestActivity";
+    private static final boolean DEBUG = true;
+    private static final String SELECT_ACTION = "android.test.leanback.widget.SELECT";
+    private static final int NUM_ITEMS = 100;
+    private static final boolean STAGGERED = true;
+
+    private HorizontalGridView mHorizontalGridView;
+    private int mScrollState = RecyclerView.SCROLL_STATE_IDLE;
+
+    private RecyclerView.OnScrollListener mScrollListener = new RecyclerView.OnScrollListener() {
+        @Override
+        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
+            if (DEBUG) {
+                final String[] stateNames = { "IDLE", "DRAGGING", "SETTLING" };
+                Log.v(TAG, "onScrollStateChanged "
+                        + (newState < stateNames.length ? stateNames[newState] : newState));
+            }
+            mScrollState = newState;
+        }
+    };
+
+    private View createView() {
+        View view = getLayoutInflater().inflate(R.layout.horizontal_grid, null, false);
+        mHorizontalGridView = (HorizontalGridView) view.findViewById(R.id.gridview);
+
+        mHorizontalGridView.setWindowAlignment(HorizontalGridView.WINDOW_ALIGN_BOTH_EDGE);
+        mHorizontalGridView.setWindowAlignmentOffsetPercent(35);
+        mHorizontalGridView.setOnChildSelectedListener(new OnChildSelectedListener() {
+            @Override
+            public void onChildSelected(ViewGroup parent, View view, int position, long id) {
+                if (DEBUG) Log.d(TAG, "onChildSelected position=" + position +  " id="+id);
+            }
+        });
+        return view;
+    }
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        if (DEBUG) Log.v(TAG, "onCreate");
+
+        RecyclerView.Adapter adapter = new MyAdapter();
+
+        View view = createView();
+
+        mHorizontalGridView.setAdapter(new MyAdapter());
+        setContentView(view);
+
+        mHorizontalGridView.setOnScrollListener(mScrollListener);
+    }
+
+    @Override
+    protected void onNewIntent(Intent intent) {
+        if (DEBUG) Log.v(TAG, "onNewIntent " + intent);
+        if (intent.getAction().equals(SELECT_ACTION)) {
+            int position = intent.getIntExtra("SELECT_POSITION", -1);
+            if (position >= 0) {
+                mHorizontalGridView.setSelectedPosition(position);
+            }
+        }
+        super.onNewIntent(intent);
+    }
+
+    private OnFocusChangeListener mItemFocusChangeListener = new OnFocusChangeListener() {
+
+        @Override
+        public void onFocusChange(View v, boolean hasFocus) {
+            if (hasFocus) {
+                v.setBackgroundColor(Color.YELLOW);
+            } else {
+                v.setBackgroundColor(Color.LTGRAY);
+            }
+        }
+    };
+
+    private OnClickListener mItemClickListener = new OnClickListener() {
+        @Override
+        public void onClick(View v) {
+            mHorizontalGridView.getAdapter().notifyDataSetChanged();
+        }
+    };
+
+    class MyAdapter extends RecyclerView.Adapter {
+
+        private int[] mItemLengths;
+
+        MyAdapter() {
+            mItemLengths = new int[NUM_ITEMS];
+            for (int i = 0; i < mItemLengths.length; i++) {
+                mItemLengths[i] = STAGGERED ? (int)(Math.random() * 180) + 180 : 240;
+            }
+        }
+
+        @Override
+        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
+            if (DEBUG) Log.v(TAG, "createViewHolder " + viewType);
+            TextView textView = new TextView(parent.getContext());
+            textView.setTextColor(Color.BLACK);
+            textView.setFocusable(true);
+            textView.setFocusableInTouchMode(true);
+            textView.setOnFocusChangeListener(mItemFocusChangeListener);
+            textView.setOnClickListener(mItemClickListener);
+            return new ViewHolder(textView);
+        }
+
+        @Override
+        public void onBindViewHolder(RecyclerView.ViewHolder baseHolder, int position) {
+            if (DEBUG) Log.v(TAG, "bindViewHolder " + position + " " + baseHolder);
+            ViewHolder holder = (ViewHolder) baseHolder;
+            ((TextView) holder.itemView).setText("Item "+position);
+            holder.itemView.setBackgroundColor(Color.LTGRAY);
+            holder.itemView.setLayoutParams(new ViewGroup.MarginLayoutParams(mItemLengths[position],
+                    80));
+        }
+
+        @Override
+        public int getItemCount() {
+            return mItemLengths.length;
+        }
+    }
+
+    static class ViewHolder extends RecyclerView.ViewHolder {
+
+        public ViewHolder(View v) {
+            super(v);
+        }
+    }
+}
diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/VerticalGridFragment.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/VerticalGridFragment.java
index ae41b11..9ffaf66 100644
--- a/samples/SupportLeanbackDemos/src/com/example/android/leanback/VerticalGridFragment.java
+++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/VerticalGridFragment.java
@@ -35,7 +35,15 @@
     private static final int NUM_ITEMS = 50;
     private static final int HEIGHT = 200;
 
-    private ArrayObjectAdapter mAdapter;
+    private static class Adapter extends ArrayObjectAdapter {
+        public Adapter(StringPresenter presenter) {
+            super(presenter);
+        }
+        public void callNotifyChanged() {
+            super.notifyChanged();
+        }
+    }
+    private Adapter mAdapter;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
@@ -53,7 +61,7 @@
         gridPresenter.setNumberOfColumns(NUM_COLUMNS);
         setGridPresenter(gridPresenter);
 
-        mAdapter = new ArrayObjectAdapter(new StringPresenter());
+        mAdapter = new Adapter(new StringPresenter());
         for (int i = 0; i < NUM_ITEMS; i++) {
             mAdapter.add(Integer.toString(i));
         }
@@ -72,6 +80,7 @@
             public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
                     RowPresenter.ViewHolder rowViewHolder, Row row) {
                 Log.i(TAG, "onItemClicked: " + item + " row " + row);
+                mAdapter.callNotifyChanged();
             }
         });
         setOnSearchClickedListener(new View.OnClickListener() {