Commit cbf35a55 authored by Himanshu Jaju's avatar Himanshu Jaju Committed by Commit Bot

Integration test for more than one device available

Bug: 982229
Change-Id: Ibb2303765ebcffca2d572d89b0a9508ec87e34e4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1731896
Commit-Queue: Himanshu Jaju <himanshujaju@chromium.org>
Reviewed-by: default avatarRichard Knoll <knollr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#683948}
parent 89b60297
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/test/bind_test_util.h" #include "base/test/bind_test_util.h"
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
...@@ -15,6 +16,7 @@ ...@@ -15,6 +16,7 @@
#include "chrome/app/chrome_command_ids.h" #include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/gcm/gcm_profile_service_factory.h" #include "chrome/browser/gcm/gcm_profile_service_factory.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h" #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
#include "chrome/browser/sharing/click_to_call/click_to_call_constants.h"
#include "chrome/browser/sharing/click_to_call/feature.h" #include "chrome/browser/sharing/click_to_call/feature.h"
#include "chrome/browser/sharing/features.h" #include "chrome/browser/sharing/features.h"
#include "chrome/browser/sharing/sharing_device_info.h" #include "chrome/browser/sharing/sharing_device_info.h"
...@@ -22,10 +24,13 @@ ...@@ -22,10 +24,13 @@
#include "chrome/browser/sharing/sharing_service.h" #include "chrome/browser/sharing/sharing_service.h"
#include "chrome/browser/sharing/sharing_service_factory.h" #include "chrome/browser/sharing/sharing_service_factory.h"
#include "chrome/browser/sharing/sharing_sync_preference.h" #include "chrome/browser/sharing/sharing_sync_preference.h"
#include "chrome/browser/sync/device_info_sync_service_factory.h"
#include "chrome/browser/sync/test/integration/sync_test.h" #include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "components/gcm_driver/fake_gcm_profile_service.h" #include "components/gcm_driver/fake_gcm_profile_service.h"
#include "components/sync/driver/profile_sync_service.h" #include "components/sync/driver/profile_sync_service.h"
#include "components/sync_device_info/device_info_sync_service.h"
#include "components/sync_device_info/fake_device_info_tracker.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace { namespace {
...@@ -66,19 +71,43 @@ class ClickToCallBrowserTest : public SyncTest { ...@@ -66,19 +71,43 @@ class ClickToCallBrowserTest : public SyncTest {
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
SharingService* service = SharingService* service =
SharingServiceFactory::GetForBrowserContext(GetProfile(i)); SharingServiceFactory::GetForBrowserContext(GetProfile(i));
service->SetDeviceInfoTrackerForTesting(&fake_device_info_tracker_);
base::RunLoop run_loop; base::RunLoop run_loop;
service->RegisterDeviceInTesting( service->RegisterDeviceInTesting(
std::string("TEST"),
static_cast<int>(SharingDeviceCapability::kTelephony), static_cast<int>(SharingDeviceCapability::kTelephony),
base::BindLambdaForTesting([&](SharingDeviceRegistrationResult r) { base::BindLambdaForTesting([&](SharingDeviceRegistrationResult r) {
ASSERT_EQ(SharingDeviceRegistrationResult::kSuccess, r); ASSERT_EQ(SharingDeviceRegistrationResult::kSuccess, r);
run_loop.Quit(); run_loop.Quit();
})); }));
run_loop.Run(); run_loop.Run();
AwaitQuiescence();
}
syncer::DeviceInfoTracker* original_device_info_tracker =
DeviceInfoSyncServiceFactory::GetForProfile(GetProfile(0))
->GetDeviceInfoTracker();
std::vector<std::unique_ptr<syncer::DeviceInfo>> original_devices =
original_device_info_tracker->GetAllDeviceInfo();
int device_id = 0;
for (auto& device : original_devices) {
std::unique_ptr<syncer::DeviceInfo> fake_device =
std::make_unique<syncer::DeviceInfo>(
device->guid(),
base::StrCat(
{"testing_device_", base::NumberToString(device_id++)}),
device->chrome_version(), device->sync_user_agent(),
device->device_type(), device->signin_scoped_device_id(),
device->last_updated_timestamp(),
device->send_tab_to_self_receiving_enabled());
fake_device_info_tracker_.Add(fake_device.get());
device_infos_.push_back(std::move(fake_device));
} }
} }
// TODO(himanshujaju): try to move to static method in
// render_view_context_menu_test_util.cc
std::unique_ptr<TestRenderViewContextMenu> InitRightClickMenu( std::unique_ptr<TestRenderViewContextMenu> InitRightClickMenu(
const GURL& url, const GURL& url,
const base::string16& link_text) { const base::string16& link_text) {
...@@ -118,6 +147,8 @@ class ClickToCallBrowserTest : public SyncTest { ...@@ -118,6 +147,8 @@ class ClickToCallBrowserTest : public SyncTest {
base::test::ScopedFeatureList scoped_feature_list_; base::test::ScopedFeatureList scoped_feature_list_;
gcm::FakeGCMProfileService* gcm_service_; gcm::FakeGCMProfileService* gcm_service_;
content::WebContents* web_contents_; content::WebContents* web_contents_;
syncer::FakeDeviceInfoTracker fake_device_info_tracker_;
std::vector<std::unique_ptr<syncer::DeviceInfo>> device_infos_;
DISALLOW_COPY_AND_ASSIGN(ClickToCallBrowserTest); DISALLOW_COPY_AND_ASSIGN(ClickToCallBrowserTest);
}; };
...@@ -125,13 +156,14 @@ class ClickToCallBrowserTest : public SyncTest { ...@@ -125,13 +156,14 @@ class ClickToCallBrowserTest : public SyncTest {
IN_PROC_BROWSER_TEST_F(ClickToCallBrowserTest, IN_PROC_BROWSER_TEST_F(ClickToCallBrowserTest,
ContextMenu_SingleDeviceAvailable) { ContextMenu_SingleDeviceAvailable) {
SetUpDevices(/*count=*/1); SetUpDevices(/*count=*/1);
AwaitQuiescence();
SharingService* sharing_service = SharingService* sharing_service =
SharingServiceFactory::GetForBrowserContext(GetProfile(0)); SharingServiceFactory::GetForBrowserContext(GetProfile(0));
auto devices = sharing_service->GetDeviceCandidates( auto devices = sharing_service->GetDeviceCandidates(
static_cast<int>(SharingDeviceCapability::kTelephony)); static_cast<int>(SharingDeviceCapability::kTelephony));
ASSERT_EQ(1u, devices.size());
std::unique_ptr<TestRenderViewContextMenu> menu = std::unique_ptr<TestRenderViewContextMenu> menu =
InitRightClickMenu(GURL(kTelUrl), base::ASCIIToUTF16("Google")); InitRightClickMenu(GURL(kTelUrl), base::ASCIIToUTF16("Google"));
...@@ -170,7 +202,6 @@ IN_PROC_BROWSER_TEST_F(ClickToCallBrowserTest, ContextMenu_NoDevicesAvailable) { ...@@ -170,7 +202,6 @@ IN_PROC_BROWSER_TEST_F(ClickToCallBrowserTest, ContextMenu_NoDevicesAvailable) {
IN_PROC_BROWSER_TEST_F(ClickToCallBrowserTest, IN_PROC_BROWSER_TEST_F(ClickToCallBrowserTest,
ContextMenu_DevicesAvailable_SyncTurnedOff) { ContextMenu_DevicesAvailable_SyncTurnedOff) {
SetUpDevices(/*count=*/1); SetUpDevices(/*count=*/1);
AwaitQuiescence();
GetSyncService(0)->GetUserSettings()->SetSyncRequested(false); GetSyncService(0)->GetUserSettings()->SetSyncRequested(false);
std::unique_ptr<TestRenderViewContextMenu> menu = std::unique_ptr<TestRenderViewContextMenu> menu =
...@@ -180,3 +211,48 @@ IN_PROC_BROWSER_TEST_F(ClickToCallBrowserTest, ...@@ -180,3 +211,48 @@ IN_PROC_BROWSER_TEST_F(ClickToCallBrowserTest,
EXPECT_FALSE(menu->IsItemPresent( EXPECT_FALSE(menu->IsItemPresent(
IDC_CONTENT_CONTEXT_SHARING_CLICK_TO_CALL_MULTIPLE_DEVICES)); IDC_CONTENT_CONTEXT_SHARING_CLICK_TO_CALL_MULTIPLE_DEVICES));
} }
IN_PROC_BROWSER_TEST_F(ClickToCallBrowserTest,
ContextMenu_MultipleDevicesAvailable) {
SetUpDevices(/*count=*/2);
SharingService* sharing_service =
SharingServiceFactory::GetForBrowserContext(GetProfile(0));
auto devices = sharing_service->GetDeviceCandidates(
static_cast<int>(SharingDeviceCapability::kTelephony));
ASSERT_EQ(2u, devices.size());
std::unique_ptr<TestRenderViewContextMenu> menu =
InitRightClickMenu(GURL(kTelUrl), base::ASCIIToUTF16("Google"));
EXPECT_FALSE(menu->IsItemPresent(
IDC_CONTENT_CONTEXT_SHARING_CLICK_TO_CALL_SINGLE_DEVICE));
ASSERT_TRUE(menu->IsItemPresent(
IDC_CONTENT_CONTEXT_SHARING_CLICK_TO_CALL_MULTIPLE_DEVICES));
ui::MenuModel* sub_menu_model = nullptr;
int device_id = -1;
ASSERT_TRUE(menu->GetMenuModelAndItemIndex(kSubMenuFirstDeviceCommandId,
&sub_menu_model, &device_id));
EXPECT_EQ(2, sub_menu_model->GetItemCount());
EXPECT_EQ(0, device_id);
// Todo(himanshujaju): Modularize the checks for gcm message
for (auto& device : devices) {
EXPECT_EQ(kSubMenuFirstDeviceCommandId + device_id,
sub_menu_model->GetCommandIdAt(device_id));
sub_menu_model->ActivatedAt(device_id);
std::string fcm_token;
GetDeviceFCMToken(sharing_service, device.guid(), &fcm_token);
EXPECT_EQ(fcm_token, GetGCMService()->last_receiver_id());
chrome_browser_sharing::SharingMessage sharing_message;
sharing_message.ParseFromString(
GetGCMService()->last_web_push_message().payload);
ASSERT_TRUE(sharing_message.has_click_to_call_message());
EXPECT_EQ(GURL(kTelUrl).GetContent(),
sharing_message.click_to_call_message().phone_number());
device_id++;
}
}
...@@ -127,6 +127,8 @@ std::vector<SharingDeviceInfo> SharingService::GetDeviceCandidates( ...@@ -127,6 +127,8 @@ std::vector<SharingDeviceInfo> SharingService::GetDeviceCandidates(
std::unordered_set<std::string> device_names; std::unordered_set<std::string> device_names;
std::vector<SharingDeviceInfo> device_candidates; std::vector<SharingDeviceInfo> device_candidates;
const syncer::DeviceInfo* local_device_info =
local_device_info_provider_->GetLocalDeviceInfo();
for (const auto& device : all_devices) { for (const auto& device : all_devices) {
// If the current device is considered expired for our purposes, stop here // If the current device is considered expired for our purposes, stop here
// since the next devices in the vector are at least as expired than this // since the next devices in the vector are at least as expired than this
...@@ -134,7 +136,8 @@ std::vector<SharingDeviceInfo> SharingService::GetDeviceCandidates( ...@@ -134,7 +136,8 @@ std::vector<SharingDeviceInfo> SharingService::GetDeviceCandidates(
if (device->last_updated_timestamp() < min_updated_time) if (device->last_updated_timestamp() < min_updated_time)
break; break;
if (GetDeviceName() == device->client_name()) { if (local_device_info &&
(local_device_info->client_name() == device->client_name())) {
continue; continue;
} }
...@@ -184,6 +187,11 @@ void SharingService::SendMessageToDevice( ...@@ -184,6 +187,11 @@ void SharingService::SendMessageToDevice(
message_guid)); message_guid));
} }
void SharingService::SetDeviceInfoTrackerForTesting(
syncer::DeviceInfoTracker* tracker) {
device_info_tracker_ = tracker;
}
void SharingService::OnMessageSent(base::TimeTicks start_time, void SharingService::OnMessageSent(base::TimeTicks start_time,
const std::string& message_guid, const std::string& message_guid,
base::Optional<std::string> message_id) { base::Optional<std::string> message_id) {
...@@ -266,10 +274,8 @@ void SharingService::RegisterDevice() { ...@@ -266,10 +274,8 @@ void SharingService::RegisterDevice() {
} }
void SharingService::RegisterDeviceInTesting( void SharingService::RegisterDeviceInTesting(
std::string device_name,
int capabilities, int capabilities,
SharingDeviceRegistration::RegistrationCallback callback) { SharingDeviceRegistration::RegistrationCallback callback) {
local_device_name_for_tests_ = std::move(device_name);
sharing_device_registration_->SetDeviceCapabilityForTesting(capabilities); sharing_device_registration_->SetDeviceCapabilityForTesting(capabilities);
sharing_device_registration_->RegisterDevice(std::move(callback)); sharing_device_registration_->RegisterDevice(std::move(callback));
} }
...@@ -376,15 +382,3 @@ bool SharingService::IsSyncDisabled() const { ...@@ -376,15 +382,3 @@ bool SharingService::IsSyncDisabled() const {
syncer::SyncService::TransportState::ACTIVE && syncer::SyncService::TransportState::ACTIVE &&
!sync_service_->GetActiveDataTypes().Has(syncer::PREFERENCES))); !sync_service_->GetActiveDataTypes().Has(syncer::PREFERENCES)));
} }
base::Optional<std::string> SharingService::GetDeviceName() const {
if (local_device_name_for_tests_)
return local_device_name_for_tests_;
const syncer::DeviceInfo* local_device_info =
local_device_info_provider_->GetLocalDeviceInfo();
if (local_device_info)
return local_device_info->client_name();
else
return base::nullopt;
}
...@@ -98,14 +98,16 @@ class SharingService : public KeyedService, ...@@ -98,14 +98,16 @@ class SharingService : public KeyedService,
// Returns the current state of SharingService. // Returns the current state of SharingService.
virtual State GetState() const; virtual State GetState() const;
// For testing // Used to register devices with required capabilities in tests.
void RegisterDeviceInTesting( void RegisterDeviceInTesting(
std::string device_name,
int capabilities, int capabilities,
SharingDeviceRegistration::RegistrationCallback callback); SharingDeviceRegistration::RegistrationCallback callback);
SharingSyncPreference* GetSyncPreferences() const; SharingSyncPreference* GetSyncPreferences() const;
// Used to fake client names in integration tests.
void SetDeviceInfoTrackerForTesting(syncer::DeviceInfoTracker* tracker);
private: private:
// Overrides for syncer::SyncServiceObserver. // Overrides for syncer::SyncServiceObserver.
void OnSyncShutdown(syncer::SyncService* sync) override; void OnSyncShutdown(syncer::SyncService* sync) override;
...@@ -131,10 +133,6 @@ class SharingService : public KeyedService, ...@@ -131,10 +133,6 @@ class SharingService : public KeyedService,
// in transitioning state. // in transitioning state.
bool IsSyncDisabled() const; bool IsSyncDisabled() const;
// Returns |local_device_name_for_tests_| if set, otherwise returns the actual
// device name from |local_device_info_provider_|.
base::Optional<std::string> GetDeviceName() const;
std::unique_ptr<SharingSyncPreference> sync_prefs_; std::unique_ptr<SharingSyncPreference> sync_prefs_;
std::unique_ptr<VapidKeyManager> vapid_key_manager_; std::unique_ptr<VapidKeyManager> vapid_key_manager_;
std::unique_ptr<SharingDeviceRegistration> sharing_device_registration_; std::unique_ptr<SharingDeviceRegistration> sharing_device_registration_;
...@@ -147,7 +145,6 @@ class SharingService : public KeyedService, ...@@ -147,7 +145,6 @@ class SharingService : public KeyedService,
PingMessageHandler ping_message_handler_; PingMessageHandler ping_message_handler_;
net::BackoffEntry backoff_entry_; net::BackoffEntry backoff_entry_;
State state_; State state_;
base::Optional<std::string> local_device_name_for_tests_;
// Map of random GUID to SendMessageCallback. // Map of random GUID to SendMessageCallback.
std::map<std::string, SendMessageCallback> send_message_callbacks_; std::map<std::string, SendMessageCallback> send_message_callbacks_;
......
...@@ -669,6 +669,7 @@ if (!is_android) { ...@@ -669,6 +669,7 @@ if (!is_android) {
"//components/strings", "//components/strings",
"//components/sync", "//components/sync",
"//components/sync:test_support_model", "//components/sync:test_support_model",
"//components/sync_device_info:test_support",
"//components/translate/core/common", "//components/translate/core/common",
"//content/public/common:buildflags", "//content/public/common:buildflags",
"//content/test:test_support", "//content/test:test_support",
......
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