drive_test_util.cc revision 58537e28ecd584eab876aee8be7156509866d23a
1// Copyright (c) 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/chromeos/file_manager/drive_test_util.h"
6
7#include "base/files/file_path.h"
8#include "base/run_loop.h"
9#include "chrome/browser/chromeos/drive/drive_integration_service.h"
10#include "chrome/browser/profiles/profile.h"
11#include "content/public/browser/browser_context.h"
12#include "webkit/browser/fileapi/external_mount_points.h"
13
14namespace file_manager {
15namespace test_util {
16
17namespace {
18
19// Helper class used to wait for |OnFileSystemMounted| event from a drive file
20// system.
21class DriveMountPointWaiter : public drive::DriveIntegrationServiceObserver {
22 public:
23  explicit DriveMountPointWaiter(
24      drive::DriveIntegrationService* integration_service)
25      : integration_service_(integration_service) {
26    integration_service_->AddObserver(this);
27  }
28
29  virtual ~DriveMountPointWaiter() {
30    integration_service_->RemoveObserver(this);
31  }
32
33  // DriveIntegrationServiceObserver override.
34  virtual void OnFileSystemMounted() OVERRIDE {
35    // Note that it is OK for |run_loop_.Quit| to be called before
36    // |run_loop_.Run|. In this case |Run| will return immediately.
37    run_loop_.Quit();
38  }
39
40  // Runs loop until the file system is mounted.
41  void Wait() {
42    run_loop_.Run();
43  }
44
45 private:
46  drive::DriveIntegrationService* integration_service_;
47  base::RunLoop run_loop_;
48};
49
50}  // namespace
51
52void WaitUntilDriveMountPointIsAdded(Profile* profile) {
53  DCHECK(profile);
54
55  // Drive mount point is added by the browser when the drive system service
56  // is first initialized. It is done asynchronously after some other parts of
57  // the service are initialized (e.g. resource metadata and cache), thus racy
58  // with the test start. To handle this raciness, the test verifies that
59  // drive mount point is added before continuing. If this is not the case,
60  // drive file system is observed for FileSystemMounted event (by
61  // |mount_point_waiter|) and test continues once the event is encountered.
62  drive::DriveIntegrationService* integration_service =
63      drive::DriveIntegrationServiceFactory::FindForProfileRegardlessOfStates(
64          profile);
65  DCHECK(integration_service);
66
67  const std::string drive_mount_point_name =
68      drive::util::GetDriveMountPointPath().BaseName().AsUTF8Unsafe();
69  base::FilePath ignored;
70  // GetRegisteredPath succeeds iff the mount point exists.
71  if (content::BrowserContext::GetMountPoints(profile)->
72      GetRegisteredPath(drive_mount_point_name, &ignored))
73    return;
74
75  DriveMountPointWaiter mount_point_waiter(integration_service);
76  LOG(INFO) << "Waiting for drive mount point to get mounted.";
77  mount_point_waiter.Wait();
78  LOG(INFO) << "Drive mount point found.";
79}
80
81}  // namespace test_util
82}  // namespace file_manager
83