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

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

Issue 2061563002: To reproduce crashes of JSONPrefStore (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bug-614753-fix2
Patch Set: Test patch. Do not commit. Created 4 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stddef.h> 5 #include <stddef.h>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 18 matching lines...) Expand all
29 29
30 #if defined(OS_CHROMEOS) 30 #if defined(OS_CHROMEOS)
31 #include "base/path_service.h" 31 #include "base/path_service.h"
32 #include "chrome/browser/chromeos/profiles/profile_helper.h" 32 #include "chrome/browser/chromeos/profiles/profile_helper.h"
33 #include "chrome/common/chrome_constants.h" 33 #include "chrome/common/chrome_constants.h"
34 #include "chrome/common/chrome_paths.h" 34 #include "chrome/common/chrome_paths.h"
35 #include "chromeos/chromeos_switches.h" 35 #include "chromeos/chromeos_switches.h"
36 #include "testing/gtest/include/gtest/gtest.h" 36 #include "testing/gtest/include/gtest/gtest.h"
37 #endif 37 #endif
38 38
39 #if defined(OS_WIN)
40 #include <windows.h>
41 #include "base/files/file_util.h"
42 #include "base/path_service.h"
43 #include "base/test/test_file_util.h"
44 #include "chrome/common/chrome_paths.h"
45 #include "chrome/common/chrome_switches.h"
46 #endif
47
39 namespace { 48 namespace {
40 49
41 const ProfileManager::CreateCallback kOnProfileSwitchDoNothing; 50 const ProfileManager::CreateCallback kOnProfileSwitchDoNothing;
42 51
43 // An observer that returns back to test code after a new profile is 52 // An observer that returns back to test code after a new profile is
44 // initialized. 53 // initialized.
45 void OnUnblockOnProfileCreation(base::RunLoop* run_loop, 54 void OnUnblockOnProfileCreation(base::RunLoop* run_loop,
46 Profile* profile, 55 Profile* profile,
47 Profile::CreateStatus status) { 56 Profile::CreateStatus status) {
48 if (status == Profile::CREATE_STATUS_INITIALIZED) 57 if (status == Profile::CREATE_STATUS_INITIALIZED)
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 501
493 EXPECT_FALSE(profile->HasOffTheRecordProfile()); 502 EXPECT_FALSE(profile->HasOffTheRecordProfile());
494 EXPECT_FALSE(profile_manager->IsValidProfile(incognito_profile)); 503 EXPECT_FALSE(profile_manager->IsValidProfile(incognito_profile));
495 EXPECT_EQ(initial_profile_count, profile_manager->GetNumberOfProfiles()); 504 EXPECT_EQ(initial_profile_count, profile_manager->GetNumberOfProfiles());
496 // After destroying the incognito profile incognito preferences should be 505 // After destroying the incognito profile incognito preferences should be
497 // cleared so the default save path should be taken from the main profile. 506 // cleared so the default save path should be taken from the main profile.
498 EXPECT_FALSE(profile->GetOffTheRecordPrefs() 507 EXPECT_FALSE(profile->GetOffTheRecordPrefs()
499 ->GetFilePath(prefs::kSaveFileDefaultDirectory) 508 ->GetFilePath(prefs::kSaveFileDefaultDirectory)
500 .empty()); 509 .empty());
501 } 510 }
511
512 #if defined(OS_WIN)
513 bool RemoveCreateDirectoryPermission(const base::FilePath& path) {
514 return base::DenyFilePermission(path, FILE_ADD_SUBDIRECTORY);
515 }
516
517 class ProfileManagerWinBrowserTest : public ProfileManagerBrowserTest {
518 public:
519 ProfileManagerWinBrowserTest() {}
520 // Tests may be reported as PASS even if the test has never been run. To
521 // mitigate the issue, a "red flag" is raised for susceptable tests. These
522 // tests need to call DropRedFlag() to pass the test.
523 void DropRedFlag() { red_flag_ = false; }
524
525 protected:
526 void SetUpCommandLine(base::CommandLine* command_line) override {
527 command_line->AppendSwitch(switches::kRestoreLastSession);
528 }
529
530 bool SetUpUserDataDirectory() override {
531 std::string testname(
532 testing::UnitTest::GetInstance()->current_test_info()->name());
533 if (testname == "LastOpenedProfileMissing") {
534 red_flag_ = true;
535
536 base::FilePath user_data_dir;
537 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
538 return false;
539
540 base::FilePath dir_to_delete = user_data_dir.AppendASCII("Profile 1");
541 return base::DirectoryExists(dir_to_delete) &&
542 DeleteFile(dir_to_delete, true) &&
543 RemoveCreateDirectoryPermission(user_data_dir);
544 } else if (testname == "SwitchToMissingProfile") {
545 red_flag_ = true;
546
547 base::FilePath user_data_dir;
548 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
549 return false;
550
551 base::FilePath dir_to_delete = user_data_dir.AppendASCII("Profile 1");
552 return base::DirectoryExists(dir_to_delete) &&
553 DeleteFile(dir_to_delete, true) &&
554 RemoveCreateDirectoryPermission(user_data_dir);
555 }
556 return true;
557 }
558
559 void TearDown() override {
560 EXPECT_FALSE(red_flag_);
561 ProfileManagerBrowserTest::TearDown();
562 }
563
564 private:
565 bool red_flag_ = false;
566 DISALLOW_COPY_AND_ASSIGN(ProfileManagerWinBrowserTest);
567 };
568
569 IN_PROC_BROWSER_TEST_F(ProfileManagerWinBrowserTest,
570 PRE_LastOpenedProfileMissing) {
571 ProfileManager* profile_manager = g_browser_process->profile_manager();
572 ASSERT_TRUE(profile_manager);
573
574 std::vector<base::FilePath> profile_paths;
575 // Create two additional profile.
576 for (int i = 0; i < 2; ++i) {
577 base::FilePath path = profile_manager->GenerateNextProfileDirectoryPath();
578 profile_paths.push_back(path);
579
580 base::RunLoop run_loop;
581 profile_manager->CreateProfileAsync(
582 path, base::Bind(&OnUnblockOnProfileCreation, &run_loop),
583 base::string16(), std::string(), std::string());
584
585 // Run the message loop to allow profile creation to take place; the loop is
586 // terminated by OnUnblockOnProfileCreation when the profile is created.
587 run_loop.Run();
588
589 profiles::SwitchToProfile(path, false, kOnProfileSwitchDoNothing,
590 ProfileMetrics::SWITCH_PROFILE_ICON);
591 }
592
593 ASSERT_EQ(base::FilePath(FILE_PATH_LITERAL("Profile 1")),
594 profile_paths[0].BaseName());
595 }
596
597 IN_PROC_BROWSER_TEST_F(ProfileManagerWinBrowserTest,
598 LastOpenedProfileMissing) {
599 DropRedFlag();
600
601 BrowserList* browser_list = BrowserList::GetInstance();
602 // Three browser windows were created in the PRE test. Since Profile 1 is
603 // being skipped, only two browser windows were created in the second run.
604 EXPECT_EQ(2u, browser_list->size());
605 for (const Browser* browser : *browser_list) {
606 EXPECT_NE(base::FilePath(FILE_PATH_LITERAL("Profile 1")),
607 browser->profile()->GetPath().BaseName());
608 }
609 }
610
611 IN_PROC_BROWSER_TEST_F(ProfileManagerWinBrowserTest,
612 PRE_SwitchToMissingProfile) {
613 ProfileManager* profile_manager = g_browser_process->profile_manager();
614 ASSERT_TRUE(profile_manager);
615
616 std::vector<base::FilePath> profile_paths;
617 // Create two additional profile.
618 for (int i = 0; i < 2; ++i) {
619 base::FilePath path = profile_manager->GenerateNextProfileDirectoryPath();
620 profile_paths.push_back(path);
621
622 base::RunLoop run_loop;
623 profile_manager->CreateProfileAsync(
624 path, base::Bind(&OnUnblockOnProfileCreation, &run_loop),
625 base::string16(), std::string(), std::string());
626
627 // Run the message loop to allow profile creation to take place; the loop is
628 // terminated by OnUnblockOnProfileCreation when the profile is created.
629 run_loop.Run();
630
631 profiles::SwitchToProfile(path, false, kOnProfileSwitchDoNothing,
632 ProfileMetrics::SWITCH_PROFILE_ICON);
633 }
634
635 ASSERT_EQ(base::FilePath(FILE_PATH_LITERAL("Profile 1")),
636 profile_paths[0].BaseName());
637 }
638
639 IN_PROC_BROWSER_TEST_F(ProfileManagerWinBrowserTest,
640 SwitchToMissingProfile) {
641 DropRedFlag();
642
643 BrowserList* browser_list = BrowserList::GetInstance();
644 // Three browser windows were created in the PRE test. Since Profile 1 is
645 // being skipped, only two browser windows were created in the second run.
646 EXPECT_EQ(2u, browser_list->size());
647 for (const Browser* browser : *browser_list) {
648 EXPECT_NE(base::FilePath(FILE_PATH_LITERAL("Profile 1")),
649 browser->profile()->GetPath().BaseName());
650 }
651
652 profiles::SwitchToProfile(g_browser_process->profile_manager()
653 ->user_data_dir().AppendASCII("Profile 1"),
654 false,
655 kOnProfileSwitchDoNothing,
656 ProfileMetrics::SWITCH_PROFILE_ICON);
657 }
658
659 #endif // defined(OS_WIN)
OLDNEW
« no previous file with comments | « chrome/browser/profiles/profile_io_data.cc ('k') | chrome/browser/safe_browsing/safe_browsing_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698