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

Side by Side Diff: content/browser/screen_orientation/screen_orientation_dispatcher_host_unittest.cc

Issue 327573002: Properly route screen orientation IPC messages. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@screen_lock_view
Patch Set: ScreenOrientationDispatcherHost owned by WebContentsImpl Created 6 years, 6 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "content/browser/screen_orientation/screen_orientation_dispatcher_host. h"
6
5 #include "base/logging.h" 7 #include "base/logging.h"
6 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
7 #include "content/browser/screen_orientation/screen_orientation_dispatcher_host. h"
8 #include "content/browser/screen_orientation/screen_orientation_provider.h" 9 #include "content/browser/screen_orientation/screen_orientation_provider.h"
9 #include "content/common/screen_orientation_messages.h" 10 #include "content/common/screen_orientation_messages.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/test/mock_render_process_host.h"
12 #include "content/public/test/test_browser_context.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "content/public/test/test_utils.h" 11 #include "content/public/test/test_utils.h"
15 #include "ipc/ipc_test_sink.h" 12 #include "ipc/ipc_test_sink.h"
16 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
17 14
18 namespace content { 15 namespace content {
19 16
20 class MockScreenOrientationProvider : public ScreenOrientationProvider { 17 class MockScreenOrientationProvider : public ScreenOrientationProvider {
21 public: 18 public:
22 MockScreenOrientationProvider() 19 MockScreenOrientationProvider()
23 : orientation_(blink::WebScreenOrientationLockPortraitPrimary), 20 : orientation_(blink::WebScreenOrientationLockPortraitPrimary),
(...skipping 22 matching lines...) Expand all
46 blink::WebScreenOrientationLockType orientation_; 43 blink::WebScreenOrientationLockType orientation_;
47 bool unlock_called_; 44 bool unlock_called_;
48 45
49 DISALLOW_COPY_AND_ASSIGN(MockScreenOrientationProvider); 46 DISALLOW_COPY_AND_ASSIGN(MockScreenOrientationProvider);
50 }; 47 };
51 48
52 class ScreenOrientationDispatcherHostWithSink FINAL : 49 class ScreenOrientationDispatcherHostWithSink FINAL :
53 public ScreenOrientationDispatcherHost { 50 public ScreenOrientationDispatcherHost {
54 public: 51 public:
55 explicit ScreenOrientationDispatcherHostWithSink(IPC::TestSink* sink) 52 explicit ScreenOrientationDispatcherHostWithSink(IPC::TestSink* sink)
56 : ScreenOrientationDispatcherHost() , sink_(sink) {} 53 : ScreenOrientationDispatcherHost(NULL), sink_(sink) {}
57 54
58 virtual bool Send(IPC::Message* message) OVERRIDE { 55 virtual bool Send(IPC::Message* message) OVERRIDE {
59 return sink_->Send(message); 56 return sink_->Send(message);
60 } 57 }
61 58
62 private: 59 private:
63 virtual ~ScreenOrientationDispatcherHostWithSink() { } 60 virtual ~ScreenOrientationDispatcherHostWithSink() { }
64 61
65 IPC::TestSink* sink_; 62 IPC::TestSink* sink_;
66 }; 63 };
67 64
68 class ScreenOrientationDispatcherHostTest : public testing::Test { 65 class ScreenOrientationDispatcherHostTest : public testing::Test {
69 protected: 66 protected:
70 virtual ScreenOrientationDispatcherHost* CreateDispatcher() { 67 virtual ScreenOrientationDispatcherHost* CreateDispatcher() {
71 return new ScreenOrientationDispatcherHost(); 68 return new ScreenOrientationDispatcherHost(NULL);
72 } 69 }
73 70
74 virtual void SetUp() OVERRIDE { 71 virtual void SetUp() OVERRIDE {
75 provider_ = new MockScreenOrientationProvider(); 72 provider_ = new MockScreenOrientationProvider();
76 73
77 dispatcher_ = CreateDispatcher(); 74 dispatcher_.reset(CreateDispatcher());
78 dispatcher_->SetProviderForTests(provider_); 75 dispatcher_->SetProviderForTests(provider_);
79 } 76 }
80 77
78 int routing_id() const {
79 // We return a fake routing_id() in the context of this test.
80 return 0;
81 }
82
81 // The dispatcher_ owns the provider_ but we still want to access it. 83 // The dispatcher_ owns the provider_ but we still want to access it.
82 MockScreenOrientationProvider* provider_; 84 MockScreenOrientationProvider* provider_;
83 scoped_refptr<ScreenOrientationDispatcherHost> dispatcher_; 85 scoped_ptr<ScreenOrientationDispatcherHost> dispatcher_;
84 }; 86 };
85 87
86 class ScreenOrientationDispatcherHostWithSinkTest : 88 class ScreenOrientationDispatcherHostWithSinkTest :
87 public ScreenOrientationDispatcherHostTest { 89 public ScreenOrientationDispatcherHostTest {
88 protected: 90 protected:
89 virtual ScreenOrientationDispatcherHost* CreateDispatcher() OVERRIDE { 91 virtual ScreenOrientationDispatcherHost* CreateDispatcher() OVERRIDE {
90 return new ScreenOrientationDispatcherHostWithSink(&sink_); 92 return new ScreenOrientationDispatcherHostWithSink(&sink_);
91 } 93 }
92 94
93 const IPC::TestSink& sink() const { 95 const IPC::TestSink& sink() const {
(...skipping 22 matching lines...) Expand all
116 // Unfortunately, initializer list constructor for std::list is not yet 118 // Unfortunately, initializer list constructor for std::list is not yet
117 // something we can use. 119 // something we can use.
118 // Keep this in sync with |orientationsToTest|. 120 // Keep this in sync with |orientationsToTest|.
119 int orientationsToTestCount = 7; 121 int orientationsToTestCount = 7;
120 122
121 for (int i = 0; i < orientationsToTestCount; ++i) { 123 for (int i = 0; i < orientationsToTestCount; ++i) {
122 bool message_was_handled = false; 124 bool message_was_handled = false;
123 blink::WebScreenOrientationLockType orientation = orientationsToTest[i]; 125 blink::WebScreenOrientationLockType orientation = orientationsToTest[i];
124 126
125 message_was_handled = dispatcher_->OnMessageReceived( 127 message_was_handled = dispatcher_->OnMessageReceived(
126 ScreenOrientationHostMsg_LockRequest(orientation, 0)); 128 ScreenOrientationHostMsg_LockRequest(routing_id(), orientation, 0));
127 129
128 EXPECT_TRUE(message_was_handled); 130 EXPECT_TRUE(message_was_handled);
129 EXPECT_EQ(orientation, provider_->orientation()); 131 EXPECT_EQ(orientation, provider_->orientation());
130 } 132 }
131 } 133 }
132 134
133 // Test that when receiving an unlock message, it is correctly dispatched to the 135 // Test that when receiving an unlock message, it is correctly dispatched to the
134 // ScreenOrientationProvider. 136 // ScreenOrientationProvider.
135 TEST_F(ScreenOrientationDispatcherHostTest, ProviderUnlock) { 137 TEST_F(ScreenOrientationDispatcherHostTest, ProviderUnlock) {
136 bool message_was_handled = dispatcher_->OnMessageReceived( 138 bool message_was_handled = dispatcher_->OnMessageReceived(
137 ScreenOrientationHostMsg_Unlock()); 139 ScreenOrientationHostMsg_Unlock(routing_id()));
138 140
139 EXPECT_TRUE(message_was_handled); 141 EXPECT_TRUE(message_was_handled);
140 EXPECT_TRUE(provider_->unlock_called()); 142 EXPECT_TRUE(provider_->unlock_called());
141 } 143 }
142 144
143 // Test that when there is no provider, a LockRequest fails with the appropriate 145 // Test that when there is no provider, a LockRequest fails with the appropriate
144 // ErrorType. 146 // ErrorType.
145 TEST_F(ScreenOrientationDispatcherHostWithSinkTest, NoProvider_LockError) { 147 TEST_F(ScreenOrientationDispatcherHostWithSinkTest, NoProvider_LockError) {
146 dispatcher_->SetProviderForTests(NULL); 148 dispatcher_->SetProviderForTests(NULL);
147 149
148 const int request_id = 3; 150 const int request_id = 3;
149 dispatcher_->OnMessageReceived(ScreenOrientationHostMsg_LockRequest( 151 dispatcher_->OnMessageReceived(ScreenOrientationHostMsg_LockRequest(
150 blink::WebScreenOrientationLockPortraitPrimary, request_id)); 152 routing_id(),
153 blink::WebScreenOrientationLockPortraitPrimary,
154 request_id));
151 155
152 EXPECT_EQ(1u, sink().message_count()); 156 EXPECT_EQ(1u, sink().message_count());
153 157
154 const IPC::Message* msg = sink().GetFirstMessageMatching( 158 const IPC::Message* msg = sink().GetFirstMessageMatching(
155 ScreenOrientationMsg_LockError::ID); 159 ScreenOrientationMsg_LockError::ID);
156 EXPECT_TRUE(msg != NULL); 160 EXPECT_TRUE(msg != NULL);
157 161
158 Tuple2<int, blink::WebLockOrientationCallback::ErrorType> params; 162 Tuple2<int, blink::WebLockOrientationCallback::ErrorType> params;
159 ScreenOrientationMsg_LockError::Read(msg, &params); 163 ScreenOrientationMsg_LockError::Read(msg, &params);
160 EXPECT_EQ(request_id, params.a); 164 EXPECT_EQ(request_id, params.a);
161 EXPECT_EQ(blink::WebLockOrientationCallback::ErrorTypeNotAvailable, params.b); 165 EXPECT_EQ(blink::WebLockOrientationCallback::ErrorTypeNotAvailable, params.b);
162 } 166 }
163 167
164 // Test that when there is a provider, we always send a success response back to 168 // Test that when there is a provider, we always send a success response back to
165 // the renderer. 169 // the renderer.
166 // TODO(mlamouri): we currently do not test the content of the message because 170 // TODO(mlamouri): we currently do not test the content of the message because
167 // it currently contains dummy values. 171 // it currently contains dummy values.
168 TEST_F(ScreenOrientationDispatcherHostWithSinkTest, WithProvider_LockSuccess) { 172 TEST_F(ScreenOrientationDispatcherHostWithSinkTest, WithProvider_LockSuccess) {
169 const int request_id = 42; 173 const int request_id = 42;
170 dispatcher_->OnMessageReceived(ScreenOrientationHostMsg_LockRequest( 174 dispatcher_->OnMessageReceived(ScreenOrientationHostMsg_LockRequest(
171 blink::WebScreenOrientationLockPortraitPrimary, request_id)); 175 routing_id(),
176 blink::WebScreenOrientationLockPortraitPrimary,
177 request_id));
172 178
173 EXPECT_EQ(1u, sink().message_count()); 179 EXPECT_EQ(1u, sink().message_count());
174 180
175 const IPC::Message* msg = sink().GetFirstMessageMatching( 181 const IPC::Message* msg = sink().GetFirstMessageMatching(
176 ScreenOrientationMsg_LockSuccess::ID); 182 ScreenOrientationMsg_LockSuccess::ID);
177 EXPECT_TRUE(msg != NULL); 183 EXPECT_TRUE(msg != NULL);
178 184
179 Tuple3<int, unsigned, blink::WebScreenOrientationType> params; 185 Tuple3<int, unsigned, blink::WebScreenOrientationType> params;
180 ScreenOrientationMsg_LockSuccess::Read(msg, &params); 186 ScreenOrientationMsg_LockSuccess::Read(msg, &params);
181 EXPECT_EQ(request_id, params.a); 187 EXPECT_EQ(request_id, params.a);
182 } 188 }
183 189
184 } // namespace content 190 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698