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.
6 #include "base/files/file_path.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/path_service.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "chrome/browser/chromeos/first_run/drive_first_run_controller.h"
12 #include "chrome/browser/extensions/crx_installer.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/extension_test_notification_observer.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "content/public/test/test_utils.h"
18 #include "extensions/browser/extension_system.h"
19 #include "net/dns/mock_host_resolver.h"
20 #include "net/http/http_status_code.h"
21 #include "net/test/embedded_test_server/embedded_test_server.h"
22 #include "net/test/embedded_test_server/http_request.h"
23 #include "net/test/embedded_test_server/http_response.h"
29 // Directory containing data files for the tests.
30 const char kTestDirectory
[] = "drive_first_run";
32 // Directory containing correct hosted app page served by the test server.
33 const char kGoodServerDirectory
[] = "good";
35 // Directory containing incorrect hosted app page served by the test server.
36 const char kBadServerDirectory
[] = "bad";
38 // Name of the test hosted app .crx file.
39 const char kTestAppCrxName
[] = "app.crx";
41 // App id of the test hosted app.
42 const char kTestAppId
[] = "kipccbklifbfblhpplnmklieangbjnhb";
44 // The endpoint belonging to the test hosted app.
45 const char kTestEndpointUrl
[] = "http://example.com/endpoint.html";
49 class DriveFirstRunTest
: public InProcessBrowserTest
,
50 public DriveFirstRunController::Observer
{
54 // InProcessBrowserTest overrides:
55 void SetUpOnMainThread() override
;
56 void TearDownOnMainThread() override
;
58 // DriveFirstRunController::Observer overrides:
59 void OnCompletion(bool success
) override
;
60 void OnTimedOut() override
;
64 void InitTestServer(const std::string
& directory
);
66 bool WaitForFirstRunResult();
68 void EnableOfflineMode();
70 void SetDelays(int initial_delay_secs
, int timeout_secs
);
72 bool timed_out() const { return timed_out_
; }
75 // |controller_| is responsible for its own lifetime.
76 DriveFirstRunController
* controller_
;
77 scoped_refptr
<content::MessageLoopRunner
> runner_
;
80 bool waiting_for_result_
;
82 base::FilePath test_data_dir_
;
83 std::string endpoint_url_
;
86 DriveFirstRunTest::DriveFirstRunTest() :
88 waiting_for_result_(false),
91 void DriveFirstRunTest::SetUpOnMainThread() {
92 InProcessBrowserTest::SetUpOnMainThread();
93 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir_
);
94 test_data_dir_
= test_data_dir_
.AppendASCII(kTestDirectory
);
96 host_resolver()->AddRule("example.com", "127.0.0.1");
98 // |controller_| will delete itself when it completes.
99 controller_
= new DriveFirstRunController(browser()->profile());
100 controller_
->AddObserver(this);
101 controller_
->SetDelaysForTest(0, 10);
102 controller_
->SetAppInfoForTest(kTestAppId
, kTestEndpointUrl
);
105 void DriveFirstRunTest::TearDownOnMainThread() {
106 content::RunAllPendingInMessageLoop();
109 void DriveFirstRunTest::InitTestServer(const std::string
& directory
) {
110 embedded_test_server()->ServeFilesFromDirectory(
111 test_data_dir_
.AppendASCII(directory
));
112 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
114 // Configure the endpoint to use the test server's port.
115 const GURL
url(kTestEndpointUrl
);
116 GURL::Replacements replacements
;
117 std::string
port(base::IntToString(embedded_test_server()->port()));
118 replacements
.SetPortStr(port
);
119 endpoint_url_
= url
.ReplaceComponents(replacements
).spec();
120 controller_
->SetAppInfoForTest(kTestAppId
, endpoint_url_
);
123 void DriveFirstRunTest::InstallApp() {
124 ExtensionService
* extension_service
= extensions::ExtensionSystem::Get(
125 browser()->profile())->extension_service();
126 scoped_refptr
<extensions::CrxInstaller
> installer
=
127 extensions::CrxInstaller::CreateSilent(extension_service
);
129 installer
->InstallCrx(test_data_dir_
.AppendASCII(kTestAppCrxName
));
130 ExtensionTestNotificationObserver
observer(browser());
131 observer
.WaitForExtensionLoad();
133 ASSERT_TRUE(extension_service
->GetExtensionById(kTestAppId
, false));
136 void DriveFirstRunTest::EnableOfflineMode() {
137 controller_
->EnableOfflineMode();
140 void DriveFirstRunTest::SetDelays(int initial_delay_secs
, int timeout_secs
) {
141 controller_
->SetDelaysForTest(initial_delay_secs
, timeout_secs
);
144 bool DriveFirstRunTest::WaitForFirstRunResult() {
145 waiting_for_result_
= true;
146 runner_
= new content::MessageLoopRunner
;
148 EXPECT_FALSE(waiting_for_result_
);
152 void DriveFirstRunTest::OnCompletion(bool success
) {
153 EXPECT_TRUE(waiting_for_result_
);
154 waiting_for_result_
= false;
158 // |controller_| will eventually delete itself upon completion, so invalidate
163 void DriveFirstRunTest::OnTimedOut() {
167 IN_PROC_BROWSER_TEST_F(DriveFirstRunTest
, OfflineEnabled
) {
169 InitTestServer(kGoodServerDirectory
);
171 EXPECT_TRUE(WaitForFirstRunResult());
174 IN_PROC_BROWSER_TEST_F(DriveFirstRunTest
, AppNotInstalled
) {
175 InitTestServer(kGoodServerDirectory
);
177 EXPECT_FALSE(WaitForFirstRunResult());
178 EXPECT_FALSE(timed_out());
181 IN_PROC_BROWSER_TEST_F(DriveFirstRunTest
, TimedOut
) {
182 // Test that the controller times out instead of hanging forever.
184 InitTestServer(kBadServerDirectory
);
187 EXPECT_FALSE(WaitForFirstRunResult());
188 EXPECT_TRUE(timed_out());
191 } // namespace chromeos