1 // Copyright 2020 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.mostvisited; 6 7 import static org.junit.Assert.assertEquals; 8 import static org.junit.Assert.assertTrue; 9 10 import androidx.test.filters.SmallTest; 11 12 import org.junit.Before; 13 import org.junit.Rule; 14 import org.junit.Test; 15 import org.junit.runner.RunWith; 16 17 import org.chromium.base.test.util.CommandLineFlags; 18 import org.chromium.chrome.browser.flags.ChromeSwitches; 19 import org.chromium.chrome.browser.suggestions.SiteSuggestion; 20 import org.chromium.chrome.browser.suggestions.tile.TileSectionType; 21 import org.chromium.chrome.browser.suggestions.tile.TileSource; 22 import org.chromium.chrome.browser.suggestions.tile.TileTitleSource; 23 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; 24 import org.chromium.chrome.test.ChromeTabbedActivityTestRule; 25 import org.chromium.url.GURL; 26 27 import java.io.File; 28 import java.io.IOException; 29 import java.util.ArrayList; 30 import java.util.Date; 31 import java.util.List; 32 import java.util.concurrent.CountDownLatch; 33 34 /** 35 * Instrumentation tests for {@link MostVisitedSitesMetadataUtils}. 36 */ 37 @RunWith(ChromeJUnit4ClassRunner.class) 38 @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE}) 39 public class MostVisitedSitesMetadataUtilsTest { 40 @Rule 41 public ChromeTabbedActivityTestRule mTestSetupRule = new ChromeTabbedActivityTestRule(); 42 43 private List<SiteSuggestion> mExpectedSiteSuggestions; 44 45 @Before setUp()46 public void setUp() { 47 mTestSetupRule.startMainActivityOnBlankPage(); 48 } 49 50 @Test 51 @SmallTest testSaveRestoreConsistency()52 public void testSaveRestoreConsistency() throws InterruptedException, IOException { 53 mExpectedSiteSuggestions = createFakeSiteSuggestions(); 54 55 // Get old file and ensure to delete it. 56 File oldFile = MostVisitedSitesMetadataUtils.getOrCreateTopSitesDirectory(); 57 assertTrue(oldFile.delete() && !oldFile.exists()); 58 59 // Save suggestion lists to file. 60 final CountDownLatch latch = new CountDownLatch(1); 61 MostVisitedSitesMetadataUtils.saveSuggestionListsToFile( 62 mExpectedSiteSuggestions, latch::countDown); 63 64 // Wait util the file has been saved. 65 latch.await(); 66 67 // Restore list from file after saving finished. 68 List<SiteSuggestion> sitesAfterRestore = 69 MostVisitedSitesMetadataUtils.restoreFileToSuggestionLists(); 70 71 // Ensure that the new list equals to old list. 72 assertEquals(mExpectedSiteSuggestions, sitesAfterRestore); 73 } 74 75 @Test(expected = IOException.class) 76 @SmallTest testRestoreException()77 public void testRestoreException() throws IOException { 78 // Get old file and ensure to delete it. 79 File oldFile = MostVisitedSitesMetadataUtils.getOrCreateTopSitesDirectory(); 80 assertTrue(oldFile.delete() || !oldFile.exists()); 81 82 // Call restore function and ensure it throws an IOException. 83 MostVisitedSitesMetadataUtils.restoreFileToSuggestionLists(); 84 } 85 createFakeSiteSuggestions()86 private static List<SiteSuggestion> createFakeSiteSuggestions() { 87 List<SiteSuggestion> siteSuggestions = new ArrayList<>(); 88 siteSuggestions.add(new SiteSuggestion("0 TOP_SITES", new GURL("https://www.foo.com"), "", 89 TileTitleSource.TITLE_TAG, TileSource.TOP_SITES, TileSectionType.PERSONALIZED, 90 new Date())); 91 siteSuggestions.add(new SiteSuggestion("1 WHITELIST", new GURL("https://www.bar.com"), "", 92 TileTitleSource.UNKNOWN, TileSource.WHITELIST, TileSectionType.PERSONALIZED, 93 new Date())); 94 siteSuggestions.get(1).faviconId = 1; 95 return siteSuggestions; 96 } 97 } 98