Port Chromium's blink_tests target to GN.
[chromium-blink-merge.git] / remoting / test / app_remoting_test_driver_environment.h
blobd2053da8f8375d5d323a108659f94778144b8f4e
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/memory/scoped_ptr.h"
13 #include "remoting/test/remote_application_details.h"
14 #include "remoting/test/remote_host_info_fetcher.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace base {
18 class MessageLoopForIO;
21 namespace remoting {
22 namespace test {
24 class AccessTokenFetcher;
25 class RefreshTokenStore;
26 struct RemoteHostInfo;
28 // Globally accessible to all test fixtures and cases and has its
29 // lifetime managed by the GTest framework. It is responsible for managing
30 // access tokens and retrieving remote host connection information.
31 class AppRemotingTestDriverEnvironment : public testing::Environment {
32 public:
33 AppRemotingTestDriverEnvironment(const std::string& user_name,
34 ServiceEnvironment service_environment);
35 ~AppRemotingTestDriverEnvironment() override;
37 // Returns false if a valid access token cannot be retrieved.
38 bool Initialize(const std::string& auth_code);
40 // Synchronously request a new access token using |refresh_token_|.
41 // Returns true if a valid access token has been retrieved.
42 bool RefreshAccessToken();
44 // Synchronously request remote host information for |application_id|.
45 // Returns true if the request was successful and |remote_host_info| is valid.
46 bool GetRemoteHostInfoForApplicationId(const std::string& application_id,
47 RemoteHostInfo* remote_host_info);
49 // Retrieves connection information for all known applications and displays
50 // their availability to STDOUT.
51 void ShowHostAvailability();
53 // Provides the RemoteApplicationDetails for the specified |application_name|.
54 const RemoteApplicationDetails& GetDetailsFromAppName(
55 const std::string& application_name);
57 // Used to set fake/mock objects for AppRemotingTestDriverEnvironment tests.
58 // The caller retains ownership of the supplied objects, and must ensure that
59 // they remain valid until the AppRemotingTestDriverEnvironment instance has
60 // been destroyed.
61 void SetAccessTokenFetcherForTest(AccessTokenFetcher* access_token_fetcher);
62 void SetRefreshTokenStoreForTest(RefreshTokenStore* refresh_token_store);
63 void SetRemoteHostInfoFetcherForTest(
64 RemoteHostInfoFetcher* remote_host_info_fetcher);
66 // Accessors for fields used by tests.
67 const std::string& access_token() const { return access_token_; }
68 const std::string& user_name() const { return user_name_; }
70 private:
71 // testing::Environment interface.
72 void TearDown() override;
74 // Used to retrieve an access token. If |auth_code| is empty, then the stored
75 // refresh_token will be used instead of |auth_code|.
76 // Returns true if a new, valid access token has been retrieved.
77 bool RetrieveAccessToken(const std::string& auth_code);
79 // Called after the access token fetcher completes.
80 // The tokens will be empty on failure.
81 void OnAccessTokenRetrieved(base::Closure done_closure,
82 const std::string& access_token,
83 const std::string& refresh_token);
85 // Called after the remote host info fetcher completes.
86 // |remote_host_info| is modified on failure.
87 void OnRemoteHostInfoRetrieved(
88 base::Closure done_closure,
89 RemoteHostInfo* remote_host_info,
90 const RemoteHostInfo& retrieved_remote_host_info);
92 // Populates |application_names_| with the names of the supported remote
93 // applications.
94 void PopulateApplicationNames();
96 // Populates |application_details_map_| with the RemoteApplicationDetails for
97 // all supported remote applications.
98 void PopulateApplicationDetailsMap();
100 // Used for authenticating with the app remoting service API.
101 std::string access_token_;
103 // Used to retrieve an access token.
104 std::string refresh_token_;
106 // Used for authentication.
107 std::string user_name_;
109 // Service API to target when retrieving remote host connection information.
110 ServiceEnvironment service_environment_;
112 // Access token fetcher used by TestDriverEnvironment tests.
113 remoting::test::AccessTokenFetcher* test_access_token_fetcher_;
115 // RefreshTokenStore used by TestDriverEnvironment tests.
116 remoting::test::RefreshTokenStore* test_refresh_token_store_;
118 // RemoteHostInfoFetcher used by TestDriverEnvironment tests.
119 remoting::test::RemoteHostInfoFetcher* test_remote_host_info_fetcher_;
121 // Used for running network request tasks.
122 scoped_ptr<base::MessageLoopForIO> message_loop_;
124 // Contains the names of all supported remote applications.
125 // Once initialized, this vector is not modified.
126 std::vector<std::string> application_names_;
128 // Contains RemoteApplicationDetails for all supported remote applications.
129 // Once initialized, this map is not modified.
130 std::map<std::string, RemoteApplicationDetails> application_details_map_;
132 DISALLOW_COPY_AND_ASSIGN(AppRemotingTestDriverEnvironment);
135 // Unfortunately a global var is how the GTEST framework handles sharing data
136 // between tests and keeping long-lived objects around. Used to share auth
137 // tokens and remote host connection information across tests.
138 extern AppRemotingTestDriverEnvironment* AppRemotingSharedData;
140 } // namespace test
141 } // namespace remoting
143 #endif // REMOTING_TEST_APP_REMOTING_TEST_DRIVER_ENVIRONMENT_H_