Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / importer / firefox_profile_lock_unittest.cc
blobc7cb642bffefe699110e9b9547c58009fcee2126
1 // Copyright (c) 2011 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.
5 #include "base/files/file_util.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/strings/string_util.h"
8 #include "build/build_config.h"
9 #include "chrome/browser/importer/firefox_profile_lock.h"
10 #include "chrome/common/chrome_paths.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 class FirefoxProfileLockTest : public testing::Test {
14 protected:
15 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
17 base::ScopedTempDir temp_dir_;
20 TEST_F(FirefoxProfileLockTest, LockTest) {
21 FirefoxProfileLock lock1(temp_dir_.path());
22 ASSERT_TRUE(lock1.HasAcquired());
23 lock1.Unlock();
24 ASSERT_FALSE(lock1.HasAcquired());
25 lock1.Lock();
26 ASSERT_TRUE(lock1.HasAcquired());
29 // Tests basic functionality and verifies that the lock file is deleted after
30 // use.
31 TEST_F(FirefoxProfileLockTest, ProfileLock) {
32 base::FilePath test_path = temp_dir_.path();
33 base::FilePath lock_file_path =
34 test_path.Append(FirefoxProfileLock::kLockFileName);
36 scoped_ptr<FirefoxProfileLock> lock;
37 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get());
38 EXPECT_FALSE(base::PathExists(lock_file_path));
39 lock.reset(new FirefoxProfileLock(test_path));
40 EXPECT_TRUE(lock->HasAcquired());
41 EXPECT_TRUE(base::PathExists(lock_file_path));
42 lock->Unlock();
43 EXPECT_FALSE(lock->HasAcquired());
45 // In the posix code, we don't delete the file when releasing the lock.
46 #if !defined(OS_POSIX)
47 EXPECT_FALSE(base::PathExists(lock_file_path));
48 #endif // !defined(OS_POSIX)
49 lock->Lock();
50 EXPECT_TRUE(lock->HasAcquired());
51 EXPECT_TRUE(base::PathExists(lock_file_path));
52 lock->Lock();
53 EXPECT_TRUE(lock->HasAcquired());
54 lock->Unlock();
55 EXPECT_FALSE(lock->HasAcquired());
56 // In the posix code, we don't delete the file when releasing the lock.
57 #if !defined(OS_POSIX)
58 EXPECT_FALSE(base::PathExists(lock_file_path));
59 #endif // !defined(OS_POSIX)
62 // If for some reason the lock file is left behind by the previous owner, we
63 // should still be able to lock it, at least in the Windows implementation.
64 TEST_F(FirefoxProfileLockTest, ProfileLockOrphaned) {
65 base::FilePath test_path = temp_dir_.path();
66 base::FilePath lock_file_path =
67 test_path.Append(FirefoxProfileLock::kLockFileName);
69 // Create the orphaned lock file.
70 FILE* lock_file = base::OpenFile(lock_file_path, "w");
71 ASSERT_TRUE(lock_file);
72 base::CloseFile(lock_file);
73 EXPECT_TRUE(base::PathExists(lock_file_path));
75 scoped_ptr<FirefoxProfileLock> lock;
76 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get());
77 lock.reset(new FirefoxProfileLock(test_path));
78 EXPECT_TRUE(lock->HasAcquired());
79 lock->Unlock();
80 EXPECT_FALSE(lock->HasAcquired());
83 // This is broken on POSIX since the same process is allowed to reacquire a
84 // lock.
85 #if !defined(OS_POSIX)
86 // Tests two locks contending for the same lock file.
87 TEST_F(FirefoxProfileLockTest, ProfileLockContention) {
88 base::FilePath test_path = temp_dir_.path();
90 scoped_ptr<FirefoxProfileLock> lock1;
91 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock1.get());
92 lock1.reset(new FirefoxProfileLock(test_path));
93 EXPECT_TRUE(lock1->HasAcquired());
95 scoped_ptr<FirefoxProfileLock> lock2;
96 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock2.get());
97 lock2.reset(new FirefoxProfileLock(test_path));
98 EXPECT_FALSE(lock2->HasAcquired());
100 lock1->Unlock();
101 EXPECT_FALSE(lock1->HasAcquired());
103 lock2->Lock();
104 EXPECT_TRUE(lock2->HasAcquired());
105 lock2->Unlock();
106 EXPECT_FALSE(lock2->HasAcquired());
108 #endif