Home | History | Annotate | Download | only in image_writer_private
      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 "chrome/browser/extensions/api/image_writer_private/error_messages.h"
      6 #include "chrome/browser/extensions/api/image_writer_private/test_utils.h"
      7 #include "chrome/browser/extensions/api/image_writer_private/write_from_file_operation.h"
      8 #include "chrome/test/base/testing_profile.h"
      9 
     10 namespace extensions {
     11 namespace image_writer {
     12 
     13 using testing::_;
     14 using testing::Lt;
     15 using testing::AnyNumber;
     16 using testing::AtLeast;
     17 
     18 class ImageWriterFromFileTest : public ImageWriterUnitTestBase {
     19  protected:
     20   ImageWriterFromFileTest()
     21       : profile_(new TestingProfile), manager_(profile_.get()) {}
     22   scoped_ptr<TestingProfile> profile_;
     23   MockOperationManager manager_;
     24 };
     25 
     26 TEST_F(ImageWriterFromFileTest, InvalidFile) {
     27   scoped_refptr<WriteFromFileOperation> op =
     28       new WriteFromFileOperation(manager_.AsWeakPtr(),
     29                                  kDummyExtensionId,
     30                                  test_utils_.GetImagePath(),
     31                                  test_utils_.GetDevicePath().AsUTF8Unsafe());
     32 
     33   base::DeleteFile(test_utils_.GetImagePath(), false);
     34 
     35   EXPECT_CALL(manager_, OnProgress(kDummyExtensionId, _, _)).Times(0);
     36   EXPECT_CALL(manager_, OnComplete(kDummyExtensionId)).Times(0);
     37   EXPECT_CALL(manager_,
     38               OnError(kDummyExtensionId,
     39                       image_writer_api::STAGE_UNKNOWN,
     40                       0,
     41                       error::kImageInvalid)).Times(1);
     42 
     43   op->Start();
     44 
     45   base::RunLoop().RunUntilIdle();
     46 }
     47 
     48 // Runs the entire WriteFromFile operation.
     49 TEST_F(ImageWriterFromFileTest, WriteFromFileEndToEnd) {
     50   scoped_refptr<WriteFromFileOperation> op =
     51       new WriteFromFileOperation(manager_.AsWeakPtr(),
     52                                  kDummyExtensionId,
     53                                  test_utils_.GetImagePath(),
     54                                  test_utils_.GetDevicePath().AsUTF8Unsafe());
     55   EXPECT_CALL(manager_,
     56               OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, _))
     57       .Times(AnyNumber());
     58   EXPECT_CALL(manager_,
     59               OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 0))
     60       .Times(AtLeast(1));
     61   EXPECT_CALL(manager_,
     62               OnProgress(kDummyExtensionId, image_writer_api::STAGE_WRITE, 100))
     63       .Times(AtLeast(1));
     64 
     65 #if !defined(OS_CHROMEOS)
     66   // Chrome OS doesn't verify.
     67   EXPECT_CALL(
     68       manager_,
     69       OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, _))
     70       .Times(AnyNumber());
     71   EXPECT_CALL(
     72       manager_,
     73       OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 0))
     74       .Times(AtLeast(1));
     75   EXPECT_CALL(
     76       manager_,
     77       OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYWRITE, 100))
     78       .Times(AtLeast(1));
     79 #endif
     80 
     81   EXPECT_CALL(manager_, OnComplete(kDummyExtensionId)).Times(1);
     82   EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0);
     83 
     84   op->Start();
     85 
     86   base::RunLoop().RunUntilIdle();
     87 #if !defined(OS_CHROMEOS)
     88   test_utils_.GetUtilityClient()->Progress(0);
     89   test_utils_.GetUtilityClient()->Progress(50);
     90   test_utils_.GetUtilityClient()->Progress(100);
     91   test_utils_.GetUtilityClient()->Success();
     92   base::RunLoop().RunUntilIdle();
     93   test_utils_.GetUtilityClient()->Progress(0);
     94   test_utils_.GetUtilityClient()->Progress(50);
     95   test_utils_.GetUtilityClient()->Progress(100);
     96   test_utils_.GetUtilityClient()->Success();
     97   base::RunLoop().RunUntilIdle();
     98 #endif
     99 }
    100 
    101 }  // namespace image_writer
    102 }  // namespace extensions
    103