/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var { createCalendar, deleteCalendars } = ChromeUtils.import(
  "resource://testing-common/calendar/CalendarUtils.jsm"
);
var { cancelItemDialog, saveAndCloseItemDialog, setData } = ChromeUtils.import(
  "resource://testing-common/calendar/ItemEditingHelpers.jsm"
);

var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

var UTF8STRING = " 💣 💥  ☣  ";

add_task(async function testUTF8() {
  Services.prefs.setStringPref("calendar.categories.names", UTF8STRING);
  createCalendar(window, UTF8STRING);
  await CalendarTestUtils.setCalendarView(window, "day");

  // Create new event.
  let eventBox = CalendarTestUtils.dayView.getHourBoxAt(window, 8);
  let { dialogWindow, iframeWindow } = await CalendarTestUtils.editNewEvent(window, eventBox);
  // Fill in name, location, description.
  await setData(dialogWindow, iframeWindow, {
    title: UTF8STRING,
    location: UTF8STRING,
    description: UTF8STRING,
    categories: [UTF8STRING],
  });
  await saveAndCloseItemDialog(dialogWindow);

  // open
  let { dialogWindow: dlgWindow, iframeDocument } = await CalendarTestUtils.dayView.editEventAt(
    window,
    1
  );
  // Check values.
  Assert.equal(iframeDocument.getElementById("item-title").value, UTF8STRING);
  Assert.equal(iframeDocument.getElementById("item-location").value, UTF8STRING);
  // The trailing spaces confuse innerText, so we'll do this longhand
  let editorEl = iframeDocument.getElementById("item-description");
  let editor = editorEl.getEditor(editorEl.contentWindow);
  let description = editor.outputToString("text/plain", 0);
  Assert.equal(description, UTF8STRING);
  Assert.ok(
    iframeDocument
      .getElementById("item-categories")
      .querySelector(`menuitem[label="${UTF8STRING}"][checked]`)
  );

  // Escape the event window.
  cancelItemDialog(dlgWindow);
});

Assert.ok(true, "Test ran to completion");

registerCleanupFunction(function teardownModule(module) {
  deleteCalendars(window, UTF8STRING);
  Services.prefs.clearUserPref("calendar.categories.names");
});
