Chromium Code Reviews| Index: chrome/test/chromedriver/commands_unittest.cc |
| diff --git a/chrome/test/chromedriver/commands_unittest.cc b/chrome/test/chromedriver/commands_unittest.cc |
| index 44571863054bbd1a38e1ca25808019295033843a..bf219118e9cb69b92d2ca8fe57a8a26c76c78a6a 100644 |
| --- a/chrome/test/chromedriver/commands_unittest.cc |
| +++ b/chrome/test/chromedriver/commands_unittest.cc |
| @@ -546,3 +546,124 @@ TEST(CommandsTest, ErrorFindChildElement) { |
| ExecuteFindChildElements( |
| 1, &session, &web_view, element_id, params, &result).code()); |
| } |
| + |
| +namespace { |
| + |
| +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
|
| + public: |
| + MockCommandListener() : called_(false) {} |
| + virtual ~MockCommandListener() {} |
| + |
| + virtual Status BeforeCommand(const std::string& command_name) OVERRIDE { |
| + called_ = true; |
| + EXPECT_STREQ("cmd", command_name.c_str()); |
| + return Status(kOk); |
| + } |
| + |
| + void VerifyCalled() { |
| + EXPECT_TRUE(called_); |
| + } |
| + |
| + void VerifyNotCalled() { |
| + EXPECT_FALSE(called_); |
| + } |
| + |
| + private: |
| + bool called_; |
| +}; |
| + |
| +Status ExecuteAddListenerToSession( |
| + MockCommandListener* listener, |
| + Session* session, |
| + const base::DictionaryValue& params, |
| + scoped_ptr<base::Value>* return_value) { |
| + session->AddListener(listener); |
| + listener->VerifyNotCalled(); |
| + return Status(kOk); |
| +} |
| + |
| +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
|
| + Session* session, |
| + const base::DictionaryValue& params, |
| + scoped_ptr<base::Value>* return_value) { |
| + session->quit = true; |
| + return Status(kOk); |
| +} |
| + |
| +void VerifyListenerNotNotifiedAfterCommandRun( |
| + MockCommandListener* listener, |
| + base::RunLoop* run_loop, |
| + const Status& status, |
| + scoped_ptr<base::Value> value, |
| + 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.
|
| + ASSERT_EQ(kOk, status.code()); |
| + listener->VerifyNotCalled(); |
| + run_loop->Quit(); |
| +} |
| + |
| + |
| +void VerifyListenerNotifiedBeforeCommandRun( |
| + MockCommandListener* listener, |
| + base::RunLoop* run_loop, |
| + const Status& status, |
| + scoped_ptr<base::Value> value, |
| + const std::string& session_id) { |
| + ASSERT_EQ(kOk, status.code()); |
| + listener->VerifyCalled(); |
| + run_loop->Quit(); |
| +} |
| + |
| +} // namespace |
| + |
| +TEST(CommandsTest, SessionNotifiedOfCommand) { |
| + SessionThreadMap map; |
| + linked_ptr<base::Thread> thread(new base::Thread("1")); |
| + ASSERT_TRUE(thread->Start()); |
| + std::string id("id"); |
| + thread->message_loop()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&internal::CreateSessionOnSessionThreadForTesting, id)); |
| + map[id] = thread; |
| + |
| + base::DictionaryValue params; |
| + // |listener| is owned by and will be destroyed by |session| |
| + 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
|
| + SessionCommand cmd = base::Bind( |
| + &ExecuteAddListenerToSession, listener); |
| + |
| + base::MessageLoop loop; |
| + base::RunLoop run_loop_addlistener; |
| + |
| + // |CommandListener|s are notified immediately before commands are run. |
| + // Here, the command adds |listener| to the session, so |listener| |
| + // should not be notified since it will not have been added yet. |
| + ExecuteSessionCommand( |
| + &map, |
| + "cmd", |
| + cmd, |
| + false, |
| + params, |
| + id, |
| + base::Bind(&VerifyListenerNotNotifiedAfterCommandRun, listener, |
| + &run_loop_addlistener)); |
| + |
| + run_loop_addlistener.Run(); |
| + |
| + base::RunLoop run_loop_testlistener; |
| + cmd = base::Bind( |
| + &ExecuteTestListener); |
| + |
| + // |listener| has been added to the session and should be notified of |
| + // the ExecuteTestListener command. |
| + ExecuteSessionCommand( |
| + &map, |
| + "cmd", |
| + cmd, |
| + false, |
| + params, |
| + id, |
| + base::Bind(&VerifyListenerNotifiedBeforeCommandRun, listener, |
| + &run_loop_testlistener)); |
| + |
| + run_loop_testlistener.Run(); |
| +} |