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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
539 scoped_ptr<base::Value> result; | 539 scoped_ptr<base::Value> result; |
540 ASSERT_EQ( | 540 ASSERT_EQ( |
541 kStaleElementReference, | 541 kStaleElementReference, |
542 ExecuteFindChildElement( | 542 ExecuteFindChildElement( |
543 1, &session, &web_view, element_id, params, &result).code()); | 543 1, &session, &web_view, element_id, params, &result).code()); |
544 ASSERT_EQ( | 544 ASSERT_EQ( |
545 kStaleElementReference, | 545 kStaleElementReference, |
546 ExecuteFindChildElements( | 546 ExecuteFindChildElements( |
547 1, &session, &web_view, element_id, params, &result).code()); | 547 1, &session, &web_view, element_id, params, &result).code()); |
548 } | 548 } |
549 | |
550 namespace { | |
551 | |
552 class MockCommandListener : public CommandListener { | |
553 public: | |
554 MockCommandListener() : called_(false) {} | |
555 virtual ~MockCommandListener() {} | |
556 | |
557 virtual Status OnCommand(const std::string& command_name) OVERRIDE { | |
558 called_ = true; | |
559 EXPECT_STREQ("cmd", command_name.c_str()); | |
560 return Status(kOk); | |
561 } | |
562 | |
563 void VerifyCalled() { | |
564 EXPECT_TRUE(called_); | |
565 } | |
566 | |
567 void VerifyNotCalled() { | |
568 EXPECT_FALSE(called_); | |
569 } | |
570 | |
571 private: | |
572 bool called_; | |
573 }; | |
574 | |
575 Status ExecuteAddListenerToSession( | |
576 MockCommandListener* listener, | |
577 Session* session, | |
578 const base::DictionaryValue& params, | |
579 scoped_ptr<base::Value>* return_value) { | |
580 session->AddListener(listener); | |
581 listener->VerifyNotCalled(); | |
582 return Status(kOk); | |
583 } | |
584 | |
585 Status ExecuteTestListener( | |
586 Session* session, | |
587 const base::DictionaryValue& params, | |
588 scoped_ptr<base::Value>* return_value) { | |
589 session->quit = true; | |
590 return Status(kOk); | |
591 } | |
592 | |
593 void VerifyListenerNotNotifiedAfterCommandRun( | |
594 MockCommandListener* listener, | |
595 base::RunLoop* run_loop, | |
596 const Status& status, | |
597 scoped_ptr<base::Value> value, | |
598 const std::string& session_id) { | |
599 ASSERT_EQ(kOk, status.code()); | |
600 listener->VerifyNotCalled(); | |
601 run_loop->Quit(); | |
602 } | |
603 | |
604 | |
605 void VerifyListenerNotifiedBeforeCommandRun( | |
606 MockCommandListener* listener, | |
607 base::RunLoop* run_loop, | |
608 const Status& status, | |
609 scoped_ptr<base::Value> value, | |
610 const std::string& session_id) { | |
611 ASSERT_EQ(kOk, status.code()); | |
612 listener->VerifyCalled(); | |
613 run_loop->Quit(); | |
614 } | |
615 | |
616 } // namespace | |
617 | |
618 TEST(CommandsTest, SessionNotifiedOfCommand) { | |
619 SessionThreadMap map; | |
620 linked_ptr<base::Thread> thread(new base::Thread("1")); | |
621 ASSERT_TRUE(thread->Start()); | |
622 std::string id("id"); | |
623 thread->message_loop()->PostTask( | |
624 FROM_HERE, | |
625 base::Bind(&internal::CreateSessionOnSessionThreadForTesting, id)); | |
626 map[id] = thread; | |
627 | |
628 base::DictionaryValue params; | |
629 MockCommandListener* listener = new MockCommandListener(); | |
stgao
2014/06/15 23:54:40
Memory leak here.
You could use a scoped_ptr, just
johnmoore
2014/06/16 20:49:18
(See reply to other memory leak concern)
| |
630 SessionCommand cmd = base::Bind( | |
631 &ExecuteAddListenerToSession, listener); | |
632 | |
633 base::MessageLoop loop; | |
634 base::RunLoop run_loop_addlistener; | |
635 | |
636 // Listeners are notified immediately before commands are run. | |
637 // Here, the command adds the listener to the session, so the listener | |
638 // should not be notified since it will not have been added yet. | |
639 ExecuteSessionCommand( | |
640 &map, | |
641 "cmd", | |
642 cmd, | |
643 false, | |
644 params, | |
645 id, | |
646 base::Bind(&VerifyListenerNotNotifiedAfterCommandRun, listener, | |
647 &run_loop_addlistener)); | |
648 | |
649 run_loop_addlistener.Run(); | |
650 | |
651 base::RunLoop run_loop_testlistener; | |
652 cmd = base::Bind( | |
653 &ExecuteTestListener); | |
654 | |
655 // The listener has been added to the session and should be notified of | |
656 // the ExecuteTestListener command. | |
657 ExecuteSessionCommand( | |
658 &map, | |
659 "cmd", | |
660 cmd, | |
661 false, | |
662 params, | |
663 id, | |
664 base::Bind(&VerifyListenerNotifiedBeforeCommandRun, listener, | |
665 &run_loop_testlistener)); | |
666 | |
667 run_loop_testlistener.Run(); | |
668 } | |
OLD | NEW |