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.
4
5#include "base/bind.h"
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_profile.h"
11#include "chrome/browser/ui/app_list/test/fake_profile_store.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14class ProfileLoaderUnittest : public testing::Test {
15 public:
16  virtual void SetUp() OVERRIDE {
17    last_callback_result_ = NULL;
18    profile1_.reset(
19        new FakeProfile("p1", base::FilePath(FILE_PATH_LITERAL("profile1"))));
20    profile2_.reset(
21        new FakeProfile("p2", base::FilePath(FILE_PATH_LITERAL("profile2"))));
22
23    profile_store_.reset(new FakeProfileStore(
24        base::FilePath(FILE_PATH_LITERAL("udd"))));
25    loader_.reset(new ProfileLoader(profile_store_.get()));
26  }
27
28  void StartLoadingProfile(Profile* profile) {
29    loader_->LoadProfileInvalidatingOtherLoads(
30        profile->GetPath(),
31        base::Bind(&ProfileLoaderUnittest::OnProfileLoaded,
32                  base::Unretained(this)));
33  }
34
35  void FinishLoadingProfile(Profile* profile) {
36    profile_store_->LoadProfile(profile);
37  }
38
39  void OnProfileLoaded(Profile* profile) {
40    last_callback_result_ = profile;
41  }
42
43  bool HasKeepAlive() const {
44    return loader_->keep_alive_.get() != NULL;
45  }
46
47 protected:
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_;
53};
54
55TEST_F(ProfileLoaderUnittest, LoadASecondProfileBeforeTheFirstFinishes) {
56  EXPECT_FALSE(loader_->IsAnyProfileLoading());
57
58  StartLoadingProfile(profile1_.get());
59  EXPECT_TRUE(loader_->IsAnyProfileLoading());
60
61  StartLoadingProfile(profile2_.get());
62  FinishLoadingProfile(profile1_.get());
63  EXPECT_EQ(NULL, last_callback_result_);
64
65  FinishLoadingProfile(profile2_.get());
66  EXPECT_EQ(profile2_.get(), last_callback_result_);
67  EXPECT_FALSE(loader_->IsAnyProfileLoading());
68}
69
70TEST_F(ProfileLoaderUnittest, TestKeepsAliveWhileLoadingProfiles) {
71  EXPECT_FALSE(loader_->IsAnyProfileLoading());
72  EXPECT_FALSE(HasKeepAlive());
73
74  StartLoadingProfile(profile1_.get());
75  EXPECT_TRUE(loader_->IsAnyProfileLoading());
76  EXPECT_TRUE(HasKeepAlive());
77
78  FinishLoadingProfile(profile1_.get());
79  EXPECT_FALSE(loader_->IsAnyProfileLoading());
80  EXPECT_FALSE(HasKeepAlive());
81}
82
83TEST_F(ProfileLoaderUnittest, TestKeepsAliveWhileLoadingMultipleProfiles) {
84  EXPECT_FALSE(loader_->IsAnyProfileLoading());
85  EXPECT_FALSE(HasKeepAlive());
86
87  StartLoadingProfile(profile1_.get());
88  EXPECT_TRUE(loader_->IsAnyProfileLoading());
89  EXPECT_TRUE(HasKeepAlive());
90
91  StartLoadingProfile(profile2_.get());
92  EXPECT_TRUE(loader_->IsAnyProfileLoading());
93  EXPECT_TRUE(HasKeepAlive());
94
95  FinishLoadingProfile(profile1_.get());
96  EXPECT_TRUE(loader_->IsAnyProfileLoading());
97  EXPECT_TRUE(HasKeepAlive());
98
99  FinishLoadingProfile(profile2_.get());
100  EXPECT_FALSE(loader_->IsAnyProfileLoading());
101  EXPECT_FALSE(HasKeepAlive());
102}
103
104TEST_F(ProfileLoaderUnittest, TestInvalidatingCurrentLoad) {
105  EXPECT_FALSE(loader_->IsAnyProfileLoading());
106  EXPECT_FALSE(HasKeepAlive());
107
108  StartLoadingProfile(profile1_.get());
109  loader_->InvalidatePendingProfileLoads();
110  // The profile is still considered loading even though we will do nothing when
111  // it gets here.
112  EXPECT_TRUE(loader_->IsAnyProfileLoading());
113  EXPECT_TRUE(HasKeepAlive());
114
115  FinishLoadingProfile(profile1_.get());
116  EXPECT_EQ(NULL, last_callback_result_);
117}
118