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.
5 #include "base/command_line.h"
6 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h"
7 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h"
8 #include "chrome/browser/extensions/api/image_writer_private/test_utils.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/extension_system_factory.h"
11 #include "chrome/browser/extensions/test_extension_system.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "extensions/browser/event_router.h"
15 #if defined(OS_CHROMEOS)
16 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
17 #include "chrome/browser/chromeos/settings/cros_settings.h"
18 #include "chrome/browser/chromeos/settings/device_settings_service.h"
21 namespace extensions
{
22 namespace image_writer
{
24 // A fake for the EventRouter. If tests require monitoring of interaction with
25 // the event router put the logic here.
26 class FakeEventRouter
: public extensions::EventRouter
{
28 explicit FakeEventRouter(Profile
* profile
) : EventRouter(profile
, NULL
) {}
30 void DispatchEventToExtension(const std::string
& extension_id
,
31 scoped_ptr
<extensions::Event
> event
) override
{
32 // Do nothing with the event as no tests currently care.
36 // A fake ExtensionSystem that returns a FakeEventRouter for event_router().
37 class FakeExtensionSystem
: public extensions::TestExtensionSystem
{
39 explicit FakeExtensionSystem(Profile
* profile
)
40 : TestExtensionSystem(profile
) {
41 fake_event_router_
.reset(new FakeEventRouter(profile
));
44 EventRouter
* event_router() override
{ return fake_event_router_
.get(); }
47 scoped_ptr
<FakeEventRouter
> fake_event_router_
;
50 // Factory function to register for the ExtensionSystem.
51 KeyedService
* BuildFakeExtensionSystem(content::BrowserContext
* profile
) {
52 return new FakeExtensionSystem(static_cast<Profile
*>(profile
));
57 class ImageWriterOperationManagerTest
: public ImageWriterUnitTestBase
{
59 void StartCallback(bool success
, const std::string
& error
) {
61 start_success_
= success
;
65 void CancelCallback(bool success
, const std::string
& error
) {
67 cancel_success_
= true;
68 cancel_error_
= error
;
72 ImageWriterOperationManagerTest()
74 start_success_(false) {
77 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());
88 std::string start_error_
;
92 std::string cancel_error_
;
94 TestingProfile test_profile_
;
95 FakeExtensionSystem
* extension_system_
;
96 FakeEventRouter
* event_router_
;
98 #if defined(OS_CHROMEOS)
99 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
100 chromeos::ScopedTestCrosSettings test_cros_settings_
;
101 chromeos::ScopedTestUserManager test_user_manager_
;
105 TEST_F(ImageWriterOperationManagerTest
, WriteFromFile
) {
106 OperationManager
manager(&test_profile_
);
108 manager
.StartWriteFromFile(
110 test_utils_
.GetImagePath(),
111 test_utils_
.GetDevicePath().AsUTF8Unsafe(),
112 base::Bind(&ImageWriterOperationManagerTest::StartCallback
,
113 base::Unretained(this)));
115 EXPECT_TRUE(started_
);
116 EXPECT_TRUE(start_success_
);
117 EXPECT_EQ("", start_error_
);
121 base::Bind(&ImageWriterOperationManagerTest::CancelCallback
,
122 base::Unretained(this)));
124 EXPECT_TRUE(cancelled_
);
125 EXPECT_TRUE(cancel_success_
);
126 EXPECT_EQ("", cancel_error_
);
128 base::RunLoop().RunUntilIdle();
131 TEST_F(ImageWriterOperationManagerTest
, DestroyPartitions
) {
132 OperationManager
manager(&test_profile_
);
134 manager
.DestroyPartitions(
136 test_utils_
.GetDevicePath().AsUTF8Unsafe(),
137 base::Bind(&ImageWriterOperationManagerTest::StartCallback
,
138 base::Unretained(this)));
140 EXPECT_TRUE(started_
);
141 EXPECT_TRUE(start_success_
);
142 EXPECT_EQ("", start_error_
);
146 base::Bind(&ImageWriterOperationManagerTest::CancelCallback
,
147 base::Unretained(this)));
149 EXPECT_TRUE(cancelled_
);
150 EXPECT_TRUE(cancel_success_
);
151 EXPECT_EQ("", cancel_error_
);
153 base::RunLoop().RunUntilIdle();
157 } // namespace image_writer
158 } // namespace extensions