mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 07:36:10 -04:00
Let fake_fast_pair_repository run with SingleThreadExecutor
PiperOrigin-RevId: 532838814
This commit is contained in:
committed by
Copybara-Service
parent
7027f50d28
commit
60ba057230
@@ -145,6 +145,7 @@ cc_test(
|
||||
"//internal/platform:types",
|
||||
"//internal/platform/implementation/g3", # build_cleaner: keep
|
||||
"@com_github_protobuf_matchers//protobuf-matchers",
|
||||
"@com_google_absl//absl/functional:any_invocable",
|
||||
"@com_google_absl//absl/status",
|
||||
"@com_google_absl//absl/strings",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
|
||||
@@ -21,17 +21,14 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "protobuf-matchers/protocol-buffer-matchers.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/functional/bind_front.h"
|
||||
#include "absl/strings/escaping.h"
|
||||
#include "absl/time/time.h"
|
||||
#include "fastpair/common/constant.h"
|
||||
#include "fastpair/common/protocol.h"
|
||||
#include "fastpair/crypto/fast_pair_message_type.h"
|
||||
#include "fastpair/dataparser/fast_pair_data_parser.h"
|
||||
#include "fastpair/server_access/fake_fast_pair_repository.h"
|
||||
#include "internal/platform/count_down_latch.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace fastpair {
|
||||
@@ -51,19 +48,22 @@ constexpr char kInvalidPublicAntiSpoof[] = "InvalidPublicAntiSpoof";
|
||||
|
||||
constexpr char kValidModelId[] = "718c17";
|
||||
constexpr char kTestAddress[] = "test_address";
|
||||
constexpr absl::Duration kWaitTimeout = absl::Milliseconds(200);
|
||||
|
||||
class FastPairDataEncryptorImplTest : public testing::Test {
|
||||
public:
|
||||
void TearDown() override { data_encryptor_.reset(); }
|
||||
|
||||
void FailedSetUpNoMetadata() {
|
||||
CountDownLatch latch(1);
|
||||
repository_ = std::make_unique<FakeFastPairRepository>();
|
||||
FastPairDevice device(kValidModelId, kTestAddress,
|
||||
Protocol::kFastPairInitialPairing);
|
||||
FastPairDataEncryptorImpl::Factory::CreateAsync(
|
||||
device,
|
||||
absl::bind_front(
|
||||
&FastPairDataEncryptorImplTest::OnDataEncryptorCreateAsync, this));
|
||||
device, absl::bind_front(
|
||||
&FastPairDataEncryptorImplTest::OnDataEncryptorCreateAsync,
|
||||
this, latch));
|
||||
EXPECT_FALSE(latch.Await(kWaitTimeout).GetResult());
|
||||
}
|
||||
|
||||
void FailedSetUpNoKeyPair() {
|
||||
@@ -75,10 +75,12 @@ class FastPairDataEncryptorImplTest : public testing::Test {
|
||||
repository_->SetFakeMetadata(kValidModelId, metadata);
|
||||
FastPairDevice device(kValidModelId, kTestAddress,
|
||||
Protocol::kFastPairInitialPairing);
|
||||
CountDownLatch latch(1);
|
||||
FastPairDataEncryptorImpl::Factory::CreateAsync(
|
||||
device,
|
||||
absl::bind_front(
|
||||
&FastPairDataEncryptorImplTest::OnDataEncryptorCreateAsync, this));
|
||||
device, absl::bind_front(
|
||||
&FastPairDataEncryptorImplTest::OnDataEncryptorCreateAsync,
|
||||
this, latch));
|
||||
latch.Await();
|
||||
}
|
||||
|
||||
void SuccessfulSetUp() {
|
||||
@@ -90,15 +92,19 @@ class FastPairDataEncryptorImplTest : public testing::Test {
|
||||
repository_->SetFakeMetadata(kValidModelId, metadata);
|
||||
FastPairDevice device(kValidModelId, kTestAddress,
|
||||
Protocol::kFastPairInitialPairing);
|
||||
CountDownLatch latch(1);
|
||||
FastPairDataEncryptorImpl::Factory::CreateAsync(
|
||||
device,
|
||||
absl::bind_front(
|
||||
&FastPairDataEncryptorImplTest::OnDataEncryptorCreateAsync, this));
|
||||
device, absl::bind_front(
|
||||
&FastPairDataEncryptorImplTest::OnDataEncryptorCreateAsync,
|
||||
this, latch));
|
||||
latch.Await();
|
||||
}
|
||||
|
||||
void OnDataEncryptorCreateAsync(
|
||||
CountDownLatch latch,
|
||||
std::unique_ptr<FastPairDataEncryptor> fast_pair_data_encryptor) {
|
||||
data_encryptor_ = std::move(fast_pair_data_encryptor);
|
||||
latch.CountDown();
|
||||
}
|
||||
|
||||
std::array<uint8_t, kAesBlockByteSize> EncryptBytes() {
|
||||
@@ -108,53 +114,66 @@ class FastPairDataEncryptorImplTest : public testing::Test {
|
||||
void ParseDecryptedResponse() {
|
||||
const std::array<uint8_t, kAesBlockByteSize> bytes =
|
||||
data_encryptor_->EncryptBytes(kResponseBytes);
|
||||
|
||||
CountDownLatch latch(1);
|
||||
data_encryptor_->ParseDecryptResponse(
|
||||
std::vector<uint8_t>(bytes.begin(), bytes.end()),
|
||||
absl::bind_front(
|
||||
&FastPairDataEncryptorImplTest::ParseDecryptedResponseCallback,
|
||||
this));
|
||||
this, latch));
|
||||
latch.Await();
|
||||
}
|
||||
|
||||
void ParseDecryptedResponseInvalidBytes() {
|
||||
const std::array<uint8_t, kAesBlockByteSize> bytes =
|
||||
data_encryptor_->EncryptBytes(kResponseBytes);
|
||||
|
||||
CountDownLatch latch(1);
|
||||
data_encryptor_->ParseDecryptResponse(
|
||||
std::vector<uint8_t>(bytes.begin() + 3, bytes.end()),
|
||||
absl::bind_front(
|
||||
&FastPairDataEncryptorImplTest::ParseDecryptedResponseCallback,
|
||||
this));
|
||||
this, latch));
|
||||
latch.Await();
|
||||
}
|
||||
|
||||
void ParseDecryptedResponseCallback(
|
||||
const std::optional<DecryptedResponse>& response) {
|
||||
CountDownLatch latch, const std::optional<DecryptedResponse>& response) {
|
||||
response_ = response;
|
||||
latch.CountDown();
|
||||
}
|
||||
|
||||
void ParseDecryptedPasskey() {
|
||||
const std::array<uint8_t, kAesBlockByteSize> bytes =
|
||||
data_encryptor_->EncryptBytes(kPasskeyBytes);
|
||||
|
||||
CountDownLatch latch(1);
|
||||
data_encryptor_->ParseDecryptPasskey(
|
||||
std::vector<uint8_t>(bytes.begin(), bytes.end()),
|
||||
absl::bind_front(
|
||||
&FastPairDataEncryptorImplTest::ParseDecryptPasskeyCallback, this));
|
||||
&FastPairDataEncryptorImplTest::ParseDecryptPasskeyCallback, this,
|
||||
latch));
|
||||
latch.Await();
|
||||
}
|
||||
|
||||
void ParseDecryptedPasskeyInvalidBytes() {
|
||||
const std::array<uint8_t, kAesBlockByteSize> bytes =
|
||||
data_encryptor_->EncryptBytes(kPasskeyBytes);
|
||||
|
||||
CountDownLatch latch(1);
|
||||
data_encryptor_->ParseDecryptPasskey(
|
||||
std::vector<uint8_t>(bytes.begin() + 3, bytes.end()),
|
||||
absl::bind_front(
|
||||
&FastPairDataEncryptorImplTest::ParseDecryptPasskeyCallback, this));
|
||||
&FastPairDataEncryptorImplTest::ParseDecryptPasskeyCallback, this,
|
||||
latch));
|
||||
latch.Await();
|
||||
}
|
||||
|
||||
void ParseDecryptResponseCallback(
|
||||
CountDownLatch latch, const std::optional<DecryptedResponse>& response) {
|
||||
response_ = response;
|
||||
}
|
||||
|
||||
void ParseDecryptPasskeyCallback(
|
||||
const std::optional<DecryptedPasskey>& passkey) {
|
||||
CountDownLatch latch, const std::optional<DecryptedPasskey>& passkey) {
|
||||
passkey_ = passkey;
|
||||
latch.CountDown();
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
@@ -19,10 +19,12 @@
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "protobuf-matchers/protocol-buffer-matchers.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/functional/any_invocable.h"
|
||||
#include "absl/status/status.h"
|
||||
#include "absl/strings/escaping.h"
|
||||
#include "fastpair/common/constant.h"
|
||||
@@ -31,7 +33,7 @@
|
||||
#include "fastpair/common/protocol.h"
|
||||
#include "fastpair/handshake/fast_pair_gatt_service_client_impl.h"
|
||||
#include "fastpair/server_access/fake_fast_pair_repository.h"
|
||||
#include "internal/platform/bluetooth_utils.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/count_down_latch.h"
|
||||
#include "internal/platform/medium_environment.h"
|
||||
|
||||
@@ -69,9 +71,7 @@ constexpr absl::Duration kGattOperationTimeout = absl::Seconds(15);
|
||||
|
||||
class MediumEnvironmentStarter {
|
||||
public:
|
||||
MediumEnvironmentStarter() {
|
||||
MediumEnvironment::Instance().Start({.use_simulated_clock = true});
|
||||
}
|
||||
MediumEnvironmentStarter() { MediumEnvironment::Instance().Start(); }
|
||||
~MediumEnvironmentStarter() { MediumEnvironment::Instance().Stop(); }
|
||||
};
|
||||
|
||||
@@ -87,8 +87,17 @@ struct CharacteristicData {
|
||||
|
||||
class FastPairHandshakeImplTest : public testing::Test {
|
||||
public:
|
||||
void SetUp() override {
|
||||
repository_ = std::make_unique<FakeFastPairRepository>();
|
||||
void TearDown() override {
|
||||
repository_.reset();
|
||||
key_based_characteristic_ = std::nullopt;
|
||||
passkey_characteristic_ = std::nullopt;
|
||||
gatt_server_->Stop();
|
||||
gatt_server_.reset();
|
||||
handshake_.reset();
|
||||
}
|
||||
|
||||
void StartGattServer(
|
||||
absl::AnyInvocable<void()> trigger_keybase_value_change) {
|
||||
gatt_server_ =
|
||||
provider_ble_.StartGattServer(/*ServerGattConnectionCallback=*/{
|
||||
.on_characteristic_read_cb =
|
||||
@@ -105,11 +114,13 @@ class FastPairHandshakeImplTest : public testing::Test {
|
||||
callback(it->second.read_value);
|
||||
},
|
||||
.on_characteristic_write_cb =
|
||||
[&](const api::ble_v2::BlePeripheral& remote_device,
|
||||
[&, trigger_keybase_value_change =
|
||||
std::move(trigger_keybase_value_change)](
|
||||
const api::ble_v2::BlePeripheral& remote_device,
|
||||
const api::ble_v2::GattCharacteristic& characteristic,
|
||||
int offset, absl::string_view data,
|
||||
BleV2Medium::ServerGattConnectionCallback::
|
||||
WriteValueCallback callback) {
|
||||
WriteValueCallback callback) mutable {
|
||||
auto it = characteristics_.find(characteristic);
|
||||
if (it == characteristics_.end()) {
|
||||
callback(absl::NotFoundError("characteristic not found"));
|
||||
@@ -117,19 +128,14 @@ class FastPairHandshakeImplTest : public testing::Test {
|
||||
}
|
||||
it->second.write_value.Set(std::string(data));
|
||||
callback(it->second.write_result);
|
||||
if (it->second.write_result.ok() &&
|
||||
characteristic == *key_based_characteristic_) {
|
||||
trigger_keybase_value_change();
|
||||
}
|
||||
}});
|
||||
provider_address_ = *gatt_server_->GetBlePeripheral().GetAddress();
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
repository_.reset();
|
||||
key_based_characteristic_ = std::nullopt;
|
||||
passkey_characteristic_ = std::nullopt;
|
||||
gatt_server_->Stop();
|
||||
gatt_server_.reset();
|
||||
handshake_.reset();
|
||||
}
|
||||
|
||||
void InsertCorrectGattCharacteristics() {
|
||||
key_based_characteristic_ = gatt_server_->CreateCharacteristic(
|
||||
kFastPairServiceUuid, kKeyBasedCharacteristicUuidV2, permissions_,
|
||||
@@ -145,6 +151,7 @@ class FastPairHandshakeImplTest : public testing::Test {
|
||||
}
|
||||
|
||||
void SetUpFastPairRepository() {
|
||||
repository_ = std::make_unique<FakeFastPairRepository>();
|
||||
proto::Device metadata;
|
||||
std::string decoded_key;
|
||||
absl::Base64Unescape(kPublicAntiSpoof, &decoded_key);
|
||||
@@ -153,6 +160,7 @@ class FastPairHandshakeImplTest : public testing::Test {
|
||||
}
|
||||
|
||||
void FailedFastPairRepository() {
|
||||
repository_ = std::make_unique<FakeFastPairRepository>();
|
||||
proto::Device metadata;
|
||||
std::string decoded_key;
|
||||
absl::Base64Unescape(kInvalidPublicAntiSpoof, &decoded_key);
|
||||
@@ -202,6 +210,11 @@ class FastPairHandshakeImplTest : public testing::Test {
|
||||
};
|
||||
|
||||
TEST_F(FastPairHandshakeImplTest, Success) {
|
||||
bool notified = false;
|
||||
StartGattServer([&]() {
|
||||
notified = true;
|
||||
EXPECT_OK(TriggerKeyBasedGattChanged());
|
||||
});
|
||||
SetUpFastPairRepository();
|
||||
InsertCorrectGattCharacteristics();
|
||||
FastPairDevice device(kMetadataId, provider_address_,
|
||||
@@ -216,12 +229,17 @@ TEST_F(FastPairHandshakeImplTest, Success) {
|
||||
EXPECT_FALSE(failure.has_value());
|
||||
latch.CountDown();
|
||||
});
|
||||
EXPECT_OK(TriggerKeyBasedGattChanged());
|
||||
latch.Await();
|
||||
EXPECT_TRUE(notified);
|
||||
EXPECT_TRUE(handshake_->completed_successfully());
|
||||
}
|
||||
|
||||
TEST_F(FastPairHandshakeImplTest, GattError) {
|
||||
bool notified = false;
|
||||
StartGattServer([&]() {
|
||||
notified = true;
|
||||
EXPECT_OK(TriggerKeyBasedGattChanged());
|
||||
});
|
||||
SetUpFastPairRepository();
|
||||
FastPairDevice device(kMetadataId, provider_address_,
|
||||
Protocol::kFastPairInitialPairing);
|
||||
@@ -235,10 +253,16 @@ TEST_F(FastPairHandshakeImplTest, GattError) {
|
||||
latch.CountDown();
|
||||
});
|
||||
latch.Await();
|
||||
EXPECT_FALSE(notified);
|
||||
EXPECT_FALSE(handshake_->completed_successfully());
|
||||
}
|
||||
|
||||
TEST_F(FastPairHandshakeImplTest, DataEncryptorCreateError) {
|
||||
bool notified = false;
|
||||
StartGattServer([&]() {
|
||||
notified = true;
|
||||
EXPECT_OK(TriggerKeyBasedGattChanged());
|
||||
});
|
||||
FailedFastPairRepository();
|
||||
InsertCorrectGattCharacteristics();
|
||||
FastPairDevice device(kMetadataId, provider_address_,
|
||||
@@ -253,10 +277,12 @@ TEST_F(FastPairHandshakeImplTest, DataEncryptorCreateError) {
|
||||
latch.CountDown();
|
||||
});
|
||||
latch.Await();
|
||||
EXPECT_FALSE(notified);
|
||||
EXPECT_FALSE(handshake_->completed_successfully());
|
||||
}
|
||||
|
||||
TEST_F(FastPairHandshakeImplTest, WriteResponseError) {
|
||||
StartGattServer([]() {});
|
||||
SetUpFastPairRepository();
|
||||
InsertCorrectGattCharacteristics();
|
||||
FastPairDevice device(kMetadataId, provider_address_,
|
||||
@@ -271,12 +297,16 @@ TEST_F(FastPairHandshakeImplTest, WriteResponseError) {
|
||||
PairFailure::kKeyBasedPairingResponseTimeout);
|
||||
latch.CountDown();
|
||||
});
|
||||
SystemClock::Sleep(kGattOperationTimeout);
|
||||
latch.Await();
|
||||
EXPECT_FALSE(handshake_->completed_successfully());
|
||||
}
|
||||
|
||||
TEST_F(FastPairHandshakeImplTest, WriteResponseWrongSize) {
|
||||
bool notified = false;
|
||||
StartGattServer([&]() {
|
||||
notified = true;
|
||||
EXPECT_OK(TriggerKeyBasedGattChangedWithWrongSizeResponse());
|
||||
});
|
||||
SetUpFastPairRepository();
|
||||
InsertCorrectGattCharacteristics();
|
||||
FastPairDevice device(kMetadataId, provider_address_,
|
||||
@@ -291,12 +321,17 @@ TEST_F(FastPairHandshakeImplTest, WriteResponseWrongSize) {
|
||||
PairFailure::kKeybasedPairingResponseDecryptFailure);
|
||||
latch.CountDown();
|
||||
});
|
||||
EXPECT_OK(TriggerKeyBasedGattChangedWithWrongSizeResponse());
|
||||
latch.Await();
|
||||
EXPECT_TRUE(notified);
|
||||
EXPECT_FALSE(handshake_->completed_successfully());
|
||||
}
|
||||
|
||||
TEST_F(FastPairHandshakeImplTest, ParseResponseError) {
|
||||
bool notified = false;
|
||||
StartGattServer([&]() {
|
||||
notified = true;
|
||||
EXPECT_OK(TriggerKeyBasedGattChangedWithWrongResponse());
|
||||
});
|
||||
SetUpFastPairRepository();
|
||||
InsertCorrectGattCharacteristics();
|
||||
FastPairDevice device(kMetadataId, provider_address_,
|
||||
@@ -311,8 +346,8 @@ TEST_F(FastPairHandshakeImplTest, ParseResponseError) {
|
||||
PairFailure::kKeybasedPairingResponseDecryptFailure);
|
||||
latch.CountDown();
|
||||
});
|
||||
EXPECT_OK(TriggerKeyBasedGattChangedWithWrongResponse());
|
||||
latch.Await();
|
||||
EXPECT_TRUE(notified);
|
||||
EXPECT_FALSE(handshake_->completed_successfully());
|
||||
}
|
||||
} // namespace fastpair
|
||||
|
||||
@@ -57,6 +57,7 @@ cc_library(
|
||||
"//fastpair/common",
|
||||
"//fastpair/proto:fastpair_cc_proto",
|
||||
"//fastpair/repository",
|
||||
"//internal/platform:types",
|
||||
"@com_google_absl//absl/container:flat_hash_map",
|
||||
"@com_google_absl//absl/strings",
|
||||
],
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "fastpair/server_access/fake_fast_pair_repository.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
@@ -35,11 +36,12 @@ void FakeFastPairRepository::ClearFakeMetadata(absl::string_view hex_model_id) {
|
||||
|
||||
void FakeFastPairRepository::GetDeviceMetadata(
|
||||
absl::string_view hex_model_id, DeviceMetadataCallback callback) {
|
||||
callback_ = std::move(callback);
|
||||
if (data_.contains(hex_model_id)) {
|
||||
std::move(callback_)(*data_[hex_model_id]);
|
||||
return;
|
||||
}
|
||||
executor_.Execute([callback = std::move(callback), this,
|
||||
hex_model_id = std::string(hex_model_id)]() mutable {
|
||||
if (data_.contains(hex_model_id)) {
|
||||
callback(*data_[hex_model_id]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace fastpair
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "fastpair/repository/device_metadata.h"
|
||||
#include "fastpair/server_access/fast_pair_repository.h"
|
||||
#include "internal/platform/single_thread_executor.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace fastpair {
|
||||
@@ -40,7 +41,7 @@ class FakeFastPairRepository : public FastPairRepository {
|
||||
|
||||
private:
|
||||
absl::flat_hash_map<std::string, std::unique_ptr<DeviceMetadata>> data_;
|
||||
DeviceMetadataCallback callback_;
|
||||
SingleThreadExecutor executor_;
|
||||
};
|
||||
} // namespace fastpair
|
||||
} // namespace nearby
|
||||
|
||||
+32
-5
@@ -58,6 +58,7 @@ cc_library(
|
||||
":fast_pair_ui",
|
||||
"//fastpair/common",
|
||||
"//fastpair/repository",
|
||||
"//internal/platform:types",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -80,21 +81,47 @@ cc_library(
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "fast_pair_ui_test",
|
||||
name = "fast_pair_notification_controller_test",
|
||||
srcs = [
|
||||
"fast_pair/fast_pair_notification_controller_test.cc",
|
||||
],
|
||||
deps = [
|
||||
":fake_fast_pair_ui",
|
||||
":fast_pair_ui",
|
||||
"//fastpair/repository",
|
||||
"//internal/platform/implementation/g3", # build_cleaner: keep
|
||||
"@com_github_protobuf_matchers//protobuf-matchers",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "fast_pair_presenter_test",
|
||||
srcs = [
|
||||
"fast_pair/fast_pair_presenter_impl_test.cc",
|
||||
"ui_broker_impl_test.cc",
|
||||
],
|
||||
deps = [
|
||||
":fake_fast_pair_ui",
|
||||
":fast_pair_ui",
|
||||
"//fastpair/common",
|
||||
"//fastpair/proto:fastpair_cc_proto",
|
||||
"//fastpair/repository",
|
||||
"//fastpair/server_access:test_support",
|
||||
"//internal/network:types",
|
||||
"//internal/platform/implementation/g3",
|
||||
"//internal/platform:types",
|
||||
"//internal/platform/implementation/g3", # build_cleaner: keep
|
||||
"@com_github_protobuf_matchers//protobuf-matchers",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "ui_broker_test",
|
||||
srcs = [
|
||||
"ui_broker_impl_test.cc",
|
||||
],
|
||||
deps = [
|
||||
":fake_fast_pair_ui",
|
||||
":fast_pair_ui",
|
||||
"//internal/platform/implementation/g3", # build_cleaner: keep
|
||||
"@com_github_protobuf_matchers//protobuf-matchers",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
|
||||
@@ -19,11 +19,12 @@ namespace nearby {
|
||||
namespace fastpair {
|
||||
|
||||
enum class DiscoveryAction {
|
||||
kPairToDevice = 0,
|
||||
kDismissedByUser = 1,
|
||||
kDismissedByOs = 2,
|
||||
kLearnMore = 3,
|
||||
kDismissedByTimeout = 4,
|
||||
kUnknown = 0,
|
||||
kPairToDevice = 1,
|
||||
kDismissedByUser = 2,
|
||||
kDismissedByOs = 3,
|
||||
kLearnMore = 4,
|
||||
kDismissedByTimeout = 5,
|
||||
};
|
||||
|
||||
} // namespace fastpair
|
||||
|
||||
@@ -16,20 +16,30 @@
|
||||
#define THIRD_PARTY_NEARBY_FASTPAIR_UI_FAST_PAIR_FAKE_FAST_PAIR_NOTIFICATION_CONTROLLER_OBSERVER_H_
|
||||
|
||||
#include <algorithm>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "fastpair/repository/device_metadata.h"
|
||||
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
|
||||
#include "internal/platform/count_down_latch.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace fastpair {
|
||||
class FakeFastPairNotificationControllerObserver
|
||||
: public FastPairNotificationController::Observer {
|
||||
public:
|
||||
explicit FakeFastPairNotificationControllerObserver(
|
||||
std::optional<CountDownLatch> latch) {
|
||||
latch_ = latch;
|
||||
}
|
||||
|
||||
void OnUpdateDevice(const DeviceMetadata& device) override {
|
||||
device_metadata_name_list_.push_back(device.GetDetails().name());
|
||||
on_update_device_count_++;
|
||||
if (latch_.has_value()) {
|
||||
latch_->CountDown();
|
||||
}
|
||||
}
|
||||
|
||||
bool CheckDeviceMetadataListContainTestDevice(
|
||||
@@ -48,6 +58,7 @@ class FakeFastPairNotificationControllerObserver
|
||||
private:
|
||||
std::vector<std::string> device_metadata_name_list_;
|
||||
int on_update_device_count_ = 0;
|
||||
std::optional<CountDownLatch> latch_;
|
||||
};
|
||||
} // namespace fastpair
|
||||
} // namespace nearby
|
||||
|
||||
@@ -33,6 +33,7 @@ void FastPairNotificationController::RemoveObserver(Observer* observer) {
|
||||
|
||||
void FastPairNotificationController::NotifyShowDiscovery(
|
||||
const DeviceMetadata& device) {
|
||||
NEARBY_LOGS(INFO) << __func__;
|
||||
for (Observer* observer : observers_.GetObservers()) {
|
||||
observer->OnUpdateDevice(device);
|
||||
}
|
||||
|
||||
@@ -14,9 +14,10 @@
|
||||
|
||||
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <memory>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "protobuf-matchers/protocol-buffer-matchers.h"
|
||||
@@ -36,7 +37,11 @@ const char kDeviceName[] = "Pixel Buds Pro";
|
||||
class FastPairNotificationControllerTest : public ::testing::Test {
|
||||
protected:
|
||||
FastPairNotificationControllerTest() {
|
||||
notification_controller_.AddObserver(¬ification_controller_obsesrver_);
|
||||
notification_controller_obsesrver_ =
|
||||
std::make_unique<FakeFastPairNotificationControllerObserver>(
|
||||
std::nullopt);
|
||||
notification_controller_.AddObserver(
|
||||
notification_controller_obsesrver_.get());
|
||||
}
|
||||
|
||||
void TriggerOnUpdateDevice(DeviceMetadata& device,
|
||||
@@ -50,7 +55,8 @@ class FastPairNotificationControllerTest : public ::testing::Test {
|
||||
}
|
||||
|
||||
FastPairNotificationController notification_controller_;
|
||||
FakeFastPairNotificationControllerObserver notification_controller_obsesrver_;
|
||||
std::unique_ptr<FakeFastPairNotificationControllerObserver>
|
||||
notification_controller_obsesrver_;
|
||||
DiscoveryAction discovery_action_;
|
||||
};
|
||||
|
||||
@@ -62,9 +68,10 @@ TEST_F(FastPairNotificationControllerTest, ShowGuestDiscoveryNotification) {
|
||||
TriggerOnUpdateDevice(device_metadata, [this](DiscoveryAction action) {
|
||||
DiscoveryActionClicked(action);
|
||||
});
|
||||
|
||||
EXPECT_TRUE(notification_controller_obsesrver_
|
||||
.CheckDeviceMetadataListContainTestDevice(kDeviceName));
|
||||
EXPECT_EQ(1, notification_controller_obsesrver_.on_update_device_count());
|
||||
->CheckDeviceMetadataListContainTestDevice(kDeviceName));
|
||||
EXPECT_EQ(1, notification_controller_obsesrver_->on_update_device_count());
|
||||
notification_controller_.OnDiscoveryClicked(DiscoveryAction::kPairToDevice);
|
||||
EXPECT_EQ(DiscoveryAction::kPairToDevice, discovery_action_);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
|
||||
#include "fastpair/ui/fast_pair/fast_pair_presenter_impl.h"
|
||||
|
||||
#include <optional>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "fastpair/common/fast_pair_device.h"
|
||||
#include "fastpair/proto/fastpair_rpcs.proto.h"
|
||||
@@ -21,61 +23,79 @@
|
||||
#include "fastpair/ui/actions.h"
|
||||
#include "fastpair/ui/fast_pair/fake_fast_pair_notification_controller_observer.h"
|
||||
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
|
||||
#include "internal/platform/count_down_latch.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace fastpair {
|
||||
|
||||
constexpr absl::string_view kModelId = "000000";
|
||||
constexpr absl::string_view kAddress = "00:00:00:00:00:00";
|
||||
constexpr absl::string_view kPublicKey = "test public key";
|
||||
|
||||
namespace {
|
||||
class FastPairPresenterImplTest : public ::testing::Test {
|
||||
public:
|
||||
FastPairPresenterImplTest() {
|
||||
controller_.AddObserver(¬ification_controller_observer_);
|
||||
}
|
||||
|
||||
void OnDiscoveryAction(DiscoveryAction action) { discovery_action_ = action; }
|
||||
|
||||
protected:
|
||||
DiscoveryAction discovery_action_;
|
||||
FakeFastPairRepository repository_;
|
||||
FastPairPresenterImpl fast_pair_presenter_;
|
||||
FastPairNotificationController controller_;
|
||||
FakeFastPairNotificationControllerObserver notification_controller_observer_;
|
||||
};
|
||||
|
||||
TEST_F(FastPairPresenterImplTest, ShowDiscoveryForV1Version) {
|
||||
TEST(FastPairPresenterImplTest, ShowDiscoveryForV1Version) {
|
||||
// Setup repository with v1 version device metadata
|
||||
FakeFastPairRepository repository;
|
||||
proto::Device v1_version_device;
|
||||
repository_.SetFakeMetadata(kModelId, v1_version_device);
|
||||
repository.SetFakeMetadata(kModelId, v1_version_device);
|
||||
FastPairDevice device(kModelId, kAddress, Protocol::kFastPairInitialPairing);
|
||||
|
||||
EXPECT_EQ(0, notification_controller_observer_.on_update_device_count());
|
||||
fast_pair_presenter_.ShowDiscovery(
|
||||
device, controller_,
|
||||
[this](DiscoveryAction action) { OnDiscoveryAction(action); });
|
||||
EXPECT_EQ(1, notification_controller_observer_.on_update_device_count());
|
||||
controller_.OnDiscoveryClicked(DiscoveryAction::kPairToDevice);
|
||||
EXPECT_EQ(DiscoveryAction::kPairToDevice, discovery_action_);
|
||||
EXPECT_EQ(DeviceFastPairVersion::kV1, device.version());
|
||||
// Register FastPairNotificationControllerObserver
|
||||
auto latch_1 = std::make_optional<CountDownLatch>(1);
|
||||
FastPairNotificationController controller;
|
||||
FakeFastPairNotificationControllerObserver notification_controller_observer(
|
||||
latch_1);
|
||||
controller.AddObserver(¬ification_controller_observer);
|
||||
EXPECT_EQ(notification_controller_observer.on_update_device_count(), 0);
|
||||
|
||||
// FastPairPresenter ShowDiscovery
|
||||
CountDownLatch latch_2(1);
|
||||
FastPairPresenterImpl fast_pair_presenter;
|
||||
DiscoveryAction discovery_action = DiscoveryAction::kUnknown;
|
||||
fast_pair_presenter.ShowDiscovery(device, controller,
|
||||
[&](DiscoveryAction action) {
|
||||
discovery_action = action;
|
||||
latch_2.CountDown();
|
||||
});
|
||||
latch_1->Await();
|
||||
EXPECT_EQ(notification_controller_observer.on_update_device_count(), 1);
|
||||
EXPECT_EQ(device.version(), DeviceFastPairVersion::kV1);
|
||||
controller.OnDiscoveryClicked(DiscoveryAction::kPairToDevice);
|
||||
latch_2.Await();
|
||||
EXPECT_EQ(discovery_action, DiscoveryAction::kPairToDevice);
|
||||
}
|
||||
|
||||
TEST_F(FastPairPresenterImplTest, ShowDiscoveryForHigherThanV1Version) {
|
||||
TEST(FastPairPresenterImplTest, ShowDiscoveryForHigherThanV1Version) {
|
||||
// Setup repository with HigherThanV1Version device metadata
|
||||
FakeFastPairRepository repository;
|
||||
proto::Device higher_than_v1_version_device;
|
||||
higher_than_v1_version_device.mutable_anti_spoofing_key_pair()
|
||||
->set_public_key(kPublicKey);
|
||||
repository_.SetFakeMetadata(kModelId, higher_than_v1_version_device);
|
||||
repository.SetFakeMetadata(kModelId, higher_than_v1_version_device);
|
||||
FastPairDevice device(kModelId, kAddress, Protocol::kFastPairInitialPairing);
|
||||
|
||||
EXPECT_EQ(0, notification_controller_observer_.on_update_device_count());
|
||||
fast_pair_presenter_.ShowDiscovery(
|
||||
device, controller_,
|
||||
[this](DiscoveryAction action) { OnDiscoveryAction(action); });
|
||||
EXPECT_EQ(1, notification_controller_observer_.on_update_device_count());
|
||||
controller_.OnDiscoveryClicked(DiscoveryAction::kPairToDevice);
|
||||
EXPECT_EQ(DiscoveryAction::kPairToDevice, discovery_action_);
|
||||
EXPECT_EQ(DeviceFastPairVersion::kHigherThanV1, device.version());
|
||||
// Register FastPairNotificationControllerObserver
|
||||
auto latch_1 = std::make_optional<CountDownLatch>(1);
|
||||
FastPairNotificationController controller;
|
||||
FakeFastPairNotificationControllerObserver notification_controller_observer(
|
||||
latch_1);
|
||||
controller.AddObserver(¬ification_controller_observer);
|
||||
EXPECT_EQ(notification_controller_observer.on_update_device_count(), 0);
|
||||
|
||||
// FastPairPresenter ShowDiscovery
|
||||
CountDownLatch latch_2(1);
|
||||
FastPairPresenterImpl fast_pair_presenter;
|
||||
DiscoveryAction discovery_action = DiscoveryAction::kUnknown;
|
||||
fast_pair_presenter.ShowDiscovery(device, controller,
|
||||
[&](DiscoveryAction action) {
|
||||
discovery_action = action;
|
||||
latch_2.CountDown();
|
||||
});
|
||||
latch_1->Await();
|
||||
EXPECT_EQ(notification_controller_observer.on_update_device_count(), 1);
|
||||
EXPECT_EQ(device.version(), DeviceFastPairVersion::kHigherThanV1);
|
||||
controller.OnDiscoveryClicked(DiscoveryAction::kDismissedByUser);
|
||||
latch_2.Await();
|
||||
EXPECT_EQ(discovery_action, DiscoveryAction::kDismissedByUser);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user