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

Unified Diff: chrome/browser/ui/bluetooth/bluetooth_chooser_controller_unittest.cc

Issue 2275173002: Add unit test for BluetoothChooserController (Closed)
Patch Set: added unit test for BluetoothChooserController Created 4 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/bluetooth/bluetooth_chooser_controller_unittest.cc
diff --git a/chrome/browser/ui/bluetooth/bluetooth_chooser_controller_unittest.cc b/chrome/browser/ui/bluetooth/bluetooth_chooser_controller_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d185f5d999774eb7defb1f41c859c9fef966cad5
--- /dev/null
+++ b/chrome/browser/ui/bluetooth/bluetooth_chooser_controller_unittest.cc
@@ -0,0 +1,363 @@
+// Copyright 2016 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 <string>
+
+#include "base/bind.h"
+#include "base/macros.h"
+#include "base/strings/utf_string_conversions.h"
+#include "chrome/browser/ui/bluetooth/bluetooth_chooser_controller.h"
+#include "chrome/grit/generated_resources.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "ui/base/l10n/l10n_util.h"
+
+namespace {
+
+class MockBluetoothChooserView : public ChooserController::View {
+ public:
+ MockBluetoothChooserView() {}
+ ~MockBluetoothChooserView() override = default;
+
+ MOCK_METHOD0(OnOptionsInitialized, void());
+ MOCK_METHOD1(OnOptionAdded, void(size_t index));
+ MOCK_METHOD1(OnOptionRemoved, void(size_t index));
+ MOCK_METHOD1(OnOptionUpdated, void(size_t index));
+ MOCK_METHOD1(OnAdapterEnabledChanged, void(bool enabled));
+ MOCK_METHOD1(OnRefreshStateChanged, void(bool enabled));
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MockBluetoothChooserView);
+};
+
+} // namespace
+
+class BluetoothChooserControllerTest : public testing::Test {
+ public:
+ BluetoothChooserControllerTest() {}
+ ~BluetoothChooserControllerTest() override = default;
Jeffrey Yasskin 2016/08/26 21:46:12 You don't need to mention a defaulted destructor.
juncai 2016/08/27 00:21:30 Done.
+
+ void SetUp() override {
+ content::BluetoothChooser::EventHandler event_handler =
+ base::Bind(&BluetoothChooserControllerTest::OnBluetoothChooserEvent,
+ base::Unretained(this));
+ bluetooth_chooser_controller_.reset(
Jeffrey Yasskin 2016/08/26 21:46:12 Make this a value member, not a unique_ptr, and in
juncai 2016/08/27 00:21:30 Done.
+ new BluetoothChooserController(nullptr, event_handler));
+ mock_bluetooth_chooser_view_.reset(new MockBluetoothChooserView());
Jeffrey Yasskin 2016/08/26 21:46:12 Also make mock_bluetooth_chooser_view_ a value mem
juncai 2016/08/27 00:21:30 Done.
+ bluetooth_chooser_controller_->set_view(mock_bluetooth_chooser_view_.get());
Jeffrey Yasskin 2016/08/26 21:46:12 Since this can't fail, it's also fine to move it t
juncai 2016/08/27 00:21:30 Done.
+ }
+
+ protected:
+ void OnBluetoothChooserEvent(content::BluetoothChooser::Event event,
+ const std::string& device_id) {
+ event_ = event;
+ device_id_ = device_id;
+ }
+
+ std::unique_ptr<BluetoothChooserController> bluetooth_chooser_controller_;
+ std::unique_ptr<MockBluetoothChooserView> mock_bluetooth_chooser_view_;
+ content::BluetoothChooser::Event event_;
Jeffrey Yasskin 2016/08/26 21:46:12 I'd call these last_event_ and last_device_id_.
juncai 2016/08/27 00:21:30 Done.
+ std::string device_id_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(BluetoothChooserControllerTest);
+};
+
+TEST_F(BluetoothChooserControllerTest, AddDevice) {
+ EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionAdded(0)).Times(1);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
+ true /* is_gatt_connected */, true /* is_paired */, -1);
Jeffrey Yasskin 2016/08/26 21:46:12 Also comment the meaning of the -1: /* rssi */
juncai 2016/08/27 00:21:30 Done.
+ EXPECT_EQ(1u, bluetooth_chooser_controller_->NumOptions());
+ EXPECT_EQ(base::ASCIIToUTF16("a"),
+ bluetooth_chooser_controller_->GetOption(0));
+ EXPECT_EQ(-1, bluetooth_chooser_controller_->GetSignalStrengthLevel(0));
+
+ EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionAdded(1)).Times(1);
Jeffrey Yasskin 2016/08/26 21:46:12 Since you have three separate sets of expectations
juncai 2016/08/27 00:21:30 Done.
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
+ true /* is_gatt_connected */, true /* is_paired */, 0);
+ EXPECT_EQ(2u, bluetooth_chooser_controller_->NumOptions());
+ EXPECT_EQ(base::ASCIIToUTF16("b"),
+ bluetooth_chooser_controller_->GetOption(1));
+ EXPECT_EQ(0, bluetooth_chooser_controller_->GetSignalStrengthLevel(1));
+
+ EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionAdded(2)).Times(1);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
+ true /* is_gatt_connected */, true /* is_paired */, 1);
+ EXPECT_EQ(3u, bluetooth_chooser_controller_->NumOptions());
+ EXPECT_EQ(base::ASCIIToUTF16("c"),
+ bluetooth_chooser_controller_->GetOption(2));
+ EXPECT_EQ(1, bluetooth_chooser_controller_->GetSignalStrengthLevel(2));
+}
+
+TEST_F(BluetoothChooserControllerTest, RemoveDevice) {
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
+ true /* is_gatt_connected */, true /* is_paired */, -1);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
+ true /* is_gatt_connected */, true /* is_paired */, 0);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
+ true /* is_gatt_connected */, true /* is_paired */, 1);
+
+ EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionRemoved(1)).Times(1);
+ bluetooth_chooser_controller_->RemoveDevice("id_b");
+ EXPECT_EQ(2u, bluetooth_chooser_controller_->NumOptions());
+ EXPECT_EQ(base::ASCIIToUTF16("a"),
+ bluetooth_chooser_controller_->GetOption(0));
+ EXPECT_EQ(base::ASCIIToUTF16("c"),
+ bluetooth_chooser_controller_->GetOption(1));
+
+ // Remove a non-existent device, the number of devices should not change.
+ bluetooth_chooser_controller_->RemoveDevice("non-existent");
+ EXPECT_EQ(2u, bluetooth_chooser_controller_->NumOptions());
+ EXPECT_EQ(base::ASCIIToUTF16("a"),
+ bluetooth_chooser_controller_->GetOption(0));
+ EXPECT_EQ(base::ASCIIToUTF16("c"),
+ bluetooth_chooser_controller_->GetOption(1));
+
+ EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionRemoved(0)).Times(1);
+ bluetooth_chooser_controller_->RemoveDevice("id_a");
+ EXPECT_EQ(1u, bluetooth_chooser_controller_->NumOptions());
+ EXPECT_EQ(base::ASCIIToUTF16("c"),
+ bluetooth_chooser_controller_->GetOption(0));
+
+ EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionRemoved(0)).Times(1);
+ bluetooth_chooser_controller_->RemoveDevice("id_c");
+ EXPECT_EQ(0u, bluetooth_chooser_controller_->NumOptions());
+}
+
+TEST_F(BluetoothChooserControllerTest, AddAndRemoveDeviceWithSameName) {
Jeffrey Yasskin 2016/08/26 21:46:12 Try to name tests with an overall description of w
juncai 2016/08/27 00:21:30 Done.
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_a_1", false /* should_update_name */, base::ASCIIToUTF16("a"),
+ true /* is_gatt_connected */, true /* is_paired */, -1);
+ EXPECT_EQ(base::ASCIIToUTF16("a"),
+ bluetooth_chooser_controller_->GetOption(0));
+
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
+ true /* is_gatt_connected */, true /* is_paired */, 0);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_a_2", false /* should_update_name */, base::ASCIIToUTF16("a"),
+ true /* is_gatt_connected */, true /* is_paired */, 1);
+ EXPECT_EQ(base::ASCIIToUTF16("a (id_a_1)"),
+ bluetooth_chooser_controller_->GetOption(0));
+ EXPECT_EQ(base::ASCIIToUTF16("b"),
+ bluetooth_chooser_controller_->GetOption(1));
+ EXPECT_EQ(base::ASCIIToUTF16("a (id_a_2)"),
+ bluetooth_chooser_controller_->GetOption(2));
+
+ bluetooth_chooser_controller_->RemoveDevice("id_a_1");
+ EXPECT_EQ(base::ASCIIToUTF16("b"),
+ bluetooth_chooser_controller_->GetOption(0));
+ EXPECT_EQ(base::ASCIIToUTF16("a"),
+ bluetooth_chooser_controller_->GetOption(1));
+}
+
+TEST_F(BluetoothChooserControllerTest, UpdateDeviceName) {
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
+ true /* is_gatt_connected */, true /* is_paired */, -1);
+ EXPECT_EQ(base::ASCIIToUTF16("a"),
+ bluetooth_chooser_controller_->GetOption(0));
+
+ EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_a", false /* should_update_name */, base::ASCIIToUTF16("aa"),
+ true /* is_gatt_connected */, true /* is_paired */, -1);
+ // The name is still "a" since |should_update_name| is false.
+ EXPECT_EQ(base::ASCIIToUTF16("a"),
+ bluetooth_chooser_controller_->GetOption(0));
+
+ EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_a", true /* should_update_name */, base::ASCIIToUTF16("aa"),
+ true /* is_gatt_connected */, true /* is_paired */, -1);
+ EXPECT_EQ(1u, bluetooth_chooser_controller_->NumOptions());
+ EXPECT_EQ(base::ASCIIToUTF16("aa"),
+ bluetooth_chooser_controller_->GetOption(0));
+
+ bluetooth_chooser_controller_->RemoveDevice("id_a");
+ EXPECT_EQ(0u, bluetooth_chooser_controller_->NumOptions());
+}
+
+TEST_F(BluetoothChooserControllerTest, UpdateDeviceSignalStrengthLevel) {
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
+ true /* is_gatt_connected */, true /* is_paired */, -1);
+ EXPECT_EQ(-1, bluetooth_chooser_controller_->GetSignalStrengthLevel(0));
+
+ EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
+ true /* is_gatt_connected */, true /* is_paired */, 1);
+ EXPECT_EQ(1, bluetooth_chooser_controller_->GetSignalStrengthLevel(0));
+
+ EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1);
+ // When Bluetooth device scanning stops, an update is sent and the signal
+ // strength level is -1, and in this case, should still use the previously
+ // stored signal strength level. So here the signal strength level is
+ // still 1.
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
+ true /* is_gatt_connected */, true /* is_paired */, -1);
+ EXPECT_EQ(1, bluetooth_chooser_controller_->GetSignalStrengthLevel(0));
+}
+
+TEST_F(BluetoothChooserControllerTest, ChangeAdapterPresence) {
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
Jeffrey Yasskin 2016/08/26 21:46:12 It looks like you have several tests that need to
juncai 2016/08/27 00:21:30 Done.
+ "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
+ true /* is_gatt_connected */, true /* is_paired */, -1);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
+ true /* is_gatt_connected */, true /* is_paired */, 0);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
+ true /* is_gatt_connected */, true /* is_paired */, 1);
+
+ EXPECT_EQ(l10n_util::GetStringUTF16(
+ IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT),
+ bluetooth_chooser_controller_->GetNoOptionsText());
+ EXPECT_EQ(base::string16(), bluetooth_chooser_controller_->GetStatus());
+
+ EXPECT_CALL(
+ *mock_bluetooth_chooser_view_,
+ OnAdapterEnabledChanged(false /* Bluetooth adapter is turned off */))
+ .Times(1);
+ bluetooth_chooser_controller_->OnAdapterPresenceChanged(
+ content::BluetoothChooser::AdapterPresence::POWERED_OFF);
+ EXPECT_EQ(0u, bluetooth_chooser_controller_->NumOptions());
+ EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_ADAPTER_OFF),
+ bluetooth_chooser_controller_->GetNoOptionsText());
+ EXPECT_EQ(base::string16(), bluetooth_chooser_controller_->GetStatus());
+
+ EXPECT_CALL(
+ *mock_bluetooth_chooser_view_,
+ OnAdapterEnabledChanged(true /* Bluetooth adapter is turned on */))
+ .Times(1);
+ bluetooth_chooser_controller_->OnAdapterPresenceChanged(
+ content::BluetoothChooser::AdapterPresence::POWERED_ON);
+ EXPECT_EQ(0u, bluetooth_chooser_controller_->NumOptions());
+ EXPECT_EQ(l10n_util::GetStringUTF16(
+ IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT),
+ bluetooth_chooser_controller_->GetNoOptionsText());
+ EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN),
+ bluetooth_chooser_controller_->GetStatus());
+}
+
+TEST_F(BluetoothChooserControllerTest, ChangeDiscoveryState) {
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
+ true /* is_gatt_connected */, true /* is_paired */, -1);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
+ true /* is_gatt_connected */, true /* is_paired */, 0);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
+ true /* is_gatt_connected */, true /* is_paired */, 1);
+
+ EXPECT_EQ(base::string16(), bluetooth_chooser_controller_->GetStatus());
+
+ EXPECT_CALL(
+ *mock_bluetooth_chooser_view_,
+ OnRefreshStateChanged(true /* Refreshing options is in progress */))
+ .Times(1);
+ bluetooth_chooser_controller_->OnDiscoveryStateChanged(
+ content::BluetoothChooser::DiscoveryState::DISCOVERING);
+ EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_SCANNING),
+ bluetooth_chooser_controller_->GetStatus());
+
+ EXPECT_CALL(*mock_bluetooth_chooser_view_,
+ OnRefreshStateChanged(false /* Refreshing options is complete */))
+ .Times(1);
+ bluetooth_chooser_controller_->OnDiscoveryStateChanged(
+ content::BluetoothChooser::DiscoveryState::IDLE);
+ EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN),
+ bluetooth_chooser_controller_->GetStatus());
+
+ EXPECT_CALL(*mock_bluetooth_chooser_view_,
+ OnRefreshStateChanged(false /* Refreshing options is complete */))
+ .Times(1);
+ bluetooth_chooser_controller_->OnDiscoveryStateChanged(
+ content::BluetoothChooser::DiscoveryState::FAILED_TO_START);
+ EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN),
+ bluetooth_chooser_controller_->GetStatus());
+}
+
+TEST_F(BluetoothChooserControllerTest, RefreshOptions) {
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
+ true /* is_gatt_connected */, true /* is_paired */, -1);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
+ true /* is_gatt_connected */, true /* is_paired */, 0);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
+ true /* is_gatt_connected */, true /* is_paired */, 1);
+
+ bluetooth_chooser_controller_->RefreshOptions();
Jeffrey Yasskin 2016/08/26 21:46:12 Does this not call any view methods?
juncai 2016/08/27 00:21:30 It does call view method, but through: https://cs.
Jeffrey Yasskin 2016/08/27 00:35:45 Please test that.
juncai 2016/08/27 02:15:56 The above calling sequence is for non-testing code
Jeffrey Yasskin 2016/09/02 20:40:58 Argh, sorry, I mis-read the implementation of Refr
juncai 2016/09/02 22:41:45 Done.
+ EXPECT_EQ(0u, bluetooth_chooser_controller_->NumOptions());
+ EXPECT_EQ(content::BluetoothChooser::Event::RESCAN, event_);
+ EXPECT_EQ(std::string(), device_id_);
+}
+
+TEST_F(BluetoothChooserControllerTest, Select) {
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
+ true /* is_gatt_connected */, true /* is_paired */, -1);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
+ true /* is_gatt_connected */, true /* is_paired */, 0);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
+ true /* is_gatt_connected */, true /* is_paired */, 1);
+
+ bluetooth_chooser_controller_->Select(0);
+ EXPECT_EQ(content::BluetoothChooser::Event::SELECTED, event_);
+ EXPECT_EQ("id_a", device_id_);
+
+ bluetooth_chooser_controller_->Select(1);
+ EXPECT_EQ(content::BluetoothChooser::Event::SELECTED, event_);
+ EXPECT_EQ("id_b", device_id_);
+
+ bluetooth_chooser_controller_->Select(2);
+ EXPECT_EQ(content::BluetoothChooser::Event::SELECTED, event_);
+ EXPECT_EQ("id_c", device_id_);
+}
+
+TEST_F(BluetoothChooserControllerTest, Cancel) {
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
+ true /* is_gatt_connected */, true /* is_paired */, -1);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
+ true /* is_gatt_connected */, true /* is_paired */, 0);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
+ true /* is_gatt_connected */, true /* is_paired */, 1);
+
+ bluetooth_chooser_controller_->Cancel();
+ EXPECT_EQ(content::BluetoothChooser::Event::CANCELLED, event_);
+ EXPECT_EQ(std::string(), device_id_);
+}
+
+TEST_F(BluetoothChooserControllerTest, Close) {
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
+ true /* is_gatt_connected */, true /* is_paired */, -1);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
+ true /* is_gatt_connected */, true /* is_paired */, 0);
+ bluetooth_chooser_controller_->AddOrUpdateDevice(
+ "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
+ true /* is_gatt_connected */, true /* is_paired */, 1);
+
+ bluetooth_chooser_controller_->Close();
+ EXPECT_EQ(content::BluetoothChooser::Event::CANCELLED, event_);
+ EXPECT_EQ(std::string(), device_id_);
+}

Powered by Google App Engine
This is Rietveld 408576698