Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: content/shell/browser/layout_test/layout_test_bluetooth_chooser_factory.cc

Issue 1325953002: Add functions to let tests read and control the Bluetooth chooser state. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "content/shell/browser/layout_test/layout_test_bluetooth_chooser_factor y.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "url/gurl.h"
9
10 namespace content {
11
12 class WebContents;
13
14 // Implements a Bluetooth chooser that records events it's sent, instead of
15 // showing a dialog, selects the
ortuno 2015/09/02 16:14:25 Line wrapping.
Jeffrey Yasskin 2015/09/09 21:26:08 Actually, the content's all wrong too. Fixed.
16 // first added device. This is used as a default chooser implementation for
17 // platforms without a full UI.
18 class LayoutTestBluetoothChooserFactory::Chooser : public BluetoothChooser {
19 public:
20 Chooser(const base::WeakPtr<LayoutTestBluetoothChooserFactory>& factory,
21 const EventHandler& event_handler)
22 : event_handler(event_handler), factory_(factory) {
23 DCHECK(factory);
24 factory->choosers_.insert(this);
25 }
26
27 ~Chooser() override {
28 if (factory_) {
29 factory_->choosers_.erase(this);
30 }
31 }
32
33 // BluetoothChooser:
34 void SetAdapterPresence(AdapterPresence presence) override {
35 if (!factory_) {
ortuno 2015/09/02 16:14:25 Q: When would the factory be destroyed before the
Jeffrey Yasskin 2015/09/09 21:26:08 Good point: the factory does cancel all the choose
36 return;
37 }
38 std::string event = "SetAdapterPresence(";
39 switch (presence) {
40 case AdapterPresence::ABSENT:
41 event += "ABSENT";
42 break;
43 case AdapterPresence::POWERED_OFF:
44 event += "POWERED_OFF";
45 break;
46 case AdapterPresence::POWERED_ON:
47 event += "POWERED_ON";
48 break;
49 }
50 event += ")";
51 factory_->events_.push_back(event);
52 }
53
54 void ShowDiscoveryState(DiscoveryState state) override {
55 if (!factory_) {
56 return;
57 }
58 std::string event = "ShowDiscoveryState(";
59 switch (state) {
60 case DiscoveryState::FAILED_TO_START:
61 event += "FAILED_TO_START";
62 break;
63 case DiscoveryState::DISCOVERING:
64 event += "DISCOVERING";
65 break;
66 case DiscoveryState::IDLE:
67 event += "IDLE";
68 break;
69 }
70 event += ")";
71 factory_->events_.push_back(event);
72 }
73
74 void AddDevice(const std::string& device_id,
75 const base::string16& device_name) override {
76 if (!factory_) {
77 return;
78 }
79 std::string event = "AddDevice(";
80 event += device_id;
81 event += ", ";
82 event += UTF16ToUTF8(device_name);
83 event += ")";
84 factory_->events_.push_back(event);
85 }
86
87 void RemoveDevice(const std::string& device_id) override {
88 if (!factory_) {
89 return;
90 }
91 std::string event = "RemoveDevice(";
92 event += device_id;
93 event += ")";
94 factory_->events_.push_back(event);
95 }
96
97 EventHandler event_handler;
98
99 private:
100 base::WeakPtr<LayoutTestBluetoothChooserFactory> factory_;
101
102 DISALLOW_COPY_AND_ASSIGN(Chooser);
103 };
104
105 LayoutTestBluetoothChooserFactory::LayoutTestBluetoothChooserFactory()
106 : weak_this_(this) {}
107
108 LayoutTestBluetoothChooserFactory::~LayoutTestBluetoothChooserFactory() {
109 // Copy |choosers_| to make sure event handler executions that modify
110 // |choosers_| don't invalidate iterators.
111 std::vector<Chooser*> choosers_copy(choosers_.begin(), choosers_.end());
ortuno 2015/09/02 16:14:25 This code is the same as SendEvent. Are calls to o
Jeffrey Yasskin 2015/09/09 21:26:08 Calls to non-virtual functions are fine in destruc
112 for (Chooser* chooser : choosers_copy) {
113 chooser->event_handler.Run(BluetoothChooser::Event::CANCELLED, "");
114 }
115 }
116
117 scoped_ptr<BluetoothChooser>
118 LayoutTestBluetoothChooserFactory::RunBluetoothChooser(
119 WebContents* web_contents,
120 const BluetoothChooser::EventHandler& event_handler,
121 const GURL& origin) {
122 std::string event = "RunBluetoothChooser(";
123 event += origin.spec();
124 event += ")";
125 events_.push_back(event);
126 return make_scoped_ptr(new Chooser(weak_this_.GetWeakPtr(), event_handler));
127 }
128
129 std::vector<std::string>
130 LayoutTestBluetoothChooserFactory::GetAndResetEvents() {
131 std::vector<std::string> result;
132 result.swap(events_);
133 return result;
134 }
135
136 void LayoutTestBluetoothChooserFactory::SendEvent(
137 BluetoothChooser::Event event,
138 const std::string& device_id) {
139 // Copy |choosers_| to make sure event handler executions that modify
140 // |choosers_| don't invalidate iterators.
141 std::vector<Chooser*> choosers_copy(choosers_.begin(), choosers_.end());
142 for (Chooser* chooser : choosers_copy) {
143 chooser->event_handler.Run(event, device_id);
144 }
145 }
146
147 } // namespace content
OLDNEW
« no previous file with comments | « content/shell/browser/layout_test/layout_test_bluetooth_chooser_factory.h ('k') | content/shell/browser/shell.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698