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 164 165 166 167 168 169 170 171 172 173 174
|
// Copyright 2020 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/sync/test/integration/sync_test_utils_android.h"
#include <memory>
#include <optional>
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/token_android.h"
#include "base/functional/callback.h"
#include "base/functional/callback_forward.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/run_loop.h"
#include "base/task/thread_pool.h"
#include "base/test/bind.h"
#include "chrome/browser/android/tab_android.h"
#include "chrome/browser/ui/android/tab_model/tab_model.h"
#include "chrome/browser/ui/android/tab_model/tab_model_jni_bridge.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
#include "components/saved_tab_groups/public/android/tab_group_sync_conversions_bridge.h"
#include "components/saved_tab_groups/public/types.h"
#include "components/signin/public/identity_manager/account_capabilities_test_mutator.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/identity_test_utils.h"
#include "components/signin/public/identity_manager/signin_constants.h"
#include "google_apis/gaia/core_account_id.h"
#include "google_apis/gaia/gaia_id.h"
#include "url/android/gurl_android.h"
#include "url/gurl.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/test/sync_integration_test_support_jni_headers/SyncTestSigninUtils_jni.h"
#include "chrome/test/sync_integration_test_support_jni_headers/SyncTestTabGroupHelpers_jni.h"
namespace sync_test_utils_android {
namespace {
AccountInfo GetFakeAccountInfo(
const std::string& username,
const std::optional<std::string>& hosted_domain) {
AccountInfo account_info;
account_info.email = username;
account_info.gaia = signin::GetTestGaiaIdForEmail(username);
account_info.account_id = CoreAccountId::FromGaiaId(account_info.gaia);
account_info =
signin::WithGeneratedUserInfo(account_info, /*given_name=*/"Fake");
account_info.hosted_domain =
hosted_domain.value_or(signin::constants::kNoHostedDomainFound);
bool managed = false;
if (hosted_domain.has_value() && !hosted_domain.value().empty()) {
managed = hosted_domain.value() !=
signin::constants::kNoHostedDomainFound;
}
AccountCapabilitiesTestMutator(&account_info.capabilities)
.set_is_subject_to_enterprise_policies(managed);
return account_info;
}
} // namespace
void SetUpFakeAccountAndSignInForTesting(
const std::string& username,
const std::optional<std::string>& hosted_domain,
signin::ConsentLevel consent_level) {
base::RunLoop run_loop;
base::ThreadPool::PostTask(
FROM_HERE, {base::MayBlock()}, base::BindLambdaForTesting([&]() {
Java_SyncTestSigninUtils_setUpAccountAndSignInForTesting(
base::android::AttachCurrentThread(),
GetFakeAccountInfo(username, hosted_domain),
static_cast<int>(consent_level));
run_loop.Quit();
}));
run_loop.Run();
}
void SignOutForTesting() {
base::RunLoop run_loop;
base::ThreadPool::PostTask(FROM_HERE, {base::MayBlock()},
base::BindLambdaForTesting([&]() {
Java_SyncTestSigninUtils_signOutForTesting(
base::android::AttachCurrentThread());
run_loop.Quit();
}));
run_loop.Run();
}
void SetUpFakeAuthForTesting() {
Java_SyncTestSigninUtils_setUpFakeAuthForTesting(
base::android::AttachCurrentThread());
}
void TearDownFakeAuthForTesting() {
base::RunLoop run_loop;
base::ThreadPool::PostTask(
FROM_HERE, {base::MayBlock()}, base::BindLambdaForTesting([&]() {
Java_SyncTestSigninUtils_tearDownFakeAuthForTesting(
base::android::AttachCurrentThread());
run_loop.Quit();
}));
run_loop.Run();
}
void SetUpLiveAccountAndSignInForTesting(const std::string& username,
const std::string& password,
signin::ConsentLevel consent_level) {
base::RunLoop run_loop;
base::ThreadPool::PostTask(
FROM_HERE, {base::MayBlock()}, base::BindLambdaForTesting([&]() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_SyncTestSigninUtils_setUpLiveAccountAndSignInForTesting(
env, username, password, static_cast<int>(consent_level));
run_loop.Quit();
}));
run_loop.Run();
}
void ShutdownLiveAuthForTesting() {
base::RunLoop run_loop;
// The heap instance of the callback will be deleted by
// JNI_SyncTestSigninUtils_OnShutdownComplete when shutdown is completed.
auto heap_callback =
std::make_unique<base::OnceClosure>(run_loop.QuitClosure());
Java_SyncTestSigninUtils_shutdownLiveAuthForTesting(
base::android::AttachCurrentThread(),
reinterpret_cast<intptr_t>(heap_callback.release()));
run_loop.Run();
}
tab_groups::LocalTabGroupID CreateGroupFromTab(TabAndroid* tab) {
CHECK(tab);
JNIEnv* env = base::android::AttachCurrentThread();
auto j_group_id = Java_SyncTestTabGroupHelpers_createGroupFromTab(
env, tab->GetJavaObject());
return base::android::TokenAndroid::FromJavaToken(env, j_group_id);
}
std::optional<tab_groups::LocalTabGroupID> GetGroupIdForTab(TabAndroid* tab) {
CHECK(tab);
JNIEnv* env = base::android::AttachCurrentThread();
auto j_group_id =
Java_SyncTestTabGroupHelpers_getGroupIdForTab(env, tab->GetJavaObject());
if (j_group_id.is_null()) {
return std::nullopt;
}
return base::android::TokenAndroid::FromJavaToken(env, j_group_id);
}
void UpdateTabGroupVisualData(TabAndroid* tab,
const std::string_view& title,
tab_groups::TabGroupColorId color) {
CHECK(tab);
JNIEnv* env = base::android::AttachCurrentThread();
auto j_title = base::android::ConvertUTF8ToJavaString(env, title);
jint j_color = static_cast<jint>(color);
Java_SyncTestTabGroupHelpers_updateGroupVisualData(env, tab->GetJavaObject(),
j_title, j_color);
}
void JNI_SyncTestSigninUtils_OnShutdownComplete(JNIEnv* env,
jlong callbackPtr) {
std::unique_ptr<base::OnceClosure> heap_callback(
reinterpret_cast<base::OnceClosure*>(callbackPtr));
std::move(*heap_callback).Run();
}
} // namespace sync_test_utils_android
|