/** In version 120, value "null" meant {code NULL}. */
  public void testUpgrade() throws Exception {
    final File file = new File(getContext().getCacheDir(), "setting.xml");
    file.delete();
    final Object lock = new Object();
    final PrintStream os = new PrintStream(new FileOutputStream(file));
    os.print(
        "<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>"
            + "<settings version=\"120\">"
            + "  <setting id=\"0\" name=\"k0\" value=\"null\" package=\"null\" />"
            + "  <setting id=\"1\" name=\"k1\" value=\"\" package=\"\" />"
            + "  <setting id=\"2\" name=\"k2\" value=\"v2\" package=\"p2\" />"
            + "</settings>");
    os.close();

    final SettingsState ss =
        new SettingsState(
            lock,
            file,
            1,
            SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED,
            Looper.getMainLooper());
    synchronized (lock) {
      SettingsState.Setting s;
      s = ss.getSettingLocked("k0");
      assertEquals(null, s.getValue());
      assertEquals("null", s.getPackageName());

      s = ss.getSettingLocked("k1");
      assertEquals("", s.getValue());
      assertEquals("", s.getPackageName());

      s = ss.getSettingLocked("k2");
      assertEquals("v2", s.getValue());
      assertEquals("p2", s.getPackageName());
    }
  }
  /** Make sure settings can be written to a file and also can be read. */
  public void testReadWrite() {
    final File file = new File(getContext().getCacheDir(), "setting.xml");
    file.delete();
    final Object lock = new Object();

    final SettingsState ssWriter =
        new SettingsState(
            lock,
            file,
            1,
            SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED,
            Looper.getMainLooper());
    ssWriter.setVersionLocked(SettingsState.SETTINGS_VERSOIN_NEW_ENCODING);

    ssWriter.insertSettingLocked("k1", "\u0000", "package");
    ssWriter.insertSettingLocked("k2", "abc", "p2");
    ssWriter.insertSettingLocked("k3", null, "p2");
    ssWriter.insertSettingLocked("k4", CRAZY_STRING, "p3");
    synchronized (lock) {
      ssWriter.persistSyncLocked();
    }

    final SettingsState ssReader =
        new SettingsState(
            lock,
            file,
            1,
            SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED,
            Looper.getMainLooper());
    synchronized (lock) {
      assertEquals("\u0000", ssReader.getSettingLocked("k1").getValue());
      assertEquals("abc", ssReader.getSettingLocked("k2").getValue());
      assertEquals(null, ssReader.getSettingLocked("k3").getValue());
      assertEquals(CRAZY_STRING, ssReader.getSettingLocked("k4").getValue());
    }
  }