Home | History | Annotate | Download | only in screen_orientation
      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 "content/renderer/screen_orientation/mock_screen_orientation_controller.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/logging.h"
      9 #include "base/message_loop/message_loop.h"
     10 #include "content/renderer/render_view_impl.h"
     11 
     12 namespace content {
     13 
     14 MockScreenOrientationController::MockScreenOrientationController()
     15     : RenderViewObserver(NULL),
     16       current_lock_(blink::WebScreenOrientationLockDefault),
     17       device_orientation_(blink::WebScreenOrientationPortraitPrimary),
     18       current_orientation_(blink::WebScreenOrientationPortraitPrimary) {
     19   // Since MockScreenOrientationController is held by LazyInstance reference,
     20   // add this ref for it.
     21   AddRef();
     22 }
     23 
     24 MockScreenOrientationController::~MockScreenOrientationController() {
     25 }
     26 
     27 void MockScreenOrientationController::ResetData() {
     28   if (render_view_impl())
     29     render_view_impl()->RemoveObserver(this);
     30 
     31   current_lock_ = blink::WebScreenOrientationLockDefault;
     32   device_orientation_ = blink::WebScreenOrientationPortraitPrimary;
     33   current_orientation_ = blink::WebScreenOrientationPortraitPrimary;
     34 }
     35 
     36 void MockScreenOrientationController::UpdateDeviceOrientation(
     37     RenderView* render_view,
     38     blink::WebScreenOrientationType orientation) {
     39   if (this->render_view()) {
     40     // Make sure that render_view_ did not change during test.
     41     DCHECK_EQ(this->render_view(), render_view);
     42   } else {
     43     Observe(render_view);
     44   }
     45 
     46   if (device_orientation_ == orientation)
     47     return;
     48   device_orientation_ = orientation;
     49   if (!IsOrientationAllowedByCurrentLock(orientation))
     50     return;
     51   UpdateScreenOrientation(orientation);
     52 }
     53 
     54 RenderViewImpl* MockScreenOrientationController::render_view_impl() const {
     55   return static_cast<RenderViewImpl*>(render_view());
     56 }
     57 
     58 void MockScreenOrientationController::UpdateScreenOrientation(
     59     blink::WebScreenOrientationType orientation) {
     60   if (current_orientation_ == orientation)
     61     return;
     62   current_orientation_ = orientation;
     63   if (render_view_impl())
     64     render_view_impl()->SetScreenOrientationForTesting(orientation);
     65 }
     66 
     67 bool MockScreenOrientationController::IsOrientationAllowedByCurrentLock(
     68     blink::WebScreenOrientationType orientation) {
     69   if (current_lock_ == blink::WebScreenOrientationLockDefault ||
     70       current_lock_ == blink::WebScreenOrientationLockAny) {
     71     return true;
     72   }
     73 
     74   switch (orientation) {
     75     case blink::WebScreenOrientationPortraitPrimary:
     76       return current_lock_ == blink::WebScreenOrientationLockPortraitPrimary ||
     77              current_lock_ == blink::WebScreenOrientationLockPortrait;
     78     case blink::WebScreenOrientationPortraitSecondary:
     79       return current_lock_ ==
     80                  blink::WebScreenOrientationLockPortraitSecondary ||
     81              current_lock_ == blink::WebScreenOrientationLockPortrait;
     82     case blink::WebScreenOrientationLandscapePrimary:
     83       return current_lock_ == blink::WebScreenOrientationLockLandscapePrimary ||
     84              current_lock_ == blink::WebScreenOrientationLockLandscape;
     85     case blink::WebScreenOrientationLandscapeSecondary:
     86       return current_lock_ ==
     87                  blink::WebScreenOrientationLockLandscapeSecondary ||
     88              current_lock_ == blink::WebScreenOrientationLockLandscape;
     89     default:
     90       return false;
     91   }
     92 }
     93 
     94 void MockScreenOrientationController::OnDestruct() {
     95 }
     96 
     97 } // namespace content
     98