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

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: gyp-def out profile_loader* on android 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 =
75 base::FilePath::FromUTF8Unsafe("fake/profile 1");
76 base::FilePath fake_profile_path_2 =
77 base::FilePath::FromUTF8Unsafe("fake/profile 2");
78
79 TestProfileLoader loader;
80 EXPECT_FALSE(loader.IsAnyProfileLoading());
81
82 // path_1 never loads.
83 EXPECT_CALL(loader, GetProfileByPath(fake_profile_path_1))
84 .WillRepeatedly(Return(static_cast<Profile*>(NULL)));
85 EXPECT_CALL(loader, CreateProfileAsync(fake_profile_path_1, _, _, _, false))
86 .WillRepeatedly(WithArgs<0, 1>(
87 Invoke(&loader, &TestProfileLoader::SetCreateCallback)));
88
89 // path_2 loads after the first request.
90 EXPECT_CALL(loader, GetProfileByPath(fake_profile_path_2))
91 .WillOnce(Return(static_cast<Profile*>(NULL)))
92 .WillRepeatedly(Return(&profile));
93 EXPECT_CALL(loader, CreateProfileAsync(fake_profile_path_2, _, _, _, false))
94 .WillRepeatedly(WithArgs<0, 1>(
95 Invoke(&loader, &TestProfileLoader::SetCreateCallback)));
96
97 // Try to load both paths twice.
98 // path_1_load is never called because it is first invalidated by the load
99 // request for (path_2), and then invalidated manually.
100 // path_2_load is called both times.
101 StrictMock<MockCallback>* path_1_load = new StrictMock<MockCallback>();
102 StrictMock<MockCallback>* path_2_load = new StrictMock<MockCallback>();
103 EXPECT_CALL(*path_2_load, Run(&profile))
104 .Times(2);
105
106 // Try to load path_1.
107 loader.LoadProfileInvalidatingOtherLoads(
108 fake_profile_path_1, base::Bind(&MockCallback::Run, path_1_load));
109 EXPECT_TRUE(loader.IsAnyProfileLoading());
110
111 // Try to load path_2, this invalidates the previous request.
112 loader.LoadProfileInvalidatingOtherLoads(
113 fake_profile_path_2, base::Bind(&MockCallback::Run, path_2_load));
114
115 // Finish the load request for path_1, then for path_2.
116 loader.RunCreateCallback(fake_profile_path_1, &profile,
117 Profile::CREATE_STATUS_INITIALIZED);
118 loader.RunCreateCallback(fake_profile_path_2, &profile,
119 Profile::CREATE_STATUS_INITIALIZED);
120 EXPECT_FALSE(loader.IsAnyProfileLoading());
121
122 // The second request for path_2 should return immediately.
123 loader.LoadProfileInvalidatingOtherLoads(
124 fake_profile_path_2, base::Bind(&MockCallback::Run, path_2_load));
125
126 // Make a second request for path_1, and invalidate it.
127 loader.LoadProfileInvalidatingOtherLoads(
128 fake_profile_path_1, base::Bind(&MockCallback::Run, path_1_load));
129 loader.InvalidatePendingProfileLoads();
130 }
131
132 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/profiles/profile_loader.cc ('k') | chrome/browser/ui/app_list/app_list_service_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698