alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Android example source code file (ListButtonsDiagonalAcrossItemsTest.java)

This example Android source code file (ListButtonsDiagonalAcrossItemsTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Android by Example" TM.

Java - Android tags/keywords

activityinstrumentationtestcase, android, button, exception, listbuttonsdiagonalacrossitemstest, listview, mediumtest, override, test, ui, view, widget

The ListButtonsDiagonalAcrossItemsTest.java Android example source code

/*
 * Copyright (C) 2008 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.listview.focus;

import android.widget.listview.ListButtonsDiagonalAcrossItems;

import android.test.ActivityInstrumentationTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.view.FocusFinder;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

/**
 * Test that ListView will override default behavior of focus searching to
 * make sure going right and left doesn't change selection
 */
public class ListButtonsDiagonalAcrossItemsTest extends ActivityInstrumentationTestCase<ListButtonsDiagonalAcrossItems> {

    private Button mLeftButton;
    private Button mCenterButton;
    private Button mRightButton;
    private ListView mListView;

    public ListButtonsDiagonalAcrossItemsTest() {
        super("com.android.frameworks.coretests", ListButtonsDiagonalAcrossItems.class);
    }

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

        mLeftButton = getActivity().getLeftButton();
        mCenterButton = getActivity().getCenterButton();
        mRightButton = getActivity().getRightButton();

        mListView = getActivity().getListView();
    }

    @MediumTest
    public void testPreconditions() {
        final ListView lv = mListView;
        assertEquals("num children", 3, lv.getChildCount());

        assertEquals("selected position", 0, lv.getSelectedItemPosition());
        assertTrue("left button focused", mLeftButton.isFocused());

        assertTrue("left left of center",
                mLeftButton.getRight()
                        < mCenterButton.getLeft());

        assertTrue("center left of right",
                mCenterButton.getRight()
                        < mRightButton.getLeft());

        assertEquals("focus search right from left button should be center button",
            mCenterButton,
            FocusFinder.getInstance().findNextFocus(mListView, mLeftButton, View.FOCUS_RIGHT));
        assertEquals("focus search right from center button should be right button",
            mRightButton,
            FocusFinder.getInstance().findNextFocus(mListView, mCenterButton, View.FOCUS_RIGHT));
        assertEquals("focus search left from centr button should be left button",
            mLeftButton,
            FocusFinder.getInstance().findNextFocus(mListView, mCenterButton, View.FOCUS_LEFT));
    }

    @MediumTest
    public void testGoingRightDoesNotChangeSelection() {
        sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);

        assertEquals("selected position shouldn't have changed",
                0,
                mListView.getSelectedItemPosition());
        assertTrue("left should still be focused", mLeftButton.isFocused());
    }

    @MediumTest
    public void testGoingLeftDoesNotChangeSelection() {
        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
        assertEquals("list view postion", 1, mListView.getSelectedItemPosition());
        assertTrue("mCenterButton.isFocused()", mCenterButton.isFocused());

        sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
        assertEquals("selected position shouldn't have changed",
                1,
                mListView.getSelectedItemPosition());
        assertTrue("center should still be focused", mCenterButton.isFocused());
    }
    
}

Other Android examples (source code examples)

Here is a short list of links related to this Android ListButtonsDiagonalAcrossItemsTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.