Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / image_writer_private / operation_manager_unittest.cc
1 // Copyright 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 "base/command_line.h"
6 #include "chrome/browser/chromeos/login/user_manager.h"
7 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h"
8 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h"
9 #include "chrome/browser/extensions/api/image_writer_private/test_utils.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/extension_system_factory.h"
12 #include "chrome/browser/extensions/test_extension_system.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "extensions/browser/event_router.h"
15
16 #if defined(OS_CHROMEOS)
17 #include "chrome/browser/chromeos/login/user_manager.h"
18 #include "chrome/browser/chromeos/settings/cros_settings.h"
19 #include "chrome/browser/chromeos/settings/device_settings_service.h"
20 #endif
21
22 namespace extensions {
23 namespace image_writer {
24
25 // A fake for the EventRouter. If tests require monitoring of interaction with
26 // the event router put the logic here.
27 class FakeEventRouter : public extensions::EventRouter {
28  public:
29   explicit FakeEventRouter(Profile* profile) : EventRouter(profile, NULL) {}
30
31   virtual void DispatchEventToExtension(
32       const std::string& extension_id,
33       scoped_ptr<extensions::Event> event) OVERRIDE {
34     // Do nothing with the event as no tests currently care.
35   }
36 };
37
38 // A fake ExtensionSystem that returns a FakeEventRouter for event_router().
39 class FakeExtensionSystem : public extensions::TestExtensionSystem {
40  public:
41   explicit FakeExtensionSystem(Profile* profile)
42       : TestExtensionSystem(profile) {
43     fake_event_router_.reset(new FakeEventRouter(profile));
44   }
45
46   virtual EventRouter* event_router() OVERRIDE {
47     return fake_event_router_.get();
48   }
49
50  private:
51   scoped_ptr<FakeEventRouter> fake_event_router_;
52 };
53
54 // Factory function to register for the ExtensionSystem.
55 BrowserContextKeyedService* BuildFakeExtensionSystem(
56     content::BrowserContext* profile) {
57   return new FakeExtensionSystem(static_cast<Profile*>(profile));
58 }
59
60 namespace {
61
62 class ImageWriterOperationManagerTest
63     : public ImageWriterUnitTestBase {
64  public:
65   void StartCallback(bool success, const std::string& error) {
66     started_ = true;
67     start_success_ = success;
68     start_error_ = error;
69   }
70
71  protected:
72   ImageWriterOperationManagerTest()
73       : started_(false),
74         start_success_(false) {
75   }
76
77   virtual void SetUp() OVERRIDE {
78     ImageWriterUnitTestBase::SetUp();
79     extension_system_ = static_cast<FakeExtensionSystem*>(
80         ExtensionSystemFactory::GetInstance()->
81             SetTestingFactoryAndUse(&test_profile_, &BuildFakeExtensionSystem));
82     event_router_ = static_cast<FakeEventRouter*>(
83         extension_system_->event_router());
84   }
85
86   bool started_;
87   bool start_success_;
88   std::string start_error_;
89
90   TestingProfile test_profile_;
91   FakeExtensionSystem* extension_system_;
92   FakeEventRouter* event_router_;
93
94 #if defined(OS_CHROMEOS)
95   chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
96   chromeos::ScopedTestCrosSettings test_cros_settings_;
97   chromeos::ScopedTestUserManager test_user_manager_;
98 #endif
99 };
100
101 TEST_F(ImageWriterOperationManagerTest, WriteFromFile) {
102   OperationManager manager(&test_profile_);
103
104   manager.StartWriteFromFile(
105     kDummyExtensionId,
106     test_image_path_,
107     test_device_path_.AsUTF8Unsafe(),
108     base::Bind(&ImageWriterOperationManagerTest::StartCallback,
109                base::Unretained(this)));
110
111   EXPECT_TRUE(started_);
112   EXPECT_TRUE(start_success_);
113   EXPECT_EQ("", start_error_);
114
115   base::RunLoop().RunUntilIdle();
116 }
117
118 TEST_F(ImageWriterOperationManagerTest, DestroyPartitions) {
119   OperationManager manager(&test_profile_);
120
121   manager.DestroyPartitions(
122       kDummyExtensionId,
123       test_device_path_.AsUTF8Unsafe(),
124       base::Bind(&ImageWriterOperationManagerTest::StartCallback,
125                  base::Unretained(this)));
126
127   EXPECT_TRUE(started_);
128   EXPECT_TRUE(start_success_);
129   EXPECT_EQ("", start_error_);
130
131   base::RunLoop().RunUntilIdle();
132 }
133
134 } // namespace
135 } // namespace image_writer
136 } // namespace extensions