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
|
// Copyright 2017 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/settings/on_startup_handler.h"
#include <string>
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/values.h"
#include "build/build_config.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#endif
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_web_ui.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const char kCallbackId[] = "test-on-startup-callback-id";
class TestOnStartupHandler : public settings::OnStartupHandler {
public:
explicit TestOnStartupHandler(Profile* profile)
: settings::OnStartupHandler(profile) {}
using settings::OnStartupHandler::set_web_ui;
};
} // namespace
namespace settings {
class OnStartupHandlerTest : public testing::Test {
public:
OnStartupHandlerTest()
: profile_manager_(TestingBrowserProcess::GetGlobal()),
profile_(nullptr) {}
void SetUp() override {
ASSERT_TRUE(profile_manager_.SetUp());
#if BUILDFLAG(IS_CHROMEOS)
auto* fake_user_manager = new ash::FakeChromeUserManager;
user_manager_enabler_ = std::make_unique<user_manager::ScopedUserManager>(
base::WrapUnique(fake_user_manager));
constexpr char kFakeEmail[] = "fake_id@gmail.com";
profile_ = profile_manager_.CreateTestingProfile(kFakeEmail);
fake_user_manager->AddUser(AccountId::FromUserEmail(kFakeEmail));
#else
profile_ = profile_manager_.CreateTestingProfile("Profile 1");
#endif
handler_ = std::make_unique<TestOnStartupHandler>(profile_);
handler_->set_web_ui(&web_ui_);
}
TestOnStartupHandler* handler() { return handler_.get(); }
Profile* profile() const { return profile_; }
content::TestWebUI* web_ui() { return &web_ui_; }
private:
content::BrowserTaskEnvironment task_environment_;
TestingProfileManager profile_manager_;
std::unique_ptr<TestOnStartupHandler> handler_;
raw_ptr<Profile> profile_;
#if BUILDFLAG(IS_CHROMEOS)
std::unique_ptr<user_manager::ScopedUserManager> user_manager_enabler_;
#endif
content::TestWebUI web_ui_;
};
TEST_F(OnStartupHandlerTest, HandleGetNtpExtension) {
base::Value::List list_args;
list_args.Append(kCallbackId);
handler()->HandleGetNtpExtension(list_args);
EXPECT_EQ(1U, web_ui()->call_data().size());
const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
EXPECT_EQ("cr.webUIResponse", data.function_name());
ASSERT_TRUE(data.arg1()->is_string());
EXPECT_EQ(kCallbackId, data.arg1()->GetString());
ASSERT_TRUE(data.arg2()->is_bool());
EXPECT_TRUE(data.arg2()->GetBool());
}
TEST_F(OnStartupHandlerTest, HandleValidateStartupPage_Valid) {
base::Value::List list_args;
list_args.Append(kCallbackId);
list_args.Append("http://example.com");
handler()->HandleValidateStartupPage(list_args);
EXPECT_EQ(1U, web_ui()->call_data().size());
const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
EXPECT_EQ("cr.webUIResponse", data.function_name());
ASSERT_TRUE(data.arg1()->is_string());
EXPECT_EQ(kCallbackId, data.arg1()->GetString());
ASSERT_TRUE(data.arg2()->is_bool());
EXPECT_TRUE(data.arg2()->GetBool());
ASSERT_TRUE(data.arg3()->is_bool());
EXPECT_TRUE(data.arg3()->GetBool());
}
TEST_F(OnStartupHandlerTest, HandleValidateStartupPage_Invalid) {
base::Value::List list_args;
list_args.Append(kCallbackId);
list_args.Append("@");
handler()->HandleValidateStartupPage(list_args);
EXPECT_EQ(1U, web_ui()->call_data().size());
const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
EXPECT_EQ("cr.webUIResponse", data.function_name());
ASSERT_TRUE(data.arg1()->is_string());
EXPECT_EQ(kCallbackId, data.arg1()->GetString());
ASSERT_TRUE(data.arg2()->is_bool());
EXPECT_TRUE(data.arg2()->GetBool());
ASSERT_TRUE(data.arg3()->is_bool());
EXPECT_FALSE(data.arg3()->GetBool());
}
} // namespace settings
|