OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/base/resource/resource_bundle.h" | 5 #include "ui/base/resource/resource_bundle.h" |
6 | 6 |
7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | |
9 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/scoped_temp_dir.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 | 13 |
12 namespace ui { | 14 namespace ui { |
13 | 15 |
16 extern const char kSamplePakContents[]; | |
17 extern const size_t kSamplePakSize; | |
18 | |
14 TEST(ResourceBundle, LoadDataResourceBytes) { | 19 TEST(ResourceBundle, LoadDataResourceBytes) { |
15 // Verify that we don't crash when trying to load a resource that is not | 20 // Verify that we don't crash when trying to load a resource that is not |
16 // found. In some cases, we fail to mmap resources.pak, but try to keep | 21 // found. In some cases, we fail to mmap resources.pak, but try to keep |
17 // going anyway. | 22 // going anyway. |
18 ResourceBundle resource_bundle; | 23 ResourceBundle resource_bundle; |
19 | 24 |
20 // On Windows, the default data is compiled into the binary so this does | 25 // On Windows, the default data is compiled into the binary so this does |
21 // nothing. | 26 // nothing. |
22 FilePath data_path; | 27 ScopedTempDir dir; |
23 PathService::Get(base::DIR_SOURCE_ROOT, &data_path); | 28 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
24 data_path = data_path.Append( | 29 FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); |
25 FILE_PATH_LITERAL("ui/base/test/data/data_pack_unittest/sample.pak")); | |
26 | 30 |
31 // Dump contents into the pak file. | |
32 FILE* file = file_util::OpenFile(data_path, "wb"); | |
tony
2011/09/02 23:10:51
Can we just use file_util::WriteFile?
marcvs
2011/09/02 23:27:27
Done.
| |
33 ASSERT_TRUE(file); | |
34 ASSERT_EQ(fwrite(kSamplePakContents, 1, kSamplePakSize, file), | |
35 kSamplePakSize); | |
36 file_util::CloseFile(file); | |
37 | |
38 // Create a resource bundle from the file. | |
27 resource_bundle.LoadTestResources(data_path); | 39 resource_bundle.LoadTestResources(data_path); |
28 | 40 |
29 const int kUnfoundResourceId = 10000; | 41 const int kUnfoundResourceId = 10000; |
30 EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); | 42 EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); |
31 | 43 |
32 // Give a .pak file that doesn't exist so we will fail to load it. | 44 // Give a .pak file that doesn't exist so we will fail to load it. |
33 resource_bundle.AddDataPackToSharedInstance(FilePath( | 45 resource_bundle.AddDataPackToSharedInstance(FilePath( |
34 FILE_PATH_LITERAL("non-existant-file.pak"))); | 46 FILE_PATH_LITERAL("non-existant-file.pak"))); |
35 EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); | 47 EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); |
36 } | 48 } |
37 | 49 |
38 } // namespace ui | 50 } // namespace ui |
OLD | NEW |