OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/page/EventSource.h" | 5 #include "core/page/EventSource.h" |
6 | 6 |
7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
8 #include "bindings/core/v8/ScriptState.h" | 8 #include "bindings/core/v8/ScriptState.h" |
9 #include "bindings/core/v8/ScriptValue.h" | 9 #include "bindings/core/v8/ScriptValue.h" |
10 #include "bindings/core/v8/SerializedScriptValue.h" | 10 #include "bindings/core/v8/SerializedScriptValue.h" |
11 #include "bindings/core/v8/V8Binding.h" | 11 #include "bindings/core/v8/V8Binding.h" |
12 #include "core/dom/ExecutionContext.h" | 12 #include "core/dom/ExecutionContext.h" |
13 #include "core/dom/MessagePort.h" | 13 #include "core/dom/MessagePort.h" |
14 #include "core/events/Event.h" | 14 #include "core/events/Event.h" |
15 #include "core/events/EventListener.h" | 15 #include "core/events/EventListener.h" |
16 #include "core/events/MessageEvent.h" | 16 #include "core/events/MessageEvent.h" |
17 #include "core/loader/MockThreadableLoader.h" | 17 #include "core/loader/MockThreadableLoader.h" |
18 #include "core/page/EventSourceInit.h" | 18 #include "core/page/EventSourceInit.h" |
19 #include "core/testing/DummyPageHolder.h" | 19 #include "core/testing/DummyPageHolder.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "wtf/text/CharacterNames.h" |
21 | 22 |
22 #include <string.h> | 23 #include <string.h> |
23 #include <v8.h> | 24 #include <v8.h> |
24 | 25 |
25 namespace blink { | 26 namespace blink { |
26 | 27 |
27 namespace { | 28 namespace { |
28 | 29 |
29 String dataAsString(ScriptState* scriptState, MessageEvent* event) | 30 String dataAsString(ScriptState* scriptState, MessageEvent* event) |
30 { | 31 { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 }; | 63 }; |
63 | 64 |
64 class EventSourceTest : public ::testing::Test { | 65 class EventSourceTest : public ::testing::Test { |
65 protected: | 66 protected: |
66 EventSourceTest() | 67 EventSourceTest() |
67 : m_page(DummyPageHolder::create()) | 68 : m_page(DummyPageHolder::create()) |
68 , m_source(EventSource::create(&document(), "https://localhost/", EventS
ourceInit(), m_exceptionState)) | 69 , m_source(EventSource::create(&document(), "https://localhost/", EventS
ourceInit(), m_exceptionState)) |
69 { | 70 { |
70 source()->setStateForTest(EventSource::OPEN); | 71 source()->setStateForTest(EventSource::OPEN); |
71 source()->setThreadableLoaderForTest(MockThreadableLoader::create()); | 72 source()->setThreadableLoaderForTest(MockThreadableLoader::create()); |
| 73 source()->setParser(new EventSourceParser("https://localhost/", source()
)); |
| 74 source()->setRequestInFlightForTest(true); |
72 } | 75 } |
73 ~EventSourceTest() override | 76 ~EventSourceTest() override |
74 { | 77 { |
75 source()->setThreadableLoaderForTest(nullptr); | 78 source()->setThreadableLoaderForTest(nullptr); |
76 source()->close(); | 79 source()->close(); |
77 | 80 |
78 // We need this because there is | 81 // We need this because there is |
79 // listener -> event -> source -> listener | 82 // listener -> event -> source -> listener |
80 // reference cycle on non-oilpan build. | 83 // reference cycle on non-oilpan build. |
81 m_source->removeAllEventListeners(); | 84 m_source->removeAllEventListeners(); |
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 EXPECT_EQ("4", bEvent->lastEventId()); | 419 EXPECT_EQ("4", bEvent->lastEventId()); |
417 | 420 |
418 ASSERT_EQ(1u, messageListener->events().size()); | 421 ASSERT_EQ(1u, messageListener->events().size()); |
419 ASSERT_EQ("message", messageListener->events()[0]->type()); | 422 ASSERT_EQ("message", messageListener->events()[0]->type()); |
420 RefPtrWillBeRawPtr<MessageEvent> messageEvent = static_cast<MessageEvent*>(m
essageListener->events()[0].get()); | 423 RefPtrWillBeRawPtr<MessageEvent> messageEvent = static_cast<MessageEvent*>(m
essageListener->events()[0].get()); |
421 ASSERT_EQ(MessageEvent::DataTypeSerializedScriptValue, messageEvent->dataTyp
e()); | 424 ASSERT_EQ(MessageEvent::DataTypeSerializedScriptValue, messageEvent->dataTyp
e()); |
422 EXPECT_EQ("bye", dataAsString(scriptState(), messageEvent.get())); | 425 EXPECT_EQ("bye", dataAsString(scriptState(), messageEvent.get())); |
423 EXPECT_EQ("8", messageEvent->lastEventId()); | 426 EXPECT_EQ("8", messageEvent->lastEventId()); |
424 } | 427 } |
425 | 428 |
| 429 TEST_F(EventSourceTest, InvalidUTF8Sequence) |
| 430 { |
| 431 RefPtrWillBeRawPtr<FakeEventListener> listener = FakeEventListener::create()
; |
| 432 |
| 433 source()->addEventListener("message", listener); |
| 434 enqueue("data:\xffhello\xc2\n\n"); |
| 435 |
| 436 ASSERT_EQ(1u, listener->events().size()); |
| 437 ASSERT_EQ("message", listener->events()[0]->type()); |
| 438 RefPtrWillBeRawPtr<MessageEvent> event = static_cast<MessageEvent*>(listener
->events()[0].get()); |
| 439 ASSERT_EQ(MessageEvent::DataTypeSerializedScriptValue, event->dataType()); |
| 440 String expected = String() + replacementCharacter + "hello" + replacementCha
racter; |
| 441 EXPECT_EQ(expected, dataAsString(scriptState(), event.get())); |
| 442 } |
| 443 |
426 } // namespace | 444 } // namespace |
427 | 445 |
428 } // namespace blink | 446 } // namespace blink |
OLD | NEW |