| OLD | NEW | 
|---|
| 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 "screen_orientation_dispatcher.h" | 5 #include "screen_orientation_dispatcher.h" | 
| 6 | 6 | 
| 7 #include <list> | 7 #include <list> | 
| 8 | 8 | 
| 9 #include "base/logging.h" | 9 #include "base/logging.h" | 
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" | 
| 11 #include "content/common/screen_orientation_messages.h" |  | 
| 12 #include "content/public/test/test_utils.h" | 11 #include "content/public/test/test_utils.h" | 
| 13 #include "ipc/ipc_test_sink.h" |  | 
| 14 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" | 
| 15 #include "third_party/WebKit/public/platform/WebLockOrientationCallback.h" | 13 #include "third_party/WebKit/public/platform/WebLockOrientationCallback.h" | 
| 16 | 14 | 
| 17 namespace content { | 15 namespace content { | 
| 18 | 16 | 
| 19 // MockLockOrientationCallback is an implementation of | 17 // MockLockOrientationCallback is an implementation of | 
| 20 // WebLockOrientationCallback and takes a LockOrientationResultHolder* as a | 18 // WebLockOrientationCallback and takes a LockOrientationResultHolder* as a | 
| 21 // parameter when being constructed. The |results_| pointer is owned by the | 19 // parameter when being constructed. The |results_| pointer is owned by the | 
| 22 // caller and not by the callback object. The intent being that as soon as the | 20 // caller and not by the callback object. The intent being that as soon as the | 
| 23 // callback is resolved, it will be killed so we use the | 21 // callback is resolved, it will be killed so we use the | 
| (...skipping 22 matching lines...) Expand all  Loading... | 
| 46     results_->failed_ = true; | 44     results_->failed_ = true; | 
| 47     results_->error_ = error; | 45     results_->error_ = error; | 
| 48   } | 46   } | 
| 49 | 47 | 
| 50  private: | 48  private: | 
| 51   virtual ~MockLockOrientationCallback() {} | 49   virtual ~MockLockOrientationCallback() {} | 
| 52 | 50 | 
| 53   LockOrientationResultHolder* results_; | 51   LockOrientationResultHolder* results_; | 
| 54 }; | 52 }; | 
| 55 | 53 | 
| 56 class ScreenOrientationDispatcherWithSink : public ScreenOrientationDispatcher { | 54 class MockScreenOrientationService : public ScreenOrientationService { | 
| 57  public: | 55  public: | 
| 58   explicit ScreenOrientationDispatcherWithSink(IPC::TestSink* sink) | 56   MockScreenOrientationService(); | 
| 59       :ScreenOrientationDispatcher(NULL) , sink_(sink) { | 57   virtual ~MockScreenOrientationService(); | 
|  | 58 | 
|  | 59   // ScreenOrientationService: | 
|  | 60   virtual void LockOrientation( | 
|  | 61       ScreenOrientationLockType lock_type, | 
|  | 62       const mojo::Callback<void(ScreenOrientationLockResult)>& callback) | 
|  | 63       OVERRIDE; | 
|  | 64   virtual void UnlockOrientation() OVERRIDE; | 
|  | 65 | 
|  | 66   void RunLockResultCallback(ScreenOrientationLockResult result_type); | 
|  | 67   const mojo::Callback<void(ScreenOrientationLockResult)>& | 
|  | 68   on_result_callback() { | 
|  | 69     return on_result_callback_; | 
| 60   } | 70   } | 
| 61 | 71 | 
| 62   virtual bool Send(IPC::Message* message) OVERRIDE { | 72  private: | 
| 63     return sink_->Send(message); | 73   mojo::Callback<void(ScreenOrientationLockResult)> on_result_callback_; | 
| 64   } | 74 }; | 
| 65 | 75 | 
| 66   IPC::TestSink* sink_; | 76 MockScreenOrientationService::MockScreenOrientationService() { | 
| 67 }; | 77 } | 
|  | 78 | 
|  | 79 MockScreenOrientationService::~MockScreenOrientationService() { | 
|  | 80 } | 
|  | 81 | 
|  | 82 void MockScreenOrientationService::LockOrientation( | 
|  | 83     ScreenOrientationLockType lock_type, | 
|  | 84     const mojo::Callback<void(ScreenOrientationLockResult)>& callback) { | 
|  | 85   on_result_callback_ = callback; | 
|  | 86 } | 
|  | 87 | 
|  | 88 void MockScreenOrientationService::UnlockOrientation() { | 
|  | 89 } | 
|  | 90 | 
|  | 91 void MockScreenOrientationService::RunLockResultCallback( | 
|  | 92     ScreenOrientationLockResult result_type) { | 
|  | 93   DCHECK(!on_result_callback_.is_null()); | 
|  | 94   on_result_callback_.Run(result_type); | 
|  | 95   on_result_callback_ = mojo::Callback<void(ScreenOrientationLockResult)>(); | 
|  | 96 } | 
| 68 | 97 | 
| 69 class ScreenOrientationDispatcherTest : public testing::Test { | 98 class ScreenOrientationDispatcherTest : public testing::Test { | 
| 70  protected: | 99  protected: | 
| 71   virtual void SetUp() OVERRIDE { | 100   virtual void SetUp() OVERRIDE { | 
| 72     dispatcher_.reset(new ScreenOrientationDispatcherWithSink(&sink_)); | 101     dispatcher_.reset( | 
|  | 102         new ScreenOrientationDispatcher(&screen_orientation_service_)); | 
| 73   } | 103   } | 
| 74 | 104 | 
| 75   int GetFirstLockRequestIdFromSink() { | 105   virtual void TearDown() OVERRIDE {} | 
| 76     const IPC::Message* msg = sink().GetFirstMessageMatching( |  | 
| 77         ScreenOrientationHostMsg_LockRequest::ID); |  | 
| 78     EXPECT_TRUE(msg != NULL); |  | 
| 79 |  | 
| 80     Tuple2<blink::WebScreenOrientationLockType,int> params; |  | 
| 81     ScreenOrientationHostMsg_LockRequest::Read(msg, ¶ms); |  | 
| 82     return params.b; |  | 
| 83   } |  | 
| 84 |  | 
| 85   IPC::TestSink& sink() { |  | 
| 86     return sink_; |  | 
| 87   } |  | 
| 88 | 106 | 
| 89   void LockOrientation(blink::WebScreenOrientationLockType orientation, | 107   void LockOrientation(blink::WebScreenOrientationLockType orientation, | 
| 90                        blink::WebLockOrientationCallback* callback) { | 108                        blink::WebLockOrientationCallback* callback) { | 
| 91     dispatcher_->lockOrientation(orientation, callback); | 109     dispatcher_->lockOrientation(orientation, callback); | 
| 92   } | 110   } | 
| 93 | 111 | 
| 94   void UnlockOrientation() { | 112   void UnlockOrientation() { | 
| 95     dispatcher_->unlockOrientation(); | 113     dispatcher_->unlockOrientation(); | 
| 96   } | 114   } | 
| 97 | 115 | 
| 98   void OnMessageReceived(const IPC::Message& message) { | 116   MockScreenOrientationService screen_orientation_service_; | 
| 99     dispatcher_->OnMessageReceived(message); |  | 
| 100   } |  | 
| 101 |  | 
| 102   int routing_id() const { |  | 
| 103     // We return a fake routing_id() in the context of this test. |  | 
| 104     return 0; |  | 
| 105   } |  | 
| 106 |  | 
| 107   IPC::TestSink sink_; |  | 
| 108   scoped_ptr<ScreenOrientationDispatcher> dispatcher_; | 117   scoped_ptr<ScreenOrientationDispatcher> dispatcher_; | 
| 109 }; | 118 }; | 
| 110 | 119 | 
| 111 // Test that calling lockOrientation() followed by unlockOrientation() cancel | 120 // Test that calling lockOrientation() followed by unlockOrientation() cancel | 
| 112 // the lockOrientation(). | 121 // the lockOrientation(). | 
| 113 TEST_F(ScreenOrientationDispatcherTest, CancelPending_Unlocking) { | 122 TEST_F(ScreenOrientationDispatcherTest, CancelPending_Unlocking) { | 
| 114   MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 123   MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 
| 115   LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 124   LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 
| 116                   new MockLockOrientationCallback(&callback_results)); | 125                   new MockLockOrientationCallback(&callback_results)); | 
| 117   UnlockOrientation(); | 126   UnlockOrientation(); | 
| (...skipping 12 matching lines...) Expand all  Loading... | 
| 130   LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 139   LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 
| 131                   new MockLockOrientationCallback(&callback_results)); | 140                   new MockLockOrientationCallback(&callback_results)); | 
| 132   LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 141   LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 
| 133                   new MockLockOrientationCallback(&callback_results2)); | 142                   new MockLockOrientationCallback(&callback_results2)); | 
| 134 | 143 | 
| 135   EXPECT_FALSE(callback_results.succeeded_); | 144   EXPECT_FALSE(callback_results.succeeded_); | 
| 136   EXPECT_TRUE(callback_results.failed_); | 145   EXPECT_TRUE(callback_results.failed_); | 
| 137   EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results.error_); | 146   EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results.error_); | 
| 138 } | 147 } | 
| 139 | 148 | 
| 140 // Test that when a LockError message is received, the request is set as failed | 149 // Test that when a LockError message is received, the request is set as | 
| 141 // with the correct values. | 150 // failed with the correct values. | 
| 142 TEST_F(ScreenOrientationDispatcherTest, LockRequest_Error) { | 151 TEST_F(ScreenOrientationDispatcherTest, LockRequest_Error) { | 
| 143   std::list<blink::WebLockOrientationError> errors; | 152   std::map<ScreenOrientationLockResult, blink::WebLockOrientationError> errors; | 
| 144   errors.push_back(blink::WebLockOrientationErrorNotAvailable); | 153   errors[SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE] = | 
| 145   errors.push_back( | 154       blink::WebLockOrientationErrorNotAvailable; | 
| 146       blink::WebLockOrientationErrorFullScreenRequired); | 155   errors[SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED] = | 
| 147   errors.push_back(blink::WebLockOrientationErrorCanceled); | 156       blink::WebLockOrientationErrorFullScreenRequired; | 
|  | 157   errors[SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED] = | 
|  | 158       blink::WebLockOrientationErrorCanceled; | 
| 148 | 159 | 
| 149   for (std::list<blink::WebLockOrientationError>::const_iterator | 160   for (std::map<ScreenOrientationLockResult, | 
| 150           it = errors.begin(); it != errors.end(); ++it) { | 161                 blink::WebLockOrientationError>::const_iterator it = | 
|  | 162            errors.begin(); | 
|  | 163        it != errors.end(); | 
|  | 164        ++it) { | 
| 151     MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 165     MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 
| 152     LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 166     LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 
| 153                     new MockLockOrientationCallback(&callback_results)); | 167                     new MockLockOrientationCallback(&callback_results)); | 
| 154 | 168     screen_orientation_service_.RunLockResultCallback(it->first); | 
| 155     int request_id = GetFirstLockRequestIdFromSink(); |  | 
| 156     OnMessageReceived( |  | 
| 157         ScreenOrientationMsg_LockError(routing_id(), request_id, *it)); |  | 
| 158 | 169 | 
| 159     EXPECT_FALSE(callback_results.succeeded_); | 170     EXPECT_FALSE(callback_results.succeeded_); | 
| 160     EXPECT_TRUE(callback_results.failed_); | 171     EXPECT_TRUE(callback_results.failed_); | 
| 161     EXPECT_EQ(*it, callback_results.error_); | 172     EXPECT_EQ(it->second, callback_results.error_); | 
| 162 |  | 
| 163     sink().ClearMessages(); |  | 
| 164   } | 173   } | 
| 165 } | 174 } | 
| 166 | 175 | 
| 167 // Test that when a LockSuccess message is received, the request is set as | 176 // Test that when a LockSuccess message is received, the request is set as | 
| 168 // succeeded. | 177 // succeeded. | 
| 169 TEST_F(ScreenOrientationDispatcherTest, LockRequest_Success) { | 178 TEST_F(ScreenOrientationDispatcherTest, LockRequest_Success) { | 
| 170   MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 179   MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 
| 171   LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 180   LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 
| 172                   new MockLockOrientationCallback(&callback_results)); | 181                   new MockLockOrientationCallback(&callback_results)); | 
| 173 | 182 | 
| 174   int request_id = GetFirstLockRequestIdFromSink(); | 183   screen_orientation_service_.RunLockResultCallback( | 
| 175   OnMessageReceived(ScreenOrientationMsg_LockSuccess(routing_id(), | 184       SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); | 
| 176                                                      request_id)); |  | 
| 177 |  | 
| 178   EXPECT_TRUE(callback_results.succeeded_); | 185   EXPECT_TRUE(callback_results.succeeded_); | 
| 179   EXPECT_FALSE(callback_results.failed_); | 186   EXPECT_FALSE(callback_results.failed_); | 
| 180 |  | 
| 181   sink().ClearMessages(); |  | 
| 182 } |  | 
| 183 |  | 
| 184 // Test an edge case: a LockSuccess is received but it matches no pending |  | 
| 185 // callback. |  | 
| 186 TEST_F(ScreenOrientationDispatcherTest, SuccessForUnknownRequest) { |  | 
| 187   MockLockOrientationCallback::LockOrientationResultHolder callback_results; |  | 
| 188   LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, |  | 
| 189                   new MockLockOrientationCallback(&callback_results)); |  | 
| 190 |  | 
| 191   int request_id = GetFirstLockRequestIdFromSink(); |  | 
| 192   OnMessageReceived(ScreenOrientationMsg_LockSuccess(routing_id(), |  | 
| 193                                                      request_id + 1)); |  | 
| 194 |  | 
| 195   EXPECT_FALSE(callback_results.succeeded_); |  | 
| 196   EXPECT_FALSE(callback_results.failed_); |  | 
| 197 } |  | 
| 198 |  | 
| 199 // Test an edge case: a LockError is received but it matches no pending |  | 
| 200 // callback. |  | 
| 201 TEST_F(ScreenOrientationDispatcherTest, ErrorForUnknownRequest) { |  | 
| 202   MockLockOrientationCallback::LockOrientationResultHolder callback_results; |  | 
| 203   LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, |  | 
| 204                   new MockLockOrientationCallback(&callback_results)); |  | 
| 205 |  | 
| 206   int request_id = GetFirstLockRequestIdFromSink(); |  | 
| 207   OnMessageReceived(ScreenOrientationMsg_LockError( |  | 
| 208       routing_id(), request_id + 1, blink::WebLockOrientationErrorCanceled)); |  | 
| 209 |  | 
| 210   EXPECT_FALSE(callback_results.succeeded_); |  | 
| 211   EXPECT_FALSE(callback_results.failed_); |  | 
| 212 } | 187 } | 
| 213 | 188 | 
| 214 // Test the following scenario: | 189 // Test the following scenario: | 
| 215 // - request1 is received by the dispatcher; | 190 // - request1 is received by the dispatcher; | 
| 216 // - request2 is received by the dispatcher; | 191 // - request2 is received by the dispatcher; | 
| 217 // - request1 is rejected; | 192 // - request1 is rejected; | 
| 218 // - request1 success response is received. | 193 // - request1 success response is received. | 
| 219 // Expected: request1 is still rejected, request2 has not been set as succeeded. | 194 // Expected: request1 is still rejected, request2 has not been set as succeeded. | 
| 220 TEST_F(ScreenOrientationDispatcherTest, RaceScenario) { | 195 TEST_F(ScreenOrientationDispatcherTest, RaceScenario) { | 
| 221   MockLockOrientationCallback::LockOrientationResultHolder callback_results1; | 196   MockLockOrientationCallback::LockOrientationResultHolder callback_results1; | 
| 222   MockLockOrientationCallback::LockOrientationResultHolder callback_results2; | 197   MockLockOrientationCallback::LockOrientationResultHolder callback_results2; | 
| 223 | 198 | 
| 224   LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 199   LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 
| 225                   new MockLockOrientationCallback(&callback_results1)); | 200                   new MockLockOrientationCallback(&callback_results1)); | 
| 226   int request_id1 = GetFirstLockRequestIdFromSink(); | 201   mojo::Callback<void(ScreenOrientationLockResult)> service_callback1 = | 
|  | 202       screen_orientation_service_.on_result_callback(); | 
| 227 | 203 | 
| 228   LockOrientation(blink::WebScreenOrientationLockLandscapePrimary, | 204   LockOrientation(blink::WebScreenOrientationLockLandscapePrimary, | 
| 229                   new MockLockOrientationCallback(&callback_results2)); | 205                   new MockLockOrientationCallback(&callback_results2)); | 
| 230 | 206 | 
| 231   // callback_results1 must be rejected, tested in CancelPending_DoubleLock. | 207   // callback_results1 must be rejected, tested in CancelPending_DoubleLock. | 
| 232 | 208 | 
| 233   OnMessageReceived(ScreenOrientationMsg_LockSuccess(routing_id(), | 209   service_callback1.Run(SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); | 
| 234                                                      request_id1)); |  | 
| 235 | 210 | 
| 236   // First request is still rejected. | 211   // First request is still rejected. | 
| 237   EXPECT_FALSE(callback_results1.succeeded_); | 212   EXPECT_FALSE(callback_results1.succeeded_); | 
| 238   EXPECT_TRUE(callback_results1.failed_); | 213   EXPECT_TRUE(callback_results1.failed_); | 
| 239   EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results1.error_); | 214   EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results1.error_); | 
| 240 | 215 | 
| 241   // Second request is still pending. | 216   // Second request is still pending. | 
| 242   EXPECT_FALSE(callback_results2.succeeded_); | 217   EXPECT_FALSE(callback_results2.succeeded_); | 
| 243   EXPECT_FALSE(callback_results2.failed_); | 218   EXPECT_FALSE(callback_results2.failed_); | 
| 244 } | 219 } | 
| 245 | 220 | 
| 246 }  // namespace content | 221 }  // namespace content | 
| OLD | NEW | 
|---|