Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / test / app_remoting_test_driver_environment.h
blob3ed22d7ee78f036be27110f4876e225b5eb36cb8
1 // Copyright 2015 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 #ifndef REMOTING_TEST_APP_REMOTING_TEST_DRIVER_ENVIRONMENT_H_
6 #define REMOTING_TEST_APP_REMOTING_TEST_DRIVER_ENVIRONMENT_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/files/file_path.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "remoting/test/remote_application_details.h"
15 #include "remoting/test/remote_host_info_fetcher.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace base {
19 class MessageLoopForIO;
22 namespace remoting {
23 namespace test {
25 class AccessTokenFetcher;
26 class AppRemotingReportIssueRequest;
27 class RefreshTokenStore;
28 struct RemoteHostInfo;
30 // Globally accessible to all test fixtures and cases and has its
31 // lifetime managed by the GTest framework. It is responsible for managing
32 // access tokens and retrieving remote host connection information.
33 class AppRemotingTestDriverEnvironment : public testing::Environment {
34 public:
35 struct EnvironmentOptions {
36 EnvironmentOptions();
37 ~EnvironmentOptions();
39 std::string user_name;
40 base::FilePath refresh_token_file_path;
41 ServiceEnvironment service_environment;
42 bool release_hosts_when_done;
45 explicit AppRemotingTestDriverEnvironment(const EnvironmentOptions& options);
46 ~AppRemotingTestDriverEnvironment() override;
48 // Returns false if a valid access token cannot be retrieved.
49 bool Initialize(const std::string& auth_code);
51 // Synchronously request a new access token using |refresh_token_|.
52 // Returns true if a valid access token has been retrieved.
53 bool RefreshAccessToken();
55 // Synchronously request remote host information for |application_id|.
56 // Returns true if the request was successful and |remote_host_info| is valid.
57 bool GetRemoteHostInfoForApplicationId(const std::string& application_id,
58 RemoteHostInfo* remote_host_info);
60 // Adds the host_id to the list of hosts that will be released after the tests
61 // have all been run.
62 void AddHostToReleaseList(const std::string& application_id,
63 const std::string& host_id);
65 // Retrieves connection information for all known applications and displays
66 // their availability to STDOUT.
67 void ShowHostAvailability();
69 // Provides the RemoteApplicationDetails for the specified |application_name|.
70 const RemoteApplicationDetails& GetDetailsFromAppName(
71 const std::string& application_name);
73 // Used to set fake/mock objects for AppRemotingTestDriverEnvironment tests.
74 // The caller retains ownership of the supplied objects, and must ensure that
75 // they remain valid until the AppRemotingTestDriverEnvironment instance has
76 // been destroyed.
77 void SetAccessTokenFetcherForTest(AccessTokenFetcher* access_token_fetcher);
78 void SetAppRemotingReportIssueRequestForTest(
79 AppRemotingReportIssueRequest* app_remoting_report_issue_request);
80 void SetRefreshTokenStoreForTest(RefreshTokenStore* refresh_token_store);
81 void SetRemoteHostInfoFetcherForTest(
82 RemoteHostInfoFetcher* remote_host_info_fetcher);
84 // Accessors for fields used by tests.
85 const std::string& access_token() const { return access_token_; }
86 const std::string& user_name() const { return user_name_; }
88 protected:
89 // Contains the names of all supported remote applications.
90 // Once initialized, this vector is not modified.
91 std::vector<std::string> application_names_;
93 // Contains RemoteApplicationDetails for all supported remote applications.
94 // Once initialized, this map is not modified.
95 std::map<std::string, RemoteApplicationDetails> application_details_map_;
97 private:
98 // testing::Environment interface.
99 void TearDown() override;
101 // Used to retrieve an access token. If |auth_code| is empty, then the stored
102 // refresh_token will be used instead of |auth_code|.
103 // Returns true if a new, valid access token has been retrieved.
104 bool RetrieveAccessToken(const std::string& auth_code);
106 // Called after the access token fetcher completes.
107 // The tokens will be empty on failure.
108 void OnAccessTokenRetrieved(base::Closure done_closure,
109 const std::string& access_token,
110 const std::string& refresh_token);
112 // Called after the remote host info fetcher completes.
113 // |remote_host_info| is modified on failure.
114 void OnRemoteHostInfoRetrieved(
115 base::Closure done_closure,
116 RemoteHostInfo* remote_host_info,
117 const RemoteHostInfo& retrieved_remote_host_info);
119 // Used for authenticating with the app remoting service API.
120 std::string access_token_;
122 // Used to retrieve an access token.
123 std::string refresh_token_;
125 // Used for authentication.
126 std::string user_name_;
128 // Service API to target when retrieving remote host connection information.
129 ServiceEnvironment service_environment_;
131 // Specifies whether to tell the service to release the remote hosts we
132 // requested after the tests have completed.
133 bool release_hosts_when_done_;
135 // Path to a JSON file containing refresh tokens.
136 base::FilePath refresh_token_file_path_;
138 // Access token fetcher used by TestDriverEnvironment tests.
139 remoting::test::AccessTokenFetcher* test_access_token_fetcher_;
141 // AppRemotingReportIssueRequest used by TestDriverEnvironment tests.
142 remoting::test::AppRemotingReportIssueRequest*
143 test_app_remoting_report_issue_request_;
145 // RefreshTokenStore used by TestDriverEnvironment tests.
146 remoting::test::RefreshTokenStore* test_refresh_token_store_;
148 // RemoteHostInfoFetcher used by TestDriverEnvironment tests.
149 remoting::test::RemoteHostInfoFetcher* test_remote_host_info_fetcher_;
151 // Used for running network request tasks.
152 scoped_ptr<base::MessageLoopForIO> message_loop_;
154 // Contains the host ids to release when the environment is torn down.
155 // The key is the application id and the value is a list of hosts.
156 std::map<std::string, std::vector<std::string>> host_ids_to_release_;
158 DISALLOW_COPY_AND_ASSIGN(AppRemotingTestDriverEnvironment);
161 // Used to provide application specific instances of the
162 // AppRemotingTestDriverEnvironment class.
163 extern scoped_ptr<AppRemotingTestDriverEnvironment>
164 CreateAppRemotingTestDriverEnvironment(
165 const AppRemotingTestDriverEnvironment::EnvironmentOptions& options);
167 // Unfortunately a global var is how the GTEST framework handles sharing data
168 // between tests and keeping long-lived objects around. Used to share auth
169 // tokens and remote host connection information across tests.
170 extern AppRemotingTestDriverEnvironment* AppRemotingSharedData;
172 } // namespace test
173 } // namespace remoting
175 #endif // REMOTING_TEST_APP_REMOTING_TEST_DRIVER_ENVIRONMENT_H_