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/shell/renderer/test_runner/mock_screen_orientation_client.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "third_party/WebKit/public/web/WebFrame.h"
14 MockScreenOrientationClient::MockScreenOrientationClient()
16 current_lock_(blink::WebScreenOrientationLockDefault),
17 device_orientation_(blink::WebScreenOrientationPortraitPrimary),
18 current_orientation_(blink::WebScreenOrientationPortraitPrimary) {
21 MockScreenOrientationClient::~MockScreenOrientationClient() {
24 void MockScreenOrientationClient::ResetData() {
25 current_lock_ = blink::WebScreenOrientationLockDefault;
26 device_orientation_ = blink::WebScreenOrientationPortraitPrimary;
27 current_orientation_ = blink::WebScreenOrientationPortraitPrimary;
30 void MockScreenOrientationClient::UpdateDeviceOrientation(
31 blink::WebFrame* main_frame,
32 blink::WebScreenOrientationType orientation) {
33 main_frame_ = main_frame;
34 if (device_orientation_ == orientation)
36 device_orientation_ = orientation;
37 if (!IsOrientationAllowedByCurrentLock(orientation))
39 UpdateScreenOrientation(orientation);
42 void MockScreenOrientationClient::UpdateScreenOrientation(
43 blink::WebScreenOrientationType orientation) {
44 if (current_orientation_ == orientation)
46 current_orientation_ = orientation;
48 main_frame_->sendOrientationChangeEvent();
51 blink::WebScreenOrientationType
52 MockScreenOrientationClient::CurrentOrientationType() const {
53 return current_orientation_;
56 unsigned MockScreenOrientationClient::CurrentOrientationAngle() const {
57 return OrientationTypeToAngle(current_orientation_);
60 unsigned MockScreenOrientationClient::OrientationTypeToAngle(
61 blink::WebScreenOrientationType type) {
63 // FIXME(ostap): This relationship between orientationType and
64 // orientationAngle is temporary. The test should be able to specify
65 // the angle in addition to the orientation type.
67 case blink::WebScreenOrientationLandscapePrimary:
70 case blink::WebScreenOrientationLandscapeSecondary:
73 case blink::WebScreenOrientationPortraitSecondary:
82 bool MockScreenOrientationClient::IsOrientationAllowedByCurrentLock(
83 blink::WebScreenOrientationType orientation) {
84 if (current_lock_ == blink::WebScreenOrientationLockDefault ||
85 current_lock_ == blink::WebScreenOrientationLockAny) {
89 switch (orientation) {
90 case blink::WebScreenOrientationPortraitPrimary:
91 return current_lock_ == blink::WebScreenOrientationLockPortraitPrimary ||
92 current_lock_ == blink::WebScreenOrientationLockPortrait;
93 case blink::WebScreenOrientationPortraitSecondary:
94 return current_lock_ ==
95 blink::WebScreenOrientationLockPortraitSecondary ||
96 current_lock_ == blink::WebScreenOrientationLockPortrait;
97 case blink::WebScreenOrientationLandscapePrimary:
98 return current_lock_ == blink::WebScreenOrientationLockLandscapePrimary ||
99 current_lock_ == blink::WebScreenOrientationLockLandscape;
100 case blink::WebScreenOrientationLandscapeSecondary:
101 return current_lock_ ==
102 blink::WebScreenOrientationLockLandscapeSecondary ||
103 current_lock_ == blink::WebScreenOrientationLockLandscape;
109 void MockScreenOrientationClient::lockOrientation(
110 blink::WebScreenOrientationLockType orientation,
111 blink::WebLockOrientationCallback* callback) {
112 base::MessageLoop::current()->PostTask(
114 base::Bind(&MockScreenOrientationClient::UpdateLockSync,
115 base::Unretained(this),
120 void MockScreenOrientationClient::unlockOrientation() {
121 base::MessageLoop::current()->PostTask(
123 base::Bind(&MockScreenOrientationClient::ResetLockSync,
124 base::Unretained(this)));
127 void MockScreenOrientationClient::UpdateLockSync(
128 blink::WebScreenOrientationLockType lock,
129 blink::WebLockOrientationCallback* callback) {
130 DCHECK(lock != blink::WebScreenOrientationLockDefault);
131 current_lock_ = lock;
132 if (!IsOrientationAllowedByCurrentLock(current_orientation_))
133 UpdateScreenOrientation(SuitableOrientationForCurrentLock());
134 callback->onSuccess();
138 void MockScreenOrientationClient::ResetLockSync() {
139 bool will_screen_orientation_need_updating =
140 !IsOrientationAllowedByCurrentLock(device_orientation_);
141 current_lock_ = blink::WebScreenOrientationLockDefault;
142 if (will_screen_orientation_need_updating)
143 UpdateScreenOrientation(device_orientation_);
146 blink::WebScreenOrientationType
147 MockScreenOrientationClient::SuitableOrientationForCurrentLock() {
148 switch (current_lock_) {
149 case blink::WebScreenOrientationLockPortraitSecondary:
150 return blink::WebScreenOrientationPortraitSecondary;
151 case blink::WebScreenOrientationLockLandscapePrimary:
152 case blink::WebScreenOrientationLockLandscape:
153 return blink::WebScreenOrientationLandscapePrimary;
154 case blink::WebScreenOrientationLockLandscapeSecondary:
155 return blink::WebScreenOrientationLandscapePrimary;
157 return blink::WebScreenOrientationPortraitPrimary;
161 } // namespace content