summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/login/wallpaper_manager_unittest.cc
blob: c47745631a58bfd68fef53bc63731ed414d7c342 (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <cstdlib>
#include <cstring>

#include "ash/ash_resources/grit/ash_resources.h"
#include "ash/desktop_background/desktop_background_controller.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/testing_pref_service.h"
#include "chrome/browser/chromeos/login/fake_user_manager.h"
#include "chrome/browser/chromeos/login/startup_utils.h"
#include "chrome/browser/chromeos/login/user_manager_impl.h"
#include "chrome/browser/chromeos/login/wallpaper_manager.h"
#include "chrome/browser/chromeos/settings/cros_settings.h"
#include "chrome/browser/chromeos/settings/device_settings_service.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chromeos/chromeos_switches.h"
#include "chromeos/settings/cros_settings_names.h"
#include "chromeos/settings/cros_settings_provider.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/resource/resource_bundle.h"

using namespace ash;

const char kTestUser1[] = "test-user@example.com";
const char kTestUser1Hash[] = "test-user@example.com-hash";

namespace chromeos {

class WallpaperManagerTest : public test::AshTestBase {
 public:
  WallpaperManagerTest() : command_line_(CommandLine::NO_PROGRAM) {}

  virtual ~WallpaperManagerTest() {}

  virtual void SetUp() OVERRIDE {
    test::AshTestBase::SetUp();

    // Register an in-memory local settings instance.
    local_state_.reset(new TestingPrefServiceSimple);
    TestingBrowserProcess::GetGlobal()->SetLocalState(local_state_.get());
    UserManager::RegisterPrefs(local_state_->registry());
    // Wallpaper manager and user image managers prefs will be accessed by the
    // unit-test as well.
    UserImageManager::RegisterPrefs(local_state_->registry());
    WallpaperManager::RegisterPrefs(local_state_->registry());

    StartupUtils::RegisterPrefs(local_state_->registry());
    StartupUtils::MarkDeviceRegistered();

    ResetUserManager();
  }

  virtual void TearDown() OVERRIDE {
    // Unregister the in-memory local settings instance.
    TestingBrowserProcess::GetGlobal()->SetLocalState(0);

    // Shut down the DeviceSettingsService.
    DeviceSettingsService::Get()->UnsetSessionManager();

    test::AshTestBase::TearDown();
  }

  void ResetUserManager() {
    // Reset the UserManager singleton.
    user_manager_enabler_.reset();
    // Initialize the UserManager singleton to a fresh UserManagerImpl instance.
    user_manager_enabler_.reset(
        new ScopedUserManagerEnabler(new UserManagerImpl));
  }

  void AppendGuestSwitch() {
    command_line_.AppendSwitch(switches::kGuestSession);
    WallpaperManager::Get()->set_command_line_for_testing(&command_line_);
  }

  void WaitAsyncWallpaperLoad() {
    base::MessageLoop::current()->RunUntilIdle();
  }

 protected:
  CommandLine command_line_;

  scoped_ptr<TestingPrefServiceSimple> local_state_;

  ScopedTestDeviceSettingsService test_device_settings_service_;
  ScopedTestCrosSettings test_cros_settings_;

  scoped_ptr<ScopedUserManagerEnabler> user_manager_enabler_;

 private:
  DISALLOW_COPY_AND_ASSIGN(WallpaperManagerTest);
};

// Test for crbug.com/260755. If this test fails, it is probably because the
// wallpaper of last logged in user is set as guest wallpaper.
TEST_F(WallpaperManagerTest, GuestUserUseGuestWallpaper) {
  UserManager::Get()->UserLoggedIn(kTestUser1, kTestUser1Hash, false);

  std::string relative_path =
      base::FilePath(kTestUser1Hash).Append(FILE_PATH_LITERAL("DUMMY")).value();
  // Saves wallpaper info to local state for user |kTestUser1|.
  WallpaperInfo info = {
      relative_path,
      WALLPAPER_LAYOUT_CENTER_CROPPED,
      User::CUSTOMIZED,
      base::Time::Now().LocalMidnight()
  };
  WallpaperManager::Get()->SetUserWallpaperInfo(kTestUser1, info, true);
  ResetUserManager();

  AppendGuestSwitch();
  scoped_ptr<WallpaperManager::TestApi> test_api;
  test_api.reset(new WallpaperManager::TestApi(WallpaperManager::Get()));
  // If last logged in user's wallpaper is used in function InitializeWallpaper,
  // this test will crash. InitializeWallpaper should be a noop after
  // AppendGuestSwitch being called.
  WallpaperManager::Get()->InitializeWallpaper();
  EXPECT_TRUE(test_api->current_wallpaper_path().empty());
  UserManager::Get()->UserLoggedIn(UserManager::kGuestUserName,
                                   UserManager::kGuestUserName, false);
  WaitAsyncWallpaperLoad();
  EXPECT_FALSE(ash::Shell::GetInstance()->desktop_background_controller()->
      SetDefaultWallpaper(true));
}

class WallpaperManagerCacheTest : public test::AshTestBase {
 public:
  WallpaperManagerCacheTest()
      : fake_user_manager_(new FakeUserManager()),
        scoped_user_manager_(fake_user_manager_) {
  }

 protected:
  virtual ~WallpaperManagerCacheTest() {}

  FakeUserManager* fake_user_manager() { return fake_user_manager_; }

  virtual void SetUp() OVERRIDE {
    CommandLine::ForCurrentProcess()->AppendSwitch(::switches::kMultiProfiles);
    test::AshTestBase::SetUp();
  }

  // Creates a test image of size 1x1.
  gfx::ImageSkia CreateTestImage(SkColor color) {
    SkBitmap bitmap;
    bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
    bitmap.allocPixels();
    bitmap.eraseColor(color);
    return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
  }

 private:
  FakeUserManager* fake_user_manager_;
  ScopedUserManagerEnabler scoped_user_manager_;
};

TEST_F(WallpaperManagerCacheTest, VerifyWallpaperCache) {
  // Add three users to known users.
  std::string test_user_1 = "test1@example.com";
  std::string test_user_2 = "test2@example.com";
  std::string test_user_3 = "test3@example.com";
  fake_user_manager()->AddUser(test_user_1);
  fake_user_manager()->AddUser(test_user_2);
  fake_user_manager()->AddUser(test_user_3);

  // Login two users.
  fake_user_manager()->LoginUser(test_user_1);
  fake_user_manager()->LoginUser(test_user_2);

  scoped_ptr<WallpaperManager::TestApi> test_api;
  test_api.reset(new WallpaperManager::TestApi(WallpaperManager::Get()));

  gfx::ImageSkia test_user_1_wallpaper = CreateTestImage(SK_ColorRED);
  gfx::ImageSkia test_user_2_wallpaper = CreateTestImage(SK_ColorGREEN);
  gfx::ImageSkia test_user_3_wallpaper = CreateTestImage(SK_ColorWHITE);
  test_api->SetWallpaperCache(test_user_1, test_user_1_wallpaper);
  test_api->SetWallpaperCache(test_user_2, test_user_2_wallpaper);
  test_api->SetWallpaperCache(test_user_3, test_user_3_wallpaper);

  test_api->ClearDisposableWallpaperCache();

  gfx::ImageSkia cached_wallpaper;
  EXPECT_TRUE(test_api->GetWallpaperFromCache(test_user_1, &cached_wallpaper));
  // Logged in users' wallpaper cache should be kept.
  EXPECT_TRUE(cached_wallpaper.BackedBySameObjectAs(test_user_1_wallpaper));
  EXPECT_TRUE(test_api->GetWallpaperFromCache(test_user_2, &cached_wallpaper));
  EXPECT_TRUE(cached_wallpaper.BackedBySameObjectAs(test_user_2_wallpaper));

  // Not logged in user's wallpaper cache should be cleared.
  EXPECT_FALSE(test_api->GetWallpaperFromCache(test_user_3, &cached_wallpaper));
}

}  // namespace chromeos