1// Copyright 2014 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/message_loop/message_loop.h"
6#include "chrome/browser/extensions/api/file_system/file_system_api.h"
7#include "chrome/browser/extensions/api/image_writer_private/operation.h"
8#include "chrome/browser/extensions/api/image_writer_private/removable_storage_provider.h"
9#include "chrome/browser/extensions/api/image_writer_private/test_utils.h"
10#include "chrome/browser/extensions/extension_apitest.h"
11#include "chrome/common/extensions/api/image_writer_private.h"
12#include "content/public/browser/browser_thread.h"
13
14namespace extensions {
15
16using api::image_writer_private::RemovableStorageDevice;
17using extensions::image_writer::FakeImageWriterClient;
18
19class ImageWriterPrivateApiTest : public ExtensionApiTest {
20 public:
21  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
22    ExtensionApiTest::SetUpInProcessBrowserTestFixture();
23    test_utils_.SetUp(true);
24
25    ASSERT_TRUE(test_utils_.FillFile(test_utils_.GetImagePath(),
26                                     image_writer::kImagePattern,
27                                     image_writer::kTestFileSize));
28    ASSERT_TRUE(test_utils_.FillFile(test_utils_.GetDevicePath(),
29                                     image_writer::kDevicePattern,
30                                     image_writer::kTestFileSize));
31
32    scoped_refptr<StorageDeviceList> device_list(new StorageDeviceList);
33
34    RemovableStorageDevice* expected1 = new RemovableStorageDevice();
35    expected1->vendor = "Vendor 1";
36    expected1->model = "Model 1";
37    expected1->capacity = image_writer::kTestFileSize;
38    expected1->removable = true;
39#if defined(OS_WIN)
40    expected1->storage_unit_id = test_utils_.GetDevicePath().AsUTF8Unsafe();
41#else
42    expected1->storage_unit_id = test_utils_.GetDevicePath().value();
43#endif
44
45    RemovableStorageDevice* expected2 = new RemovableStorageDevice();
46    expected2->vendor = "Vendor 2";
47    expected2->model = "Model 2";
48    expected2->capacity = image_writer::kTestFileSize << 2;
49    expected2->removable = false;
50#if defined(OS_WIN)
51    expected2->storage_unit_id = test_utils_.GetDevicePath().AsUTF8Unsafe();
52#else
53    expected2->storage_unit_id = test_utils_.GetDevicePath().value();
54#endif
55
56    linked_ptr<RemovableStorageDevice> device1(expected1);
57    device_list->data.push_back(device1);
58    linked_ptr<RemovableStorageDevice> device2(expected2);
59    device_list->data.push_back(device2);
60
61    RemovableStorageProvider::SetDeviceListForTesting(device_list);
62  }
63
64  virtual void TearDownInProcessBrowserTestFixture() OVERRIDE {
65    ExtensionApiTest::TearDownInProcessBrowserTestFixture();
66    test_utils_.TearDown();
67    RemovableStorageProvider::ClearDeviceListForTesting();
68    FileSystemChooseEntryFunction::StopSkippingPickerForTest();
69  }
70
71#if !defined(OS_CHROMEOS)
72  void ImageWriterUtilityClientCall() {
73    content::BrowserThread::PostTask(
74        content::BrowserThread::FILE,
75        FROM_HERE,
76        base::Bind(&FakeImageWriterClient::Progress,
77                   test_utils_.GetUtilityClient(),
78                   0));
79    content::BrowserThread::PostTask(
80        content::BrowserThread::FILE,
81        FROM_HERE,
82        base::Bind(&FakeImageWriterClient::Progress,
83                   test_utils_.GetUtilityClient(),
84                   50));
85    content::BrowserThread::PostTask(
86        content::BrowserThread::FILE,
87        FROM_HERE,
88        base::Bind(&FakeImageWriterClient::Progress,
89                   test_utils_.GetUtilityClient(),
90                   100));
91    content::BrowserThread::PostTask(
92        content::BrowserThread::FILE,
93        FROM_HERE,
94        base::Bind(&FakeImageWriterClient::Success,
95                   test_utils_.GetUtilityClient()));
96  }
97#endif
98
99 protected:
100  image_writer::ImageWriterTestUtils test_utils_;
101};
102
103IN_PROC_BROWSER_TEST_F(ImageWriterPrivateApiTest, TestListDevices) {
104  ASSERT_TRUE(RunExtensionTest("image_writer_private/list_devices"))
105      << message_;
106}
107
108IN_PROC_BROWSER_TEST_F(ImageWriterPrivateApiTest, TestWriteFromFile) {
109  FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
110      "test_temp", test_utils_.GetTempDir());
111
112  base::FilePath selected_image(test_utils_.GetImagePath());
113  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
114      &selected_image);
115
116#if !defined(OS_CHROMEOS)
117  test_utils_.GetUtilityClient()->SetWriteCallback(base::Bind(
118      &ImageWriterPrivateApiTest::ImageWriterUtilityClientCall, this));
119  test_utils_.GetUtilityClient()->SetVerifyCallback(base::Bind(
120      &ImageWriterPrivateApiTest::ImageWriterUtilityClientCall, this));
121#endif
122
123  ASSERT_TRUE(RunPlatformAppTest("image_writer_private/write_from_file"))
124      << message_;
125}
126
127}  // namespace extensions
128