OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <list> | 5 #include <list> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 base::Bind(&ReturnOutOfOrderResponses, &recurse_count, &client)); | 560 base::Bind(&ReturnOutOfOrderResponses, &recurse_count, &client)); |
561 base::DictionaryValue params; | 561 base::DictionaryValue params; |
562 params.SetInteger("param", 1); | 562 params.SetInteger("param", 1); |
563 scoped_ptr<base::DictionaryValue> result; | 563 scoped_ptr<base::DictionaryValue> result; |
564 ASSERT_TRUE(client.SendCommandAndGetResult("method", params, &result).IsOk()); | 564 ASSERT_TRUE(client.SendCommandAndGetResult("method", params, &result).IsOk()); |
565 ASSERT_TRUE(result); | 565 ASSERT_TRUE(result); |
566 int key; | 566 int key; |
567 ASSERT_TRUE(result->GetInteger("key", &key)); | 567 ASSERT_TRUE(result->GetInteger("key", &key)); |
568 ASSERT_EQ(2, key); | 568 ASSERT_EQ(2, key); |
569 } | 569 } |
| 570 |
| 571 namespace { |
| 572 |
| 573 class OnConnectedListener : public DevToolsEventListener { |
| 574 public: |
| 575 OnConnectedListener(const std::string& method, DevToolsClient* client) |
| 576 : method_(method), |
| 577 client_(client), |
| 578 on_connected_called_(false), |
| 579 on_event_called_(false) { |
| 580 client_->AddListener(this); |
| 581 } |
| 582 virtual ~OnConnectedListener() {} |
| 583 |
| 584 void VerifyCalled() { |
| 585 EXPECT_TRUE(on_connected_called_); |
| 586 EXPECT_TRUE(on_event_called_); |
| 587 } |
| 588 |
| 589 virtual Status OnConnected() OVERRIDE { |
| 590 EXPECT_FALSE(on_connected_called_); |
| 591 EXPECT_FALSE(on_event_called_); |
| 592 on_connected_called_ = true; |
| 593 base::DictionaryValue params; |
| 594 return client_->SendCommand(method_, params); |
| 595 } |
| 596 |
| 597 virtual void OnEvent(const std::string& method, |
| 598 const base::DictionaryValue& params) OVERRIDE { |
| 599 EXPECT_TRUE(on_connected_called_); |
| 600 on_event_called_ = true; |
| 601 } |
| 602 |
| 603 private: |
| 604 std::string method_; |
| 605 DevToolsClient* client_; |
| 606 bool on_connected_called_; |
| 607 bool on_event_called_; |
| 608 }; |
| 609 |
| 610 class OnConnectedSyncWebSocket : public SyncWebSocket { |
| 611 public: |
| 612 OnConnectedSyncWebSocket() : connected_(false) {} |
| 613 virtual ~OnConnectedSyncWebSocket() {} |
| 614 |
| 615 virtual bool Connect(const GURL& url) OVERRIDE { |
| 616 connected_ = true; |
| 617 return true; |
| 618 } |
| 619 |
| 620 virtual bool Send(const std::string& message) OVERRIDE { |
| 621 EXPECT_TRUE(connected_); |
| 622 scoped_ptr<base::Value> value(base::JSONReader::Read(message)); |
| 623 base::DictionaryValue* dict = NULL; |
| 624 EXPECT_TRUE(value->GetAsDictionary(&dict)); |
| 625 if (!dict) |
| 626 return false; |
| 627 int id; |
| 628 EXPECT_TRUE(dict->GetInteger("id", &id)); |
| 629 std::string method; |
| 630 EXPECT_TRUE(dict->GetString("method", &method)); |
| 631 |
| 632 base::DictionaryValue response; |
| 633 response.SetInteger("id", id); |
| 634 response.Set("result", new base::DictionaryValue()); |
| 635 std::string json_response; |
| 636 base::JSONWriter::Write(&response, &json_response); |
| 637 queued_response_.push_back(json_response); |
| 638 |
| 639 // Push one event. |
| 640 base::DictionaryValue event; |
| 641 event.SetString("method", "updateEvent"); |
| 642 event.Set("params", new base::DictionaryValue()); |
| 643 std::string json_event; |
| 644 base::JSONWriter::Write(&event, &json_event); |
| 645 queued_response_.push_back(json_event); |
| 646 |
| 647 return true; |
| 648 } |
| 649 |
| 650 virtual bool ReceiveNextMessage(std::string* message) OVERRIDE { |
| 651 if (queued_response_.empty()) |
| 652 return false; |
| 653 *message = queued_response_.front(); |
| 654 queued_response_.pop_front(); |
| 655 return true; |
| 656 } |
| 657 |
| 658 virtual bool HasNextMessage() OVERRIDE { |
| 659 return !queued_response_.empty(); |
| 660 } |
| 661 |
| 662 private: |
| 663 bool connected_; |
| 664 std::list<std::string> queued_response_; |
| 665 }; |
| 666 |
| 667 } // namespace |
| 668 |
| 669 TEST(DevToolsClientImpl, ProcessOnConnectedBeforeOnEvent) { |
| 670 SyncWebSocketFactory factory = |
| 671 base::Bind(&CreateMockSyncWebSocket<OnConnectedSyncWebSocket>); |
| 672 DevToolsClientImpl client(factory, "http://url"); |
| 673 OnConnectedListener listener1("DOM.getDocument", &client); |
| 674 OnConnectedListener listener2("Runtime.enable", &client); |
| 675 OnConnectedListener listener3("Page.enable", &client); |
| 676 base::DictionaryValue params; |
| 677 EXPECT_EQ(kOk, client.SendCommand("Runtime.execute", params).code()); |
| 678 listener1.VerifyCalled(); |
| 679 listener2.VerifyCalled(); |
| 680 listener3.VerifyCalled(); |
| 681 } |
OLD | NEW |