OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ppapi/tests/test_websocket.h" | 5 #include "ppapi/tests/test_websocket.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "ppapi/c/dev/ppb_testing_dev.h" | 10 #include "ppapi/c/dev/ppb_testing_dev.h" |
11 #include "ppapi/c/dev/ppb_websocket_dev.h" | 11 #include "ppapi/c/dev/ppb_websocket_dev.h" |
12 #include "ppapi/c/pp_errors.h" | 12 #include "ppapi/c/pp_errors.h" |
13 #include "ppapi/c/pp_var.h" | 13 #include "ppapi/c/pp_var.h" |
14 #include "ppapi/c/pp_completion_callback.h" | 14 #include "ppapi/c/pp_completion_callback.h" |
15 #include "ppapi/c/ppb_core.h" | 15 #include "ppapi/c/ppb_core.h" |
16 #include "ppapi/c/ppb_var.h" | 16 #include "ppapi/c/ppb_var.h" |
17 #include "ppapi/cpp/dev/websocket_dev.h" | 17 #include "ppapi/cpp/dev/websocket_dev.h" |
18 #include "ppapi/cpp/helper/dev/websocket_api_dev.h" | |
18 #include "ppapi/cpp/instance.h" | 19 #include "ppapi/cpp/instance.h" |
19 #include "ppapi/cpp/module.h" | 20 #include "ppapi/cpp/module.h" |
20 #include "ppapi/tests/test_utils.h" | 21 #include "ppapi/tests/test_utils.h" |
21 #include "ppapi/tests/testing_instance.h" | 22 #include "ppapi/tests/testing_instance.h" |
22 | 23 |
23 const char kEchoServerURL[] = | 24 const char kEchoServerURL[] = |
24 "ws://localhost:8880/websocket/tests/hybi/echo"; | 25 "ws://localhost:8880/websocket/tests/hybi/echo"; |
25 | 26 |
26 const char kCloseServerURL[] = | 27 const char kCloseServerURL[] = |
27 "ws://localhost:8880/websocket/tests/hybi/close"; | 28 "ws://localhost:8880/websocket/tests/hybi/close"; |
28 | 29 |
29 const char kProtocolTestServerURL[] = | 30 const char kProtocolTestServerURL[] = |
30 "ws://localhost:8880/websocket/tests/hybi/protocol-test?protocol="; | 31 "ws://localhost:8880/websocket/tests/hybi/protocol-test?protocol="; |
31 | 32 |
32 const char* const kInvalidURLs[] = { | 33 const char* const kInvalidURLs[] = { |
33 "http://www.google.com/invalid_scheme", | 34 "http://www.google.com/invalid_scheme", |
34 "ws://www.google.com/invalid#fragment", | 35 "ws://www.google.com/invalid#fragment", |
35 "ws://www.google.com:65535/invalid_port", | 36 "ws://www.google.com:65535/invalid_port", |
36 NULL | 37 NULL |
37 }; | 38 }; |
38 | 39 |
39 // Connection close code is defined in WebSocket protocol specification. | 40 // Connection close code is defined in WebSocket protocol specification. |
40 // The magic number 1000 means gracefull closure without any error. | 41 // The magic number 1000 means gracefull closure without any error. |
41 // See section 7.4.1. of RFC 6455. | 42 // See section 7.4.1. of RFC 6455. |
42 const uint16_t kCloseCodeNormalClosure = 1000U; | 43 const uint16_t kCloseCodeNormalClosure = 1000U; |
43 | 44 |
45 namespace { | |
46 | |
47 struct WebSocketEvent { | |
48 enum EventType { | |
49 EVENT_OPEN, | |
50 EVENT_MESSAGE, | |
51 EVENT_ERROR, | |
52 EVENT_CLOSE | |
53 }; | |
54 | |
55 WebSocketEvent(EventType type, | |
56 bool was_clean, | |
57 uint16_t close_code, | |
58 const pp::Var& var) | |
59 : event_type(type), | |
60 was_clean(was_clean), | |
61 close_code(close_code), | |
62 var(var) {} | |
63 EventType event_type; | |
64 bool was_clean; | |
65 uint16_t close_code; | |
66 pp::Var var; | |
67 }; | |
68 | |
69 class TestWebSocketAPI : public pp::helper::WebSocketAPI_Dev { | |
70 public: | |
71 explicit TestWebSocketAPI(pp::Instance* instance) | |
72 : pp::helper::WebSocketAPI_Dev(instance), | |
73 connected_(false), | |
74 received_(false), | |
75 closed_(false), | |
76 wait_for_connected_(false), | |
77 wait_for_received_(false), | |
78 wait_for_closed_(false), | |
79 instance_(instance->pp_instance()) {} | |
80 | |
81 virtual void OnOpen() { | |
82 events_.push_back( | |
83 WebSocketEvent(WebSocketEvent::EVENT_OPEN, true, 0U, pp::Var())); | |
84 connected_ = true; | |
85 if (wait_for_connected_) { | |
86 GetTestingInterface()->QuitMessageLoop(instance_); | |
87 wait_for_connected_ = false; | |
88 } | |
89 } | |
90 | |
91 virtual void OnMessage(const pp::Var &message) { | |
92 events_.push_back( | |
93 WebSocketEvent(WebSocketEvent::EVENT_MESSAGE, true, 0U, message)); | |
94 received_ = true; | |
95 if (wait_for_received_) { | |
96 GetTestingInterface()->QuitMessageLoop(instance_); | |
97 wait_for_received_ = false; | |
98 received_ = false; | |
99 } | |
100 } | |
101 | |
102 virtual void OnError() { | |
103 events_.push_back( | |
104 WebSocketEvent(WebSocketEvent::EVENT_ERROR, true, 0U, pp::Var())); | |
105 } | |
106 | |
107 virtual void OnClose( | |
108 bool was_clean, uint16_t code, const pp::Var& reason) { | |
109 events_.push_back( | |
110 WebSocketEvent(WebSocketEvent::EVENT_CLOSE, was_clean, code, reason)); | |
111 connected_ = true; | |
112 closed_ = true; | |
113 if (wait_for_connected_ || wait_for_closed_) { | |
114 GetTestingInterface()->QuitMessageLoop(instance_); | |
115 wait_for_connected_ = false; | |
116 wait_for_closed_ = false; | |
117 } | |
118 } | |
119 | |
120 void WaitForConnected() { | |
121 if (!connected_) { | |
122 wait_for_connected_ = true; | |
123 GetTestingInterface()->RunMessageLoop(instance_); | |
124 } | |
125 } | |
126 | |
127 void WaitForReceived() { | |
128 if (!received_) { | |
129 wait_for_received_ = true; | |
130 GetTestingInterface()->RunMessageLoop(instance_); | |
131 } | |
132 } | |
133 | |
134 void WaitForClosed() { | |
135 if (!closed_) { | |
136 wait_for_closed_ = true; | |
137 GetTestingInterface()->RunMessageLoop(instance_); | |
138 } | |
139 } | |
140 | |
141 const std::vector<WebSocketEvent>& GetSeenEvents() const { | |
142 return events_; | |
143 } | |
144 | |
145 private: | |
146 std::vector<WebSocketEvent> events_; | |
147 bool connected_; | |
148 bool received_; | |
149 bool closed_; | |
150 bool wait_for_connected_; | |
151 bool wait_for_received_; | |
152 bool wait_for_closed_; | |
153 PP_Instance instance_; | |
154 }; | |
155 | |
156 } // namespace | |
157 | |
44 REGISTER_TEST_CASE(WebSocket); | 158 REGISTER_TEST_CASE(WebSocket); |
45 | 159 |
46 bool TestWebSocket::Init() { | 160 bool TestWebSocket::Init() { |
47 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( | 161 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( |
48 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); | 162 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); |
49 var_interface_ = static_cast<const PPB_Var*>( | 163 var_interface_ = static_cast<const PPB_Var*>( |
50 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); | 164 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); |
51 core_interface_ = static_cast<const PPB_Core*>( | 165 core_interface_ = static_cast<const PPB_Core*>( |
52 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); | 166 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); |
53 if (!websocket_interface_ || !var_interface_ || !core_interface_) | 167 if (!websocket_interface_ || !var_interface_ || !core_interface_) |
54 return false; | 168 return false; |
55 | 169 |
56 return InitTestingInterface(); | 170 return InitTestingInterface(); |
57 } | 171 } |
58 | 172 |
59 void TestWebSocket::RunTests(const std::string& filter) { | 173 void TestWebSocket::RunTests(const std::string& filter) { |
60 RUN_TEST_WITH_REFERENCE_CHECK(IsWebSocket, filter); | 174 RUN_TEST_WITH_REFERENCE_CHECK(IsWebSocket, filter); |
61 RUN_TEST_WITH_REFERENCE_CHECK(UninitializedPropertiesAccess, filter); | 175 RUN_TEST_WITH_REFERENCE_CHECK(UninitializedPropertiesAccess, filter); |
62 RUN_TEST_WITH_REFERENCE_CHECK(InvalidConnect, filter); | 176 RUN_TEST_WITH_REFERENCE_CHECK(InvalidConnect, filter); |
63 RUN_TEST_WITH_REFERENCE_CHECK(Protocols, filter); | 177 RUN_TEST_WITH_REFERENCE_CHECK(Protocols, filter); |
64 RUN_TEST_WITH_REFERENCE_CHECK(GetURL, filter); | 178 RUN_TEST_WITH_REFERENCE_CHECK(GetURL, filter); |
65 RUN_TEST_WITH_REFERENCE_CHECK(ValidConnect, filter); | 179 RUN_TEST_WITH_REFERENCE_CHECK(ValidConnect, filter); |
66 RUN_TEST_WITH_REFERENCE_CHECK(InvalidClose, filter); | 180 RUN_TEST_WITH_REFERENCE_CHECK(InvalidClose, filter); |
67 RUN_TEST_WITH_REFERENCE_CHECK(ValidClose, filter); | 181 RUN_TEST_WITH_REFERENCE_CHECK(ValidClose, filter); |
68 RUN_TEST_WITH_REFERENCE_CHECK(GetProtocol, filter); | 182 RUN_TEST_WITH_REFERENCE_CHECK(GetProtocol, filter); |
69 RUN_TEST_WITH_REFERENCE_CHECK(TextSendReceive, filter); | 183 RUN_TEST_WITH_REFERENCE_CHECK(TextSendReceive, filter); |
70 | 184 |
71 RUN_TEST_WITH_REFERENCE_CHECK(CcInterfaces, filter); | 185 RUN_TEST_WITH_REFERENCE_CHECK(CcInterfaces, filter); |
186 | |
187 RUN_TEST_WITH_REFERENCE_CHECK(HelperInvalidConnect, filter); | |
188 RUN_TEST_WITH_REFERENCE_CHECK(HelperProtocols, filter); | |
189 RUN_TEST_WITH_REFERENCE_CHECK(HelperGetURL, filter); | |
190 RUN_TEST_WITH_REFERENCE_CHECK(HelperValidConnect, filter); | |
191 RUN_TEST_WITH_REFERENCE_CHECK(HelperInvalidClose, filter); | |
192 RUN_TEST_WITH_REFERENCE_CHECK(HelperValidClose, filter); | |
193 RUN_TEST_WITH_REFERENCE_CHECK(HelperGetProtocol, filter); | |
194 RUN_TEST_WITH_REFERENCE_CHECK(HelperTextSendReceive, filter); | |
72 } | 195 } |
73 | 196 |
74 PP_Var TestWebSocket::CreateVar(const char* string) { | 197 PP_Var TestWebSocket::CreateVar(const char* string) { |
75 return var_interface_->VarFromUtf8(string, strlen(string)); | 198 return var_interface_->VarFromUtf8(string, strlen(string)); |
76 } | 199 } |
77 | 200 |
78 void TestWebSocket::ReleaseVar(const PP_Var& var) { | 201 void TestWebSocket::ReleaseVar(const PP_Var& var) { |
79 var_interface_->Release(var); | 202 var_interface_->Release(var); |
80 } | 203 } |
81 | 204 |
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
416 | 539 |
417 // TODO(toyoshim): Add tests for GetBufferedAmount(). | 540 // TODO(toyoshim): Add tests for GetBufferedAmount(). |
418 // For now, the function doesn't work fine because update callback in WebKit is | 541 // For now, the function doesn't work fine because update callback in WebKit is |
419 // not landed yet. | 542 // not landed yet. |
420 | 543 |
421 // TODO(toyoshim): Add tests for didReceiveMessageError(). | 544 // TODO(toyoshim): Add tests for didReceiveMessageError(). |
422 | 545 |
423 // TODO(toyoshim): Add other function tests. | 546 // TODO(toyoshim): Add other function tests. |
424 | 547 |
425 std::string TestWebSocket::TestCcInterfaces() { | 548 std::string TestWebSocket::TestCcInterfaces() { |
426 // C++ bindings is simple straightforward, then just verifies interfaces work | 549 // The C++ bindings is simple straightforward. This just verifies that the |
427 // as a interface bridge fine. | 550 // bindings work fine as an interface bridge. |
428 pp::WebSocket_Dev ws(instance_); | 551 pp::WebSocket_Dev ws(instance_); |
429 | 552 |
430 // Check uninitialized properties access. | 553 // Check uninitialized properties access. |
431 ASSERT_EQ(0, ws.GetBufferedAmount()); | 554 ASSERT_EQ(0, ws.GetBufferedAmount()); |
432 ASSERT_EQ(0, ws.GetCloseCode()); | 555 ASSERT_EQ(0, ws.GetCloseCode()); |
433 ASSERT_TRUE(AreEqual(ws.GetCloseReason().pp_var(), "")); | 556 ASSERT_TRUE(AreEqual(ws.GetCloseReason().pp_var(), "")); |
434 ASSERT_EQ(false, ws.GetCloseWasClean()); | 557 ASSERT_EQ(false, ws.GetCloseWasClean()); |
435 ASSERT_TRUE(AreEqual(ws.GetExtensions().pp_var(), "")); | 558 ASSERT_TRUE(AreEqual(ws.GetExtensions().pp_var(), "")); |
436 ASSERT_TRUE(AreEqual(ws.GetProtocol().pp_var(), "")); | 559 ASSERT_TRUE(AreEqual(ws.GetProtocol().pp_var(), "")); |
437 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_INVALID_DEV, ws.GetReadyState()); | 560 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_INVALID_DEV, ws.GetReadyState()); |
438 ASSERT_TRUE(AreEqual(ws.GetURL().pp_var(), "")); | 561 ASSERT_TRUE(AreEqual(ws.GetURL().pp_var(), "")); |
439 | 562 |
440 // Check communication interfaces (connect, send, receive, and close). | 563 // Check communication interfaces (connect, send, receive, and close). |
441 TestCompletionCallback connect_callback(instance_->pp_instance()); | 564 TestCompletionCallback connect_callback(instance_->pp_instance()); |
442 int32_t result = ws.Connect(pp::Var(std::string(kCloseServerURL)), NULL, 0U, | 565 int32_t result = ws.Connect( |
443 connect_callback); | 566 pp::Var(std::string(kCloseServerURL)), NULL, 0U, connect_callback); |
444 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | 567 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
445 result = connect_callback.WaitForResult(); | 568 result = connect_callback.WaitForResult(); |
446 ASSERT_EQ(PP_OK, result); | 569 ASSERT_EQ(PP_OK, result); |
447 | 570 |
448 std::string message("hello C++"); | 571 std::string message("hello C++"); |
449 result = ws.SendMessage(pp::Var(message)); | 572 result = ws.SendMessage(pp::Var(message)); |
450 ASSERT_EQ(PP_OK, result); | 573 ASSERT_EQ(PP_OK, result); |
451 | 574 |
452 pp::Var receive_var; | 575 pp::Var receive_var; |
453 TestCompletionCallback receive_callback(instance_->pp_instance()); | 576 TestCompletionCallback receive_callback(instance_->pp_instance()); |
(...skipping 15 matching lines...) Expand all Loading... | |
469 ASSERT_EQ(kCloseCodeNormalClosure, ws.GetCloseCode()); | 592 ASSERT_EQ(kCloseCodeNormalClosure, ws.GetCloseCode()); |
470 ASSERT_TRUE(AreEqual(ws.GetCloseReason().pp_var(), reason.c_str())); | 593 ASSERT_TRUE(AreEqual(ws.GetCloseReason().pp_var(), reason.c_str())); |
471 ASSERT_EQ(true, ws.GetCloseWasClean()); | 594 ASSERT_EQ(true, ws.GetCloseWasClean()); |
472 ASSERT_TRUE(AreEqual(ws.GetExtensions().pp_var(), "")); | 595 ASSERT_TRUE(AreEqual(ws.GetExtensions().pp_var(), "")); |
473 ASSERT_TRUE(AreEqual(ws.GetProtocol().pp_var(), "")); | 596 ASSERT_TRUE(AreEqual(ws.GetProtocol().pp_var(), "")); |
474 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, ws.GetReadyState()); | 597 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, ws.GetReadyState()); |
475 ASSERT_TRUE(AreEqual(ws.GetURL().pp_var(), kCloseServerURL)); | 598 ASSERT_TRUE(AreEqual(ws.GetURL().pp_var(), kCloseServerURL)); |
476 | 599 |
477 PASS(); | 600 PASS(); |
478 } | 601 } |
602 | |
603 std::string TestWebSocket::TestHelperInvalidConnect() { | |
604 const pp::Var protocols[] = { pp::Var() }; | |
605 | |
606 TestWebSocketAPI websocket(instance_); | |
607 int32_t result = websocket.Connect(pp::Var(), protocols, 1U); | |
608 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | |
609 ASSERT_EQ(0U, websocket.GetSeenEvents().size()); | |
610 | |
611 result = websocket.Connect(pp::Var(), protocols, 1U); | |
612 ASSERT_EQ(PP_ERROR_INPROGRESS, result); | |
613 ASSERT_EQ(0U, websocket.GetSeenEvents().size()); | |
614 | |
615 for (int i = 0; kInvalidURLs[i]; ++i) { | |
616 TestWebSocketAPI ws(instance_); | |
617 result = ws.Connect(pp::Var(std::string(kInvalidURLs[i])), protocols, 0U); | |
618 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | |
619 ASSERT_EQ(0U, ws.GetSeenEvents().size()); | |
620 } | |
621 | |
622 PASS(); | |
623 } | |
624 | |
625 std::string TestWebSocket::TestHelperProtocols() { | |
626 const pp::Var bad_protocols[] = { | |
627 pp::Var(std::string("x-test")), pp::Var(std::string("x-test")) }; | |
628 const pp::Var good_protocols[] = { | |
629 pp::Var(std::string("x-test")), pp::Var(std::string("x-yatest")) }; | |
630 | |
631 { | |
632 TestWebSocketAPI websocket(instance_); | |
633 int32_t result = websocket.Connect( | |
634 pp::Var(std::string(kEchoServerURL)), bad_protocols, 2U); | |
635 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | |
636 ASSERT_EQ(0U, websocket.GetSeenEvents().size()); | |
637 } | |
638 | |
639 { | |
640 TestWebSocketAPI websocket(instance_); | |
641 int32_t result = websocket.Connect( | |
642 pp::Var(std::string(kEchoServerURL)), good_protocols, 2U); | |
643 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
644 websocket.WaitForConnected(); | |
645 const std::vector<WebSocketEvent>& events = websocket.GetSeenEvents(); | |
646 // Protocol arguments are valid, but this test run without a WebSocket | |
647 // server. As a result, OnError() and OnClose() are invoked because of | |
648 // a connection establishment failure. | |
649 ASSERT_EQ(2U, events.size()); | |
650 ASSERT_EQ(WebSocketEvent::EVENT_ERROR, events[0].event_type); | |
651 ASSERT_EQ(WebSocketEvent::EVENT_CLOSE, events[1].event_type); | |
652 ASSERT_FALSE(events[1].was_clean); | |
653 } | |
654 | |
655 PASS(); | |
656 } | |
657 | |
658 std::string TestWebSocket::TestHelperGetURL() { | |
659 const pp::Var protocols[] = { pp::Var() }; | |
660 | |
661 for (int i = 0; kInvalidURLs[i]; ++i) { | |
662 TestWebSocketAPI websocket(instance_); | |
663 int32_t result = websocket.Connect( | |
664 pp::Var(std::string(kInvalidURLs[i])), protocols, 0U); | |
665 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | |
666 pp::Var url = websocket.GetURL(); | |
667 ASSERT_TRUE(AreEqual(url.pp_var(), kInvalidURLs[i])); | |
668 ASSERT_EQ(0U, websocket.GetSeenEvents().size()); | |
669 } | |
670 | |
671 PASS(); | |
672 } | |
673 | |
674 std::string TestWebSocket::TestHelperValidConnect() { | |
675 const pp::Var protocols[] = { pp::Var() }; | |
676 TestWebSocketAPI websocket(instance_); | |
677 int32_t result = websocket.Connect( | |
678 pp::Var(std::string(kEchoServerURL)), protocols, 0U); | |
679 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
680 websocket.WaitForConnected(); | |
681 const std::vector<WebSocketEvent>& events = websocket.GetSeenEvents(); | |
682 ASSERT_EQ(1U, events.size()); | |
683 ASSERT_EQ(WebSocketEvent::EVENT_OPEN, events[0].event_type); | |
684 | |
685 PASS(); | |
686 } | |
687 | |
688 std::string TestWebSocket::TestHelperInvalidClose() { | |
689 const pp::Var reason = pp::Var(std::string("close for test")); | |
690 | |
691 // Close before connect. | |
692 { | |
693 TestWebSocketAPI websocket(instance_); | |
694 int32_t result = websocket.Close(kCloseCodeNormalClosure, reason); | |
695 ASSERT_EQ(PP_ERROR_FAILED, result); | |
696 ASSERT_EQ(0U, websocket.GetSeenEvents().size()); | |
697 } | |
698 | |
699 // Close with bad arguments. | |
700 { | |
701 TestWebSocketAPI websocket(instance_); | |
702 int32_t result = websocket.Connect(pp::Var(std::string(kEchoServerURL)), | |
703 NULL, 0); | |
704 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
705 websocket.WaitForConnected(); | |
706 result = websocket.Close(1U, reason); | |
707 ASSERT_EQ(PP_ERROR_NOACCESS, result); | |
708 const std::vector<WebSocketEvent>& events = websocket.GetSeenEvents(); | |
709 ASSERT_EQ(1U, events.size()); | |
710 ASSERT_EQ(WebSocketEvent::EVENT_OPEN, events[0].event_type); | |
711 } | |
712 | |
713 PASS(); | |
714 } | |
715 | |
716 std::string TestWebSocket::TestHelperValidClose() { | |
717 std::string reason("close for test"); | |
718 pp::Var url = pp::Var(std::string(kCloseServerURL)); | |
719 | |
720 // Close. | |
721 { | |
722 TestWebSocketAPI websocket(instance_); | |
723 int32_t result = websocket.Connect(url, NULL, 0U); | |
724 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
725 websocket.WaitForConnected(); | |
726 result = websocket.Close(kCloseCodeNormalClosure, pp::Var(reason)); | |
727 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
728 websocket.WaitForClosed(); | |
729 const std::vector<WebSocketEvent>& events = websocket.GetSeenEvents(); | |
730 ASSERT_EQ(2U, events.size()); | |
731 ASSERT_EQ(WebSocketEvent::EVENT_OPEN, events[0].event_type); | |
732 ASSERT_EQ(WebSocketEvent::EVENT_CLOSE, events[1].event_type); | |
733 ASSERT_TRUE(events[1].was_clean); | |
734 ASSERT_EQ(kCloseCodeNormalClosure, events[1].close_code); | |
735 ASSERT_TRUE(AreEqual(events[1].var.pp_var(), reason.c_str())); | |
736 } | |
737 | |
738 // Close in connecting. | |
739 // The ongoing connect failed with PP_ERROR_ABORTED, then the close is done | |
740 // successfully. | |
741 { | |
742 TestWebSocketAPI websocket(instance_); | |
743 int32_t result = websocket.Connect(url, NULL, 0U); | |
744 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
745 result = websocket.Close(kCloseCodeNormalClosure, pp::Var(reason)); | |
746 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
747 websocket.WaitForClosed(); | |
748 const std::vector<WebSocketEvent>& events = websocket.GetSeenEvents(); | |
749 ASSERT_TRUE(events.size() == 2 || events.size() == 3); | |
750 int index = 0; | |
751 if (events.size() == 3) | |
752 ASSERT_EQ(WebSocketEvent::EVENT_OPEN, events[index++].event_type); | |
753 ASSERT_EQ(WebSocketEvent::EVENT_ERROR, events[index++].event_type); | |
754 ASSERT_EQ(WebSocketEvent::EVENT_CLOSE, events[index].event_type); | |
755 ASSERT_FALSE(events[index].was_clean); | |
756 } | |
757 | |
758 // Close in closing. | |
759 // The first close will be done successfully, then the second one failed with | |
760 // with PP_ERROR_INPROGRESS immediately. | |
761 { | |
762 TestWebSocketAPI websocket(instance_); | |
763 int32_t result = websocket.Connect(url, NULL, 0U); | |
764 result = websocket.Close(kCloseCodeNormalClosure, pp::Var(reason)); | |
765 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
766 result = websocket.Close(kCloseCodeNormalClosure, pp::Var(reason)); | |
767 ASSERT_EQ(PP_ERROR_INPROGRESS, result); | |
768 websocket.WaitForClosed(); | |
769 const std::vector<WebSocketEvent>& events = websocket.GetSeenEvents(); | |
770 ASSERT_TRUE(events.size() == 2 || events.size() == 3) | |
771 int index = 0; | |
772 if (events.size() == 3) | |
773 ASSERT_EQ(WebSocketEvent::EVENT_OPEN, events[index++].event_type); | |
774 ASSERT_EQ(WebSocketEvent::EVENT_ERROR, events[index++].event_type); | |
775 ASSERT_EQ(WebSocketEvent::EVENT_CLOSE, events[index].event_type); | |
776 ASSERT_FALSE(events[index].was_clean); | |
777 } | |
778 | |
779 PASS(); | |
780 } | |
781 | |
782 std::string TestWebSocket::TestHelperGetProtocol() { | |
783 const std::string protocol("x-chat"); | |
784 const pp::Var protocols[] = { pp::Var(protocol) }; | |
785 std::string url(kProtocolTestServerURL); | |
786 url += protocol; | |
787 TestWebSocketAPI websocket(instance_); | |
788 int32_t result = websocket.Connect(pp::Var(url), protocols, 1U); | |
789 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
790 websocket.WaitForReceived(); | |
791 ASSERT_TRUE(AreEqual(websocket.GetProtocol().pp_var(), protocol.c_str())); | |
792 const std::vector<WebSocketEvent>& events = websocket.GetSeenEvents(); | |
793 ASSERT_EQ(2U, events.size()); | |
794 ASSERT_EQ(WebSocketEvent::EVENT_OPEN, events[0].event_type); | |
795 ASSERT_EQ(WebSocketEvent::EVENT_MESSAGE, events[1].event_type); | |
dmichael (off chromium)
2011/12/16 16:11:10
Why do you receive a message here? Can you documen
Takashi Toyoshima
2011/12/19 05:14:01
OK, I added comments on server side handler behavi
| |
796 ASSERT_TRUE(AreEqual(events[1].var.pp_var(), protocol.c_str())); | |
797 ASSERT_TRUE(events[1].was_clean); | |
798 | |
799 PASS(); | |
800 } | |
801 | |
802 std::string TestWebSocket::TestHelperTextSendReceive() { | |
803 const pp::Var protocols[] = { pp::Var() }; | |
804 TestWebSocketAPI websocket(instance_); | |
805 int32_t result = | |
806 websocket.Connect(pp::Var(std::string(kEchoServerURL)), protocols, 0U); | |
807 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
808 websocket.WaitForConnected(); | |
809 | |
810 // Send 'hello pepper'. | |
811 std::string message1("hello pepper"); | |
812 result = websocket.Send(pp::Var(std::string(message1))); | |
813 ASSERT_EQ(PP_OK, result); | |
814 | |
815 // Receive echoed 'hello pepper'. | |
816 websocket.WaitForReceived(); | |
817 | |
818 // Send 'goodbye pepper'. | |
819 std::string message2("goodbye pepper"); | |
820 result = websocket.Send(pp::Var(std::string(message2))); | |
821 | |
822 // Receive echoed 'goodbye pepper'. | |
823 websocket.WaitForReceived(); | |
824 | |
825 const std::vector<WebSocketEvent>& events = websocket.GetSeenEvents(); | |
826 ASSERT_EQ(3U, events.size()); | |
827 ASSERT_EQ(WebSocketEvent::EVENT_OPEN, events[0].event_type); | |
828 ASSERT_EQ(WebSocketEvent::EVENT_MESSAGE, events[1].event_type); | |
829 ASSERT_TRUE(AreEqual(events[1].var.pp_var(), message1.c_str())); | |
830 ASSERT_EQ(WebSocketEvent::EVENT_MESSAGE, events[2].event_type); | |
831 ASSERT_TRUE(AreEqual(events[2].var.pp_var(), message2.c_str())); | |
832 | |
833 PASS(); | |
834 } | |
OLD | NEW |