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