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 { | |
stgao
2014/07/01 19:02:27
duplicate code, how about extracting it out as a c
johnmoore
2014/07/02 22:17:44
I have removed session->AddListener, so the duplic
stgao
2014/07/11 21:36:07
It seems there is another MockCommandListener in c
| |
553 public: | |
554 MockCommandListener() : called_(false) {} | |
555 virtual ~MockCommandListener() {} | |
556 | |
557 virtual Status BeforeCommand(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( | |
stgao
2014/07/01 19:02:27
The name is not that clear for setting session->qu
johnmoore
2014/07/02 22:17:44
I have updated the command and callback names to m
| |
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) { | |
stgao
2014/07/01 19:02:27
Seems some parameters are not used. Could they be
johnmoore
2014/07/02 22:17:44
They could be removed if I re-implemented ExecuteS
stgao
2014/07/11 21:36:06
OK. That's fine.
| |
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 // |listener| is owned by and will be destroyed by |session| | |
630 MockCommandListener* listener = new MockCommandListener(); | |
stgao
2014/07/01 19:02:27
As we have a reference to this |listener|, we coul
johnmoore
2014/07/02 22:17:44
I have fixed the command and callback method names
stgao
2014/07/11 21:36:06
You are right. We can't do something like ErrorFin
| |
631 SessionCommand cmd = base::Bind( | |
632 &ExecuteAddListenerToSession, listener); | |
633 | |
634 base::MessageLoop loop; | |
635 base::RunLoop run_loop_addlistener; | |
636 | |
637 // |CommandListener|s are notified immediately before commands are run. | |
638 // Here, the command adds |listener| to the session, so |listener| | |
639 // should not be notified since it will not have been added yet. | |
640 ExecuteSessionCommand( | |
641 &map, | |
642 "cmd", | |
643 cmd, | |
644 false, | |
645 params, | |
646 id, | |
647 base::Bind(&VerifyListenerNotNotifiedAfterCommandRun, listener, | |
648 &run_loop_addlistener)); | |
649 | |
650 run_loop_addlistener.Run(); | |
651 | |
652 base::RunLoop run_loop_testlistener; | |
653 cmd = base::Bind( | |
654 &ExecuteTestListener); | |
655 | |
656 // |listener| has been added to the session and should be notified of | |
657 // the ExecuteTestListener command. | |
658 ExecuteSessionCommand( | |
659 &map, | |
660 "cmd", | |
661 cmd, | |
662 false, | |
663 params, | |
664 id, | |
665 base::Bind(&VerifyListenerNotifiedBeforeCommandRun, listener, | |
666 &run_loop_testlistener)); | |
667 | |
668 run_loop_testlistener.Run(); | |
669 } | |
OLD | NEW |