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 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/test/test_util.h"
#include "chrome/browser/policy/policy_test_utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/policy/policy_constants.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "third_party/blink/public/common/features_generated.h"
using on_device_translation::CreateFakeDictionaryData;
using on_device_translation::LanguagePackKey;
using on_device_translation::MockComponentManager;
using on_device_translation::TestCreateTranslator;
using on_device_translation::TestSimpleTranslationWorks;
using on_device_translation::TestTranslationAvailable;
namespace policy {
// Test for TranslatorAPIAllowed policy.
class TranslatorAPIPolicyTest : public PolicyTest {
public:
TranslatorAPIPolicyTest() {
// Need to enable the feature to test the policy.
scoped_feature_list_.InitAndEnableFeature(blink::features::kTranslationAPI);
CHECK(tmp_dir_.CreateUniqueTempDir());
}
~TranslatorAPIPolicyTest() override = default;
void SetUpOnMainThread() override {
PolicyTest::SetUpOnMainThread();
base::FilePath test_data_dir;
GetTestDataDirectory(&test_data_dir);
embedded_test_server()->ServeFilesFromDirectory(test_data_dir);
ASSERT_TRUE(embedded_test_server()->Start());
mock_component_manager_ =
std::make_unique<MockComponentManager>(GetTempDir());
// Install the mock TranslateKit component.
mock_component_manager_->InstallMockTranslateKitComponent();
// Install the mock language pack.
mock_component_manager_->InstallMockLanguagePack(
LanguagePackKey::kEn_Ja, CreateFakeDictionaryData("en", "ja"));
}
void TearDownOnMainThread() override { mock_component_manager_.reset(); }
protected:
const base::FilePath& GetTempDir() { return tmp_dir_.GetPath(); }
MockComponentManager& mock_component_manager() {
CHECK(mock_component_manager_);
return *mock_component_manager_;
}
// Sets the TranslatorAPIAllowed policy.
void SetTranslatorAPIAllowedPolicy(bool enabled) {
PolicyMap policies;
policies.Set(key::kTranslatorAPIAllowed, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(enabled),
nullptr);
UpdateProviderPolicy(policies);
}
// Navigates to an empty page.
void NavigateToEmptyPage() {
CHECK(ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL("/empty.html")));
}
private:
base::ScopedTempDir tmp_dir_;
base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<MockComponentManager> mock_component_manager_;
};
// Test that the default value of the policy is allowed.
IN_PROC_BROWSER_TEST_F(TranslatorAPIPolicyTest, DefaultAllowed) {
NavigateToEmptyPage();
TestSimpleTranslationWorks(browser(), "en", "ja");
TestTranslationAvailable(browser(), "en", "ja", "available");
}
// Test that set the policy to false will disallow the API.
IN_PROC_BROWSER_TEST_F(TranslatorAPIPolicyTest, Disallow) {
NavigateToEmptyPage();
SetTranslatorAPIAllowedPolicy(false);
TestCreateTranslator(browser(), "en", "ja",
"NotSupportedError: Unable to create translator for the "
"given source and target language.");
TestTranslationAvailable(browser(), "en", "ja", "unavailable");
}
// Test that set the policy to true will allow the API.
IN_PROC_BROWSER_TEST_F(TranslatorAPIPolicyTest, Allow) {
NavigateToEmptyPage();
SetTranslatorAPIAllowedPolicy(true);
TestSimpleTranslationWorks(browser(), "en", "ja");
TestTranslationAvailable(browser(), "en", "ja", "available");
}
// Test that the policy can be dynamically refreshed.
IN_PROC_BROWSER_TEST_F(TranslatorAPIPolicyTest,
DynamicRefreshForExistingTranslator) {
NavigateToEmptyPage();
// Create a translator.
ASSERT_EQ(EvalJs(browser()->tab_strip_model()->GetActiveWebContents(), R"(
(async () => {
try {
window._translator = await Translator.create({
sourceLanguage: 'en',
targetLanguage: 'ja',
});
return 'OK';
} catch (e) {
return e.toString();
}
})();
)")
.ExtractString(),
"OK");
// Disallow the API.
SetTranslatorAPIAllowedPolicy(false);
// The translator should not be able to translate.
ASSERT_EQ(EvalJs(browser()->tab_strip_model()->GetActiveWebContents(), R"(
(async () => {
try {
return await window._translator.translate('hello');
} catch (e) {
return e.toString();
}
})();
)")
.ExtractString(),
"UnknownError: Other generic failures occurred.");
// Allow the API.
SetTranslatorAPIAllowedPolicy(true);
// The translator should be able to translate.
ASSERT_EQ(EvalJs(browser()->tab_strip_model()->GetActiveWebContents(), R"(
(async () => {
try {
return await window._translator.translate('hello');
} catch (e) {
return e.toString();
}
})();
)")
.ExtractString(),
"en to ja: hello");
}
} // namespace policy
|