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