Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2012 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 #ifndef CHROME_TEST_BASE_TESTING_PROFILE_MANAGER_H_
      6 #define CHROME_TEST_BASE_TESTING_PROFILE_MANAGER_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "base/compiler_specific.h"
     12 #include "base/files/file_path.h"
     13 #include "base/files/scoped_temp_dir.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/strings/string16.h"
     16 #include "chrome/test/base/scoped_testing_local_state.h"
     17 
     18 class PrefServiceSyncable;
     19 class ProfileInfoCache;
     20 class ProfileManager;
     21 class TestingBrowserProcess;
     22 class TestingProfile;
     23 
     24 // The TestingProfileManager is a TestingProfile factory for a multi-profile
     25 // environment. It will bring up a full ProfileManager and attach it to the
     26 // TestingBrowserProcess set up in your test.
     27 //
     28 // When a Profile is needed for testing, create it through the factory method
     29 // below instead of creating it via |new TestingProfile|. It is not possible
     30 // to register profiles created in that fashion with the ProfileManager.
     31 class TestingProfileManager {
     32  public:
     33   explicit TestingProfileManager(TestingBrowserProcess* browser_process);
     34   ~TestingProfileManager();
     35 
     36   // This needs to be called in testing::Test::SetUp() to put the object in a
     37   // valid state. Some work cannot be done in a constructor because it may
     38   // call gtest asserts to verify setup. The result of this call can be used
     39   // to ASSERT before doing more SetUp work in the test.
     40   bool SetUp() WARN_UNUSED_RESULT;
     41 
     42   // Creates a new TestingProfile whose data lives in a directory related to
     43   // profile_name, which is a non-user-visible key for the test environment.
     44   // |prefs| is the PrefService used by the profile. If it is NULL, the profile
     45   // creates a PrefService on demand.
     46   // |user_name|, |avatar_id| and |managed_user_id| are passed along to the
     47   // ProfileInfoCache and provide the user-visible profile metadata. This will
     48   // register the TestingProfile with the profile subsystem as well. The
     49   // subsystem owns the Profile and returns a weak pointer.
     50   TestingProfile* CreateTestingProfile(const std::string& profile_name,
     51                                        scoped_ptr<PrefServiceSyncable> prefs,
     52                                        const string16& user_name,
     53                                        int avatar_id,
     54                                        const std::string& managed_user_id);
     55 
     56   // Small helper for creating testing profiles. Just forwards to above.
     57   TestingProfile* CreateTestingProfile(const std::string& name);
     58 
     59   // Deletes a TestingProfile from the profile subsystem.
     60   void DeleteTestingProfile(const std::string& profile_name);
     61 
     62   // Deletes the cache instance. This is useful for testing that the cache is
     63   // properly persisting data.
     64   void DeleteProfileInfoCache();
     65 
     66   // Sets ProfileManager's logged_in state. This is only useful on ChromeOS.
     67   void SetLoggedIn(bool logged_in);
     68 
     69   // Helper accessors.
     70   const base::FilePath& profiles_dir();
     71   ProfileManager* profile_manager();
     72   ProfileInfoCache* profile_info_cache();
     73 
     74  private:
     75   typedef std::map<std::string, TestingProfile*> TestingProfilesMap;
     76 
     77   // Does the actual ASSERT-checked SetUp work. This function cannot have a
     78   // return value, so it sets the |called_set_up_| flag on success and that is
     79   // returned in the public SetUp.
     80   void SetUpInternal();
     81 
     82   // Whether SetUp() was called to put the object in a valid state.
     83   bool called_set_up_;
     84 
     85   // The directory in which new profiles are placed.
     86   base::ScopedTempDir profiles_dir_;
     87 
     88   // Weak reference to the browser process on which the ProfileManager is set.
     89   TestingBrowserProcess* browser_process_;
     90 
     91   // Local state in which all the profiles are registered.
     92   ScopedTestingLocalState local_state_;
     93 
     94   // Weak reference to the profile manager.
     95   ProfileManager* profile_manager_;
     96 
     97   // Map of profile_name to TestingProfile* from CreateTestingProfile().
     98   TestingProfilesMap testing_profiles_;
     99 
    100   DISALLOW_COPY_AND_ASSIGN(TestingProfileManager);
    101 };
    102 
    103 #endif  // CHROME_TEST_BASE_TESTING_PROFILE_MANAGER_H_
    104