| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.history; | 5 package org.chromium.chrome.browser.history; |
| 6 | 6 |
| 7 import static org.chromium.chrome.browser.widget.DateDividedAdapter.TYPE_DATE; | 7 import static org.chromium.chrome.browser.widget.DateDividedAdapter.TYPE_DATE; |
| 8 import static org.chromium.chrome.browser.widget.DateDividedAdapter.TYPE_HEADER; | 8 import static org.chromium.chrome.browser.widget.DateDividedAdapter.TYPE_HEADER; |
| 9 import static org.chromium.chrome.browser.widget.DateDividedAdapter.TYPE_NORMAL; | 9 import static org.chromium.chrome.browser.widget.DateDividedAdapter.TYPE_NORMAL; |
| 10 | 10 |
| 11 import android.support.test.filters.SmallTest; | 11 import android.support.test.filters.SmallTest; |
| 12 import android.test.InstrumentationTestCase; | 12 |
| 13 import org.junit.Assert; |
| 14 import org.junit.Before; |
| 15 import org.junit.Test; |
| 16 import org.junit.runner.RunWith; |
| 13 | 17 |
| 14 import org.chromium.base.ContextUtils; | 18 import org.chromium.base.ContextUtils; |
| 15 import org.chromium.base.ThreadUtils; | 19 import org.chromium.base.ThreadUtils; |
| 16 import org.chromium.chrome.R; | 20 import org.chromium.chrome.R; |
| 17 import org.chromium.chrome.browser.widget.selection.SelectionDelegate; | 21 import org.chromium.chrome.browser.widget.selection.SelectionDelegate; |
| 22 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
| 18 | 23 |
| 19 import java.util.Date; | 24 import java.util.Date; |
| 20 import java.util.concurrent.TimeUnit; | 25 import java.util.concurrent.TimeUnit; |
| 21 | 26 |
| 22 /** | 27 /** |
| 23 * Tests for the {@link HistoryAdapter}. | 28 * Tests for the {@link HistoryAdapter}. |
| 24 */ | 29 */ |
| 25 public class HistoryAdapterTest extends InstrumentationTestCase { | 30 @RunWith(ChromeJUnit4ClassRunner.class) |
| 31 public class HistoryAdapterTest { |
| 26 private StubbedHistoryProvider mHistoryProvider; | 32 private StubbedHistoryProvider mHistoryProvider; |
| 27 private HistoryAdapter mAdapter; | 33 private HistoryAdapter mAdapter; |
| 28 | 34 |
| 29 @Override | 35 @Before |
| 30 public void setUp() throws Exception { | 36 public void setUp() throws Exception { |
| 31 super.setUp(); | |
| 32 mHistoryProvider = new StubbedHistoryProvider(); | 37 mHistoryProvider = new StubbedHistoryProvider(); |
| 33 mAdapter = new HistoryAdapter(new SelectionDelegate<HistoryItem>(), null
, mHistoryProvider); | 38 mAdapter = new HistoryAdapter(new SelectionDelegate<HistoryItem>(), null
, mHistoryProvider); |
| 34 } | 39 } |
| 35 | 40 |
| 36 private void initializeAdapter() { | 41 private void initializeAdapter() { |
| 37 ThreadUtils.runOnUiThreadBlocking(new Runnable(){ | 42 ThreadUtils.runOnUiThreadBlocking(new Runnable(){ |
| 38 @Override | 43 @Override |
| 39 public void run() { | 44 public void run() { |
| 40 mAdapter.initialize(); | 45 mAdapter.initialize(); |
| 41 } | 46 } |
| 42 }); | 47 }); |
| 43 } | 48 } |
| 44 | 49 |
| 50 @Test |
| 45 @SmallTest | 51 @SmallTest |
| 46 public void testInitialize_Empty() { | 52 public void testInitialize_Empty() { |
| 47 initializeAdapter(); | 53 initializeAdapter(); |
| 48 checkAdapterContents(false); | 54 checkAdapterContents(false); |
| 49 } | 55 } |
| 50 | 56 |
| 57 @Test |
| 51 @SmallTest | 58 @SmallTest |
| 52 public void testInitialize_SingleItem() { | 59 public void testInitialize_SingleItem() { |
| 53 Date today = new Date(); | 60 Date today = new Date(); |
| 54 long[] timestamps = {today.getTime(), today.getTime() - 100}; | 61 long[] timestamps = {today.getTime(), today.getTime() - 100}; |
| 55 HistoryItem item1 = StubbedHistoryProvider.createHistoryItem(0, timestam
ps); | 62 HistoryItem item1 = StubbedHistoryProvider.createHistoryItem(0, timestam
ps); |
| 56 mHistoryProvider.addItem(item1); | 63 mHistoryProvider.addItem(item1); |
| 57 | 64 |
| 58 initializeAdapter(); | 65 initializeAdapter(); |
| 59 | 66 |
| 60 // There should be three items - the header, a date and the history item
. | 67 // There should be three items - the header, a date and the history item
. |
| 61 checkAdapterContents(true, null, null, item1); | 68 checkAdapterContents(true, null, null, item1); |
| 62 } | 69 } |
| 63 | 70 |
| 71 @Test |
| 64 @SmallTest | 72 @SmallTest |
| 65 public void testRemove_TwoItemsOneDate() { | 73 public void testRemove_TwoItemsOneDate() { |
| 66 Date today = new Date(); | 74 Date today = new Date(); |
| 67 long[] timestamps = {today.getTime()}; | 75 long[] timestamps = {today.getTime()}; |
| 68 HistoryItem item1 = StubbedHistoryProvider.createHistoryItem(0, timestam
ps); | 76 HistoryItem item1 = StubbedHistoryProvider.createHistoryItem(0, timestam
ps); |
| 69 mHistoryProvider.addItem(item1); | 77 mHistoryProvider.addItem(item1); |
| 70 | 78 |
| 71 HistoryItem item2 = StubbedHistoryProvider.createHistoryItem(1, timestam
ps); | 79 HistoryItem item2 = StubbedHistoryProvider.createHistoryItem(1, timestam
ps); |
| 72 mHistoryProvider.addItem(item2); | 80 mHistoryProvider.addItem(item2); |
| 73 | 81 |
| 74 initializeAdapter(); | 82 initializeAdapter(); |
| 75 | 83 |
| 76 // There should be four items - the list header, a date header and two h
istory items. | 84 // There should be four items - the list header, a date header and two h
istory items. |
| 77 checkAdapterContents(true, null, null, item1, item2); | 85 checkAdapterContents(true, null, null, item1, item2); |
| 78 | 86 |
| 79 mAdapter.markItemForRemoval(item1); | 87 mAdapter.markItemForRemoval(item1); |
| 80 | 88 |
| 81 // Check that one item was removed. | 89 // Check that one item was removed. |
| 82 checkAdapterContents(true, null, null, item2); | 90 checkAdapterContents(true, null, null, item2); |
| 83 assertEquals(1, mHistoryProvider.markItemForRemovalCallback.getCallCount
()); | 91 Assert.assertEquals(1, mHistoryProvider.markItemForRemovalCallback.getCa
llCount()); |
| 84 assertEquals(0, mHistoryProvider.removeItemsCallback.getCallCount()); | 92 Assert.assertEquals(0, mHistoryProvider.removeItemsCallback.getCallCount
()); |
| 85 | 93 |
| 86 mAdapter.markItemForRemoval(item2); | 94 mAdapter.markItemForRemoval(item2); |
| 87 | 95 |
| 88 // There should no longer be any items in the adapter. | 96 // There should no longer be any items in the adapter. |
| 89 checkAdapterContents(false); | 97 checkAdapterContents(false); |
| 90 assertEquals(2, mHistoryProvider.markItemForRemovalCallback.getCallCount
()); | 98 Assert.assertEquals(2, mHistoryProvider.markItemForRemovalCallback.getCa
llCount()); |
| 91 assertEquals(0, mHistoryProvider.removeItemsCallback.getCallCount()); | 99 Assert.assertEquals(0, mHistoryProvider.removeItemsCallback.getCallCount
()); |
| 92 | 100 |
| 93 mAdapter.removeItems(); | 101 mAdapter.removeItems(); |
| 94 assertEquals(1, mHistoryProvider.removeItemsCallback.getCallCount()); | 102 Assert.assertEquals(1, mHistoryProvider.removeItemsCallback.getCallCount
()); |
| 95 } | 103 } |
| 96 | 104 |
| 105 @Test |
| 97 @SmallTest | 106 @SmallTest |
| 98 public void testRemove_TwoItemsTwoDates() { | 107 public void testRemove_TwoItemsTwoDates() { |
| 99 Date today = new Date(); | 108 Date today = new Date(); |
| 100 long[] timestamps = {today.getTime()}; | 109 long[] timestamps = {today.getTime()}; |
| 101 HistoryItem item1 = StubbedHistoryProvider.createHistoryItem(0, timestam
ps); | 110 HistoryItem item1 = StubbedHistoryProvider.createHistoryItem(0, timestam
ps); |
| 102 mHistoryProvider.addItem(item1); | 111 mHistoryProvider.addItem(item1); |
| 103 | 112 |
| 104 long[] timestamps2 = {today.getTime() - TimeUnit.DAYS.toMillis(3)}; | 113 long[] timestamps2 = {today.getTime() - TimeUnit.DAYS.toMillis(3)}; |
| 105 HistoryItem item2 = StubbedHistoryProvider.createHistoryItem(1, timestam
ps2); | 114 HistoryItem item2 = StubbedHistoryProvider.createHistoryItem(1, timestam
ps2); |
| 106 mHistoryProvider.addItem(item2); | 115 mHistoryProvider.addItem(item2); |
| 107 | 116 |
| 108 initializeAdapter(); | 117 initializeAdapter(); |
| 109 | 118 |
| 110 // There should be five items - the list header, a date header, a histor
y item, another | 119 // There should be five items - the list header, a date header, a histor
y item, another |
| 111 // date header and another history item. | 120 // date header and another history item. |
| 112 checkAdapterContents(true, null, null, item1, null, item2); | 121 checkAdapterContents(true, null, null, item1, null, item2); |
| 113 | 122 |
| 114 mAdapter.markItemForRemoval(item1); | 123 mAdapter.markItemForRemoval(item1); |
| 115 | 124 |
| 116 // Check that the first item and date header were removed. | 125 // Check that the first item and date header were removed. |
| 117 checkAdapterContents(true, null, null, item2); | 126 checkAdapterContents(true, null, null, item2); |
| 118 assertEquals(1, mHistoryProvider.markItemForRemovalCallback.getCallCount
()); | 127 Assert.assertEquals(1, mHistoryProvider.markItemForRemovalCallback.getCa
llCount()); |
| 119 assertEquals(0, mHistoryProvider.removeItemsCallback.getCallCount()); | 128 Assert.assertEquals(0, mHistoryProvider.removeItemsCallback.getCallCount
()); |
| 120 | 129 |
| 121 mAdapter.markItemForRemoval(item2); | 130 mAdapter.markItemForRemoval(item2); |
| 122 | 131 |
| 123 // There should no longer be any items in the adapter. | 132 // There should no longer be any items in the adapter. |
| 124 checkAdapterContents(false); | 133 checkAdapterContents(false); |
| 125 assertEquals(2, mHistoryProvider.markItemForRemovalCallback.getCallCount
()); | 134 Assert.assertEquals(2, mHistoryProvider.markItemForRemovalCallback.getCa
llCount()); |
| 126 assertEquals(0, mHistoryProvider.removeItemsCallback.getCallCount()); | 135 Assert.assertEquals(0, mHistoryProvider.removeItemsCallback.getCallCount
()); |
| 127 | 136 |
| 128 mAdapter.removeItems(); | 137 mAdapter.removeItems(); |
| 129 assertEquals(1, mHistoryProvider.removeItemsCallback.getCallCount()); | 138 Assert.assertEquals(1, mHistoryProvider.removeItemsCallback.getCallCount
()); |
| 130 } | 139 } |
| 131 | 140 |
| 141 @Test |
| 132 @SmallTest | 142 @SmallTest |
| 133 public void testSearch() { | 143 public void testSearch() { |
| 134 Date today = new Date(); | 144 Date today = new Date(); |
| 135 long[] timestamps = {today.getTime()}; | 145 long[] timestamps = {today.getTime()}; |
| 136 HistoryItem item1 = StubbedHistoryProvider.createHistoryItem(0, timestam
ps); | 146 HistoryItem item1 = StubbedHistoryProvider.createHistoryItem(0, timestam
ps); |
| 137 mHistoryProvider.addItem(item1); | 147 mHistoryProvider.addItem(item1); |
| 138 | 148 |
| 139 long[] timestamps2 = {today.getTime() - TimeUnit.DAYS.toMillis(3)}; | 149 long[] timestamps2 = {today.getTime() - TimeUnit.DAYS.toMillis(3)}; |
| 140 HistoryItem item2 = StubbedHistoryProvider.createHistoryItem(1, timestam
ps2); | 150 HistoryItem item2 = StubbedHistoryProvider.createHistoryItem(1, timestam
ps2); |
| 141 mHistoryProvider.addItem(item2); | 151 mHistoryProvider.addItem(item2); |
| 142 | 152 |
| 143 initializeAdapter(); | 153 initializeAdapter(); |
| 144 checkAdapterContents(true, null, null, item1, null, item2); | 154 checkAdapterContents(true, null, null, item1, null, item2); |
| 145 | 155 |
| 146 mAdapter.search("google"); | 156 mAdapter.search("google"); |
| 147 | 157 |
| 148 // The header should be hidden during the search. | 158 // The header should be hidden during the search. |
| 149 checkAdapterContents(false, null, item1); | 159 checkAdapterContents(false, null, item1); |
| 150 | 160 |
| 151 mAdapter.onEndSearch(); | 161 mAdapter.onEndSearch(); |
| 152 | 162 |
| 153 // The header should be shown again after the search. | 163 // The header should be shown again after the search. |
| 154 checkAdapterContents(true, null, null, item1, null, item2); | 164 checkAdapterContents(true, null, null, item1, null, item2); |
| 155 } | 165 } |
| 156 | 166 |
| 167 @Test |
| 157 @SmallTest | 168 @SmallTest |
| 158 public void testLoadMoreItems() { | 169 public void testLoadMoreItems() { |
| 159 Date today = new Date(); | 170 Date today = new Date(); |
| 160 long[] timestamps = {today.getTime()}; | 171 long[] timestamps = {today.getTime()}; |
| 161 HistoryItem item1 = StubbedHistoryProvider.createHistoryItem(0, timestam
ps); | 172 HistoryItem item1 = StubbedHistoryProvider.createHistoryItem(0, timestam
ps); |
| 162 mHistoryProvider.addItem(item1); | 173 mHistoryProvider.addItem(item1); |
| 163 | 174 |
| 164 HistoryItem item2 = StubbedHistoryProvider.createHistoryItem(1, timestam
ps); | 175 HistoryItem item2 = StubbedHistoryProvider.createHistoryItem(1, timestam
ps); |
| 165 mHistoryProvider.addItem(item2); | 176 mHistoryProvider.addItem(item2); |
| 166 | 177 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 178 mHistoryProvider.addItem(item6); | 189 mHistoryProvider.addItem(item6); |
| 179 | 190 |
| 180 long[] timestamps3 = {today.getTime() - TimeUnit.DAYS.toMillis(4)}; | 191 long[] timestamps3 = {today.getTime() - TimeUnit.DAYS.toMillis(4)}; |
| 181 HistoryItem item7 = StubbedHistoryProvider.createHistoryItem(1, timestam
ps3); | 192 HistoryItem item7 = StubbedHistoryProvider.createHistoryItem(1, timestam
ps3); |
| 182 mHistoryProvider.addItem(item7); | 193 mHistoryProvider.addItem(item7); |
| 183 | 194 |
| 184 initializeAdapter(); | 195 initializeAdapter(); |
| 185 | 196 |
| 186 // Only the first five of the seven items should be loaded. | 197 // Only the first five of the seven items should be loaded. |
| 187 checkAdapterContents(true, null, null, item1, item2, item3, item4, null,
item5); | 198 checkAdapterContents(true, null, null, item1, item2, item3, item4, null,
item5); |
| 188 assertTrue(mAdapter.canLoadMoreItems()); | 199 Assert.assertTrue(mAdapter.canLoadMoreItems()); |
| 189 | 200 |
| 190 mAdapter.loadMoreItems(); | 201 mAdapter.loadMoreItems(); |
| 191 | 202 |
| 192 // All items should now be loaded. | 203 // All items should now be loaded. |
| 193 checkAdapterContents(true, null, null, item1, item2, item3, item4, null,
item5, item6, | 204 checkAdapterContents(true, null, null, item1, item2, item3, item4, null,
item5, item6, |
| 194 null, item7); | 205 null, item7); |
| 195 assertFalse(mAdapter.canLoadMoreItems()); | 206 Assert.assertFalse(mAdapter.canLoadMoreItems()); |
| 196 } | 207 } |
| 197 | 208 |
| 209 @Test |
| 198 @SmallTest | 210 @SmallTest |
| 199 public void testOnHistoryDeleted() throws Exception { | 211 public void testOnHistoryDeleted() throws Exception { |
| 200 Date today = new Date(); | 212 Date today = new Date(); |
| 201 long[] timestamps = {today.getTime()}; | 213 long[] timestamps = {today.getTime()}; |
| 202 HistoryItem item1 = StubbedHistoryProvider.createHistoryItem(0, timestam
ps); | 214 HistoryItem item1 = StubbedHistoryProvider.createHistoryItem(0, timestam
ps); |
| 203 mHistoryProvider.addItem(item1); | 215 mHistoryProvider.addItem(item1); |
| 204 | 216 |
| 205 initializeAdapter(); | 217 initializeAdapter(); |
| 206 | 218 |
| 207 checkAdapterContents(true, null, null, item1); | 219 checkAdapterContents(true, null, null, item1); |
| 208 | 220 |
| 209 mHistoryProvider.removeItem(item1); | 221 mHistoryProvider.removeItem(item1); |
| 210 | 222 |
| 211 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 223 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 212 @Override | 224 @Override |
| 213 public void run() { | 225 public void run() { |
| 214 mAdapter.onHistoryDeleted(); | 226 mAdapter.onHistoryDeleted(); |
| 215 } | 227 } |
| 216 }); | 228 }); |
| 217 | 229 |
| 218 checkAdapterContents(false); | 230 checkAdapterContents(false); |
| 219 } | 231 } |
| 220 | 232 |
| 233 @Test |
| 221 @SmallTest | 234 @SmallTest |
| 222 public void testBlockedSite() { | 235 public void testBlockedSite() { |
| 223 Date today = new Date(); | 236 Date today = new Date(); |
| 224 long[] timestamps = {today.getTime()}; | 237 long[] timestamps = {today.getTime()}; |
| 225 HistoryItem item1 = StubbedHistoryProvider.createHistoryItem(0, timestam
ps); | 238 HistoryItem item1 = StubbedHistoryProvider.createHistoryItem(0, timestam
ps); |
| 226 mHistoryProvider.addItem(item1); | 239 mHistoryProvider.addItem(item1); |
| 227 | 240 |
| 228 HistoryItem item2 = StubbedHistoryProvider.createHistoryItem(5, timestam
ps); | 241 HistoryItem item2 = StubbedHistoryProvider.createHistoryItem(5, timestam
ps); |
| 229 mHistoryProvider.addItem(item2); | 242 mHistoryProvider.addItem(item2); |
| 230 | 243 |
| 231 initializeAdapter(); | 244 initializeAdapter(); |
| 232 | 245 |
| 233 checkAdapterContents(true, null, null, item1, item2); | 246 checkAdapterContents(true, null, null, item1, item2); |
| 234 assertEquals(ContextUtils.getApplicationContext().getString( | 247 Assert.assertEquals(ContextUtils.getApplicationContext().getString( |
| 235 R.string.android_history_blocked_site), item2.getTitle()); | 248 R.string.android_history_blocked_site), |
| 236 assertTrue(item2.wasBlockedVisit()); | 249 item2.getTitle()); |
| 250 Assert.assertTrue(item2.wasBlockedVisit()); |
| 237 } | 251 } |
| 238 | 252 |
| 239 private void checkAdapterContents(boolean hasHeader, Object... expectedItems
) { | 253 private void checkAdapterContents(boolean hasHeader, Object... expectedItems
) { |
| 240 assertEquals(expectedItems.length, mAdapter.getItemCount()); | 254 Assert.assertEquals(expectedItems.length, mAdapter.getItemCount()); |
| 241 assertEquals(hasHeader, mAdapter.hasListHeader()); | 255 Assert.assertEquals(hasHeader, mAdapter.hasListHeader()); |
| 242 | 256 |
| 243 for (int i = 0; i < expectedItems.length; i++) { | 257 for (int i = 0; i < expectedItems.length; i++) { |
| 244 if (i == 0 && hasHeader) { | 258 if (i == 0 && hasHeader) { |
| 245 assertEquals(TYPE_HEADER, mAdapter.getItemViewType(i)); | 259 Assert.assertEquals(TYPE_HEADER, mAdapter.getItemViewType(i)); |
| 246 continue; | 260 continue; |
| 247 } | 261 } |
| 248 | 262 |
| 249 if (expectedItems[i] == null) { | 263 if (expectedItems[i] == null) { |
| 250 // TODO(twellington): Check what date header is showing. | 264 // TODO(twellington): Check what date header is showing. |
| 251 assertEquals(TYPE_DATE, mAdapter.getItemViewType(i)); | 265 Assert.assertEquals(TYPE_DATE, mAdapter.getItemViewType(i)); |
| 252 } else { | 266 } else { |
| 253 assertEquals(TYPE_NORMAL, mAdapter.getItemViewType(i)); | 267 Assert.assertEquals(TYPE_NORMAL, mAdapter.getItemViewType(i)); |
| 254 assertEquals(expectedItems[i], mAdapter.getItemAt(i).second); | 268 Assert.assertEquals(expectedItems[i], mAdapter.getItemAt(i).seco
nd); |
| 255 } | 269 } |
| 256 } | 270 } |
| 257 } | 271 } |
| 258 } | 272 } |
| OLD | NEW |