Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(717)

Side by Side Diff: chrome/browser/extensions/api/cast_channel/cast_channel_apitest.cc

Issue 165293002: Add an API test to exersize the open error case. This is a regression test for (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/command_line.h" 6 #include "base/command_line.h"
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "chrome/browser/extensions/api/cast_channel/cast_channel_api.h" 8 #include "chrome/browser/extensions/api/cast_channel/cast_channel_api.h"
9 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h" 9 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h"
10 #include "chrome/browser/extensions/extension_apitest.h" 10 #include "chrome/browser/extensions/extension_apitest.h"
11 #include "chrome/browser/extensions/extension_service.h" 11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/common/chrome_switches.h" 12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/extensions/api/cast_channel.h" 13 #include "chrome/common/extensions/api/cast_channel.h"
14 #include "content/public/browser/browser_thread.h" 14 #include "content/public/browser/browser_thread.h"
15 #include "net/base/capturing_net_log.h" 15 #include "net/base/capturing_net_log.h"
16 #include "net/base/completion_callback.h" 16 #include "net/base/completion_callback.h"
17 #include "net/base/net_errors.h" 17 #include "net/base/net_errors.h"
18 #include "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gmock_mutant.h"
19 20
20 namespace cast_channel = extensions::api::cast_channel; 21 namespace cast_channel = extensions::api::cast_channel;
21 using cast_channel::CastSocket; 22 using cast_channel::CastSocket;
23 using cast_channel::ChannelError;
22 using cast_channel::ChannelInfo; 24 using cast_channel::ChannelInfo;
23 using cast_channel::MessageInfo; 25 using cast_channel::MessageInfo;
24 26
27 using ::testing::_;
25 using ::testing::A; 28 using ::testing::A;
26 using ::testing::_; 29 using ::testing::DoAll;
27 using ::testing::Invoke; 30 using ::testing::Invoke;
28 using ::testing::Return; 31 using ::testing::Return;
29 32
30 namespace { 33 namespace {
31 34
32 const char kTestExtensionId[] = "ddchlicdkolnonkihahngkmmmjnjlkkf"; 35 const char kTestExtensionId[] = "ddchlicdkolnonkihahngkmmmjnjlkkf";
33 const char kTestUrl[] = "cast://192.168.1.1:8009"; 36 const char kTestUrl[] = "cast://192.168.1.1:8009";
34 37
35 static void FillMessageInfo(MessageInfo* message_info, 38 static void FillMessageInfo(MessageInfo* message_info,
36 const std::string& message) { 39 const std::string& message) {
37 message_info->namespace_ = "foo"; 40 message_info->namespace_ = "foo";
38 message_info->source_id = "src"; 41 message_info->source_id = "src";
39 message_info->destination_id = "dest"; 42 message_info->destination_id = "dest";
40 message_info->data.reset(new base::StringValue(message)); 43 message_info->data.reset(new base::StringValue(message));
41 } 44 }
42 45
43 ACTION_TEMPLATE(InvokeCompletionCallback, 46 ACTION_TEMPLATE(InvokeCompletionCallback,
44 HAS_1_TEMPLATE_PARAMS(int, k), 47 HAS_1_TEMPLATE_PARAMS(int, k),
45 AND_1_VALUE_PARAMS(result)) { 48 AND_1_VALUE_PARAMS(result)) {
46 ::std::tr1::get<k>(args).Run(result); 49 ::std::tr1::get<k>(args).Run(result);
47 } 50 }
48 51
52 ACTION_P2(InvokeDelegateOnError, api_test, api) {
53 api_test->CallOnError(api);
54 }
55
49 class MockCastSocket : public CastSocket { 56 class MockCastSocket : public CastSocket {
50 public: 57 public:
51 explicit MockCastSocket(CastSocket::Delegate* delegate, 58 explicit MockCastSocket(CastSocket::Delegate* delegate,
52 net::NetLog* net_log) 59 net::NetLog* net_log)
53 : CastSocket(kTestExtensionId, GURL(kTestUrl), delegate, net_log) {} 60 : CastSocket(kTestExtensionId, GURL(kTestUrl), delegate, net_log) {}
54 virtual ~MockCastSocket() {} 61 virtual ~MockCastSocket() {}
55 62
56 virtual bool CalledOnValidThread() const OVERRIDE { 63 virtual bool CalledOnValidThread() const OVERRIDE {
57 // Always return true in testing. 64 // Always return true in testing.
58 return true; 65 return true;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 channel_info->error_state = cast_channel::CHANNEL_ERROR_NONE; 103 channel_info->error_state = cast_channel::CHANNEL_ERROR_NONE;
97 } 104 }
98 105
99 static void FillChannelInfoForClosedState(ChannelInfo* channel_info) { 106 static void FillChannelInfoForClosedState(ChannelInfo* channel_info) {
100 channel_info->channel_id = 1; 107 channel_info->channel_id = 1;
101 channel_info->url = kTestUrl; 108 channel_info->url = kTestUrl;
102 channel_info->ready_state = cast_channel::READY_STATE_CLOSED; 109 channel_info->ready_state = cast_channel::READY_STATE_CLOSED;
103 channel_info->error_state = cast_channel::CHANNEL_ERROR_NONE; 110 channel_info->error_state = cast_channel::CHANNEL_ERROR_NONE;
104 } 111 }
105 112
113 void CallOnError(extensions::CastChannelAPI* api) {
114 api->OnError(mock_cast_socket_,
115 cast_channel::CHANNEL_ERROR_CONNECT_ERROR);
116 }
117
106 protected: 118 protected:
107 void CallOnMessage(const std::string& message) { 119 void CallOnMessage(const std::string& message) {
108 content::BrowserThread::PostTask( 120 content::BrowserThread::PostTask(
109 content::BrowserThread::IO, 121 content::BrowserThread::IO,
110 FROM_HERE, 122 FROM_HERE,
111 base::Bind(&CastChannelAPITest::DoCallOnMessage, this, 123 base::Bind(&CastChannelAPITest::DoCallOnMessage, this,
112 GetApi(), mock_cast_socket_, message)); 124 mock_cast_socket_, message));
113 } 125 }
114 126
115 void DoCallOnMessage(extensions::CastChannelAPI* api, 127 void DoCallOnMessage(MockCastSocket* cast_socket,
116 MockCastSocket* cast_socket,
117 const std::string& message) { 128 const std::string& message) {
118 MessageInfo message_info; 129 MessageInfo message_info;
119 FillMessageInfo(&message_info, message); 130 FillMessageInfo(&message_info, message);
120 api->OnMessage(cast_socket, message_info); 131 GetApi()->OnMessage(cast_socket, message_info);
121 } 132 }
122 133
123 MockCastSocket* mock_cast_socket_; 134 MockCastSocket* mock_cast_socket_;
124 ChannelInfo channel_info; 135 ChannelInfo channel_info;
125 net::CapturingNetLog capturing_net_log_; 136 net::CapturingNetLog capturing_net_log_;
126 }; 137 };
127 138
128 // TODO(munjal): Win Dbg has a workaround that makes RunExtensionSubtest 139 // TODO(munjal): Win Dbg has a workaround that makes RunExtensionSubtest
129 // always return true without actually running the test. Remove when fixed. 140 // always return true without actually running the test. Remove when fixed.
130 #if defined(OS_WIN) && !defined(NDEBUG) 141 #if defined(OS_WIN) && !defined(NDEBUG)
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 WillOnce(InvokeCompletionCallback<0>(net::OK)); 186 WillOnce(InvokeCompletionCallback<0>(net::OK));
176 187
177 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api", 188 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api",
178 "test_open_receive_close.html")); 189 "test_open_receive_close.html"));
179 190
180 ResultCatcher catcher; 191 ResultCatcher catcher;
181 CallOnMessage("some-message"); 192 CallOnMessage("some-message");
182 CallOnMessage("some-message"); 193 CallOnMessage("some-message");
183 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); 194 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
184 } 195 }
196
197 // TODO(munjal): Win Dbg has a workaround that makes RunExtensionSubtest
198 // always return true without actually running the test. Remove when fixed.
199 #if defined(OS_WIN) && !defined(NDEBUG)
200 #define MAYBE_TestOpenError DISABLED_TestOpenError
201 #else
202 #define MAYBE_TestOpenError TestOpenError
203 #endif
204 // Test the case when socket open results in an error.
205 IN_PROC_BROWSER_TEST_F(CastChannelAPITest, MAYBE_TestOpenError) {
206 SetUpMockCastSocket();
207
208 EXPECT_CALL(*mock_cast_socket_, Connect(_))
209 .WillOnce(DoAll(
Wez 2014/03/04 21:29:53 nit: Indentation
Munjal (Google) 2014/05/21 22:41:06 It is indented 4 spaces from .WillOnce. That is no
Wez 2014/05/21 22:57:34 Indentation of .WillOnce is six from EXPECT_CALL,
210 InvokeDelegateOnError(this, GetApi()),
211 InvokeCompletionCallback<0>(net::ERR_FAILED)));
212 EXPECT_CALL(*mock_cast_socket_, FillChannelInfo(_))
213 .WillOnce(Invoke(FillChannelInfoForClosedState))
214 .WillOnce(Invoke(FillChannelInfoForClosedState));
215 EXPECT_CALL(*mock_cast_socket_, Close(_));
216
217 EXPECT_TRUE(RunExtensionSubtest("cast_channel/api",
218 "test_open_error.html"));
219
220 ResultCatcher catcher;
221 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
222 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698