Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_manager / drive_test_util.cc
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
14 namespace file_manager {
15 namespace test_util {
16
17 namespace {
18
19 // Helper class used to wait for |OnFileSystemMounted| event from a drive file
20 // system.
21 class 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
52 void 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   DCHECK(integration_service->is_enabled());
67
68   if (integration_service->IsMounted())
69     return;
70
71   DriveMountPointWaiter mount_point_waiter(integration_service);
72   VLOG(1) << "Waiting for drive mount point to get mounted.";
73   mount_point_waiter.Wait();
74   VLOG(1) << "Drive mount point found.";
75 }
76
77 }  // namespace test_util
78 }  // namespace file_manager