1 // Copyright 2014 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 #import "chrome/browser/ui/cocoa/content_settings/collected_cookies_mac.h"
7 #include "base/mac/scoped_nsobject.h"
8 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h"
9 #import "testing/gtest_mac.h"
11 class CollectedCookiesMacTest : public CocoaTest {
13 NSTreeNode* CreateTreeNodesFromDictionary(NSDictionary* dict) {
14 base::scoped_nsobject<NSTreeNode> root(
15 [[NSTreeNode alloc] initWithRepresentedObject:nil]);
16 AttachTreeNodeChildren(root, dict);
17 return root.autorelease();
21 void AttachTreeNodeChildren(NSTreeNode* parent, NSDictionary* data) {
22 NSMutableArray* children = [parent mutableChildNodes];
23 for (NSString* key in data) {
24 NSTreeNode* node = [NSTreeNode treeNodeWithRepresentedObject:key];
25 [children addObject:node];
27 for (NSDictionary* data_child in [data objectForKey:key])
28 AttachTreeNodeChildren(node, data_child);
33 TEST_F(CollectedCookiesMacTest, NormalizeSelection) {
34 NSTreeNode* root = CreateTreeNodesFromDictionary(@{
35 @"one" : @[ @{ @"one.1" : @[ @{ @"one.1.a" : @[], @"one.1.b" : @[] } ] } ],
37 @"three" : @[ @{ @"three.1" : @[] } ]
39 NSArray* nodes = [root childNodes];
40 NSTreeNode* one = [nodes objectAtIndex:0];
41 NSTreeNode* one1 = [[one childNodes] objectAtIndex:0];
42 NSTreeNode* one1a = [[one1 childNodes] objectAtIndex:0];
43 NSTreeNode* one1b = [[one1 childNodes] objectAtIndex:1];
44 NSTreeNode* two = [nodes objectAtIndex:1];
45 NSTreeNode* three = [nodes objectAtIndex:2];
46 NSTreeNode* three1 = [[three childNodes] objectAtIndex:0];
48 NSArray* selection = @[ one, one1a, three ];
49 NSArray* normalized = @[ one, three ];
51 [CollectedCookiesWindowController normalizeNodeSelection:selection];
52 EXPECT_NSEQ(normalized, actual);
54 selection = @[ two, one1b, three1, one, one1a ];
55 normalized = @[ two, three1, one ];
57 [CollectedCookiesWindowController normalizeNodeSelection:selection];
58 EXPECT_NSEQ(normalized, actual);
60 selection = @[ one, one1, one1a, one1b ];
61 normalized = @[ one ];
63 [CollectedCookiesWindowController normalizeNodeSelection:selection];
64 EXPECT_NSEQ(normalized, actual);