Better testing of CJS_Icon from JavaScript

Also improve the error message when failing to set an icon name.

Change-Id: I954d7ca8cf4e3df3254a1c9acaf42d1476f0cd8d
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/62170
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/fxjs/cjs_document.cpp b/fxjs/cjs_document.cpp
index c7965f0..0b3f472 100644
--- a/fxjs/cjs_document.cpp
+++ b/fxjs/cjs_document.cpp
@@ -1116,6 +1116,8 @@
 }
 
 CJS_Result CJS_Document::get_icons(CJS_Runtime* pRuntime) {
+  // TODO(tsepez): Maybe make consistent with Acrobat Reader behavior which
+  // is to throw an exception under the default security settings.
   if (m_IconNames.empty())
     return CJS_Result::Success(pRuntime->NewUndefined());
 
diff --git a/fxjs/cjs_icon.cpp b/fxjs/cjs_icon.cpp
index 29fde8a..80be5c8 100644
--- a/fxjs/cjs_icon.cpp
+++ b/fxjs/cjs_icon.cpp
@@ -34,5 +34,5 @@
 }
 
 CJS_Result CJS_Icon::set_name(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
-  return CJS_Result::Failure(JSMessage::kNotSupportedError);
+  return CJS_Result::Failure(JSMessage::kReadOnlyError);
 }
diff --git a/testing/resources/javascript/icons.in b/testing/resources/javascript/icons.in
new file mode 100644
index 0000000..8f6ef4c
--- /dev/null
+++ b/testing/resources/javascript/icons.in
@@ -0,0 +1,42 @@
+{{header}}
+{{include field.fragment}}
+{{object 16 0}} <<
+  {{streamlen}}
+>>
+stream
+// In order to get icons from a document, you have to first add icons
+// to the document, and the only way to initially get an icon object,
+// it would seem, is from a button in a field. Then it can be added
+// any number of times, which does nothing but accumulate the list of
+// names of the added icons. Whee.
+try {
+  var doc = this;
+  app.alert("doc is " + doc);
+  var icon = doc.getField("MyField.MyPushButton").buttonGetIcon();
+  app.alert("icon is " + icon);
+  doc.addIcon("icon1", icon);
+  doc.addIcon("icon2", icon);
+  app.alert("icon list is now " + doc.icons);
+  var icon1 = doc.icons[0];
+  var icon2 = doc.icons[1];
+  app.alert("they are named " + icon1.name + " and " + icon2.name);
+  var icon1_again = doc.getIcon("icon1");
+  var icon2_again = doc.getIcon("icon2");
+  app.alert("they are also named " + icon1_again.name +
+            " and " + icon2_again.name);
+  app.alert("but they are made anew each time since " +
+             (icon1 == icon1_again) + " is returned from comparison");
+} catch (e) {
+  app.alert("error: " + e);
+}
+try {
+  icon1.name = "chowder";
+} catch (e) {
+  app.alert("As expected, trying to change the name gave: " + e);
+}
+endstream
+endobj
+{{xref}}
+{{trailer}}
+{{startxref}}
+%%EOF
diff --git a/testing/resources/javascript/icons_expected.txt b/testing/resources/javascript/icons_expected.txt
new file mode 100644
index 0000000..bace26d
--- /dev/null
+++ b/testing/resources/javascript/icons_expected.txt
@@ -0,0 +1,7 @@
+Alert: doc is [object global]
+Alert: icon is [object Object]
+Alert: icon list is now [object Object],[object Object]
+Alert: they are named icon1 and icon2
+Alert: they are also named icon1 and icon2
+Alert: but they are made anew each time since false is returned from comparison
+Alert: As expected, trying to change the name gave: Icon.name: Cannot assign to readonly property.