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