OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.suggestions; |
| 6 |
| 7 import static org.hamcrest.CoreMatchers.equalTo; |
| 8 import static org.junit.Assert.assertFalse; |
| 9 import static org.junit.Assert.assertThat; |
| 10 import static org.junit.Assert.assertTrue; |
| 11 |
| 12 import android.graphics.drawable.Drawable; |
| 13 import android.graphics.drawable.GradientDrawable; |
| 14 |
| 15 import org.junit.Before; |
| 16 import org.junit.Test; |
| 17 import org.junit.runner.RunWith; |
| 18 import org.mockito.MockitoAnnotations; |
| 19 import org.robolectric.annotation.Config; |
| 20 |
| 21 import org.chromium.chrome.browser.ntp.MostVisitedTileType; |
| 22 import org.chromium.chrome.browser.ntp.NTPTileSource; |
| 23 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 24 |
| 25 /** |
| 26 * Unit tests for {@link Tile}. |
| 27 */ |
| 28 @RunWith(LocalRobolectricTestRunner.class) |
| 29 @Config(manifest = Config.NONE) |
| 30 public class TileTest { |
| 31 private static final String[] URLS = {"https://www.google.com", "https://tel
lmedadjokes.com"}; |
| 32 |
| 33 @Before |
| 34 public void setUp() { |
| 35 MockitoAnnotations.initMocks(this); |
| 36 } |
| 37 |
| 38 @Test |
| 39 public void testImportDataWithNewTitle() { |
| 40 Tile tile = new Tile("title", URLS[0], "", 0, NTPTileSource.TOP_SITES); |
| 41 Tile oldTile = new Tile("oldTitle", URLS[0], "", 0, NTPTileSource.TOP_SI
TES); |
| 42 |
| 43 // importData should report the change, and the new tile should keep its
own data. |
| 44 assertTrue(tile.importData(oldTile)); |
| 45 assertThat(tile.getTitle(), equalTo("title")); |
| 46 } |
| 47 |
| 48 @Test(expected = AssertionError.class) |
| 49 public void testImportDataWithNewUrl() { |
| 50 Tile tile = new Tile("title", URLS[0], "", 0, NTPTileSource.TOP_SITES); |
| 51 Tile oldTile = new Tile("title", URLS[1], "", 0, NTPTileSource.TOP_SITES
); |
| 52 |
| 53 // Importing from a tile associated to a different URL should crash, as
bug detection |
| 54 // measure. |
| 55 tile.importData(oldTile); |
| 56 } |
| 57 |
| 58 @Test |
| 59 public void testImportDataWithNewWhitelistIconPath() { |
| 60 Tile tile = new Tile("title", URLS[0], "", 0, NTPTileSource.TOP_SITES); |
| 61 Tile oldTile = new Tile("title", URLS[0], "foobar", 0, NTPTileSource.TOP
_SITES); |
| 62 |
| 63 // importData should report the change, and the new tile should keep its
own data. |
| 64 assertTrue(tile.importData(oldTile)); |
| 65 assertThat(tile.getWhitelistIconPath(), equalTo("")); |
| 66 } |
| 67 |
| 68 @Test |
| 69 public void testImportDataWithNewIndex() { |
| 70 Tile tile = new Tile("title", URLS[0], "", 0, NTPTileSource.TOP_SITES); |
| 71 Tile oldTile = new Tile("title", URLS[0], "", 1, NTPTileSource.TOP_SITES
); |
| 72 |
| 73 // importData should report the change, and the new tile should keep its
own data. |
| 74 assertTrue(tile.importData(oldTile)); |
| 75 assertThat(tile.getIndex(), equalTo(0)); |
| 76 } |
| 77 |
| 78 @Test |
| 79 public void testImportDataWithNewSource() { |
| 80 Tile tile = new Tile("title", URLS[0], "", 0, NTPTileSource.TOP_SITES); |
| 81 Tile oldTile = new Tile("title", URLS[0], "", 0, NTPTileSource.POPULAR); |
| 82 |
| 83 // importData should not report the change since it has no visible impac
t. The new tile |
| 84 // still keeps its own data. |
| 85 assertFalse(tile.importData(oldTile)); |
| 86 assertThat(tile.getSource(), equalTo(NTPTileSource.TOP_SITES)); |
| 87 } |
| 88 |
| 89 @Test |
| 90 public void testTileImportWithSameData() { |
| 91 Tile tile = new Tile("title", URLS[0], "", 0, NTPTileSource.TOP_SITES); |
| 92 Tile oldTile = new Tile("title", URLS[0], "", 0, NTPTileSource.TOP_SITES
); |
| 93 |
| 94 // No change should be reported |
| 95 assertFalse(tile.importData(oldTile)); |
| 96 } |
| 97 |
| 98 @Test |
| 99 public void testTileImportWithTransientData() { |
| 100 Tile tile = new Tile("title", URLS[0], "", 0, NTPTileSource.TOP_SITES); |
| 101 |
| 102 Drawable dummyDrawable = new GradientDrawable(); |
| 103 Tile oldTile = new Tile("title", URLS[0], "", 0, NTPTileSource.TOP_SITES
); |
| 104 oldTile.setOfflinePageOfflineId(42L); |
| 105 oldTile.setIcon(dummyDrawable); |
| 106 oldTile.setType(MostVisitedTileType.ICON_REAL); |
| 107 |
| 108 // Old transient data should be copied over to the new tile without flag
ging the change. |
| 109 assertFalse(tile.importData(oldTile)); |
| 110 assertThat(tile.getOfflinePageOfflineId(), equalTo(42L)); |
| 111 assertThat(tile.getIcon(), equalTo(dummyDrawable)); |
| 112 assertThat(tile.getType(), equalTo(MostVisitedTileType.ICON_REAL)); |
| 113 } |
| 114 } |
OLD | NEW |