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

Unified Diff: chrome/browser/profiles/profile_attributes_storage_unittest.cc

Issue 1214483002: Improve the ProfileInfoCache API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review feedback Created 5 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/profiles/profile_attributes_storage_unittest.cc
diff --git a/chrome/browser/profiles/profile_attributes_storage_unittest.cc b/chrome/browser/profiles/profile_attributes_storage_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..441652b720a92a3bb0e925e747115c88f06fd6c9
--- /dev/null
+++ b/chrome/browser/profiles/profile_attributes_storage_unittest.cc
@@ -0,0 +1,263 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/strings/stringprintf.h"
+#include "base/strings/utf_string_conversions.h"
+#include "chrome/browser/profiles/profile_info_cache.h"
+#include "chrome/browser/profiles/profile_manager.h"
+#include "chrome/browser/supervised_user/supervised_user_constants.h"
+#include "chrome/test/base/testing_browser_process.h"
+#include "chrome/test/base/testing_profile_manager.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+// The ProfileMetadataEntry accessors aren't just plain old accessors to local
+// members so they'll be tested. The following helpers will make the testing
+// code less verbose.
+#define TEST_ACCESSORS(entry, member, first_value, second_value) \
+ TestAccessors(&entry, \
+ &decltype(entry)::Get ## member, \
+ &decltype(entry)::Set ## member, \
+ first_value, \
+ second_value);
+
+#define TEST_STRING16_ACCESSORS(entry, member) \
+ TEST_ACCESSORS(entry, member, \
+ base::ASCIIToUTF16("first_" #member "_value"), \
+ base::ASCIIToUTF16("second_" #member "_value"));
+
+#define TEST_STRING_ACCESSORS(entry, member) \
+ TEST_ACCESSORS(entry, member, \
+ std::string("first_" #member "_value"), \
+ std::string("second_" #member "_value"));
+
+#define TEST_BOOL_ACCESSORS(entry, member) \
+TestAccessors(&entry, \
+ &decltype(entry)::member, \
+ &decltype(entry)::Set ## member, \
+ false, \
+ true);
+
+template<typename TValue, typename TGetter, typename TSetter>
+void TestAccessors(ProfileAttributesEntry* entry,
+ TGetter getter_func,
+ TSetter setter_func,
+ TValue first_value,
+ TValue second_value) {
+ (entry->*setter_func)(first_value);
+ EXPECT_EQ(first_value, (entry->*getter_func)());
+ (entry->*setter_func)(second_value);
+ EXPECT_EQ(second_value, (entry->*getter_func)());
+}
+} // namespace
+
+class ProfileAttributesStorageTest : public testing::Test {
+ public:
+ ProfileAttributesStorageTest()
+ : testing_profile_manager_(TestingBrowserProcess::GetGlobal()) {}
+ ~ProfileAttributesStorageTest() override {}
+
+ protected:
+ void SetUp() override {
+ ASSERT_TRUE(testing_profile_manager_.SetUp());
+ }
+
+ void TearDown() override {
+ }
+
+ base::FilePath GetProfilePath(const std::string& base_name) {
+ return testing_profile_manager_.profile_manager()->user_data_dir().
+ AppendASCII(base_name);
+ }
+
+ ProfileAttributesStorage* storage() {
+ return testing_profile_manager_.profile_info_cache();
+ }
+
+ void AddTestingProfile() {
+ size_t number_of_profiles = storage()->GetNumberOfProfiles();
+
+ storage()->AddProfile(
+ GetProfilePath(
+ base::StringPrintf("testing_profile_path%lu", number_of_profiles)),
+ base::ASCIIToUTF16(
+ base::StringPrintf("testing_profile_name%lu", number_of_profiles)),
+ std::string(
+ base::StringPrintf("testing_profile_gaia%lu", number_of_profiles)),
+ base::ASCIIToUTF16(
+ base::StringPrintf("testing_profile_user%lu", number_of_profiles)),
+ number_of_profiles,
+ std::string(""));
+
+ EXPECT_EQ(number_of_profiles + 1, storage()->GetNumberOfProfiles());
+ }
+
+ private:
+ TestingProfileManager testing_profile_manager_;
+};
+
+TEST_F(ProfileAttributesStorageTest, ProfileNotFound) {
+ EXPECT_EQ(0U, storage()->GetNumberOfProfiles());
+
+ ProfileAttributesEntry entry;
+ ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
+ GetProfilePath("testing_profile_path0"), &entry));
+
+ AddTestingProfile();
+ EXPECT_EQ(1U, storage()->GetNumberOfProfiles());
+
+ ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
+ GetProfilePath("testing_profile_path0"), &entry));
+ ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
+ GetProfilePath("testing_profile_path1"), &entry));
+}
+
+TEST_F(ProfileAttributesStorageTest, AddProfile) {
+ EXPECT_EQ(0U, storage()->GetNumberOfProfiles());
+
+ storage()->AddProfile(GetProfilePath("new_profile_path_1"),
+ base::ASCIIToUTF16("new_profile_name_1"),
+ std::string("new_profile_gaia_1"),
+ base::ASCIIToUTF16("new_profile_username_1"),
+ 1,
+ std::string(""));
+
+ EXPECT_EQ(1U, storage()->GetNumberOfProfiles());
+
+ ProfileAttributesEntry entry;
+ ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
+ GetProfilePath("new_profile_path_1"), &entry));
+ EXPECT_EQ(base::ASCIIToUTF16("new_profile_name_1"), entry.GetName());
+}
+
+TEST_F(ProfileAttributesStorageTest, DeleteProfile) {
+ EXPECT_EQ(0U, storage()->GetNumberOfProfiles());
+
+ ProfileAttributesEntry entry;
+ ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
+ GetProfilePath("testing_profile_path0"), &entry));
+
+ AddTestingProfile();
+ EXPECT_EQ(1U, storage()->GetNumberOfProfiles());
+
+ // Deleting an existing profile should make it un-retrievable.
+ storage()->DeleteProfile(GetProfilePath("testing_profile_path0"));
+ EXPECT_EQ(0U, storage()->GetNumberOfProfiles());
+
+ ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
+ GetProfilePath("testing_profile_path1"), &entry));
+ ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
+ GetProfilePath("testing_profile_path1"), &entry));
+}
+
+TEST_F(ProfileAttributesStorageTest, MultipleProfiles) {
+ EXPECT_EQ(0U, storage()->GetNumberOfProfiles());
+
+ for (size_t i = 0; i < 5; ++i) {
+ AddTestingProfile();
+ EXPECT_EQ(i + 1, storage()->GetNumberOfProfiles());
+ std::vector<ProfileAttributesEntry> entries =
+ storage()->GetAllProfilesAttributes();
+ EXPECT_EQ(i + 1, entries.size());
+ }
+
+ EXPECT_EQ(5U, storage()->GetNumberOfProfiles());
+
+ ProfileAttributesEntry entry;
+ ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
+ GetProfilePath("testing_profile_path0"), &entry));
+ EXPECT_EQ(base::ASCIIToUTF16("testing_profile_name0"), entry.GetName());
+
+ storage()->DeleteProfile(GetProfilePath("testing_profile_path0"));
+ ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
+ GetProfilePath("testing_profile_path0"), &entry));
+ EXPECT_EQ(4U, storage()->GetNumberOfProfiles());
+
+ std::vector<ProfileAttributesEntry> entries =
+ storage()->GetAllProfilesAttributes();
+ for (auto& entry: entries) {
+ EXPECT_NE(GetProfilePath("testing_profile_path0"), entry.GetPath());
+ }
+}
+
+TEST_F(ProfileAttributesStorageTest, InitialValues) {
+ AddTestingProfile();
+
+ ProfileAttributesEntry entry;
+ ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
+ GetProfilePath("testing_profile_path0"), &entry));
+ EXPECT_EQ(GetProfilePath("testing_profile_path0"), entry.GetPath());
+ EXPECT_EQ(base::ASCIIToUTF16("testing_profile_name0"), entry.GetName());
+ EXPECT_EQ(std::string("testing_profile_gaia0"), entry.GetGAIAId());
+ EXPECT_EQ(base::ASCIIToUTF16("testing_profile_user0"), entry.GetUserName());
+ EXPECT_EQ(0U, entry.GetAvatarIconIndex());
+ EXPECT_EQ(std::string(""), entry.GetSupervisedUserId());
+}
+
+TEST_F(ProfileAttributesStorageTest, EntryAccessors) {
+ AddTestingProfile();
+
+ ProfileAttributesEntry entry;
+ ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
+ GetProfilePath("testing_profile_path0"), &entry));
+
+ EXPECT_EQ(GetProfilePath("testing_profile_path0"), entry.GetPath());
+
+ TEST_STRING16_ACCESSORS(entry, Name);
+ TEST_STRING16_ACCESSORS(entry, ShortcutName);
+ TEST_STRING_ACCESSORS(entry, LocalAuthCredentials);
+ TEST_STRING_ACCESSORS(entry, PasswordChangeDetectionToken);
+ TEST_ACCESSORS(entry, BackgroundStatus, false, true);
+ TEST_STRING16_ACCESSORS(entry, GAIAName);
+ TEST_STRING16_ACCESSORS(entry, GAIAGivenName);
+ TEST_BOOL_ACCESSORS(entry, IsUsingGAIAPicture);
+ TEST_BOOL_ACCESSORS(entry, IsOmitted);
+ TEST_BOOL_ACCESSORS(entry, IsSigninRequired);
+ TEST_STRING_ACCESSORS(entry, SupervisedUserId);
+ TEST_BOOL_ACCESSORS(entry, IsEphemeral);
+ TEST_BOOL_ACCESSORS(entry, IsUsingDefaultName);
+ TEST_BOOL_ACCESSORS(entry, IsUsingDefaultAvatar);
+ TEST_BOOL_ACCESSORS(entry, IsAuthError);
+}
+
+TEST_F(ProfileAttributesStorageTest, AuthInfo) {
+ AddTestingProfile();
+
+ ProfileAttributesEntry entry;
+ ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
+ GetProfilePath("testing_profile_path0"), &entry));
+
+ entry.SetAuthInfo("", base::string16());
+ ASSERT_FALSE(entry.IsAuthenticated());
+ EXPECT_EQ(base::string16(), entry.GetUserName());
+ EXPECT_EQ("", entry.GetGAIAId());
+
+ entry.SetAuthInfo("foo", base::ASCIIToUTF16("bar"));
+ ASSERT_TRUE(entry.IsAuthenticated());
+ EXPECT_EQ(base::ASCIIToUTF16("bar"), entry.GetUserName());
+ EXPECT_EQ("foo", entry.GetGAIAId());
+}
+
+TEST_F(ProfileAttributesStorageTest, SupervisedUsersAccessors) {
+ AddTestingProfile();
+
+ ProfileAttributesEntry entry;
+ ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
+ GetProfilePath("testing_profile_path0"), &entry));
+
+ entry.SetSupervisedUserId("");
+ ASSERT_FALSE(entry.IsSupervised());
+ ASSERT_FALSE(entry.IsChild());
+ ASSERT_FALSE(entry.IsLegacySupervised());
+
+ entry.SetSupervisedUserId("some_supervised_user_id");
+ ASSERT_TRUE(entry.IsSupervised());
+ ASSERT_FALSE(entry.IsChild());
+ ASSERT_TRUE(entry.IsLegacySupervised());
+
+ entry.SetSupervisedUserId(supervised_users::kChildAccountSUID);
+ ASSERT_TRUE(entry.IsSupervised());
+ ASSERT_TRUE(entry.IsChild());
+ ASSERT_FALSE(entry.IsLegacySupervised());
+}
Mike Lerman 2015/07/02 18:01:30 Can you add some tests around: - I get a ProfileAt
anthonyvd 2015/07/07 21:05:32 Done. Also testing deleting from the ProfileInfoCa
Mike Lerman 2015/07/08 18:26:34 Undefined contract - understood. Makes sense. I a
anthonyvd 2015/07/09 17:02:13 Good point, I added a DCHECK in ProfileInfoAttribu

Powered by Google App Engine
This is Rietveld 408576698