1 // Copyright 2015 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/browser/layout_test/layout_test_bluetooth_chooser_factory.h"
7 #include "base/strings/utf_string_conversions.h"
14 // Implements a Bluetooth chooser that records events it's sent, instead of
15 // showing a dialog. It allows tests to control how the chooser responds.
16 class LayoutTestBluetoothChooserFactory::Chooser
: public BluetoothChooser
{
18 Chooser(const base::WeakPtr
<LayoutTestBluetoothChooserFactory
>& factory
,
19 const EventHandler
& event_handler
)
20 : event_handler(event_handler
), factory_(factory
) {
22 factory
->choosers_
.insert(this);
27 factory_
->choosers_
.erase(this);
31 void SetAdapterPresence(AdapterPresence presence
) override
{
34 case AdapterPresence::ABSENT
:
35 factory_
->events_
.push_back("adapter-removed");
37 case AdapterPresence::POWERED_OFF
:
38 factory_
->events_
.push_back("adapter-disabled");
40 case AdapterPresence::POWERED_ON
:
41 factory_
->events_
.push_back("adapter-enabled");
46 void ShowDiscoveryState(DiscoveryState state
) override
{
49 case DiscoveryState::FAILED_TO_START
:
50 factory_
->events_
.push_back("discovery-failed-to-start");
52 case DiscoveryState::DISCOVERING
:
53 factory_
->events_
.push_back("discovering");
55 case DiscoveryState::IDLE
:
56 factory_
->events_
.push_back("discovery-idle");
61 void AddDevice(const std::string
& device_id
,
62 const base::string16
& device_name
) override
{
64 std::string event
= "add-device(";
65 event
+= base::UTF16ToUTF8(device_name
);
68 factory_
->events_
.push_back(event
);
71 void RemoveDevice(const std::string
& device_id
) override
{
73 std::string event
= "remove-device(";
76 factory_
->events_
.push_back(event
);
79 EventHandler event_handler
;
82 void CheckFactory() const {
83 CHECK(factory_
) << "The factory should cancel all choosers in its "
84 "destructor, and choosers should be destroyed "
85 "synchronously when canceled.";
88 base::WeakPtr
<LayoutTestBluetoothChooserFactory
> factory_
;
90 DISALLOW_COPY_AND_ASSIGN(Chooser
);
93 LayoutTestBluetoothChooserFactory::LayoutTestBluetoothChooserFactory()
96 LayoutTestBluetoothChooserFactory::~LayoutTestBluetoothChooserFactory() {
97 SendEvent(BluetoothChooser::Event::CANCELLED
, "");
100 scoped_ptr
<BluetoothChooser
>
101 LayoutTestBluetoothChooserFactory::RunBluetoothChooser(
102 WebContents
* web_contents
,
103 const BluetoothChooser::EventHandler
& event_handler
,
104 const GURL
& origin
) {
105 std::string event
= "chooser-opened(";
106 event
+= origin
.spec();
108 events_
.push_back(event
);
109 return make_scoped_ptr(new Chooser(weak_this_
.GetWeakPtr(), event_handler
));
112 std::vector
<std::string
>
113 LayoutTestBluetoothChooserFactory::GetAndResetEvents() {
114 std::vector
<std::string
> result
;
115 result
.swap(events_
);
119 void LayoutTestBluetoothChooserFactory::SendEvent(
120 BluetoothChooser::Event event
,
121 const std::string
& device_id
) {
122 // Copy |choosers_| to make sure event handler executions that modify
123 // |choosers_| don't invalidate iterators.
124 std::vector
<Chooser
*> choosers_copy(choosers_
.begin(), choosers_
.end());
125 for (Chooser
* chooser
: choosers_copy
) {
126 chooser
->event_handler
.Run(event
, device_id
);
130 } // namespace content