Home | History | Annotate | Download | only in drive
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/chromeos/drive/test_util.h"
      6 
      7 #include <string>
      8 
      9 #include "base/message_loop/message_loop.h"
     10 #include "base/prefs/pref_registry_simple.h"
     11 #include "base/prefs/testing_pref_service.h"
     12 #include "base/run_loop.h"
     13 #include "base/threading/sequenced_worker_pool.h"
     14 #include "chrome/browser/chromeos/drive/drive.pb.h"
     15 #include "chrome/common/pref_names.h"
     16 #include "content/public/browser/browser_thread.h"
     17 
     18 namespace drive {
     19 
     20 namespace test_util {
     21 
     22 namespace {
     23 
     24 // This class is used to monitor if any task is posted to a message loop.
     25 class TaskObserver : public base::MessageLoop::TaskObserver {
     26  public:
     27   TaskObserver() : posted_(false) {}
     28   virtual ~TaskObserver() {}
     29 
     30   // MessageLoop::TaskObserver overrides.
     31   virtual void WillProcessTask(const base::PendingTask& pending_task) OVERRIDE {
     32   }
     33   virtual void DidProcessTask(const base::PendingTask& pending_task) OVERRIDE {
     34     posted_ = true;
     35   }
     36 
     37   // Returns true if any task was posted.
     38   bool posted() const { return posted_; }
     39 
     40  private:
     41   bool posted_;
     42   DISALLOW_COPY_AND_ASSIGN(TaskObserver);
     43 };
     44 
     45 }  // namespace
     46 
     47 void RunBlockingPoolTask() {
     48   while (true) {
     49     content::BrowserThread::GetBlockingPool()->FlushForTesting();
     50 
     51     TaskObserver task_observer;
     52     base::MessageLoop::current()->AddTaskObserver(&task_observer);
     53     base::RunLoop().RunUntilIdle();
     54     base::MessageLoop::current()->RemoveTaskObserver(&task_observer);
     55     if (!task_observer.posted())
     56       break;
     57   }
     58 }
     59 
     60 void RegisterDrivePrefs(PrefRegistrySimple* pref_registry) {
     61   pref_registry->RegisterBooleanPref(
     62       prefs::kDisableDrive,
     63       false);
     64   pref_registry->RegisterBooleanPref(
     65       prefs::kDisableDriveOverCellular,
     66       true);
     67   pref_registry->RegisterBooleanPref(
     68       prefs::kDisableDriveHostedFiles,
     69       false);
     70 }
     71 
     72 FakeNetworkChangeNotifier::FakeNetworkChangeNotifier()
     73     : type_(CONNECTION_WIFI) {
     74 }
     75 
     76 void FakeNetworkChangeNotifier::SetConnectionType(ConnectionType type) {
     77   type_ = type;
     78   NotifyObserversOfConnectionTypeChange();
     79 }
     80 
     81 net::NetworkChangeNotifier::ConnectionType
     82 FakeNetworkChangeNotifier::GetCurrentConnectionType() const {
     83   return type_;
     84 }
     85 
     86 }  // namespace test_util
     87 }  // namespace drive
     88