Commit 5a918a34 authored by hansberry's avatar hansberry Committed by Commit bot

HostScanner: Add scanned hosts to NetworkStateHandler and NotificationPresenter.

BUG=672263

Review-Url: https://codereview.chromium.org/2781263004
Cr-Commit-Position: refs/heads/master@{#462745}
parent b1b01762
...@@ -82,7 +82,8 @@ class HostScanSchedulerTest : public testing::Test { ...@@ -82,7 +82,8 @@ class HostScanSchedulerTest : public testing::Test {
class FakeHostScanner : public HostScanner { class FakeHostScanner : public HostScanner {
public: public:
FakeHostScanner() FakeHostScanner()
: HostScanner(nullptr, nullptr, nullptr), num_scans_started_(0) {} : HostScanner(nullptr, nullptr, nullptr, nullptr, nullptr),
num_scans_started_(0) {}
~FakeHostScanner() override {} ~FakeHostScanner() override {}
void StartScan() override { num_scans_started_++; } void StartScan() override { num_scans_started_++; }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "chromeos/components/tether/tether_host_fetcher.h" #include "chromeos/components/tether/tether_host_fetcher.h"
#include "chromeos/network/network_state.h"
#include "components/cryptauth/remote_device_loader.h" #include "components/cryptauth/remote_device_loader.h"
namespace chromeos { namespace chromeos {
...@@ -15,10 +16,14 @@ namespace tether { ...@@ -15,10 +16,14 @@ namespace tether {
HostScanner::HostScanner( HostScanner::HostScanner(
TetherHostFetcher* tether_host_fetcher, TetherHostFetcher* tether_host_fetcher,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer) HostScanDevicePrioritizer* host_scan_device_prioritizer,
NetworkStateHandler* network_state_handler,
NotificationPresenter* notification_presenter)
: tether_host_fetcher_(tether_host_fetcher), : tether_host_fetcher_(tether_host_fetcher),
connection_manager_(connection_manager), connection_manager_(connection_manager),
host_scan_device_prioritizer_(host_scan_device_prioritizer), host_scan_device_prioritizer_(host_scan_device_prioritizer),
network_state_handler_(network_state_handler),
notification_presenter_(notification_presenter),
is_fetching_hosts_(false), is_fetching_hosts_(false),
weak_ptr_factory_(this) {} weak_ptr_factory_(this) {}
...@@ -60,7 +65,23 @@ void HostScanner::OnTetherAvailabilityResponse( ...@@ -60,7 +65,23 @@ void HostScanner::OnTetherAvailabilityResponse(
bool is_final_scan_result) { bool is_final_scan_result) {
most_recent_scan_results_ = scanned_device_list_so_far; most_recent_scan_results_ = scanned_device_list_so_far;
// TODO(hansberry): Hook up to networking code. if (!scanned_device_list_so_far.empty()) {
// TODO (hansberry): Clear out old scanned hosts from NetworkStateHandler.
// TODO (hansberry): Add battery and cell strength properties once
// available.
for (auto& scanned_device_info : scanned_device_list_so_far) {
cryptauth::RemoteDevice remote_device = scanned_device_info.remote_device;
network_state_handler_->AddTetherNetworkState(remote_device.GetDeviceId(),
remote_device.name);
}
if (scanned_device_list_so_far.size() == 1) {
notification_presenter_->NotifyPotentialHotspotNearby(
scanned_device_list_so_far.at(0).remote_device);
} else {
notification_presenter_->NotifyMultiplePotentialHotspotsNearby();
}
}
if (is_final_scan_result) { if (is_final_scan_result) {
// If the final scan result has been received, the operation is finished. // If the final scan result has been received, the operation is finished.
......
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "chromeos/components/tether/host_scanner_operation.h" #include "chromeos/components/tether/host_scanner_operation.h"
#include "chromeos/components/tether/notification_presenter.h"
#include "chromeos/network/network_state_handler.h"
#include "components/cryptauth/remote_device.h" #include "components/cryptauth/remote_device.h"
namespace chromeos { namespace chromeos {
...@@ -28,7 +30,9 @@ class HostScanner : public HostScannerOperation::Observer { ...@@ -28,7 +30,9 @@ class HostScanner : public HostScannerOperation::Observer {
public: public:
HostScanner(TetherHostFetcher* tether_host_fetcher, HostScanner(TetherHostFetcher* tether_host_fetcher,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer); HostScanDevicePrioritizer* host_scan_device_prioritizer,
NetworkStateHandler* network_state_handler,
NotificationPresenter* notification_presenter);
virtual ~HostScanner(); virtual ~HostScanner();
// Starts a host scan if there is no current scan. If a scan is ongoing, this // Starts a host scan if there is no current scan. If a scan is ongoing, this
...@@ -56,6 +60,8 @@ class HostScanner : public HostScannerOperation::Observer { ...@@ -56,6 +60,8 @@ class HostScanner : public HostScannerOperation::Observer {
TetherHostFetcher* tether_host_fetcher_; TetherHostFetcher* tether_host_fetcher_;
BleConnectionManager* connection_manager_; BleConnectionManager* connection_manager_;
HostScanDevicePrioritizer* host_scan_device_prioritizer_; HostScanDevicePrioritizer* host_scan_device_prioritizer_;
NetworkStateHandler* network_state_handler_;
NotificationPresenter* notification_presenter_;
bool is_fetching_hosts_; bool is_fetching_hosts_;
std::unique_ptr<HostScannerOperation> host_scanner_operation_; std::unique_ptr<HostScannerOperation> host_scanner_operation_;
......
...@@ -9,9 +9,16 @@ ...@@ -9,9 +9,16 @@
#include <vector> #include <vector>
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "chromeos/components/tether/fake_ble_connection_manager.h" #include "chromeos/components/tether/fake_ble_connection_manager.h"
#include "chromeos/components/tether/fake_notification_presenter.h"
#include "chromeos/components/tether/fake_tether_host_fetcher.h" #include "chromeos/components/tether/fake_tether_host_fetcher.h"
#include "chromeos/components/tether/host_scan_device_prioritizer.h" #include "chromeos/components/tether/host_scan_device_prioritizer.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h"
#include "chromeos/network/network_state_test.h"
#include "components/cryptauth/remote_device_test_util.h" #include "components/cryptauth/remote_device_test_util.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -119,7 +126,7 @@ CreateFakeScannedDeviceInfos( ...@@ -119,7 +126,7 @@ CreateFakeScannedDeviceInfos(
} // namespace } // namespace
class HostScannerTest : public testing::Test { class HostScannerTest : public NetworkStateTest {
protected: protected:
HostScannerTest() HostScannerTest()
: test_devices_(cryptauth::GenerateTestRemoteDevices(4)), : test_devices_(cryptauth::GenerateTestRemoteDevices(4)),
...@@ -127,6 +134,9 @@ class HostScannerTest : public testing::Test { ...@@ -127,6 +134,9 @@ class HostScannerTest : public testing::Test {
} }
void SetUp() override { void SetUp() override {
DBusThreadManager::Initialize();
NetworkStateTest::SetUp();
scanned_device_infos_so_far_.clear(); scanned_device_infos_so_far_.clear();
fake_tether_host_fetcher_ = base::MakeUnique<FakeTetherHostFetcher>( fake_tether_host_fetcher_ = base::MakeUnique<FakeTetherHostFetcher>(
...@@ -140,9 +150,19 @@ class HostScannerTest : public testing::Test { ...@@ -140,9 +150,19 @@ class HostScannerTest : public testing::Test {
HostScannerOperation::Factory::SetInstanceForTesting( HostScannerOperation::Factory::SetInstanceForTesting(
fake_host_scanner_operation_factory_.get()); fake_host_scanner_operation_factory_.get());
fake_notification_presenter_ =
base::MakeUnique<FakeNotificationPresenter>();
host_scanner_ = base::MakeUnique<HostScanner>( host_scanner_ = base::MakeUnique<HostScanner>(
fake_tether_host_fetcher_.get(), fake_ble_connection_manager_.get(), fake_tether_host_fetcher_.get(), fake_ble_connection_manager_.get(),
fake_host_scan_device_prioritizer_.get()); fake_host_scan_device_prioritizer_.get(), network_state_handler(),
fake_notification_presenter_.get());
}
void TearDown() override {
ShutdownNetworkState();
NetworkStateTest::TearDown();
DBusThreadManager::Shutdown();
} }
// Causes |fake_operation| to receive the scan result in // Causes |fake_operation| to receive the scan result in
...@@ -158,8 +178,33 @@ class HostScannerTest : public testing::Test { ...@@ -158,8 +178,33 @@ class HostScannerTest : public testing::Test {
is_final_scan_result); is_final_scan_result);
EXPECT_EQ(scanned_device_infos_so_far_, EXPECT_EQ(scanned_device_infos_so_far_,
host_scanner_->most_recent_scan_results()); host_scanner_->most_recent_scan_results());
NetworkStateHandler::NetworkStateList tether_networks;
network_state_handler()->GetTetherNetworkList(0 /* no limit */,
&tether_networks);
EXPECT_EQ(scanned_device_infos_so_far_.size(), tether_networks.size());
for (auto& scanned_device_info : scanned_device_infos_so_far_) {
cryptauth::RemoteDevice remote_device = scanned_device_info.remote_device;
const NetworkState* tether_network =
network_state_handler()->GetNetworkStateFromGuid(
remote_device.GetDeviceId());
ASSERT_TRUE(tether_network);
EXPECT_EQ(remote_device.name, tether_network->name());
}
if (scanned_device_infos_so_far_.size() == 1) {
EXPECT_EQ(FakeNotificationPresenter::PotentialHotspotNotificationState::
SINGLE_HOTSPOT_NEARBY_SHOWN,
fake_notification_presenter_->potential_hotspot_state());
} else {
EXPECT_EQ(FakeNotificationPresenter::PotentialHotspotNotificationState::
MULTIPLE_HOTSPOTS_NEARBY_SHOWN,
fake_notification_presenter_->potential_hotspot_state());
}
} }
base::test::ScopedTaskEnvironment scoped_task_environment_;
const std::vector<cryptauth::RemoteDevice> test_devices_; const std::vector<cryptauth::RemoteDevice> test_devices_;
const std::vector<HostScannerOperation::ScannedDeviceInfo> const std::vector<HostScannerOperation::ScannedDeviceInfo>
test_scanned_device_infos; test_scanned_device_infos;
...@@ -176,6 +221,8 @@ class HostScannerTest : public testing::Test { ...@@ -176,6 +221,8 @@ class HostScannerTest : public testing::Test {
std::unique_ptr<HostScanner> host_scanner_; std::unique_ptr<HostScanner> host_scanner_;
std::unique_ptr<FakeNotificationPresenter> fake_notification_presenter_;
private: private:
DISALLOW_COPY_AND_ASSIGN(HostScannerTest); DISALLOW_COPY_AND_ASSIGN(HostScannerTest);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment