File: renderer_factory_selector_unittest.cc

package info (click to toggle)
qtwebengine-opensource-src 5.15.13%2Bdfsg-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,112,448 kB
  • sloc: cpp: 13,167,885; ansic: 4,228,702; javascript: 1,909,649; python: 554,805; asm: 529,966; xml: 496,619; java: 151,702; objc: 80,776; perl: 73,361; sh: 71,208; cs: 30,383; makefile: 20,779; yacc: 9,120; tcl: 8,394; php: 5,896; sql: 5,473; pascal: 4,510; lex: 2,884; lisp: 2,727; ruby: 559; awk: 200; sed: 40
file content (128 lines) | stat: -rw-r--r-- 4,282 bytes parent folder | download | duplicates (7)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2017 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 <memory>

#include "base/bind.h"
#include "base/macros.h"
#include "media/base/overlay_info.h"
#include "media/base/renderer_factory_selector.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {

class RendererFactorySelectorTest : public testing::Test {
 public:
  using FactoryType = RendererFactoryType;

  class FakeFactory : public RendererFactory {
   public:
    explicit FakeFactory(FactoryType type) : type_(type) {}

    std::unique_ptr<Renderer> CreateRenderer(
        const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner,
        const scoped_refptr<base::TaskRunner>& worker_task_runner,
        AudioRendererSink* audio_renderer_sink,
        VideoRendererSink* video_renderer_sink,
        RequestOverlayInfoCB request_overlay_info_cb,
        const gfx::ColorSpace& target_color_space) override {
      return nullptr;
    }

    FactoryType factory_type() { return type_; }

   private:
    FactoryType type_;
  };

  RendererFactorySelectorTest() = default;

  void AddBaseFactory(FactoryType type) {
    selector_.AddBaseFactory(type, std::make_unique<FakeFactory>(type));
  }

  void AddFactory(FactoryType type) {
    selector_.AddFactory(type, std::make_unique<FakeFactory>(type));
  }

  void AddConditionalFactory(FactoryType type) {
    condition_met_map_[type] = false;
    selector_.AddConditionalFactory(
        type, std::make_unique<FakeFactory>(type),
        base::BindRepeating(&RendererFactorySelectorTest::IsConditionMet,
                            base::Unretained(this), type));
  }

  FactoryType GetCurrentlySelectedFactoryType() {
    return reinterpret_cast<FakeFactory*>(selector_.GetCurrentFactory())
        ->factory_type();
  }

  bool IsConditionMet(FactoryType type) {
    DCHECK(condition_met_map_.count(type));
    return condition_met_map_[type];
  }

 protected:
  RendererFactorySelector selector_;
  std::map<FactoryType, bool> condition_met_map_;

  DISALLOW_COPY_AND_ASSIGN(RendererFactorySelectorTest);
};

TEST_F(RendererFactorySelectorTest, SingleFactory) {
  AddBaseFactory(FactoryType::kDefault);
  EXPECT_EQ(FactoryType::kDefault, GetCurrentlySelectedFactoryType());
}

TEST_F(RendererFactorySelectorTest, MultipleFactory) {
  AddBaseFactory(FactoryType::kDefault);
  AddFactory(FactoryType::kMojo);

  EXPECT_EQ(FactoryType::kDefault, GetCurrentlySelectedFactoryType());

  selector_.SetBaseFactoryType(FactoryType::kMojo);
  EXPECT_EQ(FactoryType::kMojo, GetCurrentlySelectedFactoryType());
}

TEST_F(RendererFactorySelectorTest, ConditionalFactory) {
  AddBaseFactory(FactoryType::kDefault);
  AddFactory(FactoryType::kMojo);
  AddConditionalFactory(FactoryType::kCourier);

  EXPECT_EQ(FactoryType::kDefault, GetCurrentlySelectedFactoryType());

  condition_met_map_[FactoryType::kCourier] = true;
  EXPECT_EQ(FactoryType::kCourier, GetCurrentlySelectedFactoryType());

  selector_.SetBaseFactoryType(FactoryType::kMojo);
  EXPECT_EQ(FactoryType::kCourier, GetCurrentlySelectedFactoryType());

  condition_met_map_[FactoryType::kCourier] = false;
  EXPECT_EQ(FactoryType::kMojo, GetCurrentlySelectedFactoryType());
}

TEST_F(RendererFactorySelectorTest, MultipleConditionalFactories) {
  AddBaseFactory(FactoryType::kDefault);
  AddConditionalFactory(FactoryType::kFlinging);
  AddConditionalFactory(FactoryType::kCourier);

  EXPECT_EQ(FactoryType::kDefault, GetCurrentlySelectedFactoryType());

  condition_met_map_[FactoryType::kFlinging] = false;
  condition_met_map_[FactoryType::kCourier] = true;
  EXPECT_EQ(FactoryType::kCourier, GetCurrentlySelectedFactoryType());

  condition_met_map_[FactoryType::kFlinging] = true;
  condition_met_map_[FactoryType::kCourier] = false;
  EXPECT_EQ(FactoryType::kFlinging, GetCurrentlySelectedFactoryType());

  // It's up to the implementation detail to decide which one to use.
  condition_met_map_[FactoryType::kFlinging] = true;
  condition_met_map_[FactoryType::kCourier] = true;
  EXPECT_TRUE(GetCurrentlySelectedFactoryType() == FactoryType::kFlinging ||
              GetCurrentlySelectedFactoryType() == FactoryType::kCourier);
}

}  // namespace media