Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/rpc_task.h" |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | #include "shill/nice_mock_control.h" |
| 10 | #include "shill/mock_adaptors.h" |
| 11 | |
| 12 | using std::map; |
| 13 | using std::string; |
| 14 | |
| 15 | namespace shill { |
| 16 | |
| 17 | class RPCTaskTest : public testing::Test, |
| 18 | public RPCTaskDelegate { |
| 19 | public: |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 20 | RPCTaskTest() |
| 21 | : get_login_calls_(0), |
| 22 | notify_calls_(0), |
| 23 | task_(&control_, this) {} |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 24 | |
| 25 | // Inherited from RPCTaskDelegate. |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 26 | virtual void GetLogin(string *user, string *password); |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 27 | virtual void Notify(const string &reason, const map<string, string> &dict); |
| 28 | |
| 29 | protected: |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 30 | int get_login_calls_; |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 31 | int notify_calls_; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 32 | string *last_user_; |
| 33 | string *last_password_; |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 34 | string last_notify_reason_; |
| 35 | map<string, string> last_notify_dict_; |
| 36 | NiceMockControl control_; |
| 37 | RPCTask task_; |
| 38 | }; |
| 39 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 40 | void RPCTaskTest::GetLogin(string *user, string *password) { |
| 41 | get_login_calls_++; |
| 42 | last_user_ = user; |
| 43 | last_password_ = password; |
| 44 | } |
| 45 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 46 | void RPCTaskTest::Notify(const string &reason, |
| 47 | const map<string, string> &dict) { |
| 48 | |
| 49 | notify_calls_++; |
| 50 | last_notify_reason_ = reason; |
| 51 | last_notify_dict_ = dict; |
| 52 | } |
| 53 | |
| 54 | TEST_F(RPCTaskTest, GetRpcIdentifiers) { |
| 55 | EXPECT_EQ(RPCTaskMockAdaptor::kRpcId, task_.GetRpcIdentifier()); |
| 56 | EXPECT_EQ(RPCTaskMockAdaptor::kRpcInterfaceId, |
| 57 | task_.GetRpcInterfaceIdentifier()); |
| 58 | EXPECT_EQ(RPCTaskMockAdaptor::kRpcConnId, task_.GetRpcConnectionIdentifier()); |
| 59 | } |
| 60 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 61 | TEST_F(RPCTaskTest, GetLogin) { |
| 62 | string user, password; |
| 63 | task_.GetLogin(&user, &password); |
| 64 | EXPECT_EQ(1, get_login_calls_); |
| 65 | EXPECT_EQ(&user, last_user_); |
| 66 | EXPECT_EQ(&password, last_password_); |
| 67 | } |
| 68 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 69 | TEST_F(RPCTaskTest, Notify) { |
| 70 | static const char kReason[] = "up"; |
| 71 | map<string, string> dict; |
| 72 | dict["foo"] = "bar"; |
| 73 | task_.Notify(kReason, dict); |
| 74 | EXPECT_EQ(1, notify_calls_); |
| 75 | EXPECT_EQ(kReason, last_notify_reason_); |
| 76 | EXPECT_EQ("bar", last_notify_dict_["foo"]); |
| 77 | } |
| 78 | |
| 79 | } // namespace shill |