1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tv.dvr.ui; 18 19 import android.support.test.filters.SmallTest; 20 import android.support.v17.leanback.widget.ClassPresenterSelector; 21 import android.support.v17.leanback.widget.ObjectAdapter; 22 23 import junit.framework.TestCase; 24 25 import java.util.Arrays; 26 import java.util.Comparator; 27 import java.util.Objects; 28 29 /** 30 * Tests for {@link SortedArrayAdapter}. 31 */ 32 @SmallTest 33 public class SortedArrayAdapterTest extends TestCase { 34 35 public static final TestData P1 = TestData.create(1, "one"); 36 public static final TestData P2 = TestData.create(2, "before"); 37 public static final TestData P3 = TestData.create(3, "other"); 38 private TestSortedArrayAdapter mAdapter; 39 40 @Override setUp()41 protected void setUp() throws Exception { 42 super.setUp(); 43 mAdapter = new TestSortedArrayAdapter(); 44 } 45 testContents_empty()46 public void testContents_empty() { 47 assertEmpty(); 48 } 49 testAdd_one()50 public void testAdd_one() { 51 mAdapter.add(P1); 52 assertNotEmpty(); 53 assertContentsInOrder(mAdapter, P1); 54 } 55 testAdd_two()56 public void testAdd_two() { 57 mAdapter.add(P1); 58 mAdapter.add(P2); 59 assertNotEmpty(); 60 assertContentsInOrder(mAdapter, P2, P1); 61 } 62 testAddAll_two()63 public void testAddAll_two() { 64 mAdapter.addAll(Arrays.asList(P1, P2)); 65 assertNotEmpty(); 66 assertContentsInOrder(mAdapter, P2, P1); 67 } 68 testRemove()69 public void testRemove() { 70 mAdapter.add(P1); 71 mAdapter.add(P2); 72 assertNotEmpty(); 73 assertContentsInOrder(mAdapter, P2, P1); 74 mAdapter.remove(P3); 75 assertContentsInOrder(mAdapter, P2, P1); 76 mAdapter.remove(P2); 77 assertContentsInOrder(mAdapter, P1); 78 mAdapter.remove(P1); 79 assertEmpty(); 80 } 81 testChange_sorting()82 public void testChange_sorting() { 83 TestData p2_changed = TestData.create(2, "z changed"); 84 mAdapter.add(P1); 85 mAdapter.add(P2); 86 assertNotEmpty(); 87 assertContentsInOrder(mAdapter, P2, P1); 88 mAdapter.change(p2_changed); 89 assertContentsInOrder(mAdapter, P1, p2_changed); 90 } 91 testChange_new()92 public void testChange_new() { 93 mAdapter.change(P1); 94 assertNotEmpty(); 95 assertContentsInOrder(mAdapter, P1); 96 } 97 assertEmpty()98 private void assertEmpty() { 99 assertEquals("empty", true, mAdapter.isEmpty()); 100 assertContentsInOrder(mAdapter, EmptyHolder.EMPTY_HOLDER); 101 } 102 assertNotEmpty()103 private void assertNotEmpty() { 104 assertEquals("empty", false, mAdapter.isEmpty()); 105 } 106 assertContentsInOrder(ObjectAdapter adapter, Object... contents)107 private static void assertContentsInOrder(ObjectAdapter adapter, Object... contents) { 108 int ex = contents.length; 109 assertEquals("size", ex, adapter.size()); 110 for (int i = 0; i < ex; i++) { 111 assertEquals("element " + 1, contents[i], adapter.get(i)); 112 } 113 } 114 115 private static class TestData { 116 @Override toString()117 public String toString() { 118 return "TestData[" + mId + "]{" + mText + '}'; 119 } 120 create(long first, String text)121 static TestData create(long first, String text) { 122 return new TestData(first, text); 123 } 124 125 private final long mId; 126 private final String mText; 127 TestData(long id, String second)128 private TestData(long id, String second) { 129 this.mId = id; 130 this.mText = second; 131 } 132 133 @Override equals(Object o)134 public boolean equals(Object o) { 135 if (this == o) return true; 136 if (!(o instanceof TestData)) return false; 137 TestData that = (TestData) o; 138 return mId == that.mId && Objects.equals(mText, that.mText); 139 } 140 141 @Override hashCode()142 public int hashCode() { 143 return Objects.hash(mId, mText); 144 } 145 } 146 147 private static class TestSortedArrayAdapter extends SortedArrayAdapter<TestData> { 148 149 private static final Comparator<TestData> TEXT_COMPARATOR = new Comparator<TestData>() { 150 @Override 151 public int compare(TestData lhs, TestData rhs) { 152 return lhs.mText.compareTo(rhs.mText); 153 } 154 }; 155 TestSortedArrayAdapter()156 TestSortedArrayAdapter() { 157 super(new ClassPresenterSelector(), TEXT_COMPARATOR); 158 } 159 160 @Override getId(TestData item)161 long getId(TestData item) { 162 return item.mId; 163 } 164 165 } 166 } 167