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.
5 #include "content/renderer/screen_orientation/mock_screen_orientation_controller.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "content/renderer/render_view_impl.h"
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.
24 MockScreenOrientationController::~MockScreenOrientationController() {
27 void MockScreenOrientationController::ResetData() {
28 if (render_view_impl())
29 render_view_impl()->RemoveObserver(this);
31 current_lock_ = blink::WebScreenOrientationLockDefault;
32 device_orientation_ = blink::WebScreenOrientationPortraitPrimary;
33 current_orientation_ = blink::WebScreenOrientationPortraitPrimary;
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);
46 if (device_orientation_ == orientation)
48 device_orientation_ = orientation;
49 if (!IsOrientationAllowedByCurrentLock(orientation))
51 UpdateScreenOrientation(orientation);
54 RenderViewImpl* MockScreenOrientationController::render_view_impl() const {
55 return static_cast<RenderViewImpl*>(render_view());
58 void MockScreenOrientationController::UpdateScreenOrientation(
59 blink::WebScreenOrientationType orientation) {
60 if (current_orientation_ == orientation)
62 current_orientation_ = orientation;
63 if (render_view_impl())
64 render_view_impl()->SetScreenOrientationForTesting(orientation);
67 bool MockScreenOrientationController::IsOrientationAllowedByCurrentLock(
68 blink::WebScreenOrientationType orientation) {
69 if (current_lock_ == blink::WebScreenOrientationLockDefault ||
70 current_lock_ == blink::WebScreenOrientationLockAny) {
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;
94 void MockScreenOrientationController::OnDestruct() {
97 } // namespace content