1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "base/files/file_path.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/app_list/profile_loader.h"
10 #include "chrome/browser/ui/app_list/test/fake_keep_alive_service.h"
11 #include "chrome/browser/ui/app_list/test/fake_profile.h"
12 #include "chrome/browser/ui/app_list/test/fake_profile_store.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 class ProfileLoaderUnittest
: public testing::Test
{
17 virtual void SetUp() OVERRIDE
{
18 last_callback_result_
= NULL
;
20 new FakeProfile("p1", base::FilePath(FILE_PATH_LITERAL("profile1"))));
22 new FakeProfile("p2", base::FilePath(FILE_PATH_LITERAL("profile2"))));
24 profile_store_
.reset(new FakeProfileStore(
25 base::FilePath(FILE_PATH_LITERAL("udd"))));
26 keep_alive_service_
= new FakeKeepAliveService
;
27 loader_
.reset(new ProfileLoader(
29 scoped_ptr
<KeepAliveService
>(keep_alive_service_
)));
32 void StartLoadingProfile(Profile
* profile
) {
33 loader_
->LoadProfileInvalidatingOtherLoads(
35 base::Bind(&ProfileLoaderUnittest::OnProfileLoaded
,
36 base::Unretained(this)));
39 void FinishLoadingProfile(Profile
* profile
) {
40 profile_store_
->LoadProfile(profile
);
43 void OnProfileLoaded(Profile
* profile
) {
44 last_callback_result_
= profile
;
47 FakeKeepAliveService
* keep_alive_service_
;
48 scoped_ptr
<ProfileLoader
> loader_
;
49 scoped_ptr
<FakeProfileStore
> profile_store_
;
50 scoped_ptr
<FakeProfile
> profile1_
;
51 scoped_ptr
<FakeProfile
> profile2_
;
52 Profile
* last_callback_result_
;
55 TEST_F(ProfileLoaderUnittest
, LoadASecondProfileBeforeTheFirstFinishes
) {
56 EXPECT_FALSE(loader_
->IsAnyProfileLoading());
58 StartLoadingProfile(profile1_
.get());
59 EXPECT_TRUE(loader_
->IsAnyProfileLoading());
61 StartLoadingProfile(profile2_
.get());
62 FinishLoadingProfile(profile1_
.get());
63 EXPECT_EQ(NULL
, last_callback_result_
);
65 FinishLoadingProfile(profile2_
.get());
66 EXPECT_EQ(profile2_
.get(), last_callback_result_
);
67 EXPECT_FALSE(loader_
->IsAnyProfileLoading());
70 TEST_F(ProfileLoaderUnittest
, TestKeepsAliveWhileLoadingProfiles
) {
71 EXPECT_FALSE(loader_
->IsAnyProfileLoading());
72 EXPECT_FALSE(keep_alive_service_
->is_keeping_alive());
74 StartLoadingProfile(profile1_
.get());
75 EXPECT_TRUE(loader_
->IsAnyProfileLoading());
76 EXPECT_TRUE(keep_alive_service_
->is_keeping_alive());
78 FinishLoadingProfile(profile1_
.get());
79 EXPECT_FALSE(loader_
->IsAnyProfileLoading());
80 EXPECT_FALSE(keep_alive_service_
->is_keeping_alive());
83 TEST_F(ProfileLoaderUnittest
, TestKeepsAliveWhileLoadingMultipleProfiles
) {
84 EXPECT_FALSE(loader_
->IsAnyProfileLoading());
85 EXPECT_FALSE(keep_alive_service_
->is_keeping_alive());
87 StartLoadingProfile(profile1_
.get());
88 EXPECT_TRUE(loader_
->IsAnyProfileLoading());
89 EXPECT_TRUE(keep_alive_service_
->is_keeping_alive());
91 StartLoadingProfile(profile2_
.get());
92 EXPECT_TRUE(loader_
->IsAnyProfileLoading());
93 EXPECT_TRUE(keep_alive_service_
->is_keeping_alive());
95 FinishLoadingProfile(profile1_
.get());
96 EXPECT_TRUE(loader_
->IsAnyProfileLoading());
97 EXPECT_TRUE(keep_alive_service_
->is_keeping_alive());
99 FinishLoadingProfile(profile2_
.get());
100 EXPECT_FALSE(loader_
->IsAnyProfileLoading());
101 EXPECT_FALSE(keep_alive_service_
->is_keeping_alive());
104 TEST_F(ProfileLoaderUnittest
, TestInvalidatingCurrentLoad
) {
105 EXPECT_FALSE(loader_
->IsAnyProfileLoading());
106 EXPECT_FALSE(keep_alive_service_
->is_keeping_alive());
108 StartLoadingProfile(profile1_
.get());
109 loader_
->InvalidatePendingProfileLoads();
110 // The profile is still considered loading even though we will do nothing when
112 EXPECT_TRUE(loader_
->IsAnyProfileLoading());
113 EXPECT_TRUE(keep_alive_service_
->is_keeping_alive());
115 FinishLoadingProfile(profile1_
.get());
116 EXPECT_EQ(NULL
, last_callback_result_
);