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 "base/memory/scoped_ptr.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 #include "extensions/browser/event_router_factory.h"
17 #if defined(OS_CHROMEOS)
18 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
19 #include "chrome/browser/chromeos/settings/cros_settings.h"
20 #include "chrome/browser/chromeos/settings/device_settings_service.h"
23 namespace extensions
{
24 namespace image_writer
{
26 // A fake for the EventRouter. If tests require monitoring of interaction with
27 // the event router put the logic here.
28 class FakeEventRouter
: public extensions::EventRouter
{
30 explicit FakeEventRouter(Profile
* profile
) : EventRouter(profile
, NULL
) {}
32 void DispatchEventToExtension(const std::string
& extension_id
,
33 scoped_ptr
<extensions::Event
> event
) override
{
34 // Do nothing with the event as no tests currently care.
38 // FakeEventRouter factory function
39 scoped_ptr
<KeyedService
> FakeEventRouterFactoryFunction(
40 content::BrowserContext
* context
) {
41 return make_scoped_ptr(new FakeEventRouter(static_cast<Profile
*>(context
)));
46 class ImageWriterOperationManagerTest
: public ImageWriterUnitTestBase
{
48 void StartCallback(bool success
, const std::string
& error
) {
50 start_success_
= success
;
54 void CancelCallback(bool success
, const std::string
& error
) {
56 cancel_success_
= true;
57 cancel_error_
= error
;
61 ImageWriterOperationManagerTest()
63 start_success_(false) {
66 void SetUp() override
{
67 ImageWriterUnitTestBase::SetUp();
68 event_router_
= static_cast<FakeEventRouter
*>(
69 extensions::EventRouterFactory::GetInstance()->SetTestingFactoryAndUse(
70 &test_profile_
, &FakeEventRouterFactoryFunction
));
75 std::string start_error_
;
79 std::string cancel_error_
;
81 TestingProfile test_profile_
;
82 FakeEventRouter
* event_router_
;
84 #if defined(OS_CHROMEOS)
85 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
86 chromeos::ScopedTestCrosSettings test_cros_settings_
;
87 chromeos::ScopedTestUserManager test_user_manager_
;
91 TEST_F(ImageWriterOperationManagerTest
, WriteFromFile
) {
92 OperationManager
manager(&test_profile_
);
94 manager
.StartWriteFromFile(
96 test_utils_
.GetImagePath(),
97 test_utils_
.GetDevicePath().AsUTF8Unsafe(),
98 base::Bind(&ImageWriterOperationManagerTest::StartCallback
,
99 base::Unretained(this)));
101 EXPECT_TRUE(started_
);
102 EXPECT_TRUE(start_success_
);
103 EXPECT_EQ("", start_error_
);
107 base::Bind(&ImageWriterOperationManagerTest::CancelCallback
,
108 base::Unretained(this)));
110 EXPECT_TRUE(cancelled_
);
111 EXPECT_TRUE(cancel_success_
);
112 EXPECT_EQ("", cancel_error_
);
114 base::RunLoop().RunUntilIdle();
117 TEST_F(ImageWriterOperationManagerTest
, DestroyPartitions
) {
118 OperationManager
manager(&test_profile_
);
120 manager
.DestroyPartitions(
122 test_utils_
.GetDevicePath().AsUTF8Unsafe(),
123 base::Bind(&ImageWriterOperationManagerTest::StartCallback
,
124 base::Unretained(this)));
126 EXPECT_TRUE(started_
);
127 EXPECT_TRUE(start_success_
);
128 EXPECT_EQ("", start_error_
);
132 base::Bind(&ImageWriterOperationManagerTest::CancelCallback
,
133 base::Unretained(this)));
135 EXPECT_TRUE(cancelled_
);
136 EXPECT_TRUE(cancel_success_
);
137 EXPECT_EQ("", cancel_error_
);
139 base::RunLoop().RunUntilIdle();
143 } // namespace image_writer
144 } // namespace extensions