profile_loader_unittest.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
17d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
27d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
37d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// found in the LICENSE file.
47d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
57d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/bind.h"
67d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/files/file_path.h"
77d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
87d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
97d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/ui/app_list/profile_loader.h"
107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/ui/app_list/test/fake_profile.h"
117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/ui/app_list/test/fake_profile_store.h"
127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class ProfileLoaderUnittest : public testing::Test {
157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) public:
167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void SetUp() OVERRIDE {
177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    last_callback_result_ = NULL;
187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    profile1_.reset(
197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        new FakeProfile("p1", base::FilePath(FILE_PATH_LITERAL("profile1"))));
207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    profile2_.reset(
217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        new FakeProfile("p2", base::FilePath(FILE_PATH_LITERAL("profile2"))));
227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    profile_store_.reset(new FakeProfileStore(
247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        base::FilePath(FILE_PATH_LITERAL("udd"))));
257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    loader_.reset(new ProfileLoader(profile_store_.get()));
267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void StartLoadingProfile(Profile* profile) {
297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    loader_->LoadProfileInvalidatingOtherLoads(
303240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch        profile->GetPath(),
313240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch        base::Bind(&ProfileLoaderUnittest::OnProfileLoaded,
323240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch                  base::Unretained(this)));
333240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  }
343240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch
353240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  void FinishLoadingProfile(Profile* profile) {
363240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    profile_store_->LoadProfile(profile);
373240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  }
383240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch
393240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  void OnProfileLoaded(Profile* profile) {
403240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    last_callback_result_ = profile;
413240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  }
423240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch
433240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  bool HasKeepAlive() const {
443240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    return loader_->keep_alive_.get() != NULL;
453240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  }
463240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch
473240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch protected:
483240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  scoped_ptr<ProfileLoader> loader_;
493240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  scoped_ptr<FakeProfileStore> profile_store_;
503240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  scoped_ptr<FakeProfile> profile1_;
517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  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