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 virtual void SetUpOnMainThread() OVERRIDE
;
56 virtual void CleanUpOnMainThread() OVERRIDE
;
58 // DriveFirstRunController::Observer overrides:
59 virtual void OnCompletion(bool success
) OVERRIDE
;
60 virtual 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::CleanUpOnMainThread() {
106 InProcessBrowserTest::CleanUpOnMainThread();
107 content::RunAllPendingInMessageLoop();
110 void DriveFirstRunTest::InitTestServer(const std::string
& directory
) {
111 embedded_test_server()->ServeFilesFromDirectory(
112 test_data_dir_
.AppendASCII(directory
));
113 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
115 // Configure the endpoint to use the test server's port.
116 const GURL
url(kTestEndpointUrl
);
117 GURL::Replacements replacements
;
118 std::string
port(base::IntToString(embedded_test_server()->port()));
119 replacements
.SetPortStr(port
);
120 endpoint_url_
= url
.ReplaceComponents(replacements
).spec();
121 controller_
->SetAppInfoForTest(kTestAppId
, endpoint_url_
);
124 void DriveFirstRunTest::InstallApp() {
125 ExtensionService
* extension_service
= extensions::ExtensionSystem::Get(
126 browser()->profile())->extension_service();
127 scoped_refptr
<extensions::CrxInstaller
> installer
=
128 extensions::CrxInstaller::CreateSilent(extension_service
);
130 installer
->InstallCrx(test_data_dir_
.AppendASCII(kTestAppCrxName
));
131 ExtensionTestNotificationObserver
observer(browser());
132 observer
.WaitForExtensionLoad();
134 ASSERT_TRUE(extension_service
->GetExtensionById(kTestAppId
, false));
137 void DriveFirstRunTest::EnableOfflineMode() {
138 controller_
->EnableOfflineMode();
141 void DriveFirstRunTest::SetDelays(int initial_delay_secs
, int timeout_secs
) {
142 controller_
->SetDelaysForTest(initial_delay_secs
, timeout_secs
);
145 bool DriveFirstRunTest::WaitForFirstRunResult() {
146 waiting_for_result_
= true;
147 runner_
= new content::MessageLoopRunner
;
149 EXPECT_FALSE(waiting_for_result_
);
153 void DriveFirstRunTest::OnCompletion(bool success
) {
154 EXPECT_TRUE(waiting_for_result_
);
155 waiting_for_result_
= false;
159 // |controller_| will eventually delete itself upon completion, so invalidate
164 void DriveFirstRunTest::OnTimedOut() {
168 IN_PROC_BROWSER_TEST_F(DriveFirstRunTest
, OfflineEnabled
) {
170 InitTestServer(kGoodServerDirectory
);
172 EXPECT_TRUE(WaitForFirstRunResult());
175 IN_PROC_BROWSER_TEST_F(DriveFirstRunTest
, AppNotInstalled
) {
176 InitTestServer(kGoodServerDirectory
);
178 EXPECT_FALSE(WaitForFirstRunResult());
179 EXPECT_FALSE(timed_out());
182 IN_PROC_BROWSER_TEST_F(DriveFirstRunTest
, TimedOut
) {
183 // Test that the controller times out instead of hanging forever.
185 InitTestServer(kBadServerDirectory
);
188 EXPECT_FALSE(WaitForFirstRunResult());
189 EXPECT_TRUE(timed_out());
192 } // namespace chromeos