Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chooser_controller/mock_chooser_controller.h" | 5 #include "chrome/browser/chooser_controller/mock_chooser_controller.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/grit/generated_resources.h" | 11 #include "chrome/grit/generated_resources.h" |
| 12 #include "ui/base/l10n/l10n_util.h" | 12 #include "ui/base/l10n/l10n_util.h" |
| 13 | 13 |
| 14 MockChooserController::MockChooserController(content::RenderFrameHost* owner) | 14 MockChooserController::MockChooserController(content::RenderFrameHost* owner) |
| 15 : ChooserController(owner, | 15 : ChooserController(owner, |
| 16 IDS_USB_DEVICE_CHOOSER_PROMPT_ORIGIN, | 16 IDS_USB_DEVICE_CHOOSER_PROMPT_ORIGIN, |
| 17 IDS_USB_DEVICE_CHOOSER_PROMPT_EXTENSION_NAME), | 17 IDS_USB_DEVICE_CHOOSER_PROMPT_EXTENSION_NAME), |
| 18 no_options_text_(l10n_util::GetStringUTF16( | 18 no_options_text_(l10n_util::GetStringUTF16( |
| 19 IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT)) {} | 19 IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT)) {} |
| 20 | 20 |
| 21 MockChooserController::~MockChooserController() {} | 21 MockChooserController::~MockChooserController() {} |
| 22 | 22 |
| 23 bool MockChooserController::ShouldShowIconBeforeText() const { | |
| 24 return true; | |
| 25 } | |
| 26 | |
| 23 base::string16 MockChooserController::GetNoOptionsText() const { | 27 base::string16 MockChooserController::GetNoOptionsText() const { |
| 24 return no_options_text_; | 28 return no_options_text_; |
| 25 } | 29 } |
| 26 | 30 |
| 27 base::string16 MockChooserController::GetOkButtonLabel() const { | 31 base::string16 MockChooserController::GetOkButtonLabel() const { |
| 28 return l10n_util::GetStringUTF16(IDS_USB_DEVICE_CHOOSER_CONNECT_BUTTON_TEXT); | 32 return l10n_util::GetStringUTF16(IDS_USB_DEVICE_CHOOSER_CONNECT_BUTTON_TEXT); |
| 29 } | 33 } |
| 30 | 34 |
| 31 size_t MockChooserController::NumOptions() const { | 35 size_t MockChooserController::NumOptions() const { |
| 32 return option_names_.size(); | 36 return options_.size(); |
| 37 } | |
| 38 | |
| 39 int MockChooserController::GetSignalStrengthLevel(size_t index) const { | |
| 40 return options_[index].signal_strength_level; | |
| 33 } | 41 } |
| 34 | 42 |
| 35 base::string16 MockChooserController::GetOption(size_t index) const { | 43 base::string16 MockChooserController::GetOption(size_t index) const { |
| 36 return option_names_[index]; | 44 return options_[index].name; |
| 37 } | 45 } |
| 38 | 46 |
| 39 base::string16 MockChooserController::GetStatus() const { | 47 base::string16 MockChooserController::GetStatus() const { |
| 40 return status_text_; | 48 return status_text_; |
| 41 } | 49 } |
| 42 | 50 |
| 43 void MockChooserController::OnAdapterPresenceChanged( | 51 void MockChooserController::OnAdapterPresenceChanged( |
| 44 content::BluetoothChooser::AdapterPresence presence) { | 52 content::BluetoothChooser::AdapterPresence presence) { |
| 45 ClearAllOptions(); | 53 ClearAllOptions(); |
| 46 switch (presence) { | 54 switch (presence) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 status_text_ = | 90 status_text_ = |
| 83 l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN); | 91 l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN); |
| 84 if (view()) { | 92 if (view()) { |
| 85 view()->OnRefreshStateChanged( | 93 view()->OnRefreshStateChanged( |
| 86 false /* Refreshing options is complete */); | 94 false /* Refreshing options is complete */); |
| 87 } | 95 } |
| 88 break; | 96 break; |
| 89 } | 97 } |
| 90 } | 98 } |
| 91 | 99 |
| 92 void MockChooserController::OptionAdded(const base::string16& option_name) { | 100 void MockChooserController::OptionAdded(const base::string16& option_name, |
| 93 option_names_.push_back(option_name); | 101 int signal_strength_level) { |
| 102 options_.push_back({option_name, signal_strength_level}); | |
| 94 if (view()) | 103 if (view()) |
| 95 view()->OnOptionAdded(option_names_.size() - 1); | 104 view()->OnOptionAdded(options_.size() - 1); |
| 96 } | 105 } |
| 97 | 106 |
| 98 void MockChooserController::OptionRemoved(const base::string16& option_name) { | 107 void MockChooserController::OptionRemoved(const base::string16& option_name) { |
| 99 auto it = std::find(option_names_.begin(), option_names_.end(), option_name); | 108 size_t index = 0; |
|
Jeffrey Yasskin
2016/08/19 18:30:05
Replace this with find_if and a lambda, instead of
juncai
2016/08/22 19:18:20
Done.
| |
| 100 if (it != option_names_.end()) { | 109 for (const auto& option : options_) { |
| 101 size_t index = std::distance(option_names_.begin(), it); | 110 if (option.name == option_name) { |
| 102 option_names_.erase(it); | 111 options_.erase(options_.begin() + index); |
| 103 if (view()) | 112 if (view()) |
| 104 view()->OnOptionRemoved(index); | 113 view()->OnOptionRemoved(index); |
| 114 return; | |
| 115 } | |
| 116 ++index; | |
| 105 } | 117 } |
| 106 } | 118 } |
| 107 | 119 |
| 108 void MockChooserController::OptionUpdated( | 120 void MockChooserController::OptionUpdated( |
| 109 const base::string16& previous_option_name, | 121 const base::string16& previous_option_name, |
| 110 const base::string16& new_option_name) { | 122 const base::string16& new_option_name, |
| 111 auto it = std::find(option_names_.begin(), option_names_.end(), | 123 int new_signal_strength_level) { |
| 112 previous_option_name); | 124 size_t index = 0; |
| 113 DCHECK(it != option_names_.end()); | 125 for (auto& option : options_) { |
| 114 size_t index = std::distance(option_names_.begin(), it); | 126 if (option.name == previous_option_name) { |
| 115 *it = new_option_name; | 127 option = {new_option_name, new_signal_strength_level}; |
| 116 if (view()) | 128 if (view()) |
| 117 view()->OnOptionUpdated(index); | 129 view()->OnOptionUpdated(index); |
| 130 return; | |
| 131 } | |
| 132 ++index; | |
| 133 } | |
| 118 } | 134 } |
| 119 | 135 |
| 136 const int MockChooserController::kNoImage = -1; | |
| 137 const int MockChooserController::kSignalStrengthLevel0Bar = 0; | |
| 138 const int MockChooserController::kSignalStrengthLevel1Bar = 1; | |
| 139 const int MockChooserController::kSignalStrengthLevel2Bar = 2; | |
| 140 const int MockChooserController::kSignalStrengthLevel3Bar = 3; | |
| 141 const int MockChooserController::kSignalStrengthLevel4Bar = 4; | |
| 142 | |
| 120 void MockChooserController::ClearAllOptions() { | 143 void MockChooserController::ClearAllOptions() { |
| 121 option_names_.clear(); | 144 options_.clear(); |
| 122 } | 145 } |
| OLD | NEW |