1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/ash/settings/pages/multidevice/multidevice_section.h"
#include <string_view>
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "base/test/scoped_feature_list.h"
#include "base/values.h"
#include "chrome/browser/ash/eche_app/eche_app_manager_factory.h"
#include "chrome/browser/ui/webui/ash/settings/search/search_tag_registry.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "chromeos/ash/components/local_search_service/public/cpp/local_search_service_proxy.h"
#include "chromeos/ash/components/multidevice/logging/logging.h"
#include "chromeos/ash/components/phonehub/fake_phone_hub_manager.h"
#include "chromeos/ash/components/phonehub/pref_names.h"
#include "chromeos/ash/services/multidevice_setup/public/cpp/fake_multidevice_setup_client.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/origin.h"
namespace ash::settings {
class MockWebUIDataSource : public content::WebUIDataSource {
public:
MockWebUIDataSource() = default;
~MockWebUIDataSource() override = default;
MockWebUIDataSource(const MockWebUIDataSource&) = delete;
MockWebUIDataSource& operator=(const MockWebUIDataSource&) = delete;
// WebUIDataSource:
MOCK_METHOD(void,
AddBoolean,
(std::string_view name, bool value),
(override));
void AddString(std::string_view name, std::u16string_view value) override {}
void AddString(std::string_view name, std::string_view value) override {}
void AddLocalizedString(std::string_view name, int ids) override {}
void AddLocalizedStrings(
base::span<const webui::LocalizedString> strings) override {}
void AddLocalizedStrings(
const base::Value::Dict& localized_strings) override {}
void AddInteger(std::string_view name, int32_t value) override {}
void AddDouble(std::string_view name, double value) override {}
void UseStringsJs() override {}
void AddResourcePath(std::string_view path, int resource_id) override {}
void AddResourcePaths(base::span<const webui::ResourcePath> paths) override {}
void SetDefaultResource(int resource_id) override {}
void SetRequestFilter(const WebUIDataSource::ShouldHandleRequestCallback&
should_handle_request_callback,
const WebUIDataSource::HandleRequestCallback&
handle_request_callback) override {}
void OverrideContentSecurityPolicy(network::mojom::CSPDirectiveName directive,
const std::string& value) override {}
void OverrideCrossOriginOpenerPolicy(const std::string& value) override {}
void OverrideCrossOriginEmbedderPolicy(const std::string& value) override {}
void OverrideCrossOriginResourcePolicy(const std::string& value) override {}
void DisableTrustedTypesCSP() override {}
void DisableDenyXFrameOptions() override {}
void EnableReplaceI18nInJS() override {}
std::string GetSource() override { return ""; }
url::Origin GetOrigin() override { return url::Origin(); }
void AddFrameAncestor(const GURL& frame_ancestor) override {}
void SetSupportedScheme(std::string_view scheme) override {}
};
class MultiDeviceSectionTest : public testing::Test {
protected:
MultiDeviceSectionTest()
: profile_manager_(TestingBrowserProcess::GetGlobal()) {
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/{features::kEcheSWA},
/*disabled_features=*/{});
}
MultiDeviceSectionTest(const MultiDeviceSectionTest&) = delete;
MultiDeviceSectionTest& operator=(const MultiDeviceSectionTest&) = delete;
~MultiDeviceSectionTest() override = default;
// testing::Test:
void SetUp() override {
ASSERT_TRUE(profile_manager_.SetUp());
pref_service_.registry()->RegisterBooleanPref(prefs::kEnableAutoScreenLock,
false);
pref_service_.registry()->RegisterIntegerPref(
phonehub::prefs::kScreenLockStatus,
static_cast<int>(phonehub::ScreenLockManager::LockStatus::kLockedOn));
mock_web_ui_data_source_ = std::make_unique<MockWebUIDataSource>();
service_proxy_ =
std::make_unique<local_search_service::LocalSearchServiceProxy>(
/*for_testing=*/true);
search_tag_registry_ =
std::make_unique<SearchTagRegistry>(service_proxy_.get());
TestingProfile* profile =
profile_manager_.CreateTestingProfile("TestingProfile");
fake_multidevice_setup_client_ =
std::make_unique<multidevice_setup::FakeMultiDeviceSetupClient>();
fake_phone_hub_manager_ = std::make_unique<phonehub::FakePhoneHubManager>();
multi_device_section_ = std::make_unique<MultiDeviceSection>(
profile, search_tag_registry_.get(),
fake_multidevice_setup_client_.get(), fake_phone_hub_manager_.get(),
&pref_service_,
eche_app::EcheAppManagerFactory::GetForProfile(profile));
}
void VerifyOnEnableScreenLockChangedIsCalled() {
EXPECT_CALL(*mock_web_ui_data_source_, AddBoolean(testing::_, testing::_))
.Times(testing::AnyNumber());
multi_device_section_->AddLoadTimeData(mock_web_ui_data_source_.get());
EXPECT_CALL(
*mock_web_ui_data_source_,
AddBoolean(testing::Eq("isChromeosScreenLockEnabled"), testing::_))
.Times(1);
pref_service_.SetBoolean(prefs::kEnableAutoScreenLock, true);
}
void VerifyOnScreenLockStatusChangedIsCalled() {
EXPECT_CALL(*mock_web_ui_data_source_, AddBoolean(testing::_, testing::_))
.Times(testing::AnyNumber());
multi_device_section_->AddLoadTimeData(mock_web_ui_data_source_.get());
EXPECT_CALL(*mock_web_ui_data_source_,
AddBoolean(testing::Eq("isPhoneScreenLockEnabled"), testing::_))
.Times(1);
pref_service_.SetInteger(
phonehub::prefs::kScreenLockStatus,
static_cast<int>(phonehub::ScreenLockManager::LockStatus::kLockedOn));
}
private:
content::BrowserTaskEnvironment task_environment_;
base::test::ScopedFeatureList scoped_feature_list_;
TestingProfileManager profile_manager_;
TestingPrefServiceSimple pref_service_;
std::unique_ptr<MockWebUIDataSource> mock_web_ui_data_source_;
std::unique_ptr<local_search_service::LocalSearchServiceProxy> service_proxy_;
std::unique_ptr<SearchTagRegistry> search_tag_registry_;
std::unique_ptr<multidevice_setup::FakeMultiDeviceSetupClient>
fake_multidevice_setup_client_;
std::unique_ptr<phonehub::FakePhoneHubManager> fake_phone_hub_manager_;
std::unique_ptr<MultiDeviceSection> multi_device_section_;
};
TEST_F(MultiDeviceSectionTest, OnEnableScreenLockChanged) {
VerifyOnEnableScreenLockChangedIsCalled();
}
TEST_F(MultiDeviceSectionTest, OnScreenLockStatusChanged) {
VerifyOnScreenLockStatusChangedIsCalled();
}
} // namespace ash::settings
|