Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(273)

Side by Side Diff: chrome/browser/profiles/profile_loader_unittest.cc

Issue 16766003: Move ProfileLoader to chrome/browser/profiles. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Take AppShimHost changes out of this CL. Add profile_loader_unittest. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 "chrome/browser/profiles/profile_loader.h"
6
7 #include <map>
8
9 #include "base/bind.h"
10 #include "base/files/file_path.h"
11 #include "base/memory/weak_ptr.h"
12 #include "chrome/browser/lifetime/application_lifetime.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/profiles/profile_loader.h"
15 #include "chrome/test/base/testing_profile.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace {
20
21 using ::testing::_;
22 using ::testing::Invoke;
23 using ::testing::Return;
24 using ::testing::StrictMock;
25 using ::testing::WithArgs;
26
27 class TestProfileLoader : public ProfileLoader {
28 public:
29 TestProfileLoader() : ProfileLoader(NULL) {}
30 virtual ~TestProfileLoader() {}
31
32 MOCK_METHOD1(GetProfileByPath, Profile*(const base::FilePath&));
33 MOCK_METHOD5(CreateProfileAsync, void(const base::FilePath&,
34 const ProfileManager::CreateCallback&,
35 const string16&,
36 const string16&,
37 bool));
38
39 void SetCreateCallback(const base::FilePath& path,
40 const ProfileManager::CreateCallback& callback) {
41 callbacks_[path] = callback;
42 }
43
44 void RunCreateCallback(const base::FilePath& path,
45 Profile* profile,
46 Profile::CreateStatus status) {
47 callbacks_[path].Run(profile, status);
48 }
49
50 private:
51 std::map<base::FilePath, ProfileManager::CreateCallback> callbacks_;
52
53 DISALLOW_COPY_AND_ASSIGN(TestProfileLoader);
54 };
55
56 class MockCallback : public base::RefCountedThreadSafe<MockCallback> {
57 public:
58 MockCallback();
59 MOCK_METHOD1(Run, void(Profile*));
60
61 protected:
62 friend class base::RefCountedThreadSafe<MockCallback>;
63 virtual ~MockCallback();
64
65 private:
66 DISALLOW_COPY_AND_ASSIGN(MockCallback);
67 };
68
69 MockCallback::MockCallback() {}
70 MockCallback::~MockCallback() {}
71
72 TEST(ProfileLoaderTest, LoadProfileInvalidatingOtherLoads) {
73 TestingProfile profile;
74 base::FilePath fake_profile_path_1("fake/profile 1");
75 base::FilePath fake_profile_path_2("fake/profile 2");
76
77 TestProfileLoader loader;
78 EXPECT_FALSE(loader.IsAnyProfileLoading());
79
80 // path_1 never loads.
81 EXPECT_CALL(loader, GetProfileByPath(fake_profile_path_1))
82 .WillRepeatedly(Return(static_cast<Profile*>(NULL)));
83 EXPECT_CALL(loader, CreateProfileAsync(fake_profile_path_1, _, _, _, false))
84 .WillRepeatedly(WithArgs<0, 1>(
85 Invoke(&loader, &TestProfileLoader::SetCreateCallback)));
86
87 // path_2 loads after the first request.
88 EXPECT_CALL(loader, GetProfileByPath(fake_profile_path_2))
89 .WillOnce(Return(static_cast<Profile*>(NULL)))
90 .WillRepeatedly(Return(&profile));
91 EXPECT_CALL(loader, CreateProfileAsync(fake_profile_path_2, _, _, _, false))
92 .WillRepeatedly(WithArgs<0, 1>(
93 Invoke(&loader, &TestProfileLoader::SetCreateCallback)));
94
95 // Try to load both paths twice.
96 // path_1_load is never called because it is first invalidated by the load
97 // request for (path_2), and then invalidated manually.
98 // path_2_load is called both times.
99 StrictMock<MockCallback>* path_1_load = new StrictMock<MockCallback>();
100 StrictMock<MockCallback>* path_2_load = new StrictMock<MockCallback>();
101 EXPECT_CALL(*path_2_load, Run(&profile))
102 .Times(2);
103
104 // Try to load path_1.
105 loader.LoadProfileInvalidatingOtherLoads(
106 fake_profile_path_1, base::Bind(&MockCallback::Run, path_1_load));
107 EXPECT_TRUE(loader.IsAnyProfileLoading());
108
109 // Try to load path_2, this invalidates the previous request.
110 loader.LoadProfileInvalidatingOtherLoads(
111 fake_profile_path_2, base::Bind(&MockCallback::Run, path_2_load));
112
113 // Finish the load request for path_1, then for path_2.
114 loader.RunCreateCallback(fake_profile_path_1, &profile,
115 Profile::CREATE_STATUS_INITIALIZED);
116 loader.RunCreateCallback(fake_profile_path_2, &profile,
117 Profile::CREATE_STATUS_INITIALIZED);
118 EXPECT_FALSE(loader.IsAnyProfileLoading());
119
120 // The second request for path_2 should return immediately.
121 loader.LoadProfileInvalidatingOtherLoads(
122 fake_profile_path_2, base::Bind(&MockCallback::Run, path_2_load));
123
124 // Make a second request for path_1, and invalidate it.
125 loader.LoadProfileInvalidatingOtherLoads(
126 fake_profile_path_1, base::Bind(&MockCallback::Run, path_1_load));
127 loader.InvalidatePendingProfileLoads();
128 }
129
130 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698