| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "ceee/ie/broker/broker_rpc_client.h" | 5 #include "ceee/ie/broker/broker_rpc_client.h" |
| 6 #include "ceee/ie/broker/broker_rpc_server.h" | 6 #include "ceee/ie/broker/broker_rpc_server.h" |
| 7 | 7 |
| 8 #include <atlbase.h> | 8 #include <atlbase.h> |
| 9 #include "broker_rpc_lib.h" // NOLINT | 9 #include "broker_rpc_lib.h" // NOLINT |
| 10 #include "ceee/ie/broker/broker_rpc_utils.h" | 10 #include "ceee/ie/broker/broker_rpc_utils.h" |
| 11 #include "ceee/ie/common/ceee_module_util.h" | 11 #include "ceee/ie/common/ceee_module_util.h" |
| 12 #include "ceee/testing/utils/mock_static.h" | 12 #include "ceee/testing/utils/mock_static.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 using testing::_; | 16 using testing::_; |
| 17 using testing::StrEq; | 17 using testing::StrEq; |
| 18 using testing::StrictMock; | 18 using testing::StrictMock; |
| 19 using testing::Return; | 19 using testing::Return; |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 using testing::_; | 23 using testing::_; |
| 24 | 24 |
| 25 MOCK_STATIC_CLASS_BEGIN(BrokerRpcMock) | 25 MOCK_STATIC_CLASS_BEGIN(BrokerRpcMock) |
| 26 MOCK_STATIC_INIT_BEGIN(BrokerRpcMock) | 26 MOCK_STATIC_INIT_BEGIN(BrokerRpcMock) |
| 27 MOCK_STATIC_INIT(GetRpcEndPointAddress); | 27 MOCK_STATIC_INIT(GetRpcEndpointAddress); |
| 28 MOCK_STATIC_INIT(BrokerRpcServer_FireEvent); | 28 MOCK_STATIC_INIT(BrokerRpcServer_FireEvent); |
| 29 MOCK_STATIC_INIT(BrokerRpcServer_SendUmaHistogramTimes); | 29 MOCK_STATIC_INIT(BrokerRpcServer_SendUmaHistogramTimes); |
| 30 MOCK_STATIC_INIT(BrokerRpcServer_SendUmaHistogramData); | 30 MOCK_STATIC_INIT(BrokerRpcServer_SendUmaHistogramData); |
| 31 MOCK_STATIC_INIT_END() | 31 MOCK_STATIC_INIT_END() |
| 32 MOCK_STATIC0(std::wstring, , GetRpcEndPointAddress); | 32 MOCK_STATIC0(std::wstring, , GetRpcEndpointAddress); |
| 33 MOCK_STATIC3(void, , BrokerRpcServer_FireEvent, handle_t, const char*, | 33 MOCK_STATIC3(void, , BrokerRpcServer_FireEvent, handle_t, const char*, |
| 34 const char*); | 34 const char*); |
| 35 MOCK_STATIC3(void, , BrokerRpcServer_SendUmaHistogramTimes, handle_t, | 35 MOCK_STATIC3(void, , BrokerRpcServer_SendUmaHistogramTimes, handle_t, |
| 36 const char*, int); | 36 const char*, int); |
| 37 MOCK_STATIC6(void, , BrokerRpcServer_SendUmaHistogramData, handle_t, | 37 MOCK_STATIC6(void, , BrokerRpcServer_SendUmaHistogramData, handle_t, |
| 38 const char*, int, int, int, int); | 38 const char*, int, int, int, int); |
| 39 MOCK_STATIC_CLASS_END(BrokerRpcMock) | 39 MOCK_STATIC_CLASS_END(BrokerRpcMock) |
| 40 | 40 |
| 41 class BrokerRpcTest : public testing::Test { | 41 class BrokerRpcTest : public testing::Test { |
| 42 protected: | 42 protected: |
| 43 virtual void SetUp() { | 43 virtual void SetUp() { |
| 44 EXPECT_CALL(broker_rpc_mock_, GetRpcEndPointAddress()) | 44 EXPECT_CALL(broker_rpc_mock_, GetRpcEndpointAddress()) |
| 45 .WillRepeatedly(Return(L"BrokerRpcTestEP")); | 45 .WillRepeatedly(Return(L"BrokerRpcTestEP")); |
| 46 } | 46 } |
| 47 | 47 |
| 48 virtual void TearDown() { | 48 virtual void TearDown() { |
| 49 } | 49 } |
| 50 | 50 |
| 51 BrokerRpcMock broker_rpc_mock_; | 51 BrokerRpcMock broker_rpc_mock_; |
| 52 }; | 52 }; |
| 53 | 53 |
| 54 TEST_F(BrokerRpcTest, ConnectNoServer) { | 54 TEST_F(BrokerRpcTest, ConnectNoServer) { |
| 55 BrokerRpcClient client; | 55 BrokerRpcClient client(false); |
| 56 ASSERT_FALSE(client.is_connected()); | 56 ASSERT_FALSE(client.is_connected()); |
| 57 ASSERT_HRESULT_FAILED(client.Connect(false)); | 57 ASSERT_HRESULT_FAILED(client.Connect(false)); |
| 58 ASSERT_FALSE(client.is_connected()); | 58 ASSERT_FALSE(client.is_connected()); |
| 59 } | 59 } |
| 60 | 60 |
| 61 TEST_F(BrokerRpcTest, Connect) { | 61 TEST_F(BrokerRpcTest, Connect) { |
| 62 BrokerRpcServer server; | 62 BrokerRpcServer server; |
| 63 ASSERT_FALSE(server.is_started()); | 63 ASSERT_FALSE(server.is_started()); |
| 64 ASSERT_TRUE(server.Start()); | 64 ASSERT_TRUE(server.Start()); |
| 65 ASSERT_TRUE(server.is_started()); | 65 ASSERT_TRUE(server.is_started()); |
| 66 BrokerRpcClient client; | 66 BrokerRpcClient client(false); |
| 67 ASSERT_HRESULT_SUCCEEDED(client.Connect(false)); | 67 ASSERT_HRESULT_SUCCEEDED(client.Connect(false)); |
| 68 ASSERT_TRUE(client.is_connected()); | 68 ASSERT_TRUE(client.is_connected()); |
| 69 } | 69 } |
| 70 | 70 |
| 71 TEST_F(BrokerRpcTest, RpcCalls) { | 71 TEST_F(BrokerRpcTest, RpcCalls) { |
| 72 BrokerRpcServer server; | 72 BrokerRpcServer server; |
| 73 ASSERT_TRUE(server.Start()); | 73 ASSERT_TRUE(server.Start()); |
| 74 | 74 |
| 75 BrokerRpcClient client; | 75 BrokerRpcClient client(false); |
| 76 ASSERT_HRESULT_SUCCEEDED(client.Connect(false)); | 76 ASSERT_HRESULT_SUCCEEDED(client.Connect(false)); |
| 77 | 77 |
| 78 const char* name = "name"; | 78 const char* name = "name"; |
| 79 const char* args = "args"; | 79 const char* args = "args"; |
| 80 | 80 |
| 81 EXPECT_CALL(broker_rpc_mock_, | 81 EXPECT_CALL(broker_rpc_mock_, |
| 82 BrokerRpcServer_FireEvent(_, StrEq(name), StrEq(args))) | 82 BrokerRpcServer_FireEvent(_, StrEq(name), StrEq(args))) |
| 83 .Times(1); | 83 .Times(1); |
| 84 | 84 |
| 85 ASSERT_HRESULT_SUCCEEDED(client.FireEvent(name, args)); | 85 ASSERT_HRESULT_SUCCEEDED(client.FireEvent(name, args)); |
| 86 | 86 |
| 87 EXPECT_CALL(broker_rpc_mock_, | 87 EXPECT_CALL(broker_rpc_mock_, |
| 88 BrokerRpcServer_SendUmaHistogramTimes(_, StrEq(name), 321)) | 88 BrokerRpcServer_SendUmaHistogramTimes(_, StrEq(name), 321)) |
| 89 .Times(1); | 89 .Times(1); |
| 90 ASSERT_HRESULT_SUCCEEDED(client.SendUmaHistogramTimes(name, 321)); | 90 ASSERT_HRESULT_SUCCEEDED(client.SendUmaHistogramTimes(name, 321)); |
| 91 | 91 |
| 92 EXPECT_CALL(broker_rpc_mock_, | 92 EXPECT_CALL(broker_rpc_mock_, |
| 93 BrokerRpcServer_SendUmaHistogramData(_, StrEq(name), 1, 2, 3, 4)) | 93 BrokerRpcServer_SendUmaHistogramData(_, StrEq(name), 1, 2, 3, 4)) |
| 94 .Times(1); | 94 .Times(1); |
| 95 ASSERT_HRESULT_SUCCEEDED(client.SendUmaHistogramData(name, 1, 2, 3, 4)); | 95 ASSERT_HRESULT_SUCCEEDED(client.SendUmaHistogramData(name, 1, 2, 3, 4)); |
| 96 } | 96 } |
| 97 | 97 |
| 98 class BrokerRpcClientMock : public BrokerRpcClient { |
| 99 public: |
| 100 explicit BrokerRpcClientMock(bool allow_restarts) |
| 101 : BrokerRpcClient(allow_restarts) {} |
| 102 MOCK_METHOD0(StartServerMock, HRESULT()); |
| 103 protected: |
| 104 HRESULT StartServer(IUnknown** server) { |
| 105 server_.Start(); |
| 106 return StartServerMock(); |
| 107 } |
| 108 |
| 109 public: |
| 110 BrokerRpcServer server_; |
| 111 }; |
| 112 |
| 113 TEST_F(BrokerRpcTest, StartServer) { |
| 114 BrokerRpcClientMock client(true); |
| 115 EXPECT_CALL(client, StartServerMock()) |
| 116 .Times(0); |
| 117 ASSERT_HRESULT_FAILED(client.Connect(false)); |
| 118 |
| 119 EXPECT_CALL(client, StartServerMock()) |
| 120 .Times(1) |
| 121 .WillOnce(Return(RPC_E_FAULT)); |
| 122 ASSERT_HRESULT_FAILED(client.Connect(true)); |
| 123 |
| 124 EXPECT_CALL(client, StartServerMock()) |
| 125 .Times(1) |
| 126 .WillOnce(Return(S_OK)); |
| 127 ASSERT_HRESULT_SUCCEEDED(client.Connect(true)); |
| 128 } |
| 129 |
| 130 TEST_F(BrokerRpcTest, NoRestartServer) { |
| 131 BrokerRpcClientMock client(false); |
| 132 EXPECT_CALL(client, StartServerMock()) |
| 133 .Times(1) |
| 134 .WillOnce(Return(S_OK)); |
| 135 ASSERT_HRESULT_SUCCEEDED(client.Connect(true)); |
| 136 client.server_.Stop(); |
| 137 |
| 138 EXPECT_CALL(client, StartServerMock()) |
| 139 .Times(0); |
| 140 EXPECT_CALL(broker_rpc_mock_, |
| 141 BrokerRpcServer_FireEvent(_, _, _)) |
| 142 .Times(0); |
| 143 ASSERT_HRESULT_FAILED(client.FireEvent("A", "B")); |
| 144 } |
| 145 |
| 146 TEST_F(BrokerRpcTest, RestartServer) { |
| 147 BrokerRpcClientMock client(true); |
| 148 EXPECT_CALL(client, StartServerMock()) |
| 149 .Times(1) |
| 150 .WillOnce(Return(S_OK)); |
| 151 ASSERT_HRESULT_SUCCEEDED(client.Connect(true)); |
| 152 client.server_.Stop(); |
| 153 |
| 154 EXPECT_CALL(client, StartServerMock()) |
| 155 .Times(1) |
| 156 .WillOnce(Return(S_OK)); |
| 157 EXPECT_CALL(broker_rpc_mock_, |
| 158 BrokerRpcServer_FireEvent(_, _, _)) |
| 159 .Times(1); |
| 160 ASSERT_HRESULT_SUCCEEDED(client.FireEvent("A", "B")); |
| 161 } |
| 162 |
| 98 } // namespace | 163 } // namespace |
| OLD | NEW |