[webnfc] Fix code trying to access a null pointer

This CL fixes a bug introduced by crrev.com/c/1824395 that attempts to
access null pointer, also adds a wpt to cover it.

BUG=520391,1026541

Change-Id: I62d5b28c3c6d39cbb27c2f21ca4d5ca91e54d892
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1925494
Commit-Queue: Leon Han <leon.han@intel.com>
Reviewed-by: Rijubrata Bhaumik <rijubrata.bhaumik@intel.com>
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#717487}
diff --git a/web-nfc/NDEFRecord_constructor.https.html b/web-nfc/NDEFRecord_constructor.https.html
index 79ce8a0..d032231 100644
--- a/web-nfc/NDEFRecord_constructor.https.html
+++ b/web-nfc/NDEFRecord_constructor.https.html
@@ -46,6 +46,8 @@
     const decoder = new TextDecoder();
     assert_equals(decoder.decode(record.data), test_text_data,
         'data has the same content with the original dictionary');
+    assert_throws('NotSupportedError', () => record.toRecords(),
+        'Only smart-poster records and external type records could have embedded records.');
   }, 'NDEFRecord constructor with text record type and string data');
 
   test(() => {
@@ -59,6 +61,8 @@
     const decoder = new TextDecoder();
     assert_equals(decoder.decode(record.data), test_text_data,
         'data has the same content with the original dictionary');
+    assert_throws('NotSupportedError', () => record.toRecords(),
+        'Only smart-poster records and external type records could have embedded records.');
   }, 'NDEFRecord constructor with text record type and arrayBuffer data');
 
   test(() => {
@@ -72,6 +76,8 @@
     const decoder = new TextDecoder();
     assert_equals(decoder.decode(record.data), test_text_data,
         'data has the same content with the original dictionary');
+    assert_throws('NotSupportedError', () => record.toRecords(),
+        'Only smart-poster records and external type records could have embedded records.');
   }, 'NDEFRecord constructor with text record type and arrayBufferView data');
 
   test(() => {
@@ -113,6 +119,8 @@
     const decoder = new TextDecoder();
     assert_equals(decoder.decode(record.data), test_url_data,
         'data has the same content with the original dictionary');
+    assert_throws('NotSupportedError', () => record.toRecords(),
+        'Only smart-poster records and external type records could have embedded records.');
   }, 'NDEFRecord constructor with url record type');
 
   test(() => {
@@ -122,6 +130,8 @@
     const decoder = new TextDecoder();
     assert_equals(decoder.decode(record.data), test_url_data,
         'data has the same content with the original dictionary');
+    assert_throws('NotSupportedError', () => record.toRecords(),
+        'Only smart-poster records and external type records could have embedded records.');
   }, 'NDEFRecord constructor with absolute-url record type');
 
   test(() => {
@@ -138,6 +148,8 @@
       assert_equals(record.mediaType, 'application/octet-stream', 'mediaType');
       assert_array_equals(new Uint8Array(record.data.buffer), [1, 2, 3, 4],
           'data has the same content with the original buffer');
+      assert_throws('NotSupportedError', () => record.toRecords(),
+          'Only smart-poster records and external type records could have embedded records.');
     }
     // Feed ArrayBufferView.
     {
@@ -146,6 +158,8 @@
       assert_equals(record.recordType, 'mime', 'recordType');
       assert_array_equals(new Uint8Array(record.data.buffer), [2, 3, 4],
           'data has the same content with the original buffer view');
+      assert_throws('NotSupportedError', () => record.toRecords(),
+          'Only smart-poster records and external type records could have embedded records.');
     }
   }, 'NDEFRecord constructor with mime record type and stream data');
 
@@ -156,6 +170,8 @@
     assert_object_equals(JSON.parse(new TextDecoder().decode(record.data)),
         test_json_data,
         'data has the same content with the original json');
+    assert_throws('NotSupportedError', () => record.toRecords(),
+        'Only smart-poster records and external type records could have embedded records.');
   }, 'NDEFRecord constructor with mime record type and json data');
 
   test(() => {
@@ -171,6 +187,8 @@
       assert_equals(record.recordType, 'unknown', 'recordType');
       assert_array_equals(new Uint8Array(record.data.buffer), [1, 2, 3, 4],
           'data has the same content with the original buffer');
+      assert_throws('NotSupportedError', () => record.toRecords(),
+          'Only smart-poster records and external type records could have embedded records.');
     }
     // Feed ArrayBufferView.
     {
@@ -179,6 +197,8 @@
       assert_equals(record.recordType, 'unknown', 'recordType');
       assert_array_equals(new Uint8Array(record.data.buffer), [2, 3, 4],
           'data has the same content with the original buffer view');
+      assert_throws('NotSupportedError', () => record.toRecords(),
+          'Only smart-poster records and external type records could have embedded records.');
     }
   }, 'NDEFRecord constructor with unknown record type');
 
@@ -192,6 +212,8 @@
     assert_equals(record.mediaType, null, 'mediaType');
     assert_array_equals(new Uint8Array(record.data.buffer), original_data,
         'data has the same content with the original buffer');
+    assert_equals(record.toRecords(), null,
+        'toRecords() returns null if the payload is not an NDEF message.');
   }, 'NDEFRecord constructor with external record type');
 
   test(() => {