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#ifndef CHROME_BROWSER_TRANSLATE_CLD_DATA_HARNESS_H_
6#define CHROME_BROWSER_TRANSLATE_CLD_DATA_HARNESS_H_
7
8#include "base/files/file_path.h"
9#include "base/macros.h"
10#include "base/memory/scoped_ptr.h"
11
12namespace test {
13
14// A utility class that sets up CLD dynamic data upon calling Init() and cleans
15// it up when destroyed.
16// Test data lives under: src/chrome/test/data/cld2_component
17//
18// This class is intended to be instantiated within IN_PROC_BROWSER_TEST_F
19// test fixtures; it uses ASSERT macros for correctness, so that tests will
20// fail gracefully in error conditions. Sample use:
21//
22//   IN_PROC_BROWSER_TEST_F(BrowserTest, PageLanguageDetection) {
23//     scoped_ptr<test::CldDataHarness> cld_data_scope =
24//       test::CreateCldDataHarness();
25//     ASSERT_NO_FATAL_FAILURE(cld_data_scope->Init());
26//     // ... your code that depends on language detection goes here
27//   }
28//
29// If you have a lot of tests that need language translation features, you can
30// add an instance of the CldDataHarness to your test class' private
31// member variables and add the call to Init() into SetUpOnMainThread.
32// Sample use:
33//
34//   class MyTestClass : public InProcessBrowserTest {
35//    public:
36//     MyTestClass() :
37//       cld_data_scope(test::CreateCldDataHarness()) {
38//     }
39//     virtual void SetUpOnMainThread() OVERRIDE {
40//       cld_data_scope->Init();
41//       InProcessBrowserTest::SetUpOnMainThread();
42//     }
43//    private:
44//     scoped_ptr<test::CldDataHarness> cld_data_scope;
45//   };
46//
47class CldDataHarness {
48 public:
49  // Reverses the work done by the Init() method: any files and/or directories
50  // that would be created by Init() (whether it was called or not) are
51  // immediately deleted.
52  // If dynamic data is not currently available for any reason, this method has
53  // no effect.
54  virtual ~CldDataHarness() {}
55
56  // Call this method, wrapping it in ASSERT_NO_FATAL_FAILURE, to initialize
57  // the harness and trigger test failure if initialization fails.
58  virtual void Init() = 0;
59
60 protected:
61  // Returns the version number of the Component Updater "extension" in the
62  // test directory. This generally corresponds the the revision of CLD2 that
63  // the data was built from. The version number is also part of the path that
64  // would be present at runtime if the component installer was used as the
65  // CLD2 data source.
66  const base::FilePath::CharType* GetTestDataSourceCrxVersion();
67
68  // Returns the path to the Component Updater "extension" files in the test
69  // directory. Within, there is a real copy of the CLD2 dynamic data that can
70  // be used in testing scenarios without accessing the network.
71  void GetTestDataSourceDirectory(base::FilePath* out_path);
72};
73
74// Static factory function that returns a data harness defined by the
75// implementation, which must be a subclass of CldDataHarness.
76scoped_ptr<CldDataHarness> CreateCldDataHarness();
77
78}  // namespace test
79
80#endif  // CHROME_BROWSER_TRANSLATE_CLD_DATA_HARNESS_H_
81