Flatten media_router.js.
[chromium-blink-merge.git] / device / nfc / nfc_ndef_record_unittest.cc
blob2b6ef48b2bbe443eec8f0fa99468e21854c6611f
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/values.h"
7 #include "device/nfc/nfc_ndef_record.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 using base::DictionaryValue;
11 using base::ListValue;
12 using base::StringValue;
14 namespace device {
16 namespace {
18 const char kTestAction[] = "test-action";
19 const char kTestEncoding[] = "test-encoding";
20 const char kTestLanguageCode[] = "test-language-code";
21 const char kTestMimeType[] = "test-mime-type";
22 const uint32 kTestTargetSize = 0;
23 const char kTestText[] = "test-text";
24 const char kTestURI[] = "test://uri";
26 } // namespace
28 TEST(NfcNdefRecordTest, PopulateTextRecord) {
29 DictionaryValue data;
30 scoped_ptr<NfcNdefRecord> record(new NfcNdefRecord());
32 // Missing text field. Should fail.
33 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeText, &data));
34 EXPECT_FALSE(record->IsPopulated());
36 // Text field with incorrect type. Should fail.
37 data.SetInteger(NfcNdefRecord::kFieldText, 0);
38 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeText, &data));
39 EXPECT_FALSE(record->IsPopulated());
41 // Text field with correct type but missing encoding and language.
42 // Should fail.
43 data.SetString(NfcNdefRecord::kFieldText, kTestText);
44 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeText, &data));
45 EXPECT_FALSE(record->IsPopulated());
47 // Populating a successfully populated record should fail.
48 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeText, &data));
50 // Incorrect type for language code.
51 data.SetInteger(NfcNdefRecord::kFieldLanguageCode, 0);
52 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeText, &data));
53 EXPECT_FALSE(record->IsPopulated());
55 // Correct type for language code, invalid encoding.
56 data.SetString(NfcNdefRecord::kFieldLanguageCode, kTestLanguageCode);
57 data.SetInteger(NfcNdefRecord::kFieldEncoding, 0);
58 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeText, &data));
59 EXPECT_FALSE(record->IsPopulated());
61 // All entries valid. Should succeed.
62 data.SetString(NfcNdefRecord::kFieldEncoding, kTestEncoding);
63 EXPECT_TRUE(record->Populate(NfcNdefRecord::kTypeText, &data));
64 EXPECT_TRUE(record->IsPopulated());
65 EXPECT_EQ(NfcNdefRecord::kTypeText, record->type());
67 // Compare record contents.
68 std::string string_value;
69 EXPECT_TRUE(record->data().GetString(
70 NfcNdefRecord::kFieldText, &string_value));
71 EXPECT_EQ(kTestText, string_value);
72 EXPECT_TRUE(record->data().GetString(
73 NfcNdefRecord::kFieldLanguageCode, &string_value));
74 EXPECT_EQ(kTestLanguageCode, string_value);
75 EXPECT_TRUE(record->data().GetString(
76 NfcNdefRecord::kFieldEncoding, &string_value));
77 EXPECT_EQ(kTestEncoding, string_value);
80 TEST(NfcNdefRecordTest, PopulateUriRecord) {
81 DictionaryValue data;
82 scoped_ptr<NfcNdefRecord> record(new NfcNdefRecord());
84 // Missing URI field. Should fail.
85 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeURI, &data));
86 EXPECT_FALSE(record->IsPopulated());
88 // URI field with incorrect type. Should fail.
89 data.SetInteger(NfcNdefRecord::kFieldURI, 0);
90 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeURI, &data));
91 EXPECT_FALSE(record->IsPopulated());
93 // URI field with correct type but invalid format.
94 data.SetString(NfcNdefRecord::kFieldURI, "test.uri");
95 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeURI, &data));
96 EXPECT_FALSE(record->IsPopulated());
98 data.SetString(NfcNdefRecord::kFieldURI, kTestURI);
99 EXPECT_TRUE(record->Populate(NfcNdefRecord::kTypeURI, &data));
100 EXPECT_TRUE(record->IsPopulated());
101 EXPECT_EQ(NfcNdefRecord::kTypeURI, record->type());
103 // Populating a successfully populated record should fail.
104 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeURI, &data));
106 // Recycle the record.
107 record.reset(new NfcNdefRecord());
108 EXPECT_FALSE(record->IsPopulated());
110 // Incorrect optional fields. Should fail.
111 data.SetInteger(NfcNdefRecord::kFieldMimeType, 0);
112 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeURI, &data));
113 EXPECT_FALSE(record->IsPopulated());
115 data.SetString(NfcNdefRecord::kFieldMimeType, kTestMimeType);
116 data.SetInteger(NfcNdefRecord::kFieldTargetSize, kTestTargetSize);
117 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeURI, &data));
118 EXPECT_FALSE(record->IsPopulated());
120 // Optional fields are correct. Should succeed.
121 data.SetDouble(NfcNdefRecord::kFieldTargetSize,
122 static_cast<double>(kTestTargetSize));
123 EXPECT_TRUE(record->Populate(NfcNdefRecord::kTypeURI, &data));
124 EXPECT_TRUE(record->IsPopulated());
125 EXPECT_EQ(NfcNdefRecord::kTypeURI, record->type());
127 // Compare record contents.
128 std::string string_value;
129 double double_value;
130 EXPECT_TRUE(record->data().GetString(
131 NfcNdefRecord::kFieldURI, &string_value));
132 EXPECT_EQ(kTestURI, string_value);
133 EXPECT_TRUE(record->data().GetString(
134 NfcNdefRecord::kFieldMimeType, &string_value));
135 EXPECT_EQ(kTestMimeType, string_value);
136 EXPECT_TRUE(record->data().GetDouble(
137 NfcNdefRecord::kFieldTargetSize, &double_value));
138 EXPECT_EQ(kTestTargetSize, static_cast<uint32>(double_value));
141 TEST(NfcNdefRecordTest, PopulateSmartPoster) {
142 DictionaryValue data;
143 scoped_ptr<NfcNdefRecord> record(new NfcNdefRecord());
145 // Missing URI field. Should fail.
146 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeSmartPoster, &data));
147 EXPECT_FALSE(record->IsPopulated());
149 // URI field with incorrect entry. Should fail.
150 data.SetInteger(NfcNdefRecord::kFieldURI, 0);
151 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeSmartPoster, &data));
152 EXPECT_FALSE(record->IsPopulated());
154 // URI field with correct entry. Should succeed.
155 data.SetString(NfcNdefRecord::kFieldURI, kTestURI);
156 EXPECT_TRUE(record->Populate(NfcNdefRecord::kTypeSmartPoster, &data));
157 EXPECT_TRUE(record->IsPopulated());
158 EXPECT_EQ(NfcNdefRecord::kTypeSmartPoster, record->type());
160 // Populating a successfully populated record should fail.
161 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeSmartPoster, &data));
163 // Recycle the record.
164 record.reset(new NfcNdefRecord());
165 EXPECT_FALSE(record->IsPopulated());
167 // Incorrect optional fields. Should fail.
168 data.SetInteger(NfcNdefRecord::kFieldAction, 0);
169 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeSmartPoster, &data));
170 EXPECT_FALSE(record->IsPopulated());
172 data.SetString(NfcNdefRecord::kFieldAction, kTestAction);
173 data.SetInteger(NfcNdefRecord::kFieldMimeType, 0);
174 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeSmartPoster, &data));
175 EXPECT_FALSE(record->IsPopulated());
177 data.SetString(NfcNdefRecord::kFieldMimeType, kTestMimeType);
178 data.SetInteger(NfcNdefRecord::kFieldTargetSize, kTestTargetSize);
179 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeSmartPoster, &data));
180 EXPECT_FALSE(record->IsPopulated());
182 data.SetDouble(NfcNdefRecord::kFieldTargetSize, kTestTargetSize);
183 data.SetInteger(NfcNdefRecord::kFieldTitles, 0);
184 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeSmartPoster, &data));
185 EXPECT_FALSE(record->IsPopulated());
187 // Empty titles value should fail.
188 ListValue* list_value = new ListValue();
189 data.Set(NfcNdefRecord::kFieldTitles, list_value);
191 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeSmartPoster, &data));
192 EXPECT_FALSE(record->IsPopulated());
194 // Title value with missing required field.
195 DictionaryValue* title_value = new DictionaryValue();
196 list_value->Append(title_value);
198 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeSmartPoster, &data));
199 EXPECT_FALSE(record->IsPopulated());
201 // Title value with invalid "text" field.
202 title_value->SetInteger(NfcNdefRecord::kFieldText, 0);
204 EXPECT_FALSE(record->Populate(NfcNdefRecord::kTypeSmartPoster, &data));
205 EXPECT_FALSE(record->IsPopulated());
207 // Title value with valid entries.
208 title_value->SetString(NfcNdefRecord::kFieldText, kTestText);
209 title_value->SetString(NfcNdefRecord::kFieldLanguageCode, kTestLanguageCode);
210 title_value->SetString(NfcNdefRecord::kFieldEncoding, kTestEncoding);
212 EXPECT_TRUE(record->Populate(NfcNdefRecord::kTypeSmartPoster, &data));
213 EXPECT_TRUE(record->IsPopulated());
215 // Verify the record contents.
216 std::string string_value;
217 double double_value;
218 const ListValue* const_list_value;
219 const DictionaryValue* const_dictionary_value;
220 EXPECT_TRUE(record->data().GetString(
221 NfcNdefRecord::kFieldURI, &string_value));
222 EXPECT_EQ(kTestURI, string_value);
223 EXPECT_TRUE(record->data().GetString(
224 NfcNdefRecord::kFieldMimeType, &string_value));
225 EXPECT_EQ(kTestMimeType, string_value);
226 EXPECT_TRUE(record->data().GetDouble(
227 NfcNdefRecord::kFieldTargetSize, &double_value));
228 EXPECT_EQ(kTestTargetSize, static_cast<uint32>(double_value));
229 EXPECT_TRUE(record->data().GetString(
230 NfcNdefRecord::kFieldAction, &string_value));
231 EXPECT_EQ(kTestAction, string_value);
232 EXPECT_TRUE(record->data().GetList(
233 NfcNdefRecord::kFieldTitles, &const_list_value));
234 EXPECT_EQ(static_cast<size_t>(1), const_list_value->GetSize());
235 EXPECT_TRUE(const_list_value->GetDictionary(0, &const_dictionary_value));
236 EXPECT_TRUE(const_dictionary_value->GetString(
237 NfcNdefRecord::kFieldText, &string_value));
238 EXPECT_EQ(kTestText, string_value);
239 EXPECT_TRUE(const_dictionary_value->GetString(
240 NfcNdefRecord::kFieldLanguageCode, &string_value));
241 EXPECT_EQ(kTestLanguageCode, string_value);
242 EXPECT_TRUE(const_dictionary_value->GetString(
243 NfcNdefRecord::kFieldEncoding, &string_value));
244 EXPECT_EQ(kTestEncoding, string_value);
247 } // namespace device