/**
   * Test that migration skips if it already has files in the new folder.
   *
   * @throws IOException
   * @throws InterruptedException
   * @throws ExecutionException
   */
  @SuppressWarnings("unused")
  @SuppressFBWarnings("DLS_DEAD_LOCAL_STORE")
  @SmallTest
  public void testSkipMigrateData() throws IOException, InterruptedException, ExecutionException {
    ApplicationData.clearAppData(getInstrumentation().getTargetContext());

    // Write old state files.
    File filesDir = getInstrumentation().getTargetContext().getFilesDir();
    File stateFile = new File(filesDir, TabPersistentStore.SAVED_STATE_FILE);
    File tab0 = new File(filesDir, TabState.SAVED_TAB_STATE_FILE_PREFIX + "0");
    File tab1 = new File(filesDir, TabState.SAVED_TAB_STATE_FILE_PREFIX + "1");
    File tab2 = new File(filesDir, TabState.SAVED_TAB_STATE_FILE_PREFIX_INCOGNITO + "2");
    File tab3 = new File(filesDir, TabState.SAVED_TAB_STATE_FILE_PREFIX_INCOGNITO + "3");

    assertTrue("Could not create state file", stateFile.createNewFile());
    assertTrue("Could not create tab 0 file", tab0.createNewFile());
    assertTrue("Could not create tab 1 file", tab1.createNewFile());
    assertTrue("Could not create tab 2 file", tab2.createNewFile());
    assertTrue("Could not create tab 3 file", tab3.createNewFile());

    // Write new state files
    File newDir = TabPersistentStore.getStateDirectory(getInstrumentation().getTargetContext(), 0);
    File newStateFile = new File(newDir, TabPersistentStore.SAVED_STATE_FILE);
    File newTab4 = new File(newDir, TabState.SAVED_TAB_STATE_FILE_PREFIX + "4");

    assertTrue("Could not create new tab 4 file", newTab4.createNewFile());
    assertTrue("Could not create new state file", newStateFile.createNewFile());

    // Build the TabPersistentStore which will try to move the files.
    MockTabModelSelector selector = new MockTabModelSelector(0, 0, null);
    TabPersistentStore store =
        new TabPersistentStore(selector, 0, getInstrumentation().getTargetContext(), null, null);
    TabPersistentStore.waitForMigrationToFinish();

    assertTrue("Could not find new state file", newStateFile.exists());
    assertTrue("Could not find new tab 4 file", newTab4.exists());

    // Make sure the old files did not move
    File newTab0 = new File(newDir, TabState.SAVED_TAB_STATE_FILE_PREFIX + "0");
    File newTab1 = new File(newDir, TabState.SAVED_TAB_STATE_FILE_PREFIX + "1");
    File newTab2 = new File(newDir, TabState.SAVED_TAB_STATE_FILE_PREFIX_INCOGNITO + "2");
    File newTab3 = new File(newDir, TabState.SAVED_TAB_STATE_FILE_PREFIX_INCOGNITO + "3");

    assertFalse("Could find new tab 0 file", newTab0.exists());
    assertFalse("Could find new tab 1 file", newTab1.exists());
    assertFalse("Could find new tab 2 file", newTab2.exists());
    assertFalse("Could find new tab 3 file", newTab3.exists());

    ApplicationData.clearAppData(getInstrumentation().getTargetContext());
  }
 private void writeStateFile(final TabModelSelector selector, int index) throws IOException {
   byte[] data =
       ThreadUtils.runOnUiThreadBlockingNoException(
           new Callable<byte[]>() {
             @Override
             public byte[] call() throws Exception {
               return TabPersistentStore.serializeTabModelSelector(selector, null);
             }
           });
   File f = TabPersistentStore.getStateDirectory(getInstrumentation().getTargetContext(), index);
   FileOutputStream fos = null;
   try {
     fos = new FileOutputStream(new File(f, TabPersistentStore.SAVED_STATE_FILE));
     fos.write(data);
   } finally {
     StreamUtil.closeQuietly(fos);
   }
 }
  /**
   * Test that the state file migration skips unrelated files.
   *
   * @throws IOException
   * @throws InterruptedException
   * @throws ExecutionException
   */
  @SuppressWarnings("unused")
  @SuppressFBWarnings("DLS_DEAD_LOCAL_STORE")
  @SmallTest
  public void testMigrationLeavesOtherFilesAlone()
      throws IOException, InterruptedException, ExecutionException {
    ApplicationData.clearAppData(getInstrumentation().getTargetContext());

    // Write old state files.
    File filesDir = getInstrumentation().getTargetContext().getFilesDir();
    File stateFile = new File(filesDir, TabPersistentStore.SAVED_STATE_FILE);
    File tab0 = new File(filesDir, TabState.SAVED_TAB_STATE_FILE_PREFIX + "0");
    File otherFile = new File(filesDir, "other.file");

    assertTrue("Could not create state file", stateFile.createNewFile());
    assertTrue("Could not create tab 0 file", tab0.createNewFile());
    assertTrue("Could not create other file", otherFile.createNewFile());

    // Build the TabPersistentStore which will try to move the files.
    MockTabModelSelector selector = new MockTabModelSelector(0, 0, null);
    TabPersistentStore store =
        new TabPersistentStore(selector, 0, getInstrumentation().getTargetContext(), null, null);
    TabPersistentStore.waitForMigrationToFinish();

    assertFalse("Could still find old state file", stateFile.exists());
    assertFalse("Could still find old tab 0 file", tab0.exists());
    assertTrue("Could not find other file", otherFile.exists());

    // Check that the files were moved.
    File newDir = TabPersistentStore.getStateDirectory(getInstrumentation().getTargetContext(), 0);
    File newStateFile = new File(newDir, TabPersistentStore.SAVED_STATE_FILE);
    File newTab0 = new File(newDir, TabState.SAVED_TAB_STATE_FILE_PREFIX + "0");
    File newOtherFile = new File(newDir, "other.file");

    assertTrue("Could not find new state file", newStateFile.exists());
    assertTrue("Could not find new tab 0 file", newTab0.exists());
    assertFalse("Could find new other file", newOtherFile.exists());

    ApplicationData.clearAppData(getInstrumentation().getTargetContext());
  }