summaryrefslogtreecommitdiffstats
path: root/chrome/test/webdriver/commands/speed_command.cc
blob: e09602d3c8068dceed244a15c8922f2de0144ce2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/utf_string_conversions.h"
#include "chrome/test/webdriver/commands/speed_command.h"

namespace webdriver {

bool SpeedCommand::Init(Response* const response) {
  std::string speed;

  if (!WebDriverCommand::Init(response)) {
    SET_WEBDRIVER_ERROR(response, "Failure on Init for setting speed",
                      kInternalServerError);
    return false;
  }

  // The speed parameter must be passed in as SLOW, MEDIUM, or FAST.
  // The command must also be in all upper case letters.
  if (!GetStringASCIIParameter("speed", &speed)) {
    SET_WEBDRIVER_ERROR(response, "Request missing speed parameter",
                        kBadRequest);
    return false;
  }

  if (speed.compare("SLOW") == 0) {
    LOG(INFO) << "Speed set to slow";
    speed_ = Session::kSlow;
  } else if (speed.compare("MEDIUM") == 0) {
    LOG(INFO) << "Speed set to medium";
    speed_ = Session::kMedium;
  } else if (speed.compare("FAST") == 0) {
    LOG(INFO) << "Speed set to fast" << std::endl;
    speed_ = Session::kFast;
  } else {
    // If the speed is invalid throw and error in the POST response.
    LOG(INFO) << "Requested an unknown speed: " << speed;
    speed_ = Session::kUnknown;
  }

  return true;
}

void SpeedCommand::ExecuteGet(Response* const response) {
  switch (session_->speed()) {
    case Session::kSlow:
      response->set_value(new StringValue("SLOW"));
      response->set_status(kSuccess);
      break;

    case Session::kMedium:
      response->set_value(new StringValue("MEDIUM"));
      response->set_status(kSuccess);
      break;

    case Session::kFast:
      response->set_value(new StringValue("FAST"));
      response->set_status(kSuccess);
      break;

    default:
      // The speed should have never been set to unknown.
      SET_WEBDRIVER_ERROR(response, "Unknown speed set",
                          kInternalServerError);
      NOTREACHED();
      break;
  }
}

void SpeedCommand::ExecutePost(Response* const response) {
  if (speed_ == Session::kUnknown) {
    SET_WEBDRIVER_ERROR(response, "Invalid speed requested",
                        kInternalServerError);
    return;
  }

  session_->set_speed(speed_);
  response->set_value(new StringValue("success"));
  response->set_status(kSuccess);
}

}  // namespace webdriver