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 161 162 163
|
// Copyright 2024 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/on_device_translation/component_manager.h"
#include <string_view>
#include "base/command_line.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/on_device_translation/constants.h"
#include "chrome/browser/on_device_translation/language_pack_util.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/component_updater/component_updater_paths.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace on_device_translation {
namespace {
// The fake path for the update check.
constexpr std::string_view kFakeUpdateCheckPath = "/fake_update_check";
} // namespace
// Tests for ComponentManager.
class ComponentManagerUpdateCheckBrowserTest : public InProcessBrowserTest {
public:
ComponentManagerUpdateCheckBrowserTest() = default;
~ComponentManagerUpdateCheckBrowserTest() override = default;
// Disallow copy and assign.
ComponentManagerUpdateCheckBrowserTest(
const ComponentManagerUpdateCheckBrowserTest&) = delete;
ComponentManagerUpdateCheckBrowserTest& operator=(
const ComponentManagerUpdateCheckBrowserTest&) = delete;
void SetUpCommandLine(base::CommandLine* command_line) override {
InProcessBrowserTest::SetUpCommandLine(command_line);
// Registers a fake component-updater check handler.
embedded_test_server()->RegisterRequestHandler(base::BindRepeating(
&ComponentManagerUpdateCheckBrowserTest::RequestHandler,
base::Unretained(this)));
ASSERT_TRUE(embedded_test_server()->Start());
// Set the component-updater check URL to the fake one.
command_line->AppendSwitchASCII(
"component-updater",
base::StrCat(
{"url-source=",
embedded_test_server()->GetURL(kFakeUpdateCheckPath).spec()}));
}
protected:
// Sets the callback to be called when an update check is requested.
void SetOnUpdateCheckRequestedCallback(
base::OnceClosure on_update_check_requested_callback) {
on_update_check_requested_callback_ =
std::move(on_update_check_requested_callback);
}
private:
// A fake update check handler that calls the callback when an update check is
// requested.
std::unique_ptr<net::test_server::HttpResponse> RequestHandler(
const net::test_server::HttpRequest& request) {
if (request.relative_url.starts_with(kFakeUpdateCheckPath) &&
on_update_check_requested_callback_) {
std::move(on_update_check_requested_callback_).Run();
}
return nullptr;
}
base::OnceClosure on_update_check_requested_callback_;
};
// Tests that the translate kit component can be registered only once.
IN_PROC_BROWSER_TEST_F(ComponentManagerUpdateCheckBrowserTest,
RegisterTranslateKitComponent) {
base::RunLoop run_loop;
SetOnUpdateCheckRequestedCallback(run_loop.QuitClosure());
EXPECT_TRUE(ComponentManager::GetInstance().RegisterTranslateKitComponent());
// Wait for the update check is requested.
run_loop.Run();
EXPECT_FALSE(ComponentManager::GetInstance().RegisterTranslateKitComponent());
}
// Tests that the translate kit language pack component can be registered and
// unregistered.
IN_PROC_BROWSER_TEST_F(ComponentManagerUpdateCheckBrowserTest,
RegisterAndUnregisterTranslateKitLanguagePackComponent) {
base::RunLoop run_loop;
SetOnUpdateCheckRequestedCallback(run_loop.QuitClosure());
ComponentManager::GetInstance().RegisterTranslateKitLanguagePackComponent(
LanguagePackKey::kEn_Ja);
// Wait for the update check is requested.
run_loop.Run();
EXPECT_TRUE(
g_browser_process->local_state()->GetBoolean(GetRegisteredFlagPrefName(
*kLanguagePackComponentConfigMap.at(LanguagePackKey::kEn_Ja))));
ComponentManager::GetInstance().UninstallTranslateKitLanguagePackComponent(
LanguagePackKey::kEn_Ja);
EXPECT_FALSE(
g_browser_process->local_state()->GetBoolean(GetRegisteredFlagPrefName(
*kLanguagePackComponentConfigMap.at(LanguagePackKey::kEn_Ja))));
}
using ComponentManagerBrowserTest = InProcessBrowserTest;
// Tests that the translate kit component path is returned correctly.
IN_PROC_BROWSER_TEST_F(ComponentManagerBrowserTest,
GetTranslateKitComponentPath) {
base::FilePath components_dir;
base::PathService::Get(component_updater::DIR_COMPONENT_USER,
&components_dir);
EXPECT_EQ(ComponentManager::GetInstance().GetTranslateKitComponentPath(),
components_dir.Append(kTranslateKitBinaryInstallationRelativeDir));
}
class ComponentManagerCustomComponentPathBrowserTest
: public InProcessBrowserTest {
public:
ComponentManagerCustomComponentPathBrowserTest() {
CHECK(tmp_dir_.CreateUniqueTempDir());
}
~ComponentManagerCustomComponentPathBrowserTest() override = default;
// Disallow copy and assign.
ComponentManagerCustomComponentPathBrowserTest(
const ComponentManagerCustomComponentPathBrowserTest&) = delete;
ComponentManagerCustomComponentPathBrowserTest& operator=(
const ComponentManagerCustomComponentPathBrowserTest&) = delete;
void SetUpCommandLine(base::CommandLine* command_line) override {
InProcessBrowserTest::SetUpCommandLine(command_line);
command_line->AppendSwitchPath("translate-kit-binary-path",
GetTempDir().AppendASCII("fake.so"));
}
protected:
const base::FilePath& GetTempDir() { return tmp_dir_.GetPath(); }
private:
base::ScopedTempDir tmp_dir_;
};
// Tests that the translate kit component path is returned correctly when the
// path is set by the command line flag.
IN_PROC_BROWSER_TEST_F(ComponentManagerCustomComponentPathBrowserTest,
GetTranslateKitComponentPath) {
EXPECT_EQ(ComponentManager::GetInstance().GetTranslateKitComponentPath(),
GetTempDir().AppendASCII("fake.so"));
}
} // namespace on_device_translation
|