Peter Qiu | c0beca5 | 2015-09-03 11:25:46 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 16 | |
| 17 | #include "shill/rpc_task.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 21 | #include "shill/mock_adaptors.h" |
Alex Vakulenko | a41ab51 | 2014-07-23 14:24:23 -0700 | [diff] [blame] | 22 | #include "shill/nice_mock_control.h" |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 23 | |
| 24 | using std::map; |
| 25 | using std::string; |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 26 | using std::vector; |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 27 | |
| 28 | namespace shill { |
| 29 | |
| 30 | class RPCTaskTest : public testing::Test, |
| 31 | public RPCTaskDelegate { |
| 32 | public: |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 33 | RPCTaskTest() |
| 34 | : get_login_calls_(0), |
| 35 | notify_calls_(0), |
| 36 | task_(&control_, this) {} |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 37 | |
| 38 | // Inherited from RPCTaskDelegate. |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 39 | virtual void GetLogin(string* user, string* password); |
| 40 | virtual void Notify(const string& reason, const map<string, string>& dict); |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 41 | |
| 42 | protected: |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 43 | int get_login_calls_; |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 44 | int notify_calls_; |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 45 | string* last_user_; |
| 46 | string* last_password_; |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 47 | string last_notify_reason_; |
| 48 | map<string, string> last_notify_dict_; |
| 49 | NiceMockControl control_; |
| 50 | RPCTask task_; |
| 51 | }; |
| 52 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 53 | void RPCTaskTest::GetLogin(string* user, string* password) { |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 54 | get_login_calls_++; |
| 55 | last_user_ = user; |
| 56 | last_password_ = password; |
| 57 | } |
| 58 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 59 | void RPCTaskTest::Notify(const string& reason, |
| 60 | const map<string, string>& dict) { |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 61 | notify_calls_++; |
| 62 | last_notify_reason_ = reason; |
| 63 | last_notify_dict_ = dict; |
| 64 | } |
| 65 | |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 66 | TEST_F(RPCTaskTest, GetEnvironment) { |
Peter Qiu | a24480a | 2015-08-11 23:09:54 -0700 | [diff] [blame] | 67 | map<string, string> env = task_.GetEnvironment(); |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 68 | ASSERT_EQ(2, env.size()); |
Peter Qiu | a24480a | 2015-08-11 23:09:54 -0700 | [diff] [blame] | 69 | EXPECT_EQ(env[kRPCTaskServiceVariable], RPCTaskMockAdaptor::kRpcConnId); |
| 70 | EXPECT_EQ(env[kRPCTaskPathVariable], RPCTaskMockAdaptor::kRpcId); |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 73 | TEST_F(RPCTaskTest, GetRpcIdentifiers) { |
| 74 | EXPECT_EQ(RPCTaskMockAdaptor::kRpcId, task_.GetRpcIdentifier()); |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 75 | EXPECT_EQ(RPCTaskMockAdaptor::kRpcConnId, task_.GetRpcConnectionIdentifier()); |
| 76 | } |
| 77 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 78 | TEST_F(RPCTaskTest, GetLogin) { |
| 79 | string user, password; |
| 80 | task_.GetLogin(&user, &password); |
| 81 | EXPECT_EQ(1, get_login_calls_); |
| 82 | EXPECT_EQ(&user, last_user_); |
| 83 | EXPECT_EQ(&password, last_password_); |
| 84 | } |
| 85 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 86 | TEST_F(RPCTaskTest, Notify) { |
| 87 | static const char kReason[] = "up"; |
| 88 | map<string, string> dict; |
| 89 | dict["foo"] = "bar"; |
| 90 | task_.Notify(kReason, dict); |
| 91 | EXPECT_EQ(1, notify_calls_); |
| 92 | EXPECT_EQ(kReason, last_notify_reason_); |
| 93 | EXPECT_EQ("bar", last_notify_dict_["foo"]); |
| 94 | } |
| 95 | |
| 96 | } // namespace shill |