This source file includes following definitions.
- GetStoreForUser
- ComputeAverageColor
- GetAverageBackgroundColor
- fake_session_manager_client_
- GetUserPolicyBuilder
- SetUpInProcessBrowserTestFixture
- SetUpCommandLine
- SetUpOnMainThread
- TearDownOnMainThread
- OnWallpaperDataChanged
- RunUntilWallpaperChangeCount
- ConstructPolicy
- InjectPolicy
- GetUserWallpaperInfo
- IN_PROC_BROWSER_TEST_P
- IN_PROC_BROWSER_TEST_P
- IN_PROC_BROWSER_TEST_P
- IN_PROC_BROWSER_TEST_P
- IN_PROC_BROWSER_TEST_P
#include <string>
#include <vector>
#include "ash/desktop_background/desktop_background_controller.h"
#include "ash/desktop_background/desktop_background_controller_observer.h"
#include "ash/shell.h"
#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/json/json_writer.h"
#include "base/memory/scoped_ptr.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "chrome/browser/chromeos/login/login_manager_test.h"
#include "chrome/browser/chromeos/login/startup_utils.h"
#include "chrome/browser/chromeos/login/user.h"
#include "chrome/browser/chromeos/login/user_manager.h"
#include "chrome/browser/chromeos/login/wallpaper_manager.h"
#include "chrome/browser/chromeos/policy/cloud_external_data_manager_base_test_util.h"
#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chromeos.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chromeos/chromeos_paths.h"
#include "chromeos/chromeos_switches.h"
#include "chromeos/dbus/cryptohome_client.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/fake_dbus_thread_manager.h"
#include "chromeos/dbus/fake_session_manager_client.h"
#include "chromeos/dbus/session_manager_client.h"
#include "components/policy/core/common/cloud/cloud_policy_core.h"
#include "components/policy/core/common/cloud/cloud_policy_store.h"
#include "components/policy/core/common/cloud/cloud_policy_validator.h"
#include "components/policy/core/common/cloud/policy_builder.h"
#include "crypto/rsa_private_key.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "policy/proto/cloud_policy.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/image/image_skia.h"
#include "url/gurl.h"
namespace chromeos {
namespace {
const char kTestUsers[2][19] = { "test-0@example.com", "test-1@example.com" };
const char kRedImageFileName[] = "chromeos/wallpapers/red.jpg";
const char kGreenImageFileName[] = "chromeos/wallpapers/green.jpg";
const char kBlueImageFileName[] = "chromeos/wallpapers/blue.jpg";
const SkColor kRedImageColor = SkColorSetARGB(255, 199, 6, 7);
const SkColor kGreenImageColor = SkColorSetARGB(255, 38, 196, 15);
policy::CloudPolicyStore* GetStoreForUser(const User* user) {
Profile* profile = UserManager::Get()->GetProfileByUser(user);
if (!profile) {
ADD_FAILURE();
return NULL;
}
policy::UserCloudPolicyManagerChromeOS* policy_manager =
policy::UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile);
if (!policy_manager) {
ADD_FAILURE();
return NULL;
}
return policy_manager->core()->store();
}
SkColor ComputeAverageColor(const SkBitmap& bitmap) {
if (bitmap.empty() || bitmap.width() < 1 || bitmap.height() < 1) {
ADD_FAILURE() << "Empty or invalid bitmap.";
return SkColorSetARGB(0, 0, 0, 0);
}
if (bitmap.isNull()) {
ADD_FAILURE() << "Bitmap has no pixelref.";
return SkColorSetARGB(0, 0, 0, 0);
}
if (bitmap.config() == SkBitmap::kNo_Config) {
ADD_FAILURE() << "Bitmap has not been configured.";
return SkColorSetARGB(0, 0, 0, 0);
}
uint64 a = 0, r = 0, g = 0, b = 0;
bitmap.lockPixels();
for (int x = 0; x < bitmap.width(); ++x) {
for (int y = 0; y < bitmap.height(); ++y) {
const SkColor color = bitmap.getColor(x, y);
a += SkColorGetA(color);
r += SkColorGetR(color);
g += SkColorGetG(color);
b += SkColorGetB(color);
}
}
bitmap.unlockPixels();
uint64 pixel_number = bitmap.width() * bitmap.height();
return SkColorSetARGB((a + pixel_number / 2) / pixel_number,
(r + pixel_number / 2) / pixel_number,
(g + pixel_number / 2) / pixel_number,
(b + pixel_number / 2) / pixel_number);
}
SkColor GetAverageBackgroundColor() {
const gfx::ImageSkia image =
ash::Shell::GetInstance()->desktop_background_controller()->
GetWallpaper();
const gfx::ImageSkiaRep& representation = image.GetRepresentation(1.);
if (representation.is_null()) {
ADD_FAILURE() << "No image representation.";
return SkColorSetARGB(0, 0, 0, 0);
}
const SkBitmap& bitmap = representation.sk_bitmap();
return ComputeAverageColor(bitmap);
}
}
class WallpaperManagerPolicyTest
: public LoginManagerTest,
public ash::DesktopBackgroundControllerObserver,
public testing::WithParamInterface<bool> {
protected:
WallpaperManagerPolicyTest()
: LoginManagerTest(true),
wallpaper_change_count_(0),
fake_dbus_thread_manager_(new FakeDBusThreadManager),
fake_session_manager_client_(new FakeSessionManagerClient) {
fake_dbus_thread_manager_->SetFakeClients();
fake_dbus_thread_manager_->SetSessionManagerClient(
scoped_ptr<SessionManagerClient>(fake_session_manager_client_));
}
scoped_ptr<policy::UserPolicyBuilder> GetUserPolicyBuilder(
const std::string& user_id) {
scoped_ptr<policy::UserPolicyBuilder>
user_policy_builder(new policy::UserPolicyBuilder());
base::FilePath user_keys_dir;
EXPECT_TRUE(PathService::Get(DIR_USER_POLICY_KEYS, &user_keys_dir));
const std::string sanitized_user_id =
CryptohomeClient::GetStubSanitizedUsername(user_id);
const base::FilePath user_key_file =
user_keys_dir.AppendASCII(sanitized_user_id)
.AppendASCII("policy.pub");
std::vector<uint8> user_key_bits;
EXPECT_TRUE(user_policy_builder->GetSigningKey()->
ExportPublicKey(&user_key_bits));
EXPECT_TRUE(base::CreateDirectory(user_key_file.DirName()));
EXPECT_EQ(base::WriteFile(
user_key_file,
reinterpret_cast<const char*>(user_key_bits.data()),
user_key_bits.size()),
static_cast<int>(user_key_bits.size()));
user_policy_builder->policy_data().set_username(user_id);
return user_policy_builder.Pass();
}
virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
DBusThreadManager::SetInstanceForTesting(fake_dbus_thread_manager_);
LoginManagerTest::SetUpInProcessBrowserTestFixture();
ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_));
}
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
command_line->AppendSwitch(switches::kLoginManager);
command_line->AppendSwitch(switches::kForceLoginManagerInTests);
if (GetParam())
command_line->AppendSwitch(::switches::kMultiProfiles);
else
command_line->AppendSwitchASCII(switches::kLoginProfile, kTestUsers[0]);
}
virtual void SetUpOnMainThread() OVERRIDE {
LoginManagerTest::SetUpOnMainThread();
ash::Shell::GetInstance()->
desktop_background_controller()->AddObserver(this);
user_policy_builders_[0] = GetUserPolicyBuilder(kTestUsers[0]);
user_policy_builders_[1] = GetUserPolicyBuilder(kTestUsers[1]);
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
}
virtual void TearDownOnMainThread() OVERRIDE {
ash::Shell::GetInstance()->
desktop_background_controller()->RemoveObserver(this);
LoginManagerTest::TearDownOnMainThread();
}
virtual void OnWallpaperDataChanged() OVERRIDE {
++wallpaper_change_count_;
if (run_loop_)
run_loop_->Quit();
}
void RunUntilWallpaperChangeCount(int count) {
while (wallpaper_change_count_ < count) {
run_loop_.reset(new base::RunLoop);
run_loop_->Run();
}
}
std::string ConstructPolicy(const std::string& relative_path) const {
std::string image_data;
if (!base::ReadFileToString(test_data_dir_.Append(relative_path),
&image_data)) {
ADD_FAILURE();
}
std::string policy;
base::JSONWriter::Write(policy::test::ConstructExternalDataReference(
embedded_test_server()->GetURL(std::string("/") + relative_path).spec(),
image_data).get(),
&policy);
return policy;
}
void InjectPolicy(int user_number, const std::string& filename) {
ASSERT_TRUE(user_number == 0 || user_number == 1);
const std::string user_id = kTestUsers[user_number];
policy::UserPolicyBuilder* builder =
user_policy_builders_[user_number].get();
if (filename != "") {
builder->payload().
mutable_wallpaperimage()->set_value(ConstructPolicy(filename));
} else {
builder->payload().Clear();
}
builder->Build();
fake_session_manager_client_->set_user_policy(user_id, builder->GetBlob());
const User* user = UserManager::Get()->FindUser(user_id);
ASSERT_TRUE(user);
policy::CloudPolicyStore* store = GetStoreForUser(user);
ASSERT_TRUE(store);
store->Load();
ASSERT_EQ(policy::CloudPolicyStore::STATUS_OK, store->status());
ASSERT_EQ(policy::CloudPolicyValidatorBase::VALIDATION_OK,
store->validation_status());
}
void GetUserWallpaperInfo(int user_number, WallpaperInfo* wallpaper_info) {
WallpaperManager::Get()->
GetUserWallpaperInfo(kTestUsers[user_number], wallpaper_info);
}
base::FilePath test_data_dir_;
scoped_ptr<base::RunLoop> run_loop_;
int wallpaper_change_count_;
scoped_ptr<policy::UserPolicyBuilder> user_policy_builders_[2];
FakeDBusThreadManager* fake_dbus_thread_manager_;
FakeSessionManagerClient* fake_session_manager_client_;
private:
DISALLOW_COPY_AND_ASSIGN(WallpaperManagerPolicyTest);
};
IN_PROC_BROWSER_TEST_P(WallpaperManagerPolicyTest, PRE_SetResetClear) {
RegisterUser(kTestUsers[0]);
RegisterUser(kTestUsers[1]);
StartupUtils::MarkOobeCompleted();
}
IN_PROC_BROWSER_TEST_P(WallpaperManagerPolicyTest, SetResetClear) {
WallpaperInfo info;
LoginUser(kTestUsers[0]);
base::RunLoop().RunUntilIdle();
RunUntilWallpaperChangeCount(1);
const SkColor original_background_color = GetAverageBackgroundColor();
InjectPolicy(1, kBlueImageFileName);
InjectPolicy(0, kRedImageFileName);
RunUntilWallpaperChangeCount(2);
GetUserWallpaperInfo(0, &info);
ASSERT_EQ(User::POLICY, info.type);
ASSERT_EQ(kRedImageColor, GetAverageBackgroundColor());
InjectPolicy(0, kGreenImageFileName);
RunUntilWallpaperChangeCount(3);
GetUserWallpaperInfo(0, &info);
ASSERT_EQ(User::POLICY, info.type);
ASSERT_EQ(kGreenImageColor, GetAverageBackgroundColor());
InjectPolicy(0, "");
RunUntilWallpaperChangeCount(4);
GetUserWallpaperInfo(0, &info);
ASSERT_EQ(User::DEFAULT, info.type);
ASSERT_EQ(original_background_color, GetAverageBackgroundColor());
ASSERT_EQ(4, wallpaper_change_count_);
}
IN_PROC_BROWSER_TEST_P(WallpaperManagerPolicyTest,
PRE_PRE_WallpaperOnLoginScreen) {
RegisterUser(kTestUsers[0]);
StartupUtils::MarkOobeCompleted();
}
IN_PROC_BROWSER_TEST_P(WallpaperManagerPolicyTest, PRE_WallpaperOnLoginScreen) {
LoginUser(kTestUsers[0]);
RunUntilWallpaperChangeCount(1);
InjectPolicy(0, kRedImageFileName);
RunUntilWallpaperChangeCount(2);
}
IN_PROC_BROWSER_TEST_P(WallpaperManagerPolicyTest, WallpaperOnLoginScreen) {
RunUntilWallpaperChangeCount(1);
ASSERT_EQ(kRedImageColor, GetAverageBackgroundColor());
}
INSTANTIATE_TEST_CASE_P(WallpaperManagerPolicyTestInstantiation,
WallpaperManagerPolicyTest, testing::Bool());
}