OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/logging.h" | |
6 #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/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" | |
15 #include "ipc/ipc_test_sink.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 namespace content { | |
19 | |
20 class MockScreenOrientationProvider : public ScreenOrientationProvider { | |
21 public: | |
22 MockScreenOrientationProvider() | |
23 : orientation_(blink::WebScreenOrientationLockPortraitPrimary), | |
24 unlock_called_(false) {} | |
25 | |
26 virtual void LockOrientation(blink::WebScreenOrientationLockType orientation) | |
27 OVERRIDE { | |
28 orientation_ = orientation; | |
29 } | |
30 | |
31 virtual void UnlockOrientation() OVERRIDE { | |
32 unlock_called_ = true; | |
33 } | |
34 | |
35 blink::WebScreenOrientationLockType orientation() const { | |
36 return orientation_; | |
37 } | |
38 | |
39 bool unlock_called() const { | |
40 return unlock_called_; | |
41 } | |
42 | |
43 virtual ~MockScreenOrientationProvider() {} | |
44 | |
45 private: | |
46 blink::WebScreenOrientationLockType orientation_; | |
47 bool unlock_called_; | |
48 | |
49 DISALLOW_COPY_AND_ASSIGN(MockScreenOrientationProvider); | |
50 }; | |
51 | |
52 class ScreenOrientationDispatcherHostWithSink FINAL : | |
53 public ScreenOrientationDispatcherHost { | |
54 public: | |
55 explicit ScreenOrientationDispatcherHostWithSink(IPC::TestSink* sink) | |
56 : ScreenOrientationDispatcherHost() , sink_(sink) {} | |
57 | |
58 virtual bool Send(IPC::Message* message) OVERRIDE { | |
59 return sink_->Send(message); | |
60 } | |
61 | |
62 private: | |
63 virtual ~ScreenOrientationDispatcherHostWithSink() { } | |
64 | |
65 IPC::TestSink* sink_; | |
66 }; | |
67 | |
68 class ScreenOrientationDispatcherHostTest : public testing::Test { | |
69 protected: | |
70 virtual ScreenOrientationDispatcherHost* CreateDispatcher() { | |
71 return new ScreenOrientationDispatcherHost(); | |
72 } | |
73 | |
74 virtual void SetUp() OVERRIDE { | |
75 provider_ = new MockScreenOrientationProvider(); | |
76 | |
77 dispatcher_ = CreateDispatcher(); | |
78 dispatcher_->SetProviderForTests(provider_); | |
79 } | |
80 | |
81 // The dispatcher_ owns the provider_ but we still want to access it. | |
82 MockScreenOrientationProvider* provider_; | |
83 scoped_refptr<ScreenOrientationDispatcherHost> dispatcher_; | |
84 }; | |
85 | |
86 class ScreenOrientationDispatcherHostWithSinkTest : | |
87 public ScreenOrientationDispatcherHostTest { | |
88 protected: | |
89 virtual ScreenOrientationDispatcherHost* CreateDispatcher() OVERRIDE { | |
90 return new ScreenOrientationDispatcherHostWithSink(&sink_); | |
91 } | |
92 | |
93 const IPC::TestSink& sink() const { | |
94 return sink_; | |
95 } | |
96 | |
97 IPC::TestSink sink_; | |
98 }; | |
99 | |
100 // Test that when receiving a lock message, it is correctly dispatched to the | |
101 // ScreenOrientationProvider. | |
102 // We don't actually need this to be a *WithSinkTest but otherwise the IPC | |
103 // messages are detected as leaked. | |
104 TEST_F(ScreenOrientationDispatcherHostWithSinkTest, ProviderLock) { | |
105 // If we change this array, update |orientationsToTestCount| below. | |
106 blink::WebScreenOrientationLockType orientationsToTest[] = { | |
107 blink::WebScreenOrientationLockPortraitPrimary, | |
108 blink::WebScreenOrientationLockPortraitSecondary, | |
109 blink::WebScreenOrientationLockLandscapePrimary, | |
110 blink::WebScreenOrientationLockLandscapeSecondary, | |
111 blink::WebScreenOrientationLockPortrait, | |
112 blink::WebScreenOrientationLockLandscapePrimary, | |
113 blink::WebScreenOrientationLockAny | |
114 }; | |
115 | |
116 // Unfortunately, initializer list constructor for std::list is not yet | |
117 // something we can use. | |
118 // Keep this in sync with |orientationsToTest|. | |
119 int orientationsToTestCount = 7; | |
120 | |
121 for (int i = 0; i < orientationsToTestCount; ++i) { | |
122 bool message_was_handled = false; | |
123 blink::WebScreenOrientationLockType orientation = orientationsToTest[i]; | |
124 | |
125 message_was_handled = dispatcher_->OnMessageReceived( | |
126 ScreenOrientationHostMsg_LockRequest(orientation, 0)); | |
127 | |
128 EXPECT_TRUE(message_was_handled); | |
129 EXPECT_EQ(orientation, provider_->orientation()); | |
130 } | |
131 } | |
132 | |
133 // Test that when receiving an unlock message, it is correctly dispatched to the | |
134 // ScreenOrientationProvider. | |
135 TEST_F(ScreenOrientationDispatcherHostTest, ProviderUnlock) { | |
136 bool message_was_handled = dispatcher_->OnMessageReceived( | |
137 ScreenOrientationHostMsg_Unlock()); | |
138 | |
139 EXPECT_TRUE(message_was_handled); | |
140 EXPECT_TRUE(provider_->unlock_called()); | |
141 } | |
142 | |
143 // Test that when there is no provider, a LockRequest fails with the appropriate | |
144 // ErrorType. | |
145 TEST_F(ScreenOrientationDispatcherHostWithSinkTest, NoProvider_LockError) { | |
146 dispatcher_->SetProviderForTests(NULL); | |
147 | |
148 const int request_id = 3; | |
149 dispatcher_->OnMessageReceived(ScreenOrientationHostMsg_LockRequest( | |
150 blink::WebScreenOrientationLockPortraitPrimary, request_id)); | |
151 | |
152 EXPECT_EQ(1u, sink().message_count()); | |
153 | |
154 const IPC::Message* msg = sink().GetFirstMessageMatching( | |
155 ScreenOrientationMsg_LockError::ID); | |
156 EXPECT_TRUE(msg != NULL); | |
157 | |
158 Tuple2<int, blink::WebLockOrientationCallback::ErrorType> params; | |
159 ScreenOrientationMsg_LockError::Read(msg, ¶ms); | |
160 EXPECT_EQ(request_id, params.a); | |
161 EXPECT_EQ(blink::WebLockOrientationCallback::ErrorTypeNotAvailable, params.b); | |
162 } | |
163 | |
164 // Test that when there is a provider, we always send a success response back to | |
165 // the renderer. | |
166 // TODO(mlamouri): we currently do not test the content of the message because | |
167 // it currently contains dummy values. | |
168 TEST_F(ScreenOrientationDispatcherHostWithSinkTest, WithProvider_LockSuccess) { | |
169 const int request_id = 42; | |
170 dispatcher_->OnMessageReceived(ScreenOrientationHostMsg_LockRequest( | |
171 blink::WebScreenOrientationLockPortraitPrimary, request_id)); | |
172 | |
173 EXPECT_EQ(1u, sink().message_count()); | |
174 | |
175 const IPC::Message* msg = sink().GetFirstMessageMatching( | |
176 ScreenOrientationMsg_LockSuccess::ID); | |
177 EXPECT_TRUE(msg != NULL); | |
178 | |
179 Tuple3<int, unsigned, blink::WebScreenOrientationType> params; | |
180 ScreenOrientationMsg_LockSuccess::Read(msg, ¶ms); | |
181 EXPECT_EQ(request_id, params.a); | |
182 } | |
183 | |
184 } // namespace content | |
OLD | NEW |