OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "base/bind.h" | |
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_system.h" | |
15 #include "chrome/browser/extensions/extension_test_notification_observer.h" | |
16 #include "chrome/common/chrome_paths.h" | |
17 #include "chrome/test/base/in_process_browser_test.h" | |
18 #include "content/public/test/test_utils.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" | |
24 | |
25 namespace chromeos { | |
26 | |
27 namespace { | |
28 | |
29 // Directory containing data files for the tests. | |
30 const char kTestDirectory[] = "drive_first_run"; | |
31 | |
32 // Directory containing correct hosted app page served by the test server. | |
33 const char kGoodServerDirectory[] = "good"; | |
34 | |
35 // Directory containing incorrect hosted app page served by the test server. | |
36 const char kBadServerDirectory[] = "bad"; | |
37 | |
38 // Name of the test hosted app .crx file. | |
39 const char kTestAppCrxName[] = "app.crx"; | |
40 | |
41 // App id of the test hosted app. | |
42 const char kTestAppId[] = "kipccbklifbfblhpplnmklieangbjnhb"; | |
43 | |
44 // The endpoint belonging to the test hosted app. | |
45 const char kTestEndpointUrl[] = "http://example.com/endpoint.html"; | |
46 | |
47 } // namespace | |
48 | |
49 class DriveFirstRunTest : public InProcessBrowserTest, | |
50 public DriveFirstRunController::Observer { | |
51 protected: | |
52 DriveFirstRunTest(); | |
53 | |
54 // InProcessBrowserTest overrides: | |
55 virtual void SetUpOnMainThread() OVERRIDE; | |
56 virtual void CleanUpOnMainThread() OVERRIDE; | |
57 | |
58 // DriveFirstRunController::Observer overrides: | |
59 virtual void OnCompletion(bool success) OVERRIDE; | |
60 virtual void OnTimedOut() OVERRIDE; | |
61 | |
62 void InstallApp(); | |
63 | |
64 void InitTestServer(const std::string& directory); | |
65 | |
66 bool WaitForFirstRunResult(); | |
67 | |
68 DriveFirstRunController* controller() const { return controller_; } | |
achuithb
2013/11/13 23:11:55
Have you considered having protected methods like
Tim Song
2013/11/14 01:18:49
Done.
| |
69 bool timed_out() const { return timed_out_; } | |
70 | |
71 private: | |
72 // |controller_| is responsible for its own lifetime. | |
73 DriveFirstRunController* controller_; | |
74 scoped_refptr<content::MessageLoopRunner> runner_; | |
75 | |
76 bool timed_out_; | |
77 bool waiting_for_result_; | |
78 bool success_; | |
79 base::FilePath test_data_dir_; | |
80 std::string endpoint_url_; | |
81 }; | |
82 | |
83 DriveFirstRunTest::DriveFirstRunTest() : | |
84 timed_out_(false), | |
85 waiting_for_result_(false), | |
86 success_(false) {} | |
87 | |
88 void DriveFirstRunTest::SetUpOnMainThread() { | |
89 InProcessBrowserTest::SetUpOnMainThread(); | |
90 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_); | |
91 test_data_dir_ = test_data_dir_.AppendASCII(kTestDirectory); | |
92 | |
93 host_resolver()->AddRule("*", "127.0.0.1"); | |
achuithb
2013/11/13 23:11:55
Maybe a comment explaining this? Wouldn't this tes
Tim Song
2013/11/14 01:18:49
Done. Changed to example.com.
| |
94 | |
95 // |controller_| will delete itself when it completes. | |
96 controller_ = new DriveFirstRunController(); | |
97 controller_->AddObserver(this); | |
98 controller_->SetDelaysForTest(0, 10); | |
99 controller_->SetAppInfoForTest(kTestAppId, kTestEndpointUrl); | |
100 } | |
101 | |
102 void DriveFirstRunTest::CleanUpOnMainThread() { | |
103 InProcessBrowserTest::CleanUpOnMainThread(); | |
104 content::RunAllPendingInMessageLoop(); | |
105 } | |
106 | |
107 void DriveFirstRunTest::InitTestServer(const std::string& directory) { | |
108 embedded_test_server()->ServeFilesFromDirectory( | |
109 test_data_dir_.AppendASCII(directory)); | |
110 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
111 | |
112 // Configure the endpoint to use the test server's port. | |
113 const GURL url(kTestEndpointUrl); | |
114 GURL::Replacements replacements; | |
115 std::string port(base::IntToString(embedded_test_server()->port())); | |
116 replacements.SetPortStr(port); | |
117 endpoint_url_ = url.ReplaceComponents(replacements).spec(); | |
118 controller_->SetAppInfoForTest(kTestAppId, endpoint_url_); | |
119 } | |
120 | |
121 void DriveFirstRunTest::InstallApp() { | |
122 ExtensionService* extension_service = extensions::ExtensionSystem::Get( | |
123 browser()->profile())->extension_service(); | |
124 scoped_refptr<extensions::CrxInstaller> installer = | |
125 extensions::CrxInstaller::CreateSilent(extension_service); | |
126 | |
127 installer->InstallCrx(test_data_dir_.AppendASCII(kTestAppCrxName)); | |
128 ExtensionTestNotificationObserver observer(browser()); | |
129 observer.WaitForExtensionLoad(); | |
130 | |
131 ASSERT_TRUE(extension_service->GetExtensionById(kTestAppId, false)); | |
132 } | |
133 | |
134 bool DriveFirstRunTest::WaitForFirstRunResult() { | |
135 waiting_for_result_ = true; | |
136 runner_ = new content::MessageLoopRunner; | |
137 runner_->Run(); | |
138 EXPECT_FALSE(waiting_for_result_); | |
139 return success_; | |
140 } | |
141 | |
142 void DriveFirstRunTest::OnCompletion(bool success) { | |
143 EXPECT_TRUE(waiting_for_result_); | |
144 waiting_for_result_ = false; | |
145 success_ = success; | |
146 runner_->Quit(); | |
147 | |
148 // |controller_| will eventually delete itself upon completion, so invalidate | |
149 // the pointer. | |
150 controller_ = NULL; | |
151 } | |
152 | |
153 void DriveFirstRunTest::OnTimedOut() { | |
154 timed_out_ = true; | |
155 } | |
156 | |
157 IN_PROC_BROWSER_TEST_F(DriveFirstRunTest, OfflineEnabled) { | |
158 InstallApp(); | |
159 InitTestServer(kGoodServerDirectory); | |
160 controller()->EnableOfflineMode(); | |
161 EXPECT_TRUE(WaitForFirstRunResult()); | |
162 } | |
163 | |
164 IN_PROC_BROWSER_TEST_F(DriveFirstRunTest, AppNotInstalled) { | |
165 InitTestServer(kGoodServerDirectory); | |
166 controller()->EnableOfflineMode(); | |
167 EXPECT_FALSE(WaitForFirstRunResult()); | |
168 EXPECT_FALSE(timed_out()); | |
169 } | |
170 | |
171 IN_PROC_BROWSER_TEST_F(DriveFirstRunTest, TimedOut) { | |
172 InstallApp(); | |
173 InitTestServer(kBadServerDirectory); | |
174 controller()->SetDelaysForTest(0, 0); | |
175 controller()->EnableOfflineMode(); | |
176 EXPECT_FALSE(WaitForFirstRunResult()); | |
177 EXPECT_TRUE(timed_out()); | |
178 } | |
179 | |
180 } // namespace chromeos | |
OLD | NEW |