Skip to content

Commit

Permalink
Tests / Adapted example
Browse files Browse the repository at this point in the history
  • Loading branch information
nhaarman committed Aug 2, 2014
1 parent 812cbcb commit 278fbf8
Show file tree
Hide file tree
Showing 16 changed files with 701 additions and 21 deletions.
18 changes: 10 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
language: android
jdk: oraclejdk7

android:
components:
- build-tools-19.1.0
- doc-19

env:
matrix:
- ANDROID_SDKS=sysimg-19 ANDROID_TARGET=android-19 ANDROID_ABI=armeabi-v7a SKIN=WXGA800 LINT=true MAVEN=true
- ANDROID_TARGET=15 LINT=false MAVEN=false
- ANDROID_TARGET=16 LINT=false MAVEN=false
- ANDROID_TARGET=17 LINT=false MAVEN=false
- ANDROID_TARGET=18 LINT=false MAVEN=false
- ANDROID_TARGET=19 LINT=true MAVEN=true
global:
- TERM=dumb
- COMPONENTS=build-tools-20.0.0,android-20,extra-android-m2repository,extra-android-support,extra-google-m2repository
- LICENSES="android-sdk-license-5be876d5|android-sdk-preview-license-52d11cd2"
- secure: Fo/btrr+HpwmQL6TNxZuh/TWRMouz9Z1RCo77tf3crNP4OHq9EAkduLc/b4i6HCGFM3fyfJ7AM5XMa3OQvXfF84FQnew7NGE6zB/CKFfKBIZyzntvrDg4VqWEAHzunT6+eBnGtxtrAE0bBo4MmUCklXWP8j5UAyjfFpSeJdOywY=
- secure: s56rqSiiwX40CQWeHTcbyVLRtwLP/pcHWqaVCKuXtZ/m9SgRI/SHp9o8ITI2dzr3syEeAkHjkJOkHQSpH0bHi1xKY4M9ODcCugwv+yDEELw1qmI6Cvwjvj1cKsG7ebt39OnCkz+TqhtI/iOEkxq7mTizFjNIxIImMkKxeQwcFgE=
- secure: UOQ9keLBuJw5Ed/eYtXarwoGuJSSvgGTob+D9aU8hVRoTUXpSDuHtk9TdS4MNXQ33jgFZEsmkpwRmV54XVEGe6xcri1uys1JDzjc+SqTEgbEh1QktfIWGvOx49YZtI2Zei0324S2qXRtemKMw4N+1XRRMP/IXEogr26Xi5JLVcI=

before_install:
- curl -3L https://raw.github.com/embarkmobile/android-sdk-installer/version-2/android-sdk-installer | bash /dev/stdin --install=$COMPONENTS,android-$ANDROID_TARGET,sys-img-armeabi-v7a-android-$ANDROID_TARGET --accept=$LICENSES && source ~/.android-sdk-installer/env
- curl -3L -o wait_for_emulator https://github.com/embarkmobile/android-sdk-installer/raw/version-2/wait_for_emulator
- sudo chmod +x wait_for_emulator

# Start the emulator
- echo no | android create avd --force -n test -t $ANDROID_TARGET --abi $ANDROID_ABI
- emulator -avd test -no-skin -no-audio -no-window &
- echo no | android create avd --force -n test -t android-$ANDROID_TARGET --abi armeabi-v7a
- emulator -avd test -no-audio -no-window &

before_script:
# Downloads gradle stuff
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013 Niek Haarman
* Copyright 2014 Niek Haarman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,13 +15,21 @@
*/
package com.haarman.listviewanimations.itemmanipulation;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.haarman.listviewanimations.MyListActivity;
import com.haarman.listviewanimations.R;
import com.nhaarman.listviewanimations.ArrayAdapter;
import com.nhaarman.listviewanimations.itemmanipulation.widget.DynamicListView;
import com.nhaarman.listviewanimations.itemmanipulation.dragdrop.DynamicListView;
import com.nhaarman.listviewanimations.itemmanipulation.dragdrop.OnItemMovedListener;
import com.nhaarman.listviewanimations.itemmanipulation.dragdrop.TouchViewDraggableManager;
import com.nhaarman.listviewanimations.swinginadapters.simple.AlphaInAnimationAdapter;

public class DragAndDropActivity extends MyListActivity {
Expand All @@ -31,32 +39,88 @@ protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draganddrop);

DynamicListView listView = (DynamicListView) findViewById(R.id.activity_draganddrop_listview);
final DynamicListView listView = (DynamicListView) findViewById(R.id.activity_draganddrop_listview);
listView.setDivider(null);

final ArrayAdapter<String> adapter = createListAdapter();
final ArrayAdapter<String> adapter = new DragAndDropListAdapter(this);
AlphaInAnimationAdapter animAdapter = new AlphaInAnimationAdapter(adapter);
animAdapter.setAbsListView(listView);

assert animAdapter.getViewAnimator() != null;
animAdapter.getViewAnimator().setInitialDelayMillis(300);

listView.setAdapter(animAdapter);
listView.setOnItemLongClickListener(new MyOnItemLongClickListener(listView));
listView.setDraggableManager(new TouchViewDraggableManager(R.id.list_row_draganddrop_touchview));

listView.setOnItemMovedListener(new MyOnItemMovedListener(adapter));

Toast.makeText(this, getString(R.string.long_press_to_drag), Toast.LENGTH_LONG).show();
}

private class MyOnItemMovedListener implements DynamicListView.OnItemMovedListener {
private static class DragAndDropListAdapter extends ArrayAdapter<String> {
private final Context mContext;

DragAndDropListAdapter(final Context context) {
mContext = context;
for (int i = 0; i < 1000; i++) {
add(mContext.getString(R.string.row_number, i));
}
}

@Override
public long getItemId(final int position) {
return getItem(position).hashCode();
}

@Override
public boolean hasStableIds() {
return true;
}

@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
View view = convertView;
if (view == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.list_row_draganddrop, parent, false);
}

((TextView) view.findViewById(R.id.list_row_draganddrop_textview)).setText(getItem(position));

return view;
}
}

private static class MyOnItemLongClickListener implements AdapterView.OnItemLongClickListener {
private final DynamicListView mListView;

MyOnItemLongClickListener(final DynamicListView listView) {
mListView = listView;
}

@Override
public boolean onItemLongClick(final AdapterView<?> parent, final View view, final int position, final long id) {
mListView.startDragging(position);
return true;
}
}

private class MyOnItemMovedListener implements OnItemMovedListener {
private final ArrayAdapter<String> mAdapter;
private Toast mToast;

MyOnItemMovedListener(final ArrayAdapter<String> adapter) {
mAdapter = adapter;
}

@Override
public void onItemMoved(final int newPosition) {
Toast.makeText(getApplicationContext(), getString(R.string.moved, mAdapter.getItem(newPosition), newPosition), Toast.LENGTH_SHORT).show();
public void onItemMoved(final int originalPosition, final int newPosition) {
if (mToast != null) {
mToast.cancel();
}

mToast = Toast.makeText(getApplicationContext(), getString(R.string.moved, mAdapter.getItem(newPosition), newPosition), Toast.LENGTH_SHORT);
mToast.show();
}
}
}
25 changes: 21 additions & 4 deletions example/src/main/res/layout/activity_draganddrop.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2014 Niek Haarman
~
~ 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.
-->

<com.nhaarman.listviewanimations.itemmanipulation.widget.DynamicListView
<merge xmlns:android="http://schemas.android.com/apk/res/android">

<com.nhaarman.listviewanimations.itemmanipulation.dragdrop.DynamicListView
android:id="@+id/activity_draganddrop_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/paddingTop"
android:divider="@null" />
android:clipToPadding="false"
android:divider="@null"
android:paddingBottom="48dp" />

</merge>
43 changes: 43 additions & 0 deletions example/src/main/res/layout/list_row_draganddrop.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2014 Niek Haarman
~
~ 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.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="UseCompoundDrawables">

<com.nhaarman.listviewanimations.itemmanipulation.dragdrop.GripView
android:id="@+id/list_row_draganddrop_touchview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:color="@android:color/darker_gray"
android:paddingBottom="4dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="4dp" />

<TextView
android:id="@+id/list_row_draganddrop_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_vertical"
android:minHeight="48dp"
android:textColor="?android:attr/textColorSecondary"
android:textSize="20sp"
tools:ignore="UnusedAttribute" />
</LinearLayout>
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2014 Niek Haarman
*
* 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.nhaarman.listviewanimations.itemmanipulation;

import android.util.Pair;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2014 Niek Haarman
*
* 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.nhaarman.listviewanimations.itemmanipulation.dragdrop;

import android.support.annotation.NonNull;
import android.test.ActivityInstrumentationTestCase2;
import android.view.View;

import org.mockito.*;

import static org.mockito.Mockito.*;

public class DynamicListViewTest extends ActivityInstrumentationTestCase2<DynamicListViewTestActivity> {

private DynamicListView mDynamicListView;

@Mock
private OnItemMovedListener mOnItemMovedListener;


public DynamicListViewTest() {
super(DynamicListViewTestActivity.class);
}

@Override
public void setUp() throws Exception {
super.setUp();

MockitoAnnotations.initMocks(this);

mDynamicListView = getActivity().getDynamicListView();
mDynamicListView.setDraggableManager(new MyDraggableManager());
mDynamicListView.setOnItemMovedListener(mOnItemMovedListener);

getInstrumentation().waitForIdleSync();
Thread.sleep(5000);
}

public void testOnItemMovedListenerCalled() throws InterruptedException {
MotionEventUtils.dispatchDragMotionEvents(getInstrumentation(), mDynamicListView, 0, 1);
verify(mOnItemMovedListener).onItemMoved(0, 1);
}

public void testReverseOnItemMovedListenerCalled() throws InterruptedException {
MotionEventUtils.dispatchDragMotionEvents(getInstrumentation(), mDynamicListView, 2, 1);
verify(mOnItemMovedListener).onItemMoved(2, 1);
}

public void testOnItemMovedListenerCalledMultipleItems() throws InterruptedException {
MotionEventUtils.dispatchDragMotionEvents(getInstrumentation(), mDynamicListView, 1, 5);
verify(mOnItemMovedListener).onItemMoved(1, 5);
}

public void testScroll() throws InterruptedException {
MotionEventUtils.dispatchDragScrollDownMotionEvents(getInstrumentation(), mDynamicListView, 1);
verify(mOnItemMovedListener).onItemMoved(1, 19);
}

private static class MyDraggableManager implements DraggableManager {

@Override
public boolean isDraggable(@NonNull final View view, final int position, final float x, final float y) {
return true;
}
}
}
Loading

0 comments on commit 278fbf8

Please sign in to comment.