summaryrefslogtreecommitdiffstats
path: root/components/test_runner/mock_constraints.cc
blob: 7e18e55ecdfc1b4fc3f68988167ad24c5b2e00af (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
// Copyright 2013 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 "components/test_runner/mock_constraints.h"

#include <stddef.h>

#include "third_party/WebKit/public/platform/WebMediaConstraints.h"
#include "third_party/WebKit/public/platform/WebString.h"

using blink::WebMediaConstraint;
using blink::WebMediaConstraints;
using blink::WebString;
using blink::WebVector;

namespace test_runner {

namespace {

bool IsSupported(const WebString& constraint) {
  return constraint == "valid_and_supported_1" ||
         constraint == "valid_and_supported_2";
}

bool IsValid(const WebString& constraint) {
  return IsSupported(constraint) || constraint == "valid_but_unsupported_1" ||
         constraint == "valid_but_unsupported_2";
}

}  // namespace

bool MockConstraints::VerifyConstraints(const WebMediaConstraints& constraints,
                                        WebString* failed_constraint) {
  WebVector<WebMediaConstraint> mandatory_constraints;
  constraints.getMandatoryConstraints(mandatory_constraints);
  if (mandatory_constraints.size()) {
    for (size_t i = 0; i < mandatory_constraints.size(); ++i) {
      const WebMediaConstraint& curr = mandatory_constraints[i];
      if (!IsSupported(curr.m_name) || curr.m_value != "1") {
        if (failed_constraint)
          *failed_constraint = curr.m_name;
        return false;
      }
    }
  }

  WebVector<WebMediaConstraint> optional_constraints;
  constraints.getOptionalConstraints(optional_constraints);
  if (optional_constraints.size()) {
    for (size_t i = 0; i < optional_constraints.size(); ++i) {
      const WebMediaConstraint& curr = optional_constraints[i];
      if (!IsValid(curr.m_name) || curr.m_value != "0") {
        if (failed_constraint)
          *failed_constraint = curr.m_name;
        return false;
      }
    }
  }

  return true;
}

}  // namespace test_runner